Skip to content

Commit

Permalink
fix: handling !pattern in gitignore to avoid excluding everything fro…
Browse files Browse the repository at this point in the history
…m watching in sandbox (#671)

* fix: handling !pattern in gitignore to avoid excluding everything from watching in sandbox

* add changeset
  • Loading branch information
Amplifiyer committed Nov 16, 2023
1 parent 0a69096 commit 730afd8
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/pretty-mice-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@aws-amplify/sandbox': patch
---

fix: handling \!pattern in gitignore to avoid excluding everything from watching in sandbox
41 changes: 41 additions & 0 deletions packages/sandbox/src/file_watching_sandbox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,47 @@ void describe('Sandbox using local project name resolver', () => {
assert.strictEqual(backendDeployerDeployMock.mock.callCount(), 1);
});

void it('handles !files in .gitignore and filter them out', async (contextual) => {
contextual.mock.method(fs, 'existsSync', () => true);
contextual.mock.method(parseGitIgnore, 'parse', () => {
return {
patterns: [
'/patternWithLeadingSlash',
'patternWithoutLeadingSlash',
'someFile.js',
'!patternThatShouldNotBeIncluded',
'overlap/',
'overlap/file',
],
};
});
({ sandboxInstance, fileChangeEventCallback } = await setupAndStartSandbox({
executor: sandboxExecutor,
cfnClient: cfnClientMock,
exclude: ['customer_exclude1', 'customer_exclude2'],
}));
await fileChangeEventCallback(null, [
{ type: 'update', path: 'foo/test1.ts' },
]);

// File watcher should be called with right excludes and not include patternThatShouldNotBeIncluded
assert.deepStrictEqual(subscribeMock.mock.calls[0].arguments[2], {
ignore: [
'.amplify',
'patternWithLeadingSlash',
'patternWithoutLeadingSlash',
'someFile.js',
'overlap/',
'overlap/file',
'customer_exclude1',
'customer_exclude2',
],
});

// BackendDeployer should also be called once
assert.strictEqual(backendDeployerDeployMock.mock.callCount(), 1);
});

void it('emits the successfulDeployment event after deployment', async () => {
const mockListener = mock.fn();
({ sandboxInstance, fileChangeEventCallback } = await setupAndStartSandbox({
Expand Down
13 changes: 12 additions & 1 deletion packages/sandbox/src/file_watching_sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,18 @@ export class FileWatchingSandbox extends EventEmitter implements Sandbox {
.parse(gitIgnoreFilePath)
.patterns.map((pattern: string) =>
pattern.startsWith('/') ? pattern.substring(1) : pattern
);
)
.filter((pattern: string) => {
if (pattern.startsWith('!')) {
console.log(
`[Sandbox] Pattern ${pattern} found in .gitignore. "${pattern.substring(
1
)}" will not be watched if other patterns in .gitignore are excluding it.`
);
return false;
}
return true;
});
}
return [];
};
Expand Down

0 comments on commit 730afd8

Please sign in to comment.