Skip to content

Commit

Permalink
Fixing up and adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NicholasLYang committed Sep 20, 2024
1 parent 76e57a5 commit 9a96c0a
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 27 deletions.
18 changes: 15 additions & 3 deletions crates/turborepo-lib/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Array<File>, Error>
async fn affected_files(
&self,
base: Option<String>,
head: Option<String>,
/// Defaults to true if `head` is not provided
include_uncommitted: Option<bool>,
/// Defaults to true
allow_unknown_objects: Option<bool>,
merge_base: Option<bool>,
) -> Result<Array<File>, 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,
)?;

Expand Down
68 changes: 64 additions & 4 deletions turborepo-tests/integration/tests/affected.t
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
}
]
}
Expand Down Expand Up @@ -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"
}
]
}
}
]
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
26 changes: 26 additions & 0 deletions turborepo-tests/integration/tests/command-query.t
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
}
46 changes: 26 additions & 20 deletions turborepo-tests/integration/tests/turbo-trace.t
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
}
}
}
Expand Down

0 comments on commit 9a96c0a

Please sign in to comment.