From 9a96c0a9fafb887c9f09db09eea9970d7243413f Mon Sep 17 00:00:00 2001 From: nicholaslyang Date: Fri, 20 Sep 2024 16:27:43 -0400 Subject: [PATCH] Fixing up and adding tests --- crates/turborepo-lib/src/query/mod.rs | 18 ++++- turborepo-tests/integration/tests/affected.t | 68 +++++++++++++++++-- .../integration/tests/command-query.t | 26 +++++++ .../integration/tests/turbo-trace.t | 46 +++++++------ 4 files changed, 131 insertions(+), 27 deletions(-) diff --git a/crates/turborepo-lib/src/query/mod.rs b/crates/turborepo-lib/src/query/mod.rs index 400c64f7c0c88..2777989254a41 100644 --- a/crates/turborepo-lib/src/query/mod.rs +++ b/crates/turborepo-lib/src/query/mod.rs @@ -334,26 +334,38 @@ impl Query { Ok(File::new(self.run.clone(), abs_path)) } + /// Get the files that have changed between the `base` and `head` commits. + /// + /// # Arguments + /// + /// * `base`: Defaults to `main` or `master` + /// * `head`: Defaults to `HEAD` + /// * `include_uncommitted`: Defaults to `true` if `head` is not provided + /// * `allow_unknown_objects`: Defaults to `false` + /// * `merge_base`: Defaults to `true` + /// + /// returns: Result, Error> async fn affected_files( &self, base: Option, head: Option, - /// Defaults to true if `head` is not provided include_uncommitted: Option, - /// Defaults to true + allow_unknown_objects: Option, merge_base: Option, ) -> Result, Error> { let base = base.as_deref(); let head = head.as_deref(); let include_uncommitted = include_uncommitted.unwrap_or_else(|| head.is_none()); let merge_base = merge_base.unwrap_or(true); + let allow_unknown_objects = allow_unknown_objects.unwrap_or(false); + let repo_root = self.run.repo_root(); let change_result = self.run.scm().changed_files( repo_root, base, head, include_uncommitted, - false, + allow_unknown_objects, merge_base, )?; diff --git a/turborepo-tests/integration/tests/affected.t b/turborepo-tests/integration/tests/affected.t index ed66bc0ed61b7..376232a41702d 100644 --- a/turborepo-tests/integration/tests/affected.t +++ b/turborepo-tests/integration/tests/affected.t @@ -57,13 +57,21 @@ Do the same thing with the `query` command } Also with `affectedFiles` in `turbo query` - $ ${TURBO} query "query { affectedFiles(base: \"main\", head: \"HEAD\") { items { path } } }" + $ ${TURBO} query "query { affectedFiles { items { path, affectedPackages { items { name } } } } }" + WARNING query command is experimental and may change in the future { "data": { "affectedFiles": { "items": [ { - "path": "apps/my-app/new.js" + "path": "apps/my-app/new.js", + "affectedPackages": { + "items": [ + { + "name": "my-app" + } + ] + } } ] } @@ -118,13 +126,21 @@ Do the same thing with the `query` command } Also with `affectedFiles` in `turbo query` - $ ${TURBO} query "query { affectedFiles(base: \"main\", head: \"HEAD\") { items { path } } }" + $ ${TURBO} query "query { affectedFiles { items { path, affectedPackages { items { name } } } } }" + WARNING query command is experimental and may change in the future { "data": { "affectedFiles": { "items": [ { - "path": "apps/my-app/package.json" + "path": "apps/my-app/package.json", + "affectedPackages": { + "items": [ + { + "name": "my-app" + } + ] + } } ] } @@ -206,6 +222,17 @@ Do the same thing with the `query` command } } +Also with `affectedFiles` in `turbo query` + $ ${TURBO} query "query { affectedFiles(base: \"HEAD\") { items { path, affectedPackages { items { name } } } } }" + WARNING query command is experimental and may change in the future + { + "data": { + "affectedFiles": { + "items": [] + } + } + } + Override the SCM head to be main, so nothing runs $ TURBO_SCM_HEAD="main" ${TURBO} run build --affected --log-order grouped \xe2\x80\xa2 Packages in scope: (esc) @@ -237,6 +264,17 @@ Do the same thing with the `query` command } } +Also with `affectedFiles` in `turbo query` + $ ${TURBO} query "query { affectedFiles(head: \"main\") { items { path, affectedPackages { items { name } } } } }" + WARNING query command is experimental and may change in the future + { + "data": { + "affectedFiles": { + "items": [] + } + } + } + Now add a commit to `main` so the merge base is different from `main` $ git checkout main --quiet $ echo "foo" >> packages/util/index.js @@ -286,6 +324,28 @@ Do the same thing with the `query` command } } +Also with `affectedFiles` in `turbo query` + $ ${TURBO} query "query { affectedFiles { items { path, affectedPackages { items { name } } } } }" + WARNING query command is experimental and may change in the future + { + "data": { + "affectedFiles": { + "items": [ + { + "path": "apps/my-app/package.json", + "affectedPackages": { + "items": [ + { + "name": "my-app" + } + ] + } + } + ] + } + } + } + Now do some magic to change the repo to be shallow $ SHALLOW=$(git rev-parse --show-toplevel)/.git/shallow $ git rev-parse HEAD > "$SHALLOW" diff --git a/turborepo-tests/integration/tests/command-query.t b/turborepo-tests/integration/tests/command-query.t index 0d924f0de5172..d791c69d5c783 100644 --- a/turborepo-tests/integration/tests/command-query.t +++ b/turborepo-tests/integration/tests/command-query.t @@ -167,4 +167,30 @@ Run the query ] } } + } + +Query a file + $ ${TURBO} query "query { file(path: \"apps/my-app/package.json\") { path, contents } }" + WARNING query command is experimental and may change in the future + { + "data": { + "file": { + "path": "apps/my-app/package.json", + "contents": "{\n \"name\": \"my-app\",\n \"scripts\": {\n \"build\": \"echo building\",\n \"maybefails\": \"exit 4\"\n },\n \"dependencies\": {\n \"util\": \"*\"\n }\n}\n" + } + } + } + +Get the file's package + $ ${TURBO} query "query { file(path: \"apps/my-app/package.json\") { path, package { ... on Package { name } } } }" + WARNING query command is experimental and may change in the future + { + "data": { + "file": { + "path": "apps/my-app/package.json", + "package": { + "name": "my-app" + } + } + } } \ No newline at end of file diff --git a/turborepo-tests/integration/tests/turbo-trace.t b/turborepo-tests/integration/tests/turbo-trace.t index c40b5a913eece..25113f40184b0 100644 --- a/turborepo-tests/integration/tests/turbo-trace.t +++ b/turborepo-tests/integration/tests/turbo-trace.t @@ -11,49 +11,55 @@ Setup } } - $ ${TURBO} query "query { file(path: \"main.ts\") { path, dependencies { path } } }" + $ ${TURBO} query "query { file(path: \"main.ts\") { path, fileDependencies { items { path } } } }" WARNING query command is experimental and may change in the future { "data": { "file": { "path": "main.ts", - "dependencies": [ - { - "path": "button.tsx" - }, - { - "path": "foo.js" - }, - { - "path": "node_modules(\/|\\\\)repeat-string(\/|\\\\)index.js" (re) - } - ] + "fileDependencies": { + "items": [ + { + "path": "button.tsx" + }, + { + "path": "foo.js" + }, + { + "path": "node_modules(\/|\\\\)repeat-string(\/|\\\\)index.js" (re) + } + ] + } } } } - $ ${TURBO} query "query { file(path: \"button.tsx\") { path, dependencies { path } } }" + $ ${TURBO} query "query { file(path: \"button.tsx\") { path, fileDependencies { items { path } } } }" WARNING query command is experimental and may change in the future { "data": { "file": { "path": "button.tsx", - "dependencies": [] + "fileDependencies": { + "items": [] + } } } } - $ ${TURBO} query "query { file(path: \"circular.ts\") { path, dependencies { path } } }" + $ ${TURBO} query "query { file(path: \"circular.ts\") { path, fileDependencies { items { path } } } }" WARNING query command is experimental and may change in the future { "data": { "file": { "path": "circular.ts", - "dependencies": [ - { - "path": "circular2.ts" - } - ] + "fileDependencies": { + "items": [ + { + "path": "circular2.ts" + } + ] + } } } }