Skip to content

Commit 4c5695e

Browse files
committed
Fixing up and adding tests
1 parent 01717a0 commit 4c5695e

File tree

3 files changed

+101
-6
lines changed

3 files changed

+101
-6
lines changed

crates/turborepo-lib/src/query/mod.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,19 +560,29 @@ impl RepositoryQuery {
560560
Ok(File::new(self.run.clone(), abs_path))
561561
}
562562

563+
/// Get the files that have changed between the `base` and `head` commits.
564+
///
565+
/// # Arguments
566+
///
567+
/// * `base`: Defaults to `main` or `master`
568+
/// * `head`: Defaults to `HEAD`
569+
/// * `include_uncommitted`: Defaults to `true` if `head` is not provided
570+
/// * `allow_unknown_objects`: Defaults to `false`
571+
/// * `merge_base`: Defaults to `true`
572+
///
573+
/// returns: Result<Array<File>, Error>
563574
async fn affected_files(
564575
&self,
565576
base: Option<String>,
566577
head: Option<String>,
567-
/// Defaults to true if `head` is not provided
568578
include_uncommitted: Option<bool>,
569-
/// Defaults to true
570579
merge_base: Option<bool>,
571580
) -> Result<Array<File>, Error> {
572581
let base = base.as_deref();
573582
let head = head.as_deref();
574583
let include_uncommitted = include_uncommitted.unwrap_or_else(|| head.is_none());
575584
let merge_base = merge_base.unwrap_or(true);
585+
576586
let repo_root = self.run.repo_root();
577587
let change_result = self
578588
.run

turborepo-tests/integration/tests/affected.t

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,21 @@ Do the same thing with the `query` command
6161
}
6262

6363
Also with `affectedFiles` in `turbo query`
64-
$ ${TURBO} query "query { affectedFiles(base: \"main\", head: \"HEAD\") { items { path } } }"
64+
$ ${TURBO} query "query { affectedFiles { items { path, affectedPackages { items { name } } } } }"
65+
WARNING query command is experimental and may change in the future
6566
{
6667
"data": {
6768
"affectedFiles": {
6869
"items": [
6970
{
70-
"path": "apps/my-app/new.js"
71+
"path": "apps/my-app/new.js",
72+
"affectedPackages": {
73+
"items": [
74+
{
75+
"name": "my-app"
76+
}
77+
]
78+
}
7179
}
7280
]
7381
}
@@ -156,13 +164,21 @@ Do the same thing with the `query` command
156164
}
157165

158166
Also with `affectedFiles` in `turbo query`
159-
$ ${TURBO} query "query { affectedFiles(base: \"main\", head: \"HEAD\") { items { path } } }"
167+
$ ${TURBO} query "query { affectedFiles { items { path, affectedPackages { items { name } } } } }"
168+
WARNING query command is experimental and may change in the future
160169
{
161170
"data": {
162171
"affectedFiles": {
163172
"items": [
164173
{
165-
"path": "apps/my-app/package.json"
174+
"path": "apps/my-app/package.json",
175+
"affectedPackages": {
176+
"items": [
177+
{
178+
"name": "my-app"
179+
}
180+
]
181+
}
166182
}
167183
]
168184
}
@@ -244,6 +260,17 @@ Do the same thing with the `query` command
244260
}
245261
}
246262
263+
Also with `affectedFiles` in `turbo query`
264+
$ ${TURBO} query "query { affectedFiles(base: \"HEAD\") { items { path, affectedPackages { items { name } } } } }"
265+
WARNING query command is experimental and may change in the future
266+
{
267+
"data": {
268+
"affectedFiles": {
269+
"items": []
270+
}
271+
}
272+
}
273+
247274
Override the SCM head to be main, so nothing runs
248275
$ TURBO_SCM_HEAD="main" ${TURBO} run build --affected --log-order grouped
249276
\xe2\x80\xa2 Packages in scope: (esc)
@@ -275,6 +302,17 @@ Do the same thing with the `query` command
275302
}
276303
}
277304
305+
Also with `affectedFiles` in `turbo query`
306+
$ ${TURBO} query "query { affectedFiles(head: \"main\") { items { path, affectedPackages { items { name } } } } }"
307+
WARNING query command is experimental and may change in the future
308+
{
309+
"data": {
310+
"affectedFiles": {
311+
"items": []
312+
}
313+
}
314+
}
315+
278316
Now add a commit to `main` so the merge base is different from `main`
279317
$ git checkout main --quiet
280318
$ echo "foo" >> packages/util/index.js
@@ -324,6 +362,28 @@ Do the same thing with the `query` command
324362
}
325363
}
326364
365+
Also with `affectedFiles` in `turbo query`
366+
$ ${TURBO} query "query { affectedFiles { items { path, affectedPackages { items { name } } } } }"
367+
WARNING query command is experimental and may change in the future
368+
{
369+
"data": {
370+
"affectedFiles": {
371+
"items": [
372+
{
373+
"path": "apps/my-app/package.json",
374+
"affectedPackages": {
375+
"items": [
376+
{
377+
"name": "my-app"
378+
}
379+
]
380+
}
381+
}
382+
]
383+
}
384+
}
385+
}
386+
327387
Now do some magic to change the repo to be shallow
328388
$ SHALLOW=$(git rev-parse --show-toplevel)/.git/shallow
329389
$ git rev-parse HEAD > "$SHALLOW"

turborepo-tests/integration/tests/command-query.t

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,28 @@ Run the query
214214
$ VERSION=${MONOREPO_ROOT_DIR}/version.txt
215215
$ diff --strip-trailing-cr <(head -n 1 ${VERSION}) <(${TURBO} --version)
216216
217+
Query a file
218+
$ ${TURBO} query "query { file(path: \"apps/my-app/package.json\") { path, contents } }"
219+
WARNING query command is experimental and may change in the future
220+
{
221+
"data": {
222+
"file": {
223+
"path": "apps/my-app/package.json",
224+
"contents": "{\n \"name\": \"my-app\",\n \"scripts\": {\n \"build\": \"echo building\",\n \"maybefails\": \"exit 4\"\n },\n \"dependencies\": {\n \"util\": \"*\"\n }\n}\n"
225+
}
226+
}
227+
}
228+
229+
Get the file's package
230+
$ ${TURBO} query "query { file(path: \"apps/my-app/package.json\") { path, package { ... on Package { name } } } }"
231+
WARNING query command is experimental and may change in the future
232+
{
233+
"data": {
234+
"file": {
235+
"path": "apps/my-app/package.json",
236+
"package": {
237+
"name": "my-app"
238+
}
239+
}
240+
}
241+
}

0 commit comments

Comments
 (0)