Skip to content

Commit

Permalink
simplify tests
Browse files Browse the repository at this point in the history
Clear a module's package.json dependencies everytime rebuild is done
using `onlyModules`.
  • Loading branch information
paul-marechal committed Nov 12, 2021
1 parent e0ba692 commit 2bd9762
Show file tree
Hide file tree
Showing 2 changed files with 15 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 });
}
59 changes: 5 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 () => {
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 @@ -142,6 +143,7 @@ describe('rebuilder', () => {
});

it('should rebuild multiple specified modules via --only option', async () => {
clearTestModuleDependencies(testModulePath);
const rebuilder = rebuild({
buildPath: testModulePath,
electronVersion: testElectronVersion,
Expand All @@ -163,6 +165,7 @@ describe('rebuilder', () => {
after(async() => await cleanupTestModule(testModulePath));

it('should have rebuilt ffi-napi module in Debug mode', async () => {
clearTestModuleDependencies(testModulePath);
await rebuild({
buildPath: testModulePath,
electronVersion: testElectronVersion,
Expand All @@ -183,6 +186,7 @@ describe('rebuilder', () => {
after(async() => await cleanupTestModule(testModulePath));

it('should have rebuilt ffi-napi module using clang mode', async () => {
clearTestModuleDependencies(testModulePath);
await rebuild({
buildPath: testModulePath,
electronVersion: testElectronVersion,
Expand All @@ -194,57 +198,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 2bd9762

Please sign in to comment.