From 1536657542d20be7c4c909d6ca65c0dbff46cc47 Mon Sep 17 00:00:00 2001 From: Sukka Date: Wed, 17 Jan 2024 16:24:18 +0800 Subject: [PATCH] test(#73/#125): add glob regex group cases (#184) Co-authored-by: JounQin --- test/pattern.spec.ts | 62 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 test/pattern.spec.ts diff --git a/test/pattern.spec.ts b/test/pattern.spec.ts new file mode 100644 index 0000000..6dde963 --- /dev/null +++ b/test/pattern.spec.ts @@ -0,0 +1,62 @@ +import execa from 'execa' +import mock from 'mock-fs' + +import prettyQuick from 'pretty-quick' + +jest.mock('execa') + +afterEach(() => { + mock.restore() + jest.clearAllMocks() +}) + +describe('match pattern', () => { + it('issue #73 #125 - regex grouping (round pattern)', () => { + const onFoundChangedFiles = jest.fn() + + mock({ + '/.git': {}, + '/src/apps/hello/foo.js': "export const foo = 'foo'", + '/src/libs/hello/bar.js': "export const bar = 'bar'", + '/src/tools/hello/baz.js': "export const baz = 'baz'", + '/src/should-not-be-included/hello/zoo.js': "export const zoo = 'zoo'", + }) + + // @ts-expect-error -- Need to find a better way to mock this + // eslint-disable-next-line @typescript-eslint/no-unsafe-call + execa.sync.mockImplementation((_command: string, args: string[]) => { + switch (args[0]) { + case 'ls-files': { + return { stdout: '' } + } + case 'diff': { + return args[2] === '--cached' + ? { stdout: '' } + : { + stdout: [ + '/src/apps/hello/foo.js', + '/src/libs/hello/bar.js', + '/src/tools/hello/baz.js', + '/src/should-not-be-included/hello/zoo.js', + ].join('\n'), + } + } + default: { + throw new Error(`unexpected arg0: ${args[0]}`) + } + } + }) + + prettyQuick('root', { + pattern: '**/(apps|libs|tools)/**/*', + since: 'fox', // This is required to prevent `scm.getSinceRevision` call + onFoundChangedFiles, + }) + + expect(onFoundChangedFiles).toHaveBeenCalledWith([ + '/src/apps/hello/foo.js', + '/src/libs/hello/bar.js', + '/src/tools/hello/baz.js', + ]) + }) +})