Skip to content

Commit 2d375d0

Browse files
committed
add tests for rebuilding scoped native modules
1 parent 64aea85 commit 2d375d0

File tree

8 files changed

+113
-6
lines changed

8 files changed

+113
-6
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "native-app",
3+
"productName": "Native App",
4+
"version": "1.0.0",
5+
"description": "",
6+
"main": "src/index.js",
7+
"scripts": {
8+
"start": "electron-forge start"
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "MIT",
13+
"config": {
14+
"forge": "./forge.config.js"
15+
},
16+
"devDependencies": {
17+
"@types/node": "^12.0.10",
18+
"@scoped/native-addon": "1.2.3",
19+
"ffi-napi": "2.4.5"
20+
},
21+
"dependencies": {
22+
"@newrelic/native-metrics": "5.3.0",
23+
"farmhash": "3.2.1",
24+
"level": "6.0.0",
25+
"native-hello-world": "2.0.0",
26+
"ref-napi": "1"
27+
}
28+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"targets": [
3+
{
4+
"target_name": "hello",
5+
"sources": [ "hello.c" ]
6+
}
7+
]
8+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <assert.h>
2+
#include <node_api.h>
3+
4+
static napi_value Method(napi_env env, napi_callback_info info) {
5+
napi_status status;
6+
napi_value world;
7+
status = napi_create_string_utf8(env, "world", 5, &world);
8+
assert(status == napi_ok);
9+
return world;
10+
}
11+
12+
#define DECLARE_NAPI_METHOD(name, func) \
13+
{ name, 0, func, 0, 0, 0, napi_default, 0 }
14+
15+
static napi_value Init(napi_env env, napi_value exports) {
16+
napi_status status;
17+
napi_property_descriptor desc = DECLARE_NAPI_METHOD("hello", Method);
18+
status = napi_define_properties(env, exports, 1, &desc);
19+
assert(status == napi_ok);
20+
return exports;
21+
}
22+
23+
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const addon = require('./build/Release/hello.node');
2+
console.log(addon.hello()); // 'world'
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"private": true,
3+
"name": "@scoped/native-addon",
4+
"version": "1.2.3",
5+
"main": "index.js",
6+
"license": "MIT"
7+
}

test/fixture/native-app2/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"private": true,
3+
"workspaces": [
4+
"native-addon",
5+
"app"
6+
]
7+
}

test/helpers/module-setup.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,22 @@ export function resetMSVSVersion(): void {
1414
}
1515
}
1616

17-
export async function resetTestModule(testModulePath: string): Promise<void> {
17+
export interface ResetTestModuleOptions {
18+
packageManager?: string
19+
fixturePath?: string
20+
}
21+
export async function resetTestModule(testModulePath: string, options: ResetTestModuleOptions = {}): Promise<void> {
22+
const {
23+
packageManager = 'npm',
24+
fixturePath = path.resolve(__dirname, '../../test/fixture/native-app1')
25+
} = options;
1826
await fs.remove(testModulePath);
1927
await fs.mkdir(testModulePath, { recursive: true });
20-
await fs.copyFile(
21-
path.resolve(__dirname, '../../test/fixture/native-app1/package.json'),
22-
path.resolve(testModulePath, 'package.json')
28+
await fs.copy(
29+
path.resolve(fixturePath),
30+
path.resolve(testModulePath),
2331
);
24-
await spawn('npm', ['install'], { cwd: testModulePath });
32+
await spawn(packageManager, ['install'], { cwd: testModulePath });
2533
resetMSVSVersion();
2634
}
2735

test/rebuild.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ describe('rebuilder', () => {
146146
buildPath: testModulePath,
147147
electronVersion: testElectronVersion,
148148
arch: process.arch,
149-
onlyModules: ['ffi-napi', 'ref-napi'], // TODO: check to see if there's a bug with scoped modules
149+
onlyModules: ['ffi-napi', 'ref-napi'],
150150
force: true
151151
});
152152
let built = 0;
@@ -194,4 +194,28 @@ describe('rebuilder', () => {
194194
await expectNativeModuleToBeRebuilt(testModulePath, 'ffi-napi');
195195
});
196196
});
197+
198+
describe('only rebuild (with scoped module)', function() {
199+
this.timeout(2 * MINUTES_IN_MILLISECONDS);
200+
201+
before(async () => await resetTestModule(testModulePath, {
202+
packageManager: 'yarn',
203+
fixturePath: path.resolve(__dirname, 'fixture/native-app2'),
204+
}));
205+
after(async () => await cleanupTestModule(testModulePath));
206+
207+
it('should rebuild multiple specified modules via --only option', async () => {
208+
const rebuilder = rebuild({
209+
buildPath: path.resolve(testModulePath, 'app'),
210+
electronVersion: testElectronVersion,
211+
arch: process.arch,
212+
onlyModules: ['ffi-napi', 'ref-napi', '@scoped/native-addon'],
213+
force: true
214+
});
215+
let built = 0;
216+
rebuilder.lifecycle.on('module-done', () => built++);
217+
await rebuilder;
218+
expect(built).to.equal(3);
219+
});
220+
});
197221
});

0 commit comments

Comments
 (0)