Skip to content

Commit

Permalink
add tests for rebuilding scoped native modules
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-marechal committed Sep 6, 2021
1 parent 64aea85 commit d84d91e
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 6 deletions.
28 changes: 28 additions & 0 deletions test/fixture/native-app2/app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "native-app",
"productName": "Native App",
"version": "1.0.0",
"description": "",
"main": "src/index.js",
"scripts": {
"start": "electron-forge start"
},
"keywords": [],
"author": "",
"license": "MIT",
"config": {
"forge": "./forge.config.js"
},
"devDependencies": {
"@types/node": "^12.0.10",
"@scoped/native-addon": "1.2.3",
"ffi-napi": "2.4.5"
},
"dependencies": {
"@newrelic/native-metrics": "5.3.0",
"farmhash": "3.2.1",
"level": "6.0.0",
"native-hello-world": "2.0.0",
"ref-napi": "1"
}
}
8 changes: 8 additions & 0 deletions test/fixture/native-app2/native-addon/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"targets": [
{
"target_name": "hello",
"sources": [ "hello.c" ]
}
]
}
23 changes: 23 additions & 0 deletions test/fixture/native-app2/native-addon/hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <assert.h>
#include <node_api.h>

static napi_value Method(napi_env env, napi_callback_info info) {
napi_status status;
napi_value world;
status = napi_create_string_utf8(env, "world", 5, &world);
assert(status == napi_ok);
return world;
}

#define DECLARE_NAPI_METHOD(name, func) \
{ name, 0, func, 0, 0, 0, napi_default, 0 }

static napi_value Init(napi_env env, napi_value exports) {
napi_status status;
napi_property_descriptor desc = DECLARE_NAPI_METHOD("hello", Method);
status = napi_define_properties(env, exports, 1, &desc);
assert(status == napi_ok);
return exports;
}

NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
2 changes: 2 additions & 0 deletions test/fixture/native-app2/native-addon/hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const addon = require('./build/Release/hello.node');
console.log(addon.hello()); // 'world'
7 changes: 7 additions & 0 deletions test/fixture/native-app2/native-addon/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"private": true,
"name": "@scoped/native-addon",
"version": "1.2.3",
"main": "index.js",
"license": "MIT"
}
7 changes: 7 additions & 0 deletions test/fixture/native-app2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"private": true,
"workspaces": [
"native-addon",
"app"
]
}
18 changes: 13 additions & 5 deletions test/helpers/module-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,22 @@ export function resetMSVSVersion(): void {
}
}

export async function resetTestModule(testModulePath: string): Promise<void> {
export interface ResetTestModuleOptions {
packageManager?: string
fixturePath?: string
}
export async function resetTestModule(testModulePath: string, options: ResetTestModuleOptions = {}): Promise<void> {
const {
packageManager = 'npm',
fixturePath = path.resolve(__dirname, '../../test/fixture/native-app1')
} = options;
await fs.remove(testModulePath);
await fs.mkdir(testModulePath, { recursive: true });
await fs.copyFile(
path.resolve(__dirname, '../../test/fixture/native-app1/package.json'),
path.resolve(testModulePath, 'package.json')
await fs.copy(
path.resolve(fixturePath),
path.resolve(testModulePath),
);
await spawn('npm', ['install'], { cwd: testModulePath });
await spawn(packageManager, ['install'], { cwd: testModulePath });
resetMSVSVersion();
}

Expand Down
40 changes: 39 additions & 1 deletion test/rebuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ describe('rebuilder', () => {
buildPath: testModulePath,
electronVersion: testElectronVersion,
arch: process.arch,
onlyModules: ['ffi-napi', 'ref-napi'], // TODO: check to see if there's a bug with scoped modules
onlyModules: ['ffi-napi', 'ref-napi'],
force: true
});
let built = 0;
Expand Down Expand Up @@ -194,4 +194,42 @@ describe('rebuilder', () => {
await expectNativeModuleToBeRebuilt(testModulePath, 'ffi-napi');
});
});

describe('only rebuild (with scoped module)', function() {
this.timeout(2 * MINUTES_IN_MILLISECONDS);

before(async () => await resetTestModule(testModulePath, {
packageManager: 'yarn',
fixturePath: path.resolve(__dirname, 'fixture/native-app2'),
}));
after(async () => await cleanupTestModule(testModulePath));

it('should rebuild multiple specified modules via --only option even when not prefixed with its scope', async () => {
const rebuilder = rebuild({
buildPath: path.resolve(testModulePath, 'app'),
electronVersion: testElectronVersion,
arch: process.arch,
onlyModules: ['ffi-napi', 'ref-napi', 'native-addon'],
force: true
});
let built = 0;
rebuilder.lifecycle.on('module-done', () => built++);
await rebuilder;
expect(built).to.equal(3);
});

it('should rebuild multiple specified modules via --only option when prefixed with its scope', async () => {
const rebuilder = rebuild({
buildPath: path.resolve(testModulePath, 'app'),
electronVersion: testElectronVersion,
arch: process.arch,
onlyModules: ['ffi-napi', 'ref-napi', '@scoped/native-addon'],
force: true
});
let built = 0;
rebuilder.lifecycle.on('module-done', () => built++);
await rebuilder;
expect(built).to.equal(3);
});
});
});

0 comments on commit d84d91e

Please sign in to comment.