From 41f618a1df6bb8c66d7968ac64456139b9f4c197 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Wed, 17 May 2023 17:38:50 -0400 Subject: fix(npm): improved optional dependency support (#19135) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Note: If the package information has already been cached, then this requires running with `--reload` or for the registry information to be fetched some other way (ex. the cache busting). Closes #15544 --------- Co-authored-by: Bartek IwaƄczuk --- cli/tests/integration/npm_tests.rs | 81 ++++++++++++++++++++++ cli/tests/testdata/npm/binary_package/main.js | 1 + .../@denotest/binary-package-linux/1.0.0/index.js | 1 + .../binary-package-linux/1.0.0/package.json | 8 +++ .../@denotest/binary-package-mac/1.0.0/index.js | 1 + .../binary-package-mac/1.0.0/package.json | 8 +++ .../binary-package-windows/1.0.0/index.js | 1 + .../binary-package-windows/1.0.0/package.json | 8 +++ .../@denotest/binary-package/1.0.0/index.js | 13 ++++ .../@denotest/binary-package/1.0.0/package.json | 10 +++ 10 files changed, 132 insertions(+) create mode 100644 cli/tests/testdata/npm/binary_package/main.js create mode 100644 cli/tests/testdata/npm/registry/@denotest/binary-package-linux/1.0.0/index.js create mode 100644 cli/tests/testdata/npm/registry/@denotest/binary-package-linux/1.0.0/package.json create mode 100644 cli/tests/testdata/npm/registry/@denotest/binary-package-mac/1.0.0/index.js create mode 100644 cli/tests/testdata/npm/registry/@denotest/binary-package-mac/1.0.0/package.json create mode 100644 cli/tests/testdata/npm/registry/@denotest/binary-package-windows/1.0.0/index.js create mode 100644 cli/tests/testdata/npm/registry/@denotest/binary-package-windows/1.0.0/package.json create mode 100644 cli/tests/testdata/npm/registry/@denotest/binary-package/1.0.0/index.js create mode 100644 cli/tests/testdata/npm/registry/@denotest/binary-package/1.0.0/package.json (limited to 'cli/tests') diff --git a/cli/tests/integration/npm_tests.rs b/cli/tests/integration/npm_tests.rs index c04322027..dc3aa7b11 100644 --- a/cli/tests/integration/npm_tests.rs +++ b/cli/tests/integration/npm_tests.rs @@ -1799,3 +1799,84 @@ fn reload_info_not_found_cache_but_exists_remote() { output.assert_exit_code(0); } } + +#[test] +fn binary_package_with_optional_dependencies() { + let context = TestContextBuilder::for_npm() + .use_sync_npm_download() + .use_separate_deno_dir() // the "npm" folder means something in the deno dir, so use a separate folder + .use_copy_temp_dir("npm/binary_package") + .cwd("npm/binary_package") + .build(); + + let temp_dir = context.temp_dir(); + let temp_dir_path = temp_dir.path(); + let project_path = temp_dir_path.join("npm/binary_package"); + + // write empty config file so a lockfile gets created + temp_dir.write("npm/binary_package/deno.json", "{}"); + + // run it twice, with the first time creating the lockfile and the second using it + for i in 0..2 { + if i == 1 { + assert!(project_path.join("deno.lock").exists()); + } + + let output = context + .new_command() + .args("run -A --node-modules-dir main.js") + .run(); + + #[cfg(target_os = "windows")] + { + output.assert_exit_code(0); + output.assert_matches_text( + "[WILDCARD]Hello from binary package on windows[WILDCARD]", + ); + assert!(project_path + .join("node_modules/.deno/@denotest+binary-package-windows@1.0.0") + .exists()); + assert!(!project_path + .join("node_modules/.deno/@denotest+binary-package-linux@1.0.0") + .exists()); + assert!(!project_path + .join("node_modules/.deno/@denotest+binary-package-mac@1.0.0") + .exists()); + } + + #[cfg(target_os = "macos")] + { + output.assert_exit_code(0); + output.assert_matches_text( + "[WILDCARD]Hello from binary package on mac[WILDCARD]", + ); + + assert!(!project_path + .join("node_modules/.deno/@denotest+binary-package-windows@1.0.0") + .exists()); + assert!(!project_path + .join("node_modules/.deno/@denotest+binary-package-linux@1.0.0") + .exists()); + assert!(project_path + .join("node_modules/.deno/@denotest+binary-package-mac@1.0.0") + .exists()); + } + + #[cfg(target_os = "linux")] + { + output.assert_exit_code(0); + output.assert_matches_text( + "[WILDCARD]Hello from binary package on linux[WILDCARD]", + ); + assert!(!project_path + .join("node_modules/.deno/@denotest+binary-package-windows@1.0.0") + .exists()); + assert!(project_path + .join("node_modules/.deno/@denotest+binary-package-linux@1.0.0") + .exists()); + assert!(!project_path + .join("node_modules/.deno/@denotest+binary-package-mac@1.0.0") + .exists()); + } + } +} diff --git a/cli/tests/testdata/npm/binary_package/main.js b/cli/tests/testdata/npm/binary_package/main.js new file mode 100644 index 000000000..8823c5a5b --- /dev/null +++ b/cli/tests/testdata/npm/binary_package/main.js @@ -0,0 +1 @@ +import "npm:@denotest/binary-package"; diff --git a/cli/tests/testdata/npm/registry/@denotest/binary-package-linux/1.0.0/index.js b/cli/tests/testdata/npm/registry/@denotest/binary-package-linux/1.0.0/index.js new file mode 100644 index 000000000..03ecfc377 --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/binary-package-linux/1.0.0/index.js @@ -0,0 +1 @@ +console.log("Hello from binary package on linux"); \ No newline at end of file diff --git a/cli/tests/testdata/npm/registry/@denotest/binary-package-linux/1.0.0/package.json b/cli/tests/testdata/npm/registry/@denotest/binary-package-linux/1.0.0/package.json new file mode 100644 index 000000000..3b450e0d9 --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/binary-package-linux/1.0.0/package.json @@ -0,0 +1,8 @@ +{ + "name": "@denotest/binary-package-linux", + "version": "1.0.0", + "main": "index.js", + "os": [ + "linux" + ] +} diff --git a/cli/tests/testdata/npm/registry/@denotest/binary-package-mac/1.0.0/index.js b/cli/tests/testdata/npm/registry/@denotest/binary-package-mac/1.0.0/index.js new file mode 100644 index 000000000..ac8c91f50 --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/binary-package-mac/1.0.0/index.js @@ -0,0 +1 @@ +console.log("Hello from binary package on mac"); \ No newline at end of file diff --git a/cli/tests/testdata/npm/registry/@denotest/binary-package-mac/1.0.0/package.json b/cli/tests/testdata/npm/registry/@denotest/binary-package-mac/1.0.0/package.json new file mode 100644 index 000000000..02916e65b --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/binary-package-mac/1.0.0/package.json @@ -0,0 +1,8 @@ +{ + "name": "@denotest/binary-package-linux", + "version": "1.0.0", + "main": "index.js", + "os": [ + "darwin" + ] +} diff --git a/cli/tests/testdata/npm/registry/@denotest/binary-package-windows/1.0.0/index.js b/cli/tests/testdata/npm/registry/@denotest/binary-package-windows/1.0.0/index.js new file mode 100644 index 000000000..57344ca00 --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/binary-package-windows/1.0.0/index.js @@ -0,0 +1 @@ +console.log("Hello from binary package on windows"); \ No newline at end of file diff --git a/cli/tests/testdata/npm/registry/@denotest/binary-package-windows/1.0.0/package.json b/cli/tests/testdata/npm/registry/@denotest/binary-package-windows/1.0.0/package.json new file mode 100644 index 000000000..1c0af637d --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/binary-package-windows/1.0.0/package.json @@ -0,0 +1,8 @@ +{ + "name": "@denotest/binary-package-windows", + "version": "1.0.0", + "main": "index.js", + "os": [ + "win32" + ] +} diff --git a/cli/tests/testdata/npm/registry/@denotest/binary-package/1.0.0/index.js b/cli/tests/testdata/npm/registry/@denotest/binary-package/1.0.0/index.js new file mode 100644 index 000000000..5870118e7 --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/binary-package/1.0.0/index.js @@ -0,0 +1,13 @@ +const packageByOs = { + "darwin": "@denotest/binary-package-mac", + "linux": "@denotest/binary-package-linux", + "win32": "@denotest/binary-package-windows", +} + +const selectedPackage = packageByOs[process.platform]; + +if (!selectedPackage) { + throw new Error("trying to run on unsupported platform"); +} + +require(selectedPackage); \ No newline at end of file diff --git a/cli/tests/testdata/npm/registry/@denotest/binary-package/1.0.0/package.json b/cli/tests/testdata/npm/registry/@denotest/binary-package/1.0.0/package.json new file mode 100644 index 000000000..dc8859bb4 --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/binary-package/1.0.0/package.json @@ -0,0 +1,10 @@ +{ + "name": "@denotest/binary-package", + "version": "1.0.0", + "main": "index.js", + "optionalDependencies": { + "@denotest/binary-package-linux": "1.0.0", + "@denotest/binary-package-mac": "1.0.0", + "@denotest/binary-package-windows": "1.0.0" + } +} -- cgit v1.2.3