Skip to content

Commit

Permalink
Merge pull request #2097 from snyk/protect/update-fix-pr-test
Browse files Browse the repository at this point in the history
test(protect): update to match fix pr setup
  • Loading branch information
Jahed Ahmed committed Jul 29, 2021
2 parents 4ded06f + 532d95d commit d8d41a4
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 53 deletions.

This file was deleted.

28 changes: 28 additions & 0 deletions 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<RunCLIResult>({
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);
});
});

This file was deleted.

14 changes: 14 additions & 0 deletions 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"
}
}
5 changes: 5 additions & 0 deletions packages/snyk-protect/test/util/createProject.ts
Expand Up @@ -2,13 +2,16 @@ 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;
read: (filePath: string) => Promise<string>;
remove: () => Promise<void>;
};

const debug = debuglog('@snyk' + __filename);

const createProject = async (fixtureName: string): Promise<TestProject> => {
const tempFolder = await fse.promises.mkdtemp(
path.resolve(os.tmpdir(), `snyk-test-${fixtureName}-`),
Expand All @@ -22,6 +25,8 @@ const createProject = async (fixtureName: string): Promise<TestProject> => {
await useLocalPackage(projectPath);
}

debug('createProject: %s', projectPath);

return {
path: (filePath = '') => path.resolve(projectPath, filePath),
read: (filePath: string) => {
Expand Down
2 changes: 1 addition & 1 deletion 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;
Expand Down
25 changes: 12 additions & 13 deletions 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<string, string>;
dependencies?: Record<string, string>;
};

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);
}
};

Expand Down

0 comments on commit d8d41a4

Please sign in to comment.