|
| 1 | +import * as fs from 'fs'; |
| 2 | +import protect from '../../src/lib'; |
| 3 | +import * as path from 'path'; |
| 4 | +import * as uuid from 'uuid'; |
| 5 | +import * as fse from 'fs-extra'; |
| 6 | + |
| 7 | +type TestProject = { |
| 8 | + path: string; |
| 9 | + file: (filePath: string) => Promise<string>; |
| 10 | +}; |
| 11 | + |
| 12 | +describe('@snyk/protect', () => { |
| 13 | + let tempFolder: string; |
| 14 | + |
| 15 | + const createProject = async (fixture: string): Promise<TestProject> => { |
| 16 | + const fixturePath = path.join(__dirname, '../fixtures', fixture); |
| 17 | + const projectPath = path.join(tempFolder, fixture); |
| 18 | + await fse.copy(fixturePath, projectPath); |
| 19 | + return { |
| 20 | + path: projectPath, |
| 21 | + file: (filePath: string) => { |
| 22 | + const fullFilePath = path.join(projectPath, filePath); |
| 23 | + return fs.promises.readFile(fullFilePath, 'utf-8'); |
| 24 | + }, |
| 25 | + }; |
| 26 | + }; |
| 27 | + |
| 28 | + beforeAll(() => { |
| 29 | + tempFolder = path.join(__dirname, '__output__', uuid.v4()); |
| 30 | + fs.mkdirSync(tempFolder, { recursive: true }); |
| 31 | + }); |
| 32 | + |
| 33 | + afterAll(() => { |
| 34 | + fs.rmdirSync(tempFolder, { recursive: true }); |
| 35 | + }); |
| 36 | + |
| 37 | + afterEach(() => { |
| 38 | + jest.restoreAllMocks(); |
| 39 | + }); |
| 40 | + |
| 41 | + describe('applies patch(es)', () => { |
| 42 | + it('works for project with a single patchable module', async () => { |
| 43 | + const project = await createProject('single-patchable-module'); |
| 44 | + |
| 45 | + await protect(project.path); |
| 46 | + |
| 47 | + expect( |
| 48 | + project.file('node_modules/nyc/node_modules/lodash/lodash.js'), |
| 49 | + ).resolves.toMatchSnapshot(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('works for project with multiple patchable modules', async () => { |
| 53 | + const project = await createProject('multiple-matching-paths'); |
| 54 | + |
| 55 | + await protect(project.path); |
| 56 | + |
| 57 | + expect( |
| 58 | + project.file('node_modules/nyc/node_modules/lodash/lodash.js'), |
| 59 | + ).resolves.toMatchSnapshot(); |
| 60 | + expect( |
| 61 | + project.file('node_modules/lodash/lodash.js'), |
| 62 | + ).resolves.toMatchSnapshot(); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + describe('does not apply any patches and does not fail', () => { |
| 67 | + // in this scenario .snyk file has a vulnId which corresponds to the `lodash` package, but there are not instances of lodash in the node_modules |
| 68 | + it('for project with no modules with the target package name', async () => { |
| 69 | + const project = await createProject('no-matching-paths'); |
| 70 | + const log = jest.spyOn(global.console, 'log'); |
| 71 | + |
| 72 | + await protect(project.path); |
| 73 | + |
| 74 | + expect(log).toHaveBeenCalledWith('Nothing to patch, done'); |
| 75 | + }); |
| 76 | + |
| 77 | + // skipped because we need to check the versions of the found modules before we attempt to patch them which we don't currently do |
| 78 | + // and in order to do that, we need to first switch over to the new endpoint |
| 79 | + // it('for a project that has an instance of the target module but we have no patches for its version', async () => { |
| 80 | + // const project = await createProject('target-module-exists-but-no-patches-for-version'); |
| 81 | + // const log = jest.spyOn(global.console, 'log'); |
| 82 | + // await protect(project.path); |
| 83 | + // expect(log).toHaveBeenCalledWith('Nothing to patch, done'); |
| 84 | + // }); |
| 85 | + |
| 86 | + // fixture has a lodash@4.14.1 which we don't have patches for |
| 87 | + it('for project with no .snyk file', async () => { |
| 88 | + const project = await createProject('no-snyk-file'); |
| 89 | + const log = jest.spyOn(global.console, 'log'); |
| 90 | + |
| 91 | + await protect(project.path); |
| 92 | + |
| 93 | + expect(log).toHaveBeenCalledWith('No .snyk file found'); |
| 94 | + }); |
| 95 | + }); |
| 96 | +}); |
0 commit comments