Skip to content

Commit

Permalink
simplify tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-marechal committed Nov 12, 2021
1 parent e0ba692 commit 1840641
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 54 deletions.
10 changes: 10 additions & 0 deletions test/helpers/module-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,13 @@ export async function cleanupTestModule(testModulePath: string): Promise<void> {
await fs.remove(testModulePath);
resetMSVSVersion();
}

export async function clearTestModuleDependencies(testModulePath: string): Promise<void> {
const packageJsonPath = path.join(testModulePath, 'package.json');
const packageJson = await fs.readJSON(packageJsonPath);
packageJson.dependencies = {};
packageJson.devDependencies = {};
packageJson.optionalDependencies = {};
packageJson.peerDependencies = {};
await fs.writeJSON(packageJsonPath, packageJson, { spaces: 2 });
}
57 changes: 3 additions & 54 deletions test/rebuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as fs from 'fs-extra';
import * as path from 'path';
import * as os from 'os';

import { cleanupTestModule, MINUTES_IN_MILLISECONDS, resetMSVSVersion, resetTestModule, TIMEOUT_IN_MILLISECONDS } from './helpers/module-setup';
import { cleanupTestModule, clearTestModuleDependencies, MINUTES_IN_MILLISECONDS, resetMSVSVersion, resetTestModule, TIMEOUT_IN_MILLISECONDS } from './helpers/module-setup';
import { expectNativeModuleToBeRebuilt, expectNativeModuleToNotBeRebuilt } from './helpers/rebuild';
import { getExactElectronVersionSync } from './helpers/electron-version';
import { rebuild } from '../src/rebuild';
Expand Down Expand Up @@ -123,6 +123,7 @@ describe('rebuilder', () => {
afterEach(async() => await cleanupTestModule(testModulePath));

it('should rebuild only specified modules', async () => {
await clearTestModuleDependencies(testModulePath);
const nativeModuleBinary = path.join(testModulePath, 'node_modules', 'native-hello-world', 'build', 'Release', 'hello_world.node');
expect(await fs.pathExists(nativeModuleBinary)).to.be.true;
await fs.remove(nativeModuleBinary);
Expand All @@ -132,6 +133,7 @@ describe('rebuilder', () => {
electronVersion: testElectronVersion,
arch: process.arch,
onlyModules: ['native-hello-world'],
extraModules: ['native-hello-world', 'ffi-napi', 'ref-napi'],
force: true
});
let built = 0;
Expand Down Expand Up @@ -194,57 +196,4 @@ describe('rebuilder', () => {
await expectNativeModuleToBeRebuilt(testModulePath, 'ffi-napi');
});
});

describe('rebuilding from different package', function() {
this.timeout(MINUTES_IN_MILLISECONDS);

beforeEach(async () => {
await resetTestModule(testModulePath);
const packageJsonPath = path.join(testModulePath, 'package.json');
const packageJson = await fs.readJSON(packageJsonPath);
packageJson.dependencies = {};
packageJson.devDependencies = {};
packageJson.optionalDependencies = {};
packageJson.peerDependencies = {};
await fs.writeJSON(packageJsonPath, packageJson, { spaces: 2 });
});
afterEach(async() => await cleanupTestModule(testModulePath));

it('should not rebuild anything if package.json does not depend on anything', async () => {
const nativeModuleBinary = path.join(testModulePath, 'node_modules', 'native-hello-world', 'build', 'Release', 'hello_world.node');
expect(await fs.pathExists(nativeModuleBinary)).to.be.true;
await fs.remove(nativeModuleBinary);
expect(await fs.pathExists(nativeModuleBinary)).to.be.false;
const rebuilder = rebuild({
buildPath: testModulePath,
electronVersion: testElectronVersion,
arch: process.arch,
force: true
});
let built = 0;
rebuilder.lifecycle.on('module-done', () => built++);
await rebuilder;
expect(built).to.equal(0);
expect(await fs.pathExists(nativeModuleBinary)).to.be.false;
});

it('should rebuild extraModules even if package.json does not depend on it', async () => {
const nativeModuleBinary = path.join(testModulePath, 'node_modules', 'native-hello-world', 'build', 'Release', 'hello_world.node');
expect(await fs.pathExists(nativeModuleBinary)).to.be.true;
await fs.remove(nativeModuleBinary);
expect(await fs.pathExists(nativeModuleBinary)).to.be.false;
const rebuilder = rebuild({
buildPath: testModulePath,
electronVersion: testElectronVersion,
arch: process.arch,
extraModules: ['native-hello-world'],
force: true
});
let built = 0;
rebuilder.lifecycle.on('module-done', () => built++);
await rebuilder;
expect(built).to.equal(1);
expect(await fs.pathExists(nativeModuleBinary)).to.be.true;
});
});
});

0 comments on commit 1840641

Please sign in to comment.