diff options
Diffstat (limited to 'cli/tests')
16 files changed, 437 insertions, 0 deletions
diff --git a/cli/tests/integration/compat_tests.rs b/cli/tests/integration/compat_tests.rs index ce89cc623..e8e6316bf 100644 --- a/cli/tests/integration/compat_tests.rs +++ b/cli/tests/integration/compat_tests.rs @@ -172,3 +172,22 @@ fn native_modules_as_global_vars() { ); assert!(out.contains("true")); } + +#[test] +fn ext_node_cjs_execution() { + let (out, _err) = util::run_and_collect_output_with_args( + true, + vec![ + "run", + "-A", + "--unstable", + "--quiet", + "commonjs/init.js", + "./example.js", + ], + None, + Some(vec![("DENO_NODE_COMPAT_URL".to_string(), std_file_url())]), + false, + ); + assert!(out.contains("{ hello: \"world\" }")); +} diff --git a/cli/tests/testdata/commonjs/data.json b/cli/tests/testdata/commonjs/data.json new file mode 100644 index 000000000..f2a886f39 --- /dev/null +++ b/cli/tests/testdata/commonjs/data.json @@ -0,0 +1,3 @@ +{ + "hello": "world" +} diff --git a/cli/tests/testdata/commonjs/example.js b/cli/tests/testdata/commonjs/example.js new file mode 100644 index 000000000..785078037 --- /dev/null +++ b/cli/tests/testdata/commonjs/example.js @@ -0,0 +1,11 @@ +// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. +// deno-lint-ignore no-undef +const processMod = require("process"); +const osMod = require("node:os"); +console.log("process.pid", processMod.pid); +console.log("os.EOL", osMod.EOL); +const leftPad = require("left-pad"); +const json = require("./data"); +console.log(json); +console.log(leftPad("foo", 5)); // => " foo" +console.log("main module", processMod.mainModule.filename); diff --git a/cli/tests/testdata/commonjs/init.js b/cli/tests/testdata/commonjs/init.js new file mode 100644 index 000000000..142ae7c76 --- /dev/null +++ b/cli/tests/testdata/commonjs/init.js @@ -0,0 +1,17 @@ +import { fromFileUrl } from "../../../../test_util/std/path/mod.ts"; + +const DENO_NODE_COMPAT_URL = Deno.env.get("DENO_NODE_COMPAT_URL"); +const moduleAllUrl = `${DENO_NODE_COMPAT_URL}node/module_all.ts`; +const processUrl = `${DENO_NODE_COMPAT_URL}node/process.ts`; +let moduleName = import.meta.resolve(Deno.args[0]); +moduleName = fromFileUrl(moduleName); + +const [moduleAll, processModule] = await Promise.all([ + import(moduleAllUrl), + import(processUrl), +]); +Deno[Deno.internal].require.initializeCommonJs( + moduleAll.default, + processModule.default, +); +Deno[Deno.internal].require.Module._load(moduleName, null, true); diff --git a/cli/tests/testdata/commonjs/node_modules/colorette/index.cjs b/cli/tests/testdata/commonjs/node_modules/colorette/index.cjs new file mode 100644 index 000000000..0590d668d --- /dev/null +++ b/cli/tests/testdata/commonjs/node_modules/colorette/index.cjs @@ -0,0 +1,74 @@ +// Copyright Jorge Bucaran. All rights reserved. MIT license. +let enabled = !("NO_COLOR" in process.env) && + ("FORCE_COLOR" in process.env || + process.platform === "win32" || + (process.stdout != null && + process.stdout.isTTY && + process.env.TERM && + process.env.TERM !== "dumb")); + +const raw = (open, close, searchRegex, replaceValue) => + (s) => + enabled + ? open + + (~(s += "").indexOf(close, 4) // skip opening \x1b[ + ? s.replace(searchRegex, replaceValue) + : s) + + close + : s; + +const init = (open, close) => { + return raw( + `\x1b[${open}m`, + `\x1b[${close}m`, + new RegExp(`\\x1b\\[${close}m`, "g"), + `\x1b[${open}m`, + ); +}; + +exports.options = Object.defineProperty({}, "enabled", { + get: () => enabled, + set: (value) => (enabled = value), +}); + +exports.reset = init(0, 0); +exports.bold = raw("\x1b[1m", "\x1b[22m", /\x1b\[22m/g, "\x1b[22m\x1b[1m"); +exports.dim = raw("\x1b[2m", "\x1b[22m", /\x1b\[22m/g, "\x1b[22m\x1b[2m"); +exports.italic = init(3, 23); +exports.underline = init(4, 24); +exports.inverse = init(7, 27); +exports.hidden = init(8, 28); +exports.strikethrough = init(9, 29); +exports.black = init(30, 39); +exports.red = init(31, 39); +exports.green = init(32, 39); +exports.yellow = init(33, 39); +exports.blue = init(34, 39); +exports.magenta = init(35, 39); +exports.cyan = init(36, 39); +exports.white = init(37, 39); +exports.gray = init(90, 39); +exports.bgBlack = init(40, 49); +exports.bgRed = init(41, 49); +exports.bgGreen = init(42, 49); +exports.bgYellow = init(43, 49); +exports.bgBlue = init(44, 49); +exports.bgMagenta = init(45, 49); +exports.bgCyan = init(46, 49); +exports.bgWhite = init(47, 49); +exports.blackBright = init(90, 39); +exports.redBright = init(91, 39); +exports.greenBright = init(92, 39); +exports.yellowBright = init(93, 39); +exports.blueBright = init(94, 39); +exports.magentaBright = init(95, 39); +exports.cyanBright = init(96, 39); +exports.whiteBright = init(97, 39); +exports.bgBlackBright = init(100, 49); +exports.bgRedBright = init(101, 49); +exports.bgGreenBright = init(102, 49); +exports.bgYellowBright = init(103, 49); +exports.bgBlueBright = init(104, 49); +exports.bgMagentaBright = init(105, 49); +exports.bgCyanBright = init(106, 49); +exports.bgWhiteBright = init(107, 49); diff --git a/cli/tests/testdata/commonjs/node_modules/colorette/index.js b/cli/tests/testdata/commonjs/node_modules/colorette/index.js new file mode 100644 index 000000000..6e83866f3 --- /dev/null +++ b/cli/tests/testdata/commonjs/node_modules/colorette/index.js @@ -0,0 +1,75 @@ +// Copyright Jorge Bucaran. All rights reserved. MIT license. +// deno-lint-ignore-file no-undef no-control-regex +let enabled = !("NO_COLOR" in process.env) && + ("FORCE_COLOR" in process.env || + process.platform === "win32" || + (process.stdout != null && + process.stdout.isTTY && + process.env.TERM && + process.env.TERM !== "dumb")); + +const raw = (open, close, searchRegex, replaceValue) => + (s) => + enabled + ? open + + (~(s += "").indexOf(close, 4) // skip opening \x1b[ + ? s.replace(searchRegex, replaceValue) + : s) + + close + : s; + +const init = (open, close) => { + return raw( + `\x1b[${open}m`, + `\x1b[${close}m`, + new RegExp(`\\x1b\\[${close}m`, "g"), + `\x1b[${open}m`, + ); +}; + +export const options = Object.defineProperty({}, "enabled", { + get: () => enabled, + set: (value) => (enabled = value), +}); + +export const reset = init(0, 0); +export const bold = raw("\x1b[1m", "\x1b[22m", /\x1b\[22m/g, "\x1b[22m\x1b[1m"); +export const dim = raw("\x1b[2m", "\x1b[22m", /\x1b\[22m/g, "\x1b[22m\x1b[2m"); +export const italic = init(3, 23); +export const underline = init(4, 24); +export const inverse = init(7, 27); +export const hidden = init(8, 28); +export const strikethrough = init(9, 29); +export const black = init(30, 39); +export const red = init(31, 39); +export const green = init(32, 39); +export const yellow = init(33, 39); +export const blue = init(34, 39); +export const magenta = init(35, 39); +export const cyan = init(36, 39); +export const white = init(37, 39); +export const gray = init(90, 39); +export const bgBlack = init(40, 49); +export const bgRed = init(41, 49); +export const bgGreen = init(42, 49); +export const bgYellow = init(43, 49); +export const bgBlue = init(44, 49); +export const bgMagenta = init(45, 49); +export const bgCyan = init(46, 49); +export const bgWhite = init(47, 49); +export const blackBright = init(90, 39); +export const redBright = init(91, 39); +export const greenBright = init(92, 39); +export const yellowBright = init(93, 39); +export const blueBright = init(94, 39); +export const magentaBright = init(95, 39); +export const cyanBright = init(96, 39); +export const whiteBright = init(97, 39); +export const bgBlackBright = init(100, 49); +export const bgRedBright = init(101, 49); +export const bgGreenBright = init(102, 49); +export const bgYellowBright = init(103, 49); +export const bgBlueBright = init(104, 49); +export const bgMagentaBright = init(105, 49); +export const bgCyanBright = init(106, 49); +export const bgWhiteBright = init(107, 49); diff --git a/cli/tests/testdata/commonjs/node_modules/colorette/package.json b/cli/tests/testdata/commonjs/node_modules/colorette/package.json new file mode 100644 index 000000000..fdca1aa24 --- /dev/null +++ b/cli/tests/testdata/commonjs/node_modules/colorette/package.json @@ -0,0 +1,23 @@ +{ + "author": { + "name": "Jorge Bucaran" + }, + "description": "Color your terminal using pure idiomatic JavaScript.", + "exports": { + "./package.json": "./package.json", + ".": { + "require": "./index.cjs", + "import": "./index.js" + } + }, + "license": "MIT", + "main": "index.cjs", + "module": "index.js", + "name": "colorette", + "repository": { + "type": "git", + "url": "git+https://github.com/jorgebucaran/colorette.git" + }, + "type": "module", + "version": "1.2.1" +} diff --git a/cli/tests/testdata/commonjs/node_modules/imports_exports/import_export.js b/cli/tests/testdata/commonjs/node_modules/imports_exports/import_export.js new file mode 100644 index 000000000..3ebd222ea --- /dev/null +++ b/cli/tests/testdata/commonjs/node_modules/imports_exports/import_export.js @@ -0,0 +1,6 @@ +import dep from "#dep"; + +export default { + bar: "bar", + dep, +}; diff --git a/cli/tests/testdata/commonjs/node_modules/imports_exports/import_polyfill.js b/cli/tests/testdata/commonjs/node_modules/imports_exports/import_polyfill.js new file mode 100644 index 000000000..76716a3ef --- /dev/null +++ b/cli/tests/testdata/commonjs/node_modules/imports_exports/import_polyfill.js @@ -0,0 +1,3 @@ +export default { + polyfill: "import", +}; diff --git a/cli/tests/testdata/commonjs/node_modules/imports_exports/package.json b/cli/tests/testdata/commonjs/node_modules/imports_exports/package.json new file mode 100644 index 000000000..5d26359db --- /dev/null +++ b/cli/tests/testdata/commonjs/node_modules/imports_exports/package.json @@ -0,0 +1,17 @@ +{ + "version": "1.0.0", + "name": "imports_exports", + "main": "./require_export.cjs", + "imports": { + "#dep": { + "import": "./import_polyfill.js", + "require": "./require_polyfill.js" + } + }, + "exports": { + ".": { + "import": "./import_export.js", + "require": "./require_export.cjs" + } + } +} diff --git a/cli/tests/testdata/commonjs/node_modules/imports_exports/require_export.cjs b/cli/tests/testdata/commonjs/node_modules/imports_exports/require_export.cjs new file mode 100644 index 000000000..48923b8d8 --- /dev/null +++ b/cli/tests/testdata/commonjs/node_modules/imports_exports/require_export.cjs @@ -0,0 +1,6 @@ +const dep = require("#dep"); + +module.exports = { + foo: "foo", + dep, +}; diff --git a/cli/tests/testdata/commonjs/node_modules/imports_exports/require_polyfill.js b/cli/tests/testdata/commonjs/node_modules/imports_exports/require_polyfill.js new file mode 100644 index 000000000..1023fd65c --- /dev/null +++ b/cli/tests/testdata/commonjs/node_modules/imports_exports/require_polyfill.js @@ -0,0 +1,3 @@ +module.exports = { + polyfill: "require", +}; diff --git a/cli/tests/testdata/commonjs/node_modules/left-pad/README.md b/cli/tests/testdata/commonjs/node_modules/left-pad/README.md new file mode 100644 index 000000000..0e70d461e --- /dev/null +++ b/cli/tests/testdata/commonjs/node_modules/left-pad/README.md @@ -0,0 +1,41 @@ +## left-pad + +String left pad + +[![Build Status][travis-image]][travis-url] + +## Install + +```bash +$ npm install left-pad +``` + +## Usage + +```js +const leftPad = require("left-pad"); + +leftPad("foo", 5); +// => " foo" + +leftPad("foobar", 6); +// => "foobar" + +leftPad(1, 2, "0"); +// => "01" + +leftPad(17, 5, 0); +// => "00017" +``` + +**NOTE:** The third argument should be a single `char`. However the module +doesn't throw an error if you supply more than one `char`s. See +[#28](https://github.com/stevemao/left-pad/pull/28). + +**NOTE:** Characters having code points outside of +[BMP plan](https://en.wikipedia.org/wiki/Plane_(Unicode)#Basic_Multilingual_Plane) +are considered a two distinct characters. See +[#58](https://github.com/stevemao/left-pad/issues/58). + +[travis-image]: https://travis-ci.org/stevemao/left-pad.svg?branch=master +[travis-url]: https://travis-ci.org/stevemao/left-pad diff --git a/cli/tests/testdata/commonjs/node_modules/left-pad/index.js b/cli/tests/testdata/commonjs/node_modules/left-pad/index.js new file mode 100644 index 000000000..a439e91de --- /dev/null +++ b/cli/tests/testdata/commonjs/node_modules/left-pad/index.js @@ -0,0 +1,54 @@ +// deno-lint-ignore-file + +/* This program is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://www.wtfpl.net/ for more details. */ +"use strict"; +module.exports = leftPad; + +var cache = [ + "", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", + " ", +]; + +function leftPad(str, len, ch) { + // convert `str` to a `string` + str = str + ""; + // `len` is the `pad`'s length now + len = len - str.length; + // doesn't need to pad + if (len <= 0) return str; + // `ch` defaults to `' '` + if (!ch && ch !== 0) ch = " "; + // convert `ch` to a `string` cuz it could be a number + ch = ch + ""; + // cache common use cases + if (ch === " " && len < 10) return cache[len] + str; + // `pad` starts with an empty string + var pad = ""; + // loop + while (true) { + // add `ch` to `pad` if `len` is odd + if (len & 1) pad += ch; + // divide `len` by 2, ditch the remainder + len >>= 1; + // "double" the `ch` so this operation count grows logarithmically on `len` + // each time `ch` is "doubled", the `len` would need to be "doubled" too + // similar to finding a value in binary search tree, hence O(log(n)) + if (len) ch += ch; + // `len` is 0, exit the loop + else break; + } + // pad `str`! + return pad + str; +} diff --git a/cli/tests/testdata/commonjs/node_modules/left-pad/package.json b/cli/tests/testdata/commonjs/node_modules/left-pad/package.json new file mode 100644 index 000000000..57be04271 --- /dev/null +++ b/cli/tests/testdata/commonjs/node_modules/left-pad/package.json @@ -0,0 +1,68 @@ +{ + "_from": "left-pad", + "_id": "left-pad@1.3.0", + "_inBundle": false, + "_integrity": "sha512-XI5MPzVNApjAyhQzphX8BkmKsKUxD4LdyK24iZeQGinBN9yTQT3bFlCBy/aVx2HrNcqQGsdot8ghrjyrvMCoEA==", + "_location": "/left-pad", + "_phantomChildren": {}, + "_requested": { + "type": "tag", + "registry": true, + "raw": "left-pad", + "name": "left-pad", + "escapedName": "left-pad", + "rawSpec": "", + "saveSpec": null, + "fetchSpec": "latest" + }, + "_requiredBy": [ + "#USER", + "/" + ], + "_resolved": "https://registry.npmjs.org/left-pad/-/left-pad-1.3.0.tgz", + "_shasum": "5b8a3a7765dfe001261dde915589e782f8c94d1e", + "_spec": "left-pad", + "_where": "/Users/kun/Projects/Deno/deno/std/node/tests", + "author": { + "name": "azer" + }, + "bugs": { + "url": "https://github.com/stevemao/left-pad/issues" + }, + "bundleDependencies": false, + "deprecated": "use String.prototype.padStart()", + "description": "String left pad", + "devDependencies": { + "benchmark": "^2.1.0", + "fast-check": "0.0.8", + "tape": "*" + }, + "homepage": "https://github.com/stevemao/left-pad#readme", + "keywords": [ + "leftpad", + "left", + "pad", + "padding", + "string", + "repeat" + ], + "license": "WTFPL", + "main": "index.js", + "maintainers": [ + { + "name": "Cameron Westland", + "email": "camwest@gmail.com" + } + ], + "name": "left-pad", + "repository": { + "url": "git+ssh://git@github.com/stevemao/left-pad.git", + "type": "git" + }, + "scripts": { + "bench": "node perf/perf.js", + "test": "node test" + }, + "types": "index.d.ts", + "version": "1.3.0" +} diff --git a/cli/tests/testdata/commonjs/package.json b/cli/tests/testdata/commonjs/package.json new file mode 100644 index 000000000..9ce08a900 --- /dev/null +++ b/cli/tests/testdata/commonjs/package.json @@ -0,0 +1,17 @@ +{ + "name": "example", + "version": "0.0.1", + "description": "", + "main": "example.js", + "dependencies": { + "colorette": "1.2.1", + "left-pad": "1.3.0", + "imports_exports": "1.0.0" + }, + "devDependencies": {}, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC" +} |