Skip to content

Commit

Permalink
fix(git-loader): respect leading './' for globs (#5244)
Browse files Browse the repository at this point in the history
* fix(git-loader): respect leading './' for globs

* chore: rename

* chore: add changeset

* chore: tests
  • Loading branch information
jeengbe authored May 8, 2023
1 parent 287be53 commit ad43095
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/short-spies-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-tools/git-loader': minor
---

respect leading './' for globs
2 changes: 1 addition & 1 deletion packages/graphql-tag-pluck/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const traverse = getDefault(traversePkg);
*/
export interface GraphQLTagPluckOptions {
/**
* Additional options for determining how a file is parsed.An array of packages that are responsible for exporting the GraphQL string parser function. The following modules are supported by default:
* Additional options for determining how a file is parsed. An array of packages that are responsible for exporting the GraphQL string parser function. The following modules are supported by default:
* ```js
* {
* modules: [
Expand Down
15 changes: 12 additions & 3 deletions packages/loaders/git/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,16 @@ export class GitLoader implements Loader<GitLoaderOptions> {
refsForPaths.get(ref).push(`!${unixify(path)}`);
}

const maybeLeadingDotSlash = path.startsWith('./') ? './' : '';

const resolved: string[] = [];
await Promise.all(
[...refsForPaths.entries()].map(async ([ref, paths]) => {
resolved.push(...micromatch(await readTreeAtRef(ref), paths).map(filePath => `git:${ref}:${filePath}`));
resolved.push(
...micromatch(await readTreeAtRef(ref), paths).map(
filePath => `git:${ref}:${maybeLeadingDotSlash}${filePath}`
)
);
})
);
return resolved;
Expand Down Expand Up @@ -116,9 +122,13 @@ export class GitLoader implements Loader<GitLoaderOptions> {
refsForPaths.get(ref).push(`!${unixify(path)}`);
}

const maybeLeadingDotSlash = path.startsWith('./') ? './' : '';

const resolved: string[] = [];
for (const [ref, paths] of refsForPaths.entries()) {
resolved.push(...micromatch(readTreeAtRefSync(ref), paths).map(filePath => `git:${ref}:${filePath}`));
resolved.push(
...micromatch(readTreeAtRefSync(ref), paths).map(filePath => `git:${ref}:${maybeLeadingDotSlash}${filePath}`)
);
}
return resolved;
}
Expand Down Expand Up @@ -227,7 +237,6 @@ export class GitLoader implements Loader<GitLoaderOptions> {
try {
if (isGlob(path)) {
const resolvedPaths = this.resolveGlobsSync(pointer, asArray(options.ignore || []));
const finalResult: Source[] = [];
for (const path of resolvedPaths) {
if (this.canLoadSync(path)) {
const results = this.loadSync(path, options);
Expand Down
130 changes: 130 additions & 0 deletions packages/loaders/git/tests/__snapshots__/loader.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,71 @@ exports[`GitLoader load async should load type definitions from a pluckable file
}
`;

exports[`GitLoader load async should work when loading glob paths that start with './' 1`] = `
{
"definitions": [
{
"description": undefined,
"directives": [],
"fields": [
{
"arguments": [],
"description": undefined,
"directives": [],
"kind": "FieldDefinition",
"loc": {
"end": 28,
"start": 15,
},
"name": {
"kind": "Name",
"loc": {
"end": 20,
"start": 15,
},
"value": "hello",
},
"type": {
"kind": "NamedType",
"loc": {
"end": 28,
"start": 22,
},
"name": {
"kind": "Name",
"loc": {
"end": 28,
"start": 22,
},
"value": "String",
},
},
},
],
"interfaces": [],
"kind": "ObjectTypeDefinition",
"loc": {
"end": 30,
"start": 0,
},
"name": {
"kind": "Name",
"loc": {
"end": 10,
"start": 5,
},
"value": "Query",
},
},
],
"kind": "Document",
"loc": {
"end": 31,
"start": 0,
},
}
`;

exports[`GitLoader load sync should load type definitions from a pluckable file 1`] = `
{
"definitions": [
Expand Down Expand Up @@ -129,3 +194,68 @@ exports[`GitLoader load sync should load type definitions from a pluckable file
},
}
`;

exports[`GitLoader load sync should work when loading glob paths that start with './' 1`] = `
{
"definitions": [
{
"description": undefined,
"directives": [],
"fields": [
{
"arguments": [],
"description": undefined,
"directives": [],
"kind": "FieldDefinition",
"loc": {
"end": 28,
"start": 15,
},
"name": {
"kind": "Name",
"loc": {
"end": 20,
"start": 15,
},
"value": "hello",
},
"type": {
"kind": "NamedType",
"loc": {
"end": 28,
"start": 22,
},
"name": {
"kind": "Name",
"loc": {
"end": 28,
"start": 22,
},
"value": "String",
},
},
},
],
"interfaces": [],
"kind": "ObjectTypeDefinition",
"loc": {
"end": 30,
"start": 0,
},
"name": {
"kind": "Name",
"loc": {
"end": 10,
"start": 5,
},
"value": "Query",
},
},
],
"kind": "Document",
"loc": {
"end": 31,
"start": 0,
},
}
`;
11 changes: 11 additions & 0 deletions packages/loaders/git/tests/loader.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { execSync } from 'child_process';
import * as path from 'path';

import { GitLoader } from '../src/index.js';
import { runTests } from '../../../testing/utils.js';
Expand Down Expand Up @@ -64,6 +65,16 @@ describe('GitLoader', () => {
const result = await load('./pluckable.ts', {});
expect(result).toEqual([]);
});

it("should work when loading glob paths that start with './'", async () => {
const saveCwd = process.cwd();
process.chdir(path.resolve(__dirname, 'test-files', 'a'));

const [result] = await load(`git:${lastCommit}:./**/*.graphql`, {});
expect(result.document).toMatchSnapshot();

process.chdir(saveCwd);
});
});
});
});
3 changes: 3 additions & 0 deletions packages/loaders/git/tests/test-files/a/b/file.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type Query {
hello: String
}

0 comments on commit ad43095

Please sign in to comment.