From 796ba363f524c3e8ee72ee58986ffc4edf88963e Mon Sep 17 00:00:00 2001 From: Jose Acevedo Date: Sat, 21 May 2022 01:53:40 -0400 Subject: [PATCH] test(napi-derive): add trybuild negative macro tests --- .github/workflows/test.yaml | 1 + examples/napi/.gitignore | 3 ++- examples/napi/Cargo.toml | 3 +++ examples/napi/tests/README.md | 16 ++++++++++++++++ examples/napi/tests/build_error_tests/mod.rs | 6 ++++++ .../tests/build_error_tests/ts_arg_type_1.rs | 13 +++++++++++++ .../tests/build_error_tests/ts_arg_type_1.stderr | 5 +++++ .../tests/build_error_tests/ts_arg_type_2.rs | 13 +++++++++++++ .../tests/build_error_tests/ts_arg_type_2.stderr | 5 +++++ .../tests/build_error_tests/ts_arg_type_3.rs | 13 +++++++++++++ .../tests/build_error_tests/ts_arg_type_3.stderr | 5 +++++ .../tests/build_error_tests/ts_arg_type_4.rs | 13 +++++++++++++ .../tests/build_error_tests/ts_arg_type_4.stderr | 5 +++++ examples/napi/tests/macro_tests.rs | 11 +++++++++++ package.json | 1 + 15 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 examples/napi/tests/README.md create mode 100644 examples/napi/tests/build_error_tests/mod.rs create mode 100644 examples/napi/tests/build_error_tests/ts_arg_type_1.rs create mode 100644 examples/napi/tests/build_error_tests/ts_arg_type_1.stderr create mode 100644 examples/napi/tests/build_error_tests/ts_arg_type_2.rs create mode 100644 examples/napi/tests/build_error_tests/ts_arg_type_2.stderr create mode 100644 examples/napi/tests/build_error_tests/ts_arg_type_3.rs create mode 100644 examples/napi/tests/build_error_tests/ts_arg_type_3.stderr create mode 100644 examples/napi/tests/build_error_tests/ts_arg_type_4.rs create mode 100644 examples/napi/tests/build_error_tests/ts_arg_type_4.stderr create mode 100644 examples/napi/tests/macro_tests.rs diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 6e7da622..ffd44074 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -70,6 +70,7 @@ jobs: yarn build:test yarn test --verbose yarn tsc -p examples/napi/tsconfig.json --noEmit + yarn test:macro - name: Electron tests if: matrix.os != 'ubuntu-latest' diff --git a/examples/napi/.gitignore b/examples/napi/.gitignore index d849c504..a59f269a 100644 --- a/examples/napi/.gitignore +++ b/examples/napi/.gitignore @@ -1 +1,2 @@ -*.node \ No newline at end of file +*.node +wip/ diff --git a/examples/napi/Cargo.toml b/examples/napi/Cargo.toml index e059d482..008ec378 100644 --- a/examples/napi/Cargo.toml +++ b/examples/napi/Cargo.toml @@ -28,3 +28,6 @@ serde_json = "1" [build-dependencies] napi-build = { path = "../../crates/build" } + +[dev-dependencies] +trybuild = "1.0" diff --git a/examples/napi/tests/README.md b/examples/napi/tests/README.md new file mode 100644 index 00000000..e14570c1 --- /dev/null +++ b/examples/napi/tests/README.md @@ -0,0 +1,16 @@ +## [trybuild](https://docs.rs/trybuild/latest/trybuild/) tests + +Set of tests to exercise various macro expansion errors. + +### Notes on adding a new test + +- Make sure to include the new test in [build_error_tests/mod.rs](./build_error_tests/mod.rs) so it gets included as + part of the crate for formatting purposes. + +### Running the tests + +> yarn test:macro + +or + +> cargo test -p napi-examples diff --git a/examples/napi/tests/build_error_tests/mod.rs b/examples/napi/tests/build_error_tests/mod.rs new file mode 100644 index 00000000..69fc2836 --- /dev/null +++ b/examples/napi/tests/build_error_tests/mod.rs @@ -0,0 +1,6 @@ +//! Include the test files here so they can be formatted properly with `cargo fmt` + +pub mod ts_arg_type_1; +pub mod ts_arg_type_2; +pub mod ts_arg_type_3; +pub mod ts_arg_type_4; diff --git a/examples/napi/tests/build_error_tests/ts_arg_type_1.rs b/examples/napi/tests/build_error_tests/ts_arg_type_1.rs new file mode 100644 index 00000000..392ba243 --- /dev/null +++ b/examples/napi/tests/build_error_tests/ts_arg_type_1.rs @@ -0,0 +1,13 @@ +//! This is testing that `#[napi(ts_args_type="...")]` and `#[napi(ts_arg_type="...")]` +//! are mutually exclusive + +use napi_derive::napi; + +#[napi(ts_args_type = "u: number, fn: object")] +pub fn add(u: u32, #[napi(ts_arg_type = "object")] f: Option) { + println!("Hello, world! {f:?}-{u}"); +} + +// Needed for the trybuild tests. +#[allow(unused)] +fn main() {} diff --git a/examples/napi/tests/build_error_tests/ts_arg_type_1.stderr b/examples/napi/tests/build_error_tests/ts_arg_type_1.stderr new file mode 100644 index 00000000..f6994ca6 --- /dev/null +++ b/examples/napi/tests/build_error_tests/ts_arg_type_1.stderr @@ -0,0 +1,5 @@ +error: Found a 'ts_args_type'="u: number, fn: object" override. Cannot use 'ts_arg_type' at the same time since they are mutually exclusive. + --> tests/build_error_tests/ts_arg_type_1.rs:7:22 + | +7 | pub fn add(u: u32, #[napi(ts_arg_type = "object")] f: Option) { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/examples/napi/tests/build_error_tests/ts_arg_type_2.rs b/examples/napi/tests/build_error_tests/ts_arg_type_2.rs new file mode 100644 index 00000000..ce7db1f2 --- /dev/null +++ b/examples/napi/tests/build_error_tests/ts_arg_type_2.rs @@ -0,0 +1,13 @@ +//! This is testing that `#[napi(ts_arg_type="...")]` fails if the argument for `ts_arg_type` +//! is not a string literal. + +use napi_derive::napi; + +#[napi] +pub fn add(u: u32, #[napi(ts_arg_type = 32)] f: Option) { + println!("Hello, world! {f:?}-{u}"); +} + +// Needed for the trybuild tests. +#[allow(unused)] +fn main() {} diff --git a/examples/napi/tests/build_error_tests/ts_arg_type_2.stderr b/examples/napi/tests/build_error_tests/ts_arg_type_2.stderr new file mode 100644 index 00000000..7b7515eb --- /dev/null +++ b/examples/napi/tests/build_error_tests/ts_arg_type_2.stderr @@ -0,0 +1,5 @@ +error: Expected a string literal + --> tests/build_error_tests/ts_arg_type_2.rs:7:41 + | +7 | pub fn add(u: u32, #[napi(ts_arg_type = 32)] f: Option) { + | ^^ diff --git a/examples/napi/tests/build_error_tests/ts_arg_type_3.rs b/examples/napi/tests/build_error_tests/ts_arg_type_3.rs new file mode 100644 index 00000000..a987f2bd --- /dev/null +++ b/examples/napi/tests/build_error_tests/ts_arg_type_3.rs @@ -0,0 +1,13 @@ +//! This is testing that `#[napi(ts_arg_type="...")]` fails if the attribute is not a `MetaNameValue` +//! i.e. it's a name value pair. + +use napi_derive::napi; + +#[napi] +pub fn add(u: u32, #[napi(ts_arg_type, not_expected)] f: Option) { + println!("Hello, world! {f:?}-{u}"); +} + +// Needed for the trybuild tests. +#[allow(unused)] +fn main() {} diff --git a/examples/napi/tests/build_error_tests/ts_arg_type_3.stderr b/examples/napi/tests/build_error_tests/ts_arg_type_3.stderr new file mode 100644 index 00000000..7c70a888 --- /dev/null +++ b/examples/napi/tests/build_error_tests/ts_arg_type_3.stderr @@ -0,0 +1,5 @@ +error: Expected Name Value + --> tests/build_error_tests/ts_arg_type_3.rs:7:27 + | +7 | pub fn add(u: u32, #[napi(ts_arg_type, not_expected)] f: Option) { + | ^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/examples/napi/tests/build_error_tests/ts_arg_type_4.rs b/examples/napi/tests/build_error_tests/ts_arg_type_4.rs new file mode 100644 index 00000000..87e55a19 --- /dev/null +++ b/examples/napi/tests/build_error_tests/ts_arg_type_4.rs @@ -0,0 +1,13 @@ +//! This is testing that `#[napi(ts_arg_type="...")]` fails if the attribute is something other than +//! `ts_arg_type` + +use napi_derive::napi; + +#[napi] +pub fn add(u: u32, #[napi(not_expected = "obj")] f: Option) { + println!("Hello, world! {f:?}-{u}"); +} + +// Needed for the trybuild tests. +#[allow(unused)] +fn main() {} diff --git a/examples/napi/tests/build_error_tests/ts_arg_type_4.stderr b/examples/napi/tests/build_error_tests/ts_arg_type_4.stderr new file mode 100644 index 00000000..dc0a40e1 --- /dev/null +++ b/examples/napi/tests/build_error_tests/ts_arg_type_4.stderr @@ -0,0 +1,5 @@ +error: Did not find 'ts_arg_type' + --> tests/build_error_tests/ts_arg_type_4.rs:7:27 + | +7 | pub fn add(u: u32, #[napi(not_expected = "obj")] f: Option) { + | ^^^^^^^^^^^^ diff --git a/examples/napi/tests/macro_tests.rs b/examples/napi/tests/macro_tests.rs new file mode 100644 index 00000000..0431043f --- /dev/null +++ b/examples/napi/tests/macro_tests.rs @@ -0,0 +1,11 @@ +// `error_try_builds` is not really a feature. +// This is used just to make the test files part of the crate so that they can get included by things +// like `cargo fmt` for example, BUT not be part of compilation when running `cargo test`. +#[cfg(feature = "error_try_builds")] +mod build_error_tests; + +#[test] +fn run_build_error_tests() { + let t = trybuild::TestCases::new(); + t.compile_fail("tests/build_error_tests/ts_arg_type_*.rs"); +} diff --git a/package.json b/package.json index 313fdbaf..1a5eaa32 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "prepublishOnly": "npm run build && pinst --disable", "test": "ava \"./examples/napi/**/*.ts\" && ava --no-worker-threads \"./examples/napi-compat-mode/**/*.ts\" && ava \"./cli/**/*.ts\"", "test:electron": "electron examples/napi/electron.js", + "test:macro": "cargo test -p napi-examples", "test:memory": "node memory-testing/index.mjs", "postinstall": "husky install", "postpublish": "pinst --enable"