Skip to content

Commit

Permalink
Merge pull request #18 from julien-capellari/fix/pattern-matching
Browse files Browse the repository at this point in the history
Fix pattern matching
  • Loading branch information
julien-capellari committed Mar 19, 2021
2 parents 62d5cac + f7b15ab commit 3cbdab7
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ jobs:
pattern: '*.ts'
affected: false

- branch: test/test-a-affected
workspace: test-a
base: master
pattern: 'src/**'
affected: true

- branch: test/test-a-affected
workspace: test-b
base: master
Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ inputs:
required: true

pattern:
description: 'Files to search in the diff'
description: 'Files to search in the diff. Relative to tested workspace root.'
required: false
default: '**'

Expand Down
2 changes: 1 addition & 1 deletion dist/bundle.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ export interface GitTagsOptions {
export const git = {
// Attributes
git: simpleGit(),
root: process.cwd(),

// Methods
setup(root: string): void {
this.git = simpleGit({ baseDir: root });
this.root = root;
},

// Commands
Expand Down
3 changes: 2 additions & 1 deletion src/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ export class Workspace {
return diff.length > 0;
}

return diff.some(minimatch.filter(pattern));
const rel = path.relative(git.root, this.root);
return diff.some(minimatch.filter(path.join(rel, pattern)));
}

private async _testDepsAffected(tested: Set<Workspace>, baseRef: string, pattern = '**'): Promise<boolean> {
Expand Down
15 changes: 8 additions & 7 deletions tests/workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,15 @@ describe('Workspace.isAffected', () => {
jest.restoreAllMocks();

prj = await Project.loadProject(TEST_PROJECT_ROOT);
git.root = TEST_PROJECT_ROOT;
});

// Tests
describe('direct change', () => {
it('should be affected (test-b, no pattern)', async () => {
// Spy
jest.spyOn(git, 'diff')
.mockResolvedValue(['test.js']);
.mockResolvedValue(['test-b/test.js']);

// Test
const wks = prj.getWorkspace('test-b');
Expand Down Expand Up @@ -94,13 +95,13 @@ describe('Workspace.isAffected', () => {
it('should be affected (test-b, with pattern)', async () => {
// Spy
jest.spyOn(git, 'diff')
.mockResolvedValue(['test.js']);
.mockResolvedValue(['test-b/src/test.js']);

// Test
const wks = prj.getWorkspace('test-b');
expect(wks).not.toBeNull();

await expect(wks!.isAffected('master', '*.js'))
await expect(wks!.isAffected('master', 'src/**'))
.resolves.toBeTruthy();

expect(git.diff).toBeCalledTimes(1);
Expand All @@ -110,7 +111,7 @@ describe('Workspace.isAffected', () => {
it('should not be affected (test-b, with pattern)', async () => {
// Spy
jest.spyOn(git, 'diff')
.mockResolvedValue(['test.js']);
.mockResolvedValue(['test-b/test.js']);

// Test
const wks = prj.getWorkspace('test-b');
Expand All @@ -128,7 +129,7 @@ describe('Workspace.isAffected', () => {
it('should be affected (test-c, no pattern)', async () => {
// Spy
jest.spyOn(git, 'diff')
.mockImplementation(async (...args) => args[args.length - 1].endsWith('test-b') ? ['test.js'] : []);
.mockImplementation(async (...args) => args[args.length - 1].endsWith('test-b') ? ['test-b/test.js'] : []);

// Test
const wks = prj.getWorkspace('test-c');
Expand Down Expand Up @@ -162,7 +163,7 @@ describe('Workspace.isAffected', () => {
it('should be affected (test-c, with pattern)', async () => {
// Spy
jest.spyOn(git, 'diff')
.mockImplementation(async (...args) => args[args.length - 1].endsWith('test-b') ? ['test.js'] : []);
.mockImplementation(async (...args) => args[args.length - 1].endsWith('test-b') ? ['test-b/test.js'] : []);

// Test
const wks = prj.getWorkspace('test-c');
Expand All @@ -179,7 +180,7 @@ describe('Workspace.isAffected', () => {
it('should not be affected (test-c, with pattern)', async () => {
// Spy
jest.spyOn(git, 'diff')
.mockImplementation(async (...args) => args[args.length - 1].endsWith('test-b') ? ['test.js'] : []);
.mockImplementation(async (...args) => args[args.length - 1].endsWith('test-b') ? ['test-b/test.js'] : []);


// Test
Expand Down

0 comments on commit 3cbdab7

Please sign in to comment.