Skip to content

Commit 9d0b0bc

Browse files
committed
wip: added tests and general polish
[ci skip]
1 parent 2fde2c4 commit 9d0b0bc

File tree

8 files changed

+51
-41
lines changed

8 files changed

+51
-41
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ exclude = ["index.node"]
88

99
[lib]
1010
crate-type = ["cdylib"]
11-
path = "src/native/napi/lib.rs"
11+
path = "src/napi/lib.rs"
1212

1313
[dependencies]
1414
napi = { version = "2", features = ["async", "napi6", "serde-json"] }

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
"prepublishOnly": "node ./scripts/prepublishOnly.js",
2727
"ts-node": "ts-node",
2828
"test": "jest",
29-
"lint": "eslint '{src,tests,scripts}/**/*.{js,ts,json}' 'benches/**/*.{js,ts}'",
30-
"lintfix": "eslint '{src,tests,scripts}/**/*.{js,ts,json}' 'benches/**/*.{js,ts}' --fix",
29+
"lint": "eslint '{src,tests,scripts}/**/*.{js,ts,json}'",
30+
"lintfix": "eslint '{src,tests,scripts}/**/*.{js,ts,json}' --fix",
3131
"lint-shell": "find ./src ./tests ./scripts -type f -regextype posix-extended -regex '.*\\.(sh)' -exec shellcheck {} +",
3232
"docs": "shx rm -rf ./docs && typedoc --gitRevision master --tsconfig ./tsconfig.build.json --out ./docs src"
3333
},

src/native/exec.ts renamed to src/exec.ts

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
/**
2-
* JS binding to NAPI quiche dynamic library.
2+
* JS binding to NAPI uapi.
33
* This code was derived from the auto-generated binding and declaration
44
* files provided by napi-rs.
55
*/
66
import process from 'process';
77
import path from 'path';
88

99
interface Exec {
10-
test(tings: Array<string>): undefined;
11-
execvpe(cmd: string, argv: Array<string>): undefined;
10+
execvpe(cmd: string, argv: Array<string>, envv: Array<string>): undefined;
1211
}
1312

14-
const projectRoot = path.join(__dirname, '../../');
13+
const projectRoot = path.join(__dirname, '../');
1514
const prebuildPath = path.join(projectRoot, 'prebuild');
1615

1716
/**
@@ -47,25 +46,10 @@ function requireBinding(targets: Array<string>): Exec {
4746
let nativeBinding: Exec;
4847

4948
/**
50-
* For desktop we only support win32, darwin and linux.
49+
* For desktop we only support darwin and linux.
5150
* Mobile OS support is pending.
5251
*/
5352
switch (process.platform) {
54-
case 'win32':
55-
switch (process.arch) {
56-
case 'x64':
57-
nativeBinding = requireBinding(['win32-x64']);
58-
break;
59-
case 'ia32':
60-
nativeBinding = requireBinding(['win32-ia32']);
61-
break;
62-
case 'arm64':
63-
nativeBinding = requireBinding(['win32-arm64']);
64-
break;
65-
default:
66-
throw new Error(`Unsupported architecture on Windows: ${process.arch}`);
67-
}
68-
break;
6953
case 'darwin':
7054
switch (process.arch) {
7155
case 'x64':
File renamed without changes.
Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,14 @@ fn set_fd_cloexec() -> uapi::Result<()> {
1616
Ok(())
1717
}
1818

19-
#[napi(ts_args_type="things: Array<string>")]
20-
pub fn test(things: Array) -> () {
21-
match things.get::<String>(0) {
22-
Ok(Some(thing)) => println!("thinggy {:?}", thing),
23-
_ => println!("failed"),
24-
}
25-
}
26-
27-
#[napi(ts_args_type="cmd: string, argv: Array<string>")]
28-
pub fn execvpe(cmd: String, argv: Array) -> napi::Result<()> {
19+
#[napi(ts_args_type="cmd: string, argv: Array<string>, envp: Array<string>")]
20+
pub fn execvpe(cmd: String, argv: Array, envp: Array) -> napi::Result<()> {
2921
// Before making the call we need to configure the stdio. This ensures we get the output of the exec'ed program.
3022
set_fd_cloexec().or_else(
3123
|e| Err(napi::Error::from_reason(e.to_string()))
3224
)?;
3325

26+
// Setting up the arg parameters
3427
let mut argv_ = uapi::UstrPtr::new();
3528
argv_.push(cmd.clone());
3629
for i in 0..argv.len() {
@@ -43,12 +36,24 @@ pub fn execvpe(cmd: String, argv: Array) -> napi::Result<()> {
4336
_ => Ok(()),
4437
}?;
4538
}
46-
let envv = uapi::UstrPtr::new();
39+
40+
// Setting up the env parameters
41+
let mut envp_ = uapi::UstrPtr::new();
42+
for i in 0..envp.len() {
43+
match envp.get::<String>(i) {
44+
Ok(Some(value)) => {
45+
envp_.push(value);
46+
Ok(())
47+
},
48+
Err(e) => Err(napi::Error::from_reason(e.to_string())),
49+
_ => Ok(()),
50+
}?;
51+
}
4752

4853
uapi::execvpe(
4954
cmd,
5055
&argv_,
51-
&envv,
56+
&envp_,
5257
).or_else(
5358
|e| Err(napi::Error::from_reason(e.to_string()))
5459
)

tests/Exec.test.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
import { exec } from '@/native'
1+
import child_process from 'child_process';
2+
import { exec } from '@';
23

3-
test('asd', async () => {
4-
5-
console.log('doing test!');
6-
console.log(exec.test());
7-
})
4+
test('execvpe should provide environment variables', async () => {
5+
const output = child_process.execFileSync('./tests/callExecvpe.ts');
6+
const jsonOut = JSON.parse(output.toString());
7+
expect(jsonOut).toMatchObject({
8+
test: 'asd',
9+
best: 'bsd',
10+
});
11+
});
12+
test('execvpe should throw when call fails', async () => {
13+
// Code 2 corresponds to `ENOENT`
14+
expect(() => exec.execvpe('invalidCommand', [], [])).toThrow('2');
15+
});

tests/callExecvpe.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env ts-node
2+
3+
import { exec } from '@';
4+
// This executes `printEnv.js` while setting environment variables
5+
// eslint-disable-next-line no-console
6+
console.log(
7+
exec.execvpe('node', ['./tests/printEnv.js'], ['test=asd', 'best=bsd']),
8+
);

tests/printEnv.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env node
2+
3+
// Only need to print out the current environment
4+
// eslint-disable-next-line no-console
5+
console.log(JSON.stringify(process.env, undefined, 1));

0 commit comments

Comments
 (0)