Skip to content

Commit

Permalink
fix: 🐛 only download binary if PACT_SKIP_BINARY_INSTALL is true
Browse files Browse the repository at this point in the history
  • Loading branch information
danymarques committed Feb 23, 2021
1 parent 1143af5 commit 69cbba0
Show file tree
Hide file tree
Showing 13 changed files with 8,338 additions and 3,310 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,13 @@ log
reports
tmp
.tmp

# jest config
!jest.config.js

# mocha config
!.mocharc.js
!ts-node.js

# jest mocks
!__mocks__
9 changes: 9 additions & 0 deletions .mocharc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
exclude: '**/*.jest.spec.ts',
require: 'ts-node/register',
timeout: '30000',
slow: '5000',
exit: true,
require: 'ts-node.js',
'check-leaks': true,
};
2 changes: 1 addition & 1 deletion download-checksums.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
require("./standalone/install").downloadChecksums();
require('./standalone/install').downloadChecksums();
9 changes: 9 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
testMatch: ['**/?(*.)jest.spec.ts'],
transform: { '^.+\\.(ts)$': 'ts-jest' },
globals: {
'ts-jest': {
tsconfig: 'tsconfig.spec.json',
},
},
};
11,286 changes: 8,110 additions & 3,176 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@
"access": "public"
},
"dependencies": {
"@types/pino": "^6.3.5",
"@types/q": "1.0.7",
"@types/request": "2.48.2",
"@types/pino": "^6.3.5",
"chalk": "2.3.1",
"check-types": "7.3.0",
"cross-spawn": "^7.0.1",
Expand All @@ -79,6 +79,7 @@
"@types/cross-spawn": "^6.0.1",
"@types/decompress": "^4.2.3",
"@types/express": "4.11.1",
"@types/jest": "^25.2.3",
"@types/mkdirp": "^0.5.2",
"@types/mocha": "2.2.48",
"@types/node": "9.4.6",
Expand All @@ -100,12 +101,14 @@
"eslint-config-prettier": "^6.3.0",
"eslint-plugin-prettier": "^3.1.1",
"express": "4.16.2",
"jest": "^25.5.4",
"mocha": "^7.1.1",
"nodemon": "^2.0.4",
"prettier": "^1.18.2",
"sinon": "9.2.4",
"snyk": "^1.230.5",
"standard-version": "^9.1.0",
"ts-jest": "^25.5.1",
"ts-node": "8.3.0",
"typescript": "3.5.3"
},
Expand All @@ -120,7 +123,7 @@
"clean": "rimraf '{src,test,bin,standalone}/**/*.{js,map,d.ts}' 'package.zip' '.tmp' 'tmp', 'standalone/{darwin,linux,win}*'",
"lint": "eslint . --ext .ts --config .eslintrc",
"pretest": "npm run prettier:check && npm run lint && npm run build && npm run download-checksums && npm run install:current",
"test": "cross-env LOGLEVEL=debug PACT_DO_NOT_TRACK=true mocha -r ts-node/register -t 30000 -s 5000 --check-leaks --exit \"{src,test,bin,standalone}/**/*.spec.ts\"",
"test": "cross-env LOGLEVEL=debug PACT_DO_NOT_TRACK=true mocha \"{src,test,bin,standalone}/**/*.spec.ts\" && jest",
"dev": "npm run lint --force && npm test && node .",
"watch": "nodemon -e ts,json --ignore '**/*.d.ts' -x npm run dev",
"build": "npm run clean && tsc",
Expand Down
29 changes: 29 additions & 0 deletions standalone/__mocks__/fs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
interface Files {
[fileName: string]: string;
}

export interface FS {
initFS: (files: Files) => void;
existsSync: (path: string) => boolean;
}

const mockFS: FS = jest.genMockFromModule('fs');

let mockedFiles: Files = {};

function initFS(mockFiles: Files): void {
mockedFiles = mockFiles;

Object.keys(mockFiles).forEach(filePath => {
jest.mock(filePath, () => mockFiles[filePath], { virtual: true });
});
}

function existsSync(path: string): boolean {
return !!mockedFiles[path];
}

mockFS.initFS = initFS;
mockFS.existsSync = existsSync;

module.exports = mockFS;
135 changes: 135 additions & 0 deletions standalone/install.jest.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import * as fs from 'fs';
import * as path from 'path';
import install, { BinaryEntry, createConfig, getBinaryEntry } from './install';
import { FS } from './__mocks__/fs';

jest.mock('fs');
jest.mock('sumchecker', () =>
jest.fn().mockImplementation(() => Promise.resolve()),
);
jest.mock('libnpmconfig', () => ({
read: (): jest.Mock => jest.fn(),
}));
jest.mock('tar');

describe('Install', () => {
const packageBasePath: string = path.resolve(__dirname, '__fixtures__');
const packagePath: string = path.resolve(packageBasePath, 'package.json');

// eslint-disable-next-line @typescript-eslint/no-var-requires
let packageConfig = JSON.parse(JSON.stringify(require('../package.json')));
let OLD_ENV: { [key: string]: string | undefined };

function initFS(config: {
pact_binary_location?: string;
pact_do_not_track?: boolean;
}): void {
((fs as unknown) as FS).initFS({
[packagePath]: {
...packageConfig,
config,
},
});
}

beforeEach(() => {
jest.resetModules();
OLD_ENV = { ...process.env };
});

afterEach(() => {
process.env = { ...OLD_ENV };
});

describe('Package.json Configuration', () => {
describe('Binary Location', () => {
it('Should be able to set binary location as an absolute path', () => {
// eslint-disable-next-line @typescript-eslint/camelcase
const pact_binary_location = '/some-location/or-other';
initFS({
// eslint-disable-next-line @typescript-eslint/camelcase
pact_binary_location,
});
const config = createConfig(packageBasePath);
config.binaries.forEach((entry: BinaryEntry) => {
expect(entry.downloadLocation).toEqual(
path.resolve(pact_binary_location),
);
});
});

it('Should be able to set binary location as an relative path', () => {
// eslint-disable-next-line @typescript-eslint/camelcase
const pact_binary_location = 'some-location/or-other';
// eslint-disable-next-line @typescript-eslint/camelcase
initFS({ pact_binary_location });
const config = createConfig(packageBasePath);
config.binaries.forEach((entry: BinaryEntry) => {
expect(entry.downloadLocation).toEqual(
path.resolve(packageBasePath, pact_binary_location),
);
});
});

it('Should be able to set binary location as an HTTP URL', () => {
// eslint-disable-next-line @typescript-eslint/camelcase
const pact_binary_location = 'http://some.url';
// eslint-disable-next-line @typescript-eslint/camelcase
initFS({ pact_binary_location });
const config = createConfig(packageBasePath);
config.binaries.forEach((entry: BinaryEntry) => {
expect(entry.downloadLocation).toEqual(pact_binary_location);
});
});

it('Should be able to set binary location as an HTTPS URL', () => {
// eslint-disable-next-line @typescript-eslint/camelcase
const pact_binary_location = 'https://some.url';
// eslint-disable-next-line @typescript-eslint/camelcase
initFS({ pact_binary_location });
const config = createConfig(packageBasePath);
config.binaries.forEach((entry: BinaryEntry) => {
expect(entry.downloadLocation).toEqual(pact_binary_location);
});
});
});

it("Should be able to set 'do not track' from package.json config", () => {
// eslint-disable-next-line @typescript-eslint/camelcase
const pact_do_not_track = true;
// eslint-disable-next-line @typescript-eslint/camelcase
initFS({ pact_do_not_track });
const config = createConfig(packageBasePath);
expect(config.doNotTrack).toEqual(pact_do_not_track);
});
});

describe('Environment variables configuration', () => {
it("Should be able to set 'do not track' from environment variable 'PACT_DO_NOT_TRACK'", () => {
const doNotTrack = true;
process.env.PACT_DO_NOT_TRACK = `${doNotTrack}`;
const config = createConfig(packageBasePath);
expect(config.doNotTrack).toEqual(doNotTrack);
});
});

describe('Skip install binary', () => {
it('Should not download it', async () => {
process.env.PACT_SKIP_BINARY_INSTALL = 'true';
const { binaryInstallSkipped } = await install('linux', 'ia32');
expect(binaryInstallSkipped).toBeTruthy();
});

it('Should download it', async () => {
const { binaryChecksum, binary } = getBinaryEntry('linux', 'ia32');
((fs as unknown) as FS).initFS({
[path.join(__dirname, binary)]: 'mock binary',
[path.join(__dirname, binaryChecksum)]: 'mock binary checksum',
});

process.env.PACT_SKIP_BINARY_INSTALL = 'false';
const { binaryInstallSkipped } = await install('linux', 'ia32');
expect(binaryInstallSkipped).toBeFalsy();
});
});
});
121 changes: 0 additions & 121 deletions standalone/install.spec.ts

This file was deleted.

Loading

0 comments on commit 69cbba0

Please sign in to comment.