Skip to content

Commit

Permalink
Merge pull request #92 from snyk/chore/introduce-jest
Browse files Browse the repository at this point in the history
chore: introduce Jest
  • Loading branch information
robcresswell authored Dec 2, 2019
2 parents c145a7f + 2d5040d commit 80bb67d
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 116 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ package-lock.json
.nyc_output

.eslintcache

# Test output reports, coverage, etc
reports/
20 changes: 20 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module.exports = {
verbose: true,
preset: 'ts-jest',
testEnvironment: 'node',
collectCoverage: false, // Run yarn test:coverage to generate coverage reports
collectCoverageFrom: ['lib/**/*.ts'],
coverageReporters: ['html', 'text-summary'],
coverageDirectory: '<rootDir>/reports/coverage',
testMatch: ['**/*.spec.ts'], // Remove when all tests are using Jest
modulePathIgnorePatterns: ['<rootDir>/dist', '<rootDir/reports>'],
reporters: [
'default',
[
'jest-junit',
{
outputDirectory: '<rootDir>/reports/jest',
},
],
],
};
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"format": "prettier --write '{lib,test}/**/*.{js,ts}'",
"prepare": "npm run build",
"test": "npm run test:pysrc && npm run test:js",
"test:js": "cross-env TS_NODE_PROJECT=tsconfig-test.json tap --node-arg=-r --node-arg=ts-node/register ./test/*.test.{js,ts} -R spec --timeout=900",
"test:js": "cross-env TS_NODE_PROJECT=tsconfig-test.json tap --node-arg=-r --node-arg=ts-node/register ./test/*.test.{js,ts} -R spec --timeout=900 && jest",
"test:pysrc": "python -m unittest discover pysrc",
"lint": "npm run build-tests && npm run format:check && eslint --cache '{lib,test}/**/*.{js,ts}'",
"semantic-release": "semantic-release"
Expand All @@ -28,16 +28,20 @@
},
"devDependencies": {
"@snyk/types-tap": "^1.1.0",
"@types/jest": "^24.0.23",
"@types/node": "^6.14.6",
"@types/tmp": "^0.1.0",
"@typescript-eslint/eslint-plugin": "^1.13.0",
"@typescript-eslint/parser": "^1.13.0",
"cross-env": "^5.2.0",
"eslint": "^5.16.0",
"eslint-config-prettier": "^6.0.0",
"jest": "^24.9.0",
"jest-junit": "^10.0.0",
"prettier": "^1.18.2",
"sinon": "^2.3.2",
"tap": "^12.6.1",
"ts-jest": "^24.2.0",
"ts-node": "^8.1.0",
"typescript": "^3.4.5"
}
Expand Down
117 changes: 117 additions & 0 deletions test/remediation.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { readdirSync, readFileSync } from 'fs';
import * as path from 'path';
import { applyRemediationToManifests } from '../lib';
import { chdirWorkspaces, deactivateVirtualenv } from './test-utils';
import { activateVirtualenv, pipInstall } from './test-utils';

function readDirAsFiles(dir: string) {
return readdirSync(dir).reduce(
(files: { [fileName: string]: string }, fileName) => {
files[fileName] = readFileSync(path.join(dir, fileName), 'utf8');
return files;
},
{}
);
}

describe('remediation', () => {
beforeEach(() => {
activateVirtualenv('remediation');
});

afterEach(() => {
deactivateVirtualenv();
});

it('fixes a pip app', async () => {
chdirWorkspaces('pip-app');
pipInstall();

const upgrades = {
'[email protected]': { upgradeTo: '[email protected]' },
'[email protected]': { upgradeTo: '[email protected]' },
};

const manifests = {
'requirements.txt': readFileSync('requirements.txt', 'utf8'),
};

const expectedUpdatedManifests = readDirAsFiles(
'../../fixtures/updated-manifest'
);

const result = await applyRemediationToManifests(
'.',
manifests,
upgrades,
{}
);

expect(result).toEqual(expectedUpdatedManifests);
});

it('retains python markers', async () => {
chdirWorkspaces('pip-app-with-python-markers');
pipInstall();

const upgrades = {
'[email protected]': { upgradeTo: '[email protected]' },
};

const manifests = {
'requirements.txt': readFileSync('requirements.txt', 'utf8'),
};

const expectedUpdatedManifests = readDirAsFiles(
'../../fixtures/updated-manifests-with-python-markers'
);

const result = await applyRemediationToManifests(
'.',
manifests,
upgrades,
{}
);

expect(result).toEqual(expectedUpdatedManifests);
});

it('handles no-op upgrades', async () => {
chdirWorkspaces('pip-app');
pipInstall();

const upgrades = {};

const manifests = {
'requirements.txt': readFileSync('requirements.txt', 'utf8'),
};

const result = await applyRemediationToManifests(
'.',
manifests,
upgrades,
{}
);

expect(result).toEqual(manifests);
});

it('cannot fix a Pipfile app', async () => {
chdirWorkspaces('pipfile-pipapp');

const upgrades = {
'[email protected]': { upgradeTo: '[email protected]' },
'[email protected]': { upgradeTo: '[email protected]' },
};

const manifests = {
Pipfile: readFileSync('Pipfile', 'utf8'),
};

await expect(
applyRemediationToManifests('.', manifests, upgrades, {})
).rejects.toMatchObject({
message: 'Remediation only supported for requirements.txt file',
});
});
});
110 changes: 0 additions & 110 deletions test/remediation.test.ts

This file was deleted.

10 changes: 5 additions & 5 deletions test/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function getActiveVenvName() {
: null;
}

function activateVirtualenv(venvName) {
function activateVirtualenv(venvName: string) {
const venvDir = path.join(path.resolve(__dirname), '.venvs', venvName);

const binDir = path.resolve(venvDir, binDirName);
Expand Down Expand Up @@ -78,7 +78,7 @@ function deactivateVirtualenv() {
};
}

function ensureVirtualenv(venvName) {
function ensureVirtualenv(venvName: string) {
const venvsBaseDir = path.join(path.resolve(__dirname), '.venvs');
try {
fs.accessSync(venvsBaseDir, fs.constants.R_OK);
Expand All @@ -96,7 +96,7 @@ function ensureVirtualenv(venvName) {
}
}

function createVenv(venvDir) {
function createVenv(venvDir: string) {
let revert = () => {};
if (process.env.VIRTUAL_ENV) {
revert = deactivateVirtualenv();
Expand Down Expand Up @@ -140,7 +140,7 @@ function pipInstall() {
}
}

function pipUninstall(pkgName) {
function pipUninstall(pkgName: string) {
const proc = subProcess.executeSync('pip', ['uninstall', '-y', pkgName]);
if (proc.status !== 0) {
throw new Error(
Expand Down Expand Up @@ -182,6 +182,6 @@ function setWorkonHome() {
};
}

export function chdirWorkspaces(dir) {
export function chdirWorkspaces(dir: string) {
process.chdir(path.resolve(__dirname, 'workspaces', dir));
}
4 changes: 4 additions & 0 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../tsconfig.json",
"include": ["**/*.ts", "setup.ts"]
}

0 comments on commit 80bb67d

Please sign in to comment.