From 532d95d7f6ff63b6940a9107b8d87685a884b729 Mon Sep 17 00:00:00 2001 From: Jahed Ahmed Date: Fri, 16 Jul 2021 13:08:05 +0000 Subject: [PATCH] test(protect): update to match fix pr setup --- .../fix-pr-prepublish-hook.smoke.spec.ts | 27 ------------------ .../test/acceptance/fix-pr.smoke.spec.ts | 28 +++++++++++++++++++ .../fix-pr-prepublish-hook/package.json | 12 -------- .../{fix-pr-prepublish-hook => fix-pr}/.snyk | 0 .../test/fixtures/fix-pr/package.json | 14 ++++++++++ .../snyk-protect/test/util/createProject.ts | 5 ++++ packages/snyk-protect/test/util/runCommand.ts | 2 +- .../snyk-protect/test/util/useLocalPackage.ts | 25 ++++++++--------- 8 files changed, 60 insertions(+), 53 deletions(-) delete mode 100644 packages/snyk-protect/test/acceptance/fix-pr-prepublish-hook.smoke.spec.ts create mode 100644 packages/snyk-protect/test/acceptance/fix-pr.smoke.spec.ts delete mode 100644 packages/snyk-protect/test/fixtures/fix-pr-prepublish-hook/package.json rename packages/snyk-protect/test/fixtures/{fix-pr-prepublish-hook => fix-pr}/.snyk (100%) create mode 100644 packages/snyk-protect/test/fixtures/fix-pr/package.json diff --git a/packages/snyk-protect/test/acceptance/fix-pr-prepublish-hook.smoke.spec.ts b/packages/snyk-protect/test/acceptance/fix-pr-prepublish-hook.smoke.spec.ts deleted file mode 100644 index b9c3aa01e92..00000000000 --- a/packages/snyk-protect/test/acceptance/fix-pr-prepublish-hook.smoke.spec.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { createProject } from '../util/createProject'; -import { getPatchedLodash } from '../util/getPatchedLodash'; -import { runCommand } from '../util/runCommand'; - -jest.setTimeout(1000 * 60); - -describe('Fix PR "prepublish" hook', () => { - afterEach(() => { - jest.restoreAllMocks(); - }); - - test('patches vulnerable dependencies on install', async () => { - const project = await createProject('fix-pr-prepublish-hook'); - const patchedLodash = await getPatchedLodash(); - - const { code, stdout, stderr } = await runCommand('npm', ['install'], { - cwd: project.path(), - }); - - expect(stderr).toEqual(''); - expect(stdout).toMatch('patched'); - expect(code).toEqual(0); - await expect( - project.read('node_modules/lodash/lodash.js'), - ).resolves.toEqual(patchedLodash); - }); -}); diff --git a/packages/snyk-protect/test/acceptance/fix-pr.smoke.spec.ts b/packages/snyk-protect/test/acceptance/fix-pr.smoke.spec.ts new file mode 100644 index 00000000000..fd564e58908 --- /dev/null +++ b/packages/snyk-protect/test/acceptance/fix-pr.smoke.spec.ts @@ -0,0 +1,28 @@ +import { createProject } from '../util/createProject'; +import { getPatchedLodash } from '../util/getPatchedLodash'; +import { RunCLIResult, runCommand } from '../util/runCommand'; + +jest.setTimeout(1000 * 60); + +describe('Fix PR', () => { + test('patches vulnerable dependencies on install', async () => { + const project = await createProject('fix-pr'); + const patchedLodash = await getPatchedLodash(); + + expect( + await runCommand('npm', ['install'], { + cwd: project.path(), + }), + ).toEqual( + expect.objectContaining({ + code: 0, + stdout: expect.stringContaining('Successfully applied Snyk patches'), + stderr: expect.any(String), + }), + ); + + await expect( + project.read('node_modules/lodash/lodash.js'), + ).resolves.toEqual(patchedLodash); + }); +}); diff --git a/packages/snyk-protect/test/fixtures/fix-pr-prepublish-hook/package.json b/packages/snyk-protect/test/fixtures/fix-pr-prepublish-hook/package.json deleted file mode 100644 index 3b6c0508c85..00000000000 --- a/packages/snyk-protect/test/fixtures/fix-pr-prepublish-hook/package.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "name": "fix-pr-prepublish-hook", - "version": "1.0.0", - "description": "Fixture with a prepublish hook similar to a Fix PR.", - "private": true, - "scripts": { - "prepublish": "npx -y -p @snyk/protect snyk-protect" - }, - "dependencies": { - "lodash": "4.17.15" - } -} diff --git a/packages/snyk-protect/test/fixtures/fix-pr-prepublish-hook/.snyk b/packages/snyk-protect/test/fixtures/fix-pr/.snyk similarity index 100% rename from packages/snyk-protect/test/fixtures/fix-pr-prepublish-hook/.snyk rename to packages/snyk-protect/test/fixtures/fix-pr/.snyk diff --git a/packages/snyk-protect/test/fixtures/fix-pr/package.json b/packages/snyk-protect/test/fixtures/fix-pr/package.json new file mode 100644 index 00000000000..12954fc6b90 --- /dev/null +++ b/packages/snyk-protect/test/fixtures/fix-pr/package.json @@ -0,0 +1,14 @@ +{ + "name": "fix-pr", + "version": "1.0.0", + "description": "Fixture with a setup similar to a Fix PR.", + "private": true, + "scripts": { + "prepare": "npm run snyk-protect", + "snyk-protect": "snyk-protect" + }, + "dependencies": { + "@snyk/protect": "latest", + "lodash": "4.17.15" + } +} diff --git a/packages/snyk-protect/test/util/createProject.ts b/packages/snyk-protect/test/util/createProject.ts index 21ebb5fdde4..0bf35510ec1 100644 --- a/packages/snyk-protect/test/util/createProject.ts +++ b/packages/snyk-protect/test/util/createProject.ts @@ -2,6 +2,7 @@ import * as fse from 'fs-extra'; import * as os from 'os'; import * as path from 'path'; import { useLocalPackage } from './useLocalPackage'; +import { debuglog } from 'util'; type TestProject = { path: (filePath?: string) => string; @@ -9,6 +10,8 @@ type TestProject = { remove: () => Promise; }; +const debug = debuglog('@snyk' + __filename); + const createProject = async (fixtureName: string): Promise => { const tempFolder = await fse.promises.mkdtemp( path.resolve(os.tmpdir(), `snyk-test-${fixtureName}-`), @@ -22,6 +25,8 @@ const createProject = async (fixtureName: string): Promise => { await useLocalPackage(projectPath); } + debug('createProject: %s', projectPath); + return { path: (filePath = '') => path.resolve(projectPath, filePath), read: (filePath: string) => { diff --git a/packages/snyk-protect/test/util/runCommand.ts b/packages/snyk-protect/test/util/runCommand.ts index bb7a9be11b2..c4238e3a5d1 100644 --- a/packages/snyk-protect/test/util/runCommand.ts +++ b/packages/snyk-protect/test/util/runCommand.ts @@ -1,7 +1,7 @@ import { SpawnOptionsWithoutStdio } from 'child_process'; import { spawn } from 'cross-spawn'; -type RunCLIResult = { +export type RunCLIResult = { code: number; stdout: string; stderr: string; diff --git a/packages/snyk-protect/test/util/useLocalPackage.ts b/packages/snyk-protect/test/util/useLocalPackage.ts index 141b78f819d..03e4500e13a 100644 --- a/packages/snyk-protect/test/util/useLocalPackage.ts +++ b/packages/snyk-protect/test/util/useLocalPackage.ts @@ -1,36 +1,35 @@ import * as fse from 'fs-extra'; import * as path from 'path'; import { runCommand } from './runCommand'; +import { debuglog } from 'util'; type PackageJSON = { - scripts?: Record; + dependencies?: Record; }; +const debug = debuglog('@snyk' + __filename); + const useLocalPackage = async (projectPath: string) => { const workspaceRoot = path.resolve(__dirname, '../..'); const { stdout: tarballName } = await runCommand('npm', ['pack'], { cwd: workspaceRoot, }); - const currentPackageJson = await fse.readFile( - path.resolve(projectPath, 'package.json'), - 'utf-8', - ); + const packageJsonPath = path.resolve(projectPath, 'package.json'); + const currentPackageJson = await fse.readFile(packageJsonPath, 'utf-8'); const packageJson: PackageJSON = JSON.parse(currentPackageJson); - if (packageJson.scripts?.prepublish) { - packageJson.scripts.prepublish = packageJson.scripts.prepublish.replace( - '@snyk/protect', - path.resolve(workspaceRoot, tarballName), + if (packageJson.dependencies) { + packageJson.dependencies['@snyk/protect'] = path.resolve( + workspaceRoot, + tarballName, ); } const nextPackageJson = JSON.stringify(packageJson, null, 2) + '\n'; if (currentPackageJson !== nextPackageJson) { - await fse.writeFile( - path.resolve(projectPath, 'package.json'), - nextPackageJson, - ); + debug('useLocalPackage: %s', packageJsonPath); + await fse.writeFile(packageJsonPath, nextPackageJson); } };