diff options
Diffstat (limited to 'cli/tests')
-rw-r--r-- | cli/tests/integration/compile_tests.rs | 305 | ||||
-rw-r--r-- | cli/tests/integration/npm_tests.rs | 10 | ||||
-rw-r--r-- | cli/tests/testdata/compile/npm_fs/main.out | 1 | ||||
-rw-r--r-- | cli/tests/testdata/compile/npm_fs/main.ts | 259 | ||||
-rw-r--r-- | cli/tests/testdata/npm/registry/@denotest/esm-basic/1.0.0/main.d.mts | 1 | ||||
-rw-r--r-- | cli/tests/testdata/npm/registry/@denotest/esm-basic/1.0.0/main.mjs | 2 | ||||
-rw-r--r-- | cli/tests/testdata/package_json/basic/main.info.out | 2 |
7 files changed, 550 insertions, 30 deletions
diff --git a/cli/tests/integration/compile_tests.rs b/cli/tests/integration/compile_tests.rs index 7835d7f0d..ac088ca90 100644 --- a/cli/tests/integration/compile_tests.rs +++ b/cli/tests/integration/compile_tests.rs @@ -5,6 +5,8 @@ use std::process::Command; use test_util as util; use test_util::TempDir; use util::assert_contains; +use util::env_vars_for_npm_tests; +use util::TestContextBuilder; #[test] fn compile() { @@ -675,30 +677,40 @@ fn workers_basic() { #[test] fn workers_not_in_module_map() { - let _guard = util::http_server(); - let dir = TempDir::new(); + let context = TestContextBuilder::for_npm() + .use_http_server() + .use_temp_cwd() + .build(); + let temp_dir = context.temp_dir(); let exe = if cfg!(windows) { - dir.path().join("not_in_module_map.exe") + temp_dir.path().join("not_in_module_map.exe") } else { - dir.path().join("not_in_module_map") + temp_dir.path().join("not_in_module_map") }; - let output = util::deno_cmd() - .current_dir(util::root_path()) - .arg("compile") - .arg("--output") - .arg(&exe) - .arg(util::testdata_path().join("./compile/workers/not_in_module_map.ts")) - .output() - .unwrap(); - assert!(output.status.success()); + let main_path = + util::testdata_path().join("./compile/workers/not_in_module_map.ts"); + let output = context + .new_command() + .args_vec([ + "compile", + "--output", + &exe.to_string_lossy(), + &main_path.to_string_lossy(), + ]) + .run(); + output.assert_exit_code(0); + output.skip_output_check(); - let output = Command::new(&exe).env("NO_COLOR", "").output().unwrap(); - assert!(!output.status.success()); - let stderr = String::from_utf8(output.stderr).unwrap(); - assert!(stderr.starts_with(concat!( - "error: Uncaught (in worker \"\") Module not found\n", - "error: Uncaught (in promise) Error: Unhandled error in child worker.\n" - ))); + let output = context + .new_command() + .command_name(exe.to_string_lossy()) + .env("NO_COLOR", "") + .run(); + output.assert_exit_code(1); + output.assert_matches_text(concat!( + "error: Uncaught (in worker \"\") Module not found: [WILDCARD]", + "error: Uncaught (in promise) Error: Unhandled error in child worker.\n[WILDCARD]" + )); } #[test] @@ -790,3 +802,256 @@ fn dynamic_import_unanalyzable() { .unwrap(); assert_eq!(String::from_utf8(output.stdout).unwrap(), expected); } + +itest!(npm_specifiers_errors_no_unstable { + args: "compile -A --quiet npm/cached_only/main.ts", + output_str: Some( + concat!( + "error: Using npm specifiers with deno compile requires the --unstable flag.", + "\n\n", + "Caused by:\n", + " npm specifiers have not yet been implemented for this subcommand (https://github.com/denoland/deno/issues/15960). Found: npm:chalk@5.0.1\n" + ) + ), + exit_code: 1, + envs: env_vars_for_npm_tests(), + http_server: true, +}); + +#[test] +fn compile_npm_specifiers() { + let context = TestContextBuilder::for_npm() + .use_sync_npm_download() + .use_temp_cwd() + .build(); + + let temp_dir = context.temp_dir(); + temp_dir.write( + "main.ts", + concat!( + "import path from 'node:path';\n", + "import { getValue, setValue } from 'npm:@denotest/esm-basic';\n", + "import getValueDefault from 'npm:@denotest/esm-import-cjs-default';\n", + "setValue(2);\n", + "console.log(path.join('testing', 'this'));", + "console.log(getValue());", + "console.log(getValueDefault());", + ), + ); + + let binary_path = if cfg!(windows) { + temp_dir.path().join("binary.exe") + } else { + temp_dir.path().join("binary") + }; + + // try with and without --node-modules-dir + let compile_commands = &[ + "compile --unstable --output binary main.ts", + "compile --unstable --node-modules-dir --output binary main.ts", + ]; + + for compile_command in compile_commands { + let output = context.new_command().args(compile_command).run(); + output.assert_exit_code(0); + output.skip_output_check(); + + let output = context + .new_command() + .command_name(binary_path.to_string_lossy()) + .run(); + output.assert_matches_text( + r#"Node esm importing node cjs +=========================== +{ + default: [Function (anonymous)], + named: [Function (anonymous)], + MyClass: [class MyClass] +} +{ default: [Function (anonymous)], named: [Function (anonymous)] } +[Module: null prototype] { + MyClass: [class MyClass], + __esModule: true, + default: { + default: [Function (anonymous)], + named: [Function (anonymous)], + MyClass: [class MyClass] + }, + named: [Function (anonymous)] +} +[Module: null prototype] { + __esModule: true, + default: { default: [Function (anonymous)], named: [Function (anonymous)] }, + named: [Function (anonymous)] +} +=========================== +static method +testing[WILDCARD]this +2 +5 +"#, + ); + } + + // try with a package.json + temp_dir.remove_dir_all("node_modules"); + temp_dir.write( + "main.ts", + concat!( + "import { getValue, setValue } from '@denotest/esm-basic';\n", + "setValue(2);\n", + "console.log(getValue());", + ), + ); + temp_dir.write( + "package.json", + r#"{ "dependencies": { "@denotest/esm-basic": "1" } }"#, + ); + + let output = context + .new_command() + .args("compile --unstable --output binary main.ts") + .run(); + output.assert_exit_code(0); + output.skip_output_check(); + + let output = context + .new_command() + .command_name(binary_path.to_string_lossy()) + .run(); + output.assert_matches_text("2\n"); +} + +#[test] +fn compile_npm_file_system() { + run_npm_bin_compile_test(RunNpmBinCompileOptions { + input_specifier: "compile/npm_fs/main.ts", + output_file: "compile/npm_fs/main.out", + node_modules_dir: true, + input_name: Some("binary"), + expected_name: "binary", + run_args: vec![], + }); +} + +#[test] +fn compile_npm_bin_esm() { + run_npm_bin_compile_test(RunNpmBinCompileOptions { + input_specifier: "npm:@denotest/bin/cli-esm", + run_args: vec!["this", "is", "a", "test"], + output_file: "npm/deno_run_esm.out", + node_modules_dir: false, + input_name: None, + expected_name: "cli-esm", + }); +} + +#[test] +fn compile_npm_bin_cjs() { + run_npm_bin_compile_test(RunNpmBinCompileOptions { + input_specifier: "npm:@denotest/bin/cli-cjs", + run_args: vec!["this", "is", "a", "test"], + output_file: "npm/deno_run_cjs.out", + node_modules_dir: false, + input_name: None, + expected_name: "cli-cjs", + }); +} + +#[test] +fn compile_npm_cowsay() { + run_npm_bin_compile_test(RunNpmBinCompileOptions { + input_specifier: "npm:cowsay@1.5.0", + run_args: vec!["Hello"], + output_file: "npm/deno_run_cowsay.out", + node_modules_dir: false, + input_name: None, + expected_name: "cowsay", + }); +} + +#[test] +fn compile_npm_cowsay_explicit() { + run_npm_bin_compile_test(RunNpmBinCompileOptions { + input_specifier: "npm:cowsay@1.5.0/cowsay", + run_args: vec!["Hello"], + output_file: "npm/deno_run_cowsay.out", + node_modules_dir: false, + input_name: None, + expected_name: "cowsay", + }); +} + +#[test] +fn compile_npm_cowthink() { + run_npm_bin_compile_test(RunNpmBinCompileOptions { + input_specifier: "npm:cowsay@1.5.0/cowthink", + run_args: vec!["Hello"], + output_file: "npm/deno_run_cowthink.out", + node_modules_dir: false, + input_name: None, + expected_name: "cowthink", + }); +} + +struct RunNpmBinCompileOptions<'a> { + input_specifier: &'a str, + output_file: &'a str, + node_modules_dir: bool, + input_name: Option<&'a str>, + expected_name: &'a str, + run_args: Vec<&'a str>, +} + +fn run_npm_bin_compile_test(opts: RunNpmBinCompileOptions) { + let context = TestContextBuilder::for_npm() + .use_sync_npm_download() + .use_temp_cwd() + .build(); + + let temp_dir = context.temp_dir(); + let testdata_path = context.testdata_path(); + let main_specifier = if opts.input_specifier.starts_with("npm:") { + opts.input_specifier.to_string() + } else { + testdata_path + .join(opts.input_specifier) + .to_string_lossy() + .to_string() + }; + + let mut args = vec![ + "compile".to_string(), + "-A".to_string(), + "--unstable".to_string(), + ]; + + if opts.node_modules_dir { + args.push("--node-modules-dir".to_string()); + } + + if let Some(bin_name) = opts.input_name { + args.push("--output".to_string()); + args.push(bin_name.to_string()); + } + + args.push(main_specifier); + + // compile + let output = context.new_command().args_vec(args).run(); + output.assert_exit_code(0); + output.skip_output_check(); + + // run + let binary_path = if cfg!(windows) { + temp_dir.path().join(format!("{}.exe", opts.expected_name)) + } else { + temp_dir.path().join(opts.expected_name) + }; + let output = context + .new_command() + .command_name(binary_path.to_string_lossy()) + .args_vec(opts.run_args) + .run(); + output.assert_matches_file(opts.output_file); +} diff --git a/cli/tests/integration/npm_tests.rs b/cli/tests/integration/npm_tests.rs index d4f2d3e45..c04322027 100644 --- a/cli/tests/integration/npm_tests.rs +++ b/cli/tests/integration/npm_tests.rs @@ -855,17 +855,9 @@ fn ensure_registry_files_local() { } } -itest!(compile_errors { - args: "compile -A --quiet npm/cached_only/main.ts", - output_str: Some("error: npm specifiers have not yet been implemented for this sub command (https://github.com/denoland/deno/issues/15960). Found: npm:chalk@5.0.1\n"), - exit_code: 1, - envs: env_vars_for_npm_tests(), - http_server: true, - }); - itest!(bundle_errors { args: "bundle --quiet npm/esm/main.js", - output_str: Some("error: npm specifiers have not yet been implemented for this sub command (https://github.com/denoland/deno/issues/15960). Found: npm:chalk@5.0.1\n"), + output_str: Some("error: npm specifiers have not yet been implemented for this subcommand (https://github.com/denoland/deno/issues/15960). Found: npm:chalk@5.0.1\n"), exit_code: 1, envs: env_vars_for_npm_tests(), http_server: true, diff --git a/cli/tests/testdata/compile/npm_fs/main.out b/cli/tests/testdata/compile/npm_fs/main.out new file mode 100644 index 000000000..2e9ba477f --- /dev/null +++ b/cli/tests/testdata/compile/npm_fs/main.out @@ -0,0 +1 @@ +success diff --git a/cli/tests/testdata/compile/npm_fs/main.ts b/cli/tests/testdata/compile/npm_fs/main.ts new file mode 100644 index 000000000..f9951d7a4 --- /dev/null +++ b/cli/tests/testdata/compile/npm_fs/main.ts @@ -0,0 +1,259 @@ +import { url } from "npm:@denotest/esm-basic"; +import { fileURLToPath } from "node:url"; +import path from "node:path"; +import assert from "node:assert/strict"; + +// will be at node_modules\.deno\@denotest+esm-basic@1.0.0\node_modules\@denotest\esm-basic +const dirPath = path.dirname(fileURLToPath(url)); +const nodeModulesPath = path.join(dirPath, "../../../../../"); +const packageJsonText = `{ + "name": "@denotest/esm-basic", + "version": "1.0.0", + "type": "module", + "main": "main.mjs", + "types": "main.d.mts" +} +`; +const vfsPackageJsonPath = path.join(dirPath, "package.json"); + +// reading a file in vfs +{ + const text = Deno.readTextFileSync(vfsPackageJsonPath); + assert.equal(text, packageJsonText); +} + +// reading a file async in vfs +{ + const text = await Deno.readTextFile(vfsPackageJsonPath); + assert.equal(text, packageJsonText); +} + +// copy file from vfs to real fs +{ + Deno.copyFileSync(vfsPackageJsonPath, "package.json"); + assert.equal(Deno.readTextFileSync("package.json"), packageJsonText); +} + +// copy to vfs +assert.throws( + () => Deno.copyFileSync("package.json", vfsPackageJsonPath), + Deno.errors.NotSupported, +); +Deno.removeSync("package.json"); + +// copy file async from vfs to real fs +{ + await Deno.copyFile(vfsPackageJsonPath, "package.json"); + assert.equal(Deno.readTextFileSync("package.json"), packageJsonText); +} + +// copy to vfs async +await assert.rejects( + () => Deno.copyFile("package.json", vfsPackageJsonPath), + Deno.errors.NotSupported, +); +Deno.removeSync("package.json"); + +// open +{ + const file = Deno.openSync(vfsPackageJsonPath); + const bytes = new Uint8Array(10); + file.seekSync(2, Deno.SeekMode.Start); + assert.equal(file.readSync(bytes), 10); + const text = new TextDecoder().decode(bytes); + assert.equal(text, packageJsonText.slice(2, 12)); +} +{ + const file = await Deno.open(vfsPackageJsonPath); + const bytes = new Uint8Array(10); + await file.seek(2, Deno.SeekMode.Start); + assert.equal(await file.read(bytes), 10); + const text = new TextDecoder().decode(bytes); + assert.equal(text, packageJsonText.slice(2, 12)); +} + +// chdir +assert.throws(() => Deno.chdir(dirPath), Deno.errors.NotSupported); + +// mkdir +assert.throws( + () => Deno.mkdirSync(path.join(dirPath, "subDir")), + Deno.errors.NotSupported, +); +await assert.rejects( + () => Deno.mkdir(path.join(dirPath, "subDir")), + Deno.errors.NotSupported, +); + +// chmod +assert.throws( + () => Deno.chmodSync(vfsPackageJsonPath, 0o777), + Deno.errors.NotSupported, +); +await assert.rejects( + () => Deno.chmod(vfsPackageJsonPath, 0o777), + Deno.errors.NotSupported, +); + +// chown +assert.throws( + () => Deno.chownSync(vfsPackageJsonPath, 1000, 1000), + Deno.errors.NotSupported, +); +await assert.rejects( + () => Deno.chown(vfsPackageJsonPath, 1000, 1000), + Deno.errors.NotSupported, +); + +// remove +assert.throws( + () => Deno.removeSync(vfsPackageJsonPath), + Deno.errors.NotSupported, +); +await assert.rejects( + () => Deno.remove(vfsPackageJsonPath), + Deno.errors.NotSupported, +); + +// stat +{ + const result = Deno.statSync(vfsPackageJsonPath); + assert(result.isFile); +} +{ + const result = await Deno.stat(vfsPackageJsonPath); + assert(result.isFile); +} + +// lstat +{ + const result = Deno.lstatSync( + path.join(nodeModulesPath, "@denotest", "esm-basic"), + ); + assert(result.isSymlink); +} +{ + const result = await Deno.lstat( + path.join(nodeModulesPath, "@denotest", "esm-basic"), + ); + assert(result.isSymlink); +} + +// realpath +{ + const result = Deno.realPathSync( + path.join(nodeModulesPath, "@denotest", "esm-basic", "package.json"), + ); + assert.equal(result, vfsPackageJsonPath); +} +{ + const result = await Deno.realPath( + path.join(nodeModulesPath, "@denotest", "esm-basic", "package.json"), + ); + assert.equal(result, vfsPackageJsonPath); +} + +// read dir +const readDirNames = ["main.d.mts", "main.mjs", "package.json"]; +{ + const names = Array.from(Deno.readDirSync(dirPath)) + .map((e) => e.name); + assert.deepEqual(readDirNames, names); +} +{ + const names = []; + for await (const entry of Deno.readDir(dirPath)) { + names.push(entry.name); + } + assert.deepEqual(readDirNames, names); +} + +// rename +assert.throws( + () => Deno.renameSync("package.json", vfsPackageJsonPath), + Deno.errors.NotSupported, +); +assert.throws( + () => Deno.renameSync(vfsPackageJsonPath, "package.json"), + Deno.errors.NotSupported, +); +await assert.rejects( + () => Deno.rename("package.json", vfsPackageJsonPath), + Deno.errors.NotSupported, +); +await assert.rejects( + () => Deno.rename(vfsPackageJsonPath, "package.json"), + Deno.errors.NotSupported, +); + +// link +assert.throws( + () => Deno.linkSync("package.json", vfsPackageJsonPath), + Deno.errors.NotSupported, +); +assert.throws( + () => Deno.linkSync(vfsPackageJsonPath, "package.json"), + Deno.errors.NotSupported, +); +await assert.rejects( + () => Deno.link("package.json", vfsPackageJsonPath), + Deno.errors.NotSupported, +); +await assert.rejects( + () => Deno.link(vfsPackageJsonPath, "package.json"), + Deno.errors.NotSupported, +); + +// symlink +assert.throws( + () => Deno.symlinkSync("package.json", vfsPackageJsonPath), + Deno.errors.NotSupported, +); +assert.throws( + () => Deno.symlinkSync(vfsPackageJsonPath, "package.json"), + Deno.errors.NotSupported, +); +await assert.rejects( + () => Deno.symlink("package.json", vfsPackageJsonPath), + Deno.errors.NotSupported, +); +await assert.rejects( + () => Deno.symlink(vfsPackageJsonPath, "package.json"), + Deno.errors.NotSupported, +); + +// read link +{ + const result = Deno.readLinkSync( + path.join(nodeModulesPath, "@denotest", "esm-basic"), + ); + assert.equal(result, dirPath); +} +{ + const result = await Deno.readLink( + path.join(nodeModulesPath, "@denotest", "esm-basic"), + ); + assert.equal(result, dirPath); +} + +// truncate +assert.throws( + () => Deno.truncateSync(vfsPackageJsonPath, 0), + Deno.errors.NotSupported, +); +await assert.rejects( + () => Deno.truncate(vfsPackageJsonPath, 0), + Deno.errors.NotSupported, +); + +// utime +assert.throws( + () => Deno.utimeSync(vfsPackageJsonPath, 0, 0), + Deno.errors.NotSupported, +); +await assert.rejects( + () => Deno.utime(vfsPackageJsonPath, 0, 0), + Deno.errors.NotSupported, +); + +console.log("success"); diff --git a/cli/tests/testdata/npm/registry/@denotest/esm-basic/1.0.0/main.d.mts b/cli/tests/testdata/npm/registry/@denotest/esm-basic/1.0.0/main.d.mts index fa7814911..29da1e6d7 100644 --- a/cli/tests/testdata/npm/registry/@denotest/esm-basic/1.0.0/main.d.mts +++ b/cli/tests/testdata/npm/registry/@denotest/esm-basic/1.0.0/main.d.mts @@ -1,2 +1,3 @@ export declare function setValue(val: number): void; export declare function getValue(): number; +export declare const url: string; diff --git a/cli/tests/testdata/npm/registry/@denotest/esm-basic/1.0.0/main.mjs b/cli/tests/testdata/npm/registry/@denotest/esm-basic/1.0.0/main.mjs index 23df4221c..0a44f7585 100644 --- a/cli/tests/testdata/npm/registry/@denotest/esm-basic/1.0.0/main.mjs +++ b/cli/tests/testdata/npm/registry/@denotest/esm-basic/1.0.0/main.mjs @@ -7,3 +7,5 @@ export function setValue(newValue) { export function getValue() { return value; } + +export const url = import.meta.url; diff --git a/cli/tests/testdata/package_json/basic/main.info.out b/cli/tests/testdata/package_json/basic/main.info.out index bf36f4f19..3572c75e1 100644 --- a/cli/tests/testdata/package_json/basic/main.info.out +++ b/cli/tests/testdata/package_json/basic/main.info.out @@ -5,4 +5,4 @@ size: [WILDCARD] file:///[WILDCARD]/main.ts (63B) └─┬ file:///[WILDCARD]/lib.ts (166B) - └── npm:@denotest/esm-basic@1.0.0 (345B) + └── npm:@denotest/esm-basic@1.0.0 (416B) |