diff options
-rw-r--r-- | .github/workflows/build.yml | 7 | ||||
-rw-r--r-- | std/installer/test.ts | 405 | ||||
-rw-r--r-- | std/prettier/main_test.ts | 26 |
3 files changed, 426 insertions, 12 deletions
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 8b99c208c..b50ebf2cb 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -61,18 +61,25 @@ jobs: - name: Environment (linux) if: startsWith(matrix.os, 'ubuntu') + # In order to test the installer scripts in std we need a deno + # executable in the path. See + # https://github.com/denoland/deno/blob/27cd2c97f18c0392bc04c66b786717b2bc677315/std/installer/mod.ts#L185-L193 + # TODO(ry) This path modification should rather be done in "cargo test". run: | echo ::add-path::`pwd`/prebuilt/linux64 + echo ::add-path::`pwd`/target/release - name: Environment (mac) if: startsWith(matrix.os, 'macOS') run: | echo ::add-path::`pwd`/prebuilt/mac + echo ::add-path::`pwd`/target/release - name: Environment (windows) if: startsWith(matrix.os, 'windows') run: | echo ::add-path::%cd%\prebuilt\win + echo ::add-path::%cd%\target\release - name: Log versions run: | diff --git a/std/installer/test.ts b/std/installer/test.ts index a1257183c..3e6572485 100644 --- a/std/installer/test.ts +++ b/std/installer/test.ts @@ -1,11 +1,404 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import { test, runIfMain } from "../testing/mod.ts"; -import { assert } from "../testing/asserts.ts"; -import { isRemoteUrl } from "./mod.ts"; +const { run, stat, makeTempDir, remove, env, readAll } = Deno; -// TODO(ry) Many installer tests were removed in order to get deno_std to merge -// into the deno repo. Bring them back. -// https://github.com/denoland/deno_std/blob/98784c305c653b1c507b4b25be82ecf40f188305/installer/test.ts +import { test, runIfMain, TestFunction } from "../testing/mod.ts"; +import { assert, assertEquals } from "../testing/asserts.ts"; +import { BufReader } from "../io/bufio.ts"; +import { TextProtoReader } from "../textproto/mod.ts"; +import * as path from "../path/mod.ts"; +import * as fs from "../fs/mod.ts"; +import { install, isRemoteUrl } from "./mod.ts"; + +let fileServer: Deno.Process; + +// copied from `http/file_server_test.ts` +async function startFileServer(): Promise<void> { + fileServer = run({ + args: [ + Deno.execPath(), + "run", + "--allow-read", + "--allow-net", + "http/file_server.ts", + ".", + "--cors" + ], + stdout: "piped" + }); + // Once fileServer is ready it will write to its stdout. + const r = new TextProtoReader(new BufReader(fileServer.stdout!)); + const s = await r.readLine(); + assert(s !== Deno.EOF && s.includes("server listening")); +} + +function killFileServer(): void { + fileServer.close(); + fileServer.stdout!.close(); +} + +function installerTest(t: TestFunction, useOriginHomeDir = false): void { + const fn = async (): Promise<void> => { + await startFileServer(); + const tempDir = await makeTempDir(); + const envVars = env(); + const originalHomeDir = envVars["HOME"]; + if (!useOriginHomeDir) { + envVars["HOME"] = tempDir; + } + + try { + await t(); + } finally { + killFileServer(); + await remove(tempDir, { recursive: true }); + if (originalHomeDir) { + envVars["HOME"] = originalHomeDir; + } + } + }; + + test(fn); +} + +installerTest(async function installBasic(): Promise<void> { + await install( + "echo_test", + "http://localhost:4500/installer/testdata/echo.ts", + [] + ); + + const { HOME } = env(); + const filePath = path.resolve(HOME, ".deno/bin/echo_test"); + const fileInfo = await stat(filePath); + assert(fileInfo.isFile()); + + if (path.isWindows) { + assertEquals( + await fs.readFileStr(filePath + ".cmd"), + /* eslint-disable max-len */ + `% This executable is generated by Deno. Please don't modify it unless you know what it means. % +@IF EXIST "%~dp0\deno.exe" ( + "%~dp0\deno.exe" "run" "http://localhost:4500/installer/testdata/echo.ts" %* +) ELSE ( + @SETLOCAL + @SET PATHEXT=%PATHEXT:;.TS;=;% + "deno" "run" "http://localhost:4500/installer/testdata/echo.ts" %* +) +` + /* eslint-enable max-len */ + ); + } + + assertEquals( + await fs.readFileStr(filePath), + /* eslint-disable max-len */ + `#!/bin/sh +# This executable is generated by Deno. Please don't modify it unless you know what it means. +basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')") + +case \`uname\` in + *CYGWIN*) basedir=\`cygpath -w "$basedir"\`;; +esac + +if [ -x "$basedir/deno" ]; then + "$basedir/deno" "run" "http://localhost:4500/installer/testdata/echo.ts" "$@" + ret=$? +else + "deno" "run" "http://localhost:4500/installer/testdata/echo.ts" "$@" + ret=$? +fi +exit $ret +` + /* eslint-enable max-len */ + ); +}); + +installerTest(async function installCustomDir(): Promise<void> { + const tempDir = await makeTempDir(); + + await install( + "echo_test", + "http://localhost:4500/installer/testdata/echo.ts", + [], + tempDir + ); + + const filePath = path.resolve(tempDir, "echo_test"); + const fileInfo = await stat(filePath); + assert(fileInfo.isFile()); + + if (path.isWindows) { + assertEquals( + await fs.readFileStr(filePath + ".cmd"), + /* eslint-disable max-len */ + `% This executable is generated by Deno. Please don't modify it unless you know what it means. % +@IF EXIST "%~dp0\deno.exe" ( + "%~dp0\deno.exe" "run" "http://localhost:4500/installer/testdata/echo.ts" %* +) ELSE ( + @SETLOCAL + @SET PATHEXT=%PATHEXT:;.TS;=;% + "deno" "run" "http://localhost:4500/installer/testdata/echo.ts" %* +) +` + /* eslint-enable max-len */ + ); + } + + assertEquals( + await fs.readFileStr(filePath), + /* eslint-disable max-len */ + `#!/bin/sh +# This executable is generated by Deno. Please don't modify it unless you know what it means. +basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')") + +case \`uname\` in + *CYGWIN*) basedir=\`cygpath -w "$basedir"\`;; +esac + +if [ -x "$basedir/deno" ]; then + "$basedir/deno" "run" "http://localhost:4500/installer/testdata/echo.ts" "$@" + ret=$? +else + "deno" "run" "http://localhost:4500/installer/testdata/echo.ts" "$@" + ret=$? +fi +exit $ret +` + /* eslint-enable max-len */ + ); +}); + +installerTest(async function installLocalModule(): Promise<void> { + let localModule = path.join(Deno.cwd(), "installer", "testdata", "echo.ts"); + await install("echo_test", localModule, []); + + const { HOME } = env(); + const filePath = path.resolve(HOME, ".deno/bin/echo_test"); + const fileInfo = await stat(filePath); + assert(fileInfo.isFile()); + + if (path.isWindows) { + localModule = localModule.replace(/\\/g, "\\\\"); + } + + if (path.isWindows) { + assertEquals( + await fs.readFileStr(filePath + ".cmd"), + /* eslint-disable max-len */ + `% This executable is generated by Deno. Please don't modify it unless you know what it means. % +@IF EXIST "%~dp0\deno.exe" ( + "%~dp0\deno.exe" "run" "${localModule}" %* +) ELSE ( + @SETLOCAL + @SET PATHEXT=%PATHEXT:;.TS;=;% + "deno" "run" "${localModule}" %* +) +` + /* eslint-enable max-len */ + ); + } + + assertEquals( + await fs.readFileStr(filePath), + /* eslint-disable max-len */ + `#!/bin/sh +# This executable is generated by Deno. Please don't modify it unless you know what it means. +basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')") + +case \`uname\` in + *CYGWIN*) basedir=\`cygpath -w "$basedir"\`;; +esac + +if [ -x "$basedir/deno" ]; then + "$basedir/deno" "run" "${localModule}" "$@" + ret=$? +else + "deno" "run" "${localModule}" "$@" + ret=$? +fi +exit $ret +` + /* eslint-enable max-len */ + ); +}); + +installerTest(async function installWithFlags(): Promise<void> { + await install( + "echo_test", + "http://localhost:4500/installer/testdata/echo.ts", + ["--allow-net", "--allow-read", "--foobar"] + ); + + const { HOME } = env(); + const filePath = path.resolve(HOME, ".deno/bin/echo_test"); + + if (path.isWindows) { + assertEquals( + await fs.readFileStr(filePath + ".cmd"), + /* eslint-disable max-len */ + `% This executable is generated by Deno. Please don't modify it unless you know what it means. % +@IF EXIST "%~dp0\deno.exe" ( + "%~dp0\deno.exe" "run" "--allow-net" "--allow-read" "http://localhost:4500/installer/testdata/echo.ts" "--foobar" %* +) ELSE ( + @SETLOCAL + @SET PATHEXT=%PATHEXT:;.TS;=;% + "deno" "run" "--allow-net" "--allow-read" "http://localhost:4500/installer/testdata/echo.ts" "--foobar" %* +) +` + /* eslint-enable max-len */ + ); + } + + assertEquals( + await fs.readFileStr(filePath), + /* eslint-disable max-len */ + `#!/bin/sh +# This executable is generated by Deno. Please don't modify it unless you know what it means. +basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')") + +case \`uname\` in + *CYGWIN*) basedir=\`cygpath -w "$basedir"\`;; +esac + +if [ -x "$basedir/deno" ]; then + "$basedir/deno" "run" "--allow-net" "--allow-read" "http://localhost:4500/installer/testdata/echo.ts" "--foobar" "$@" + ret=$? +else + "deno" "run" "--allow-net" "--allow-read" "http://localhost:4500/installer/testdata/echo.ts" "--foobar" "$@" + ret=$? +fi +exit $ret +` + /* eslint-enable max-len */ + ); +}); + +installerTest(async function installLocalModuleAndRun(): Promise<void> { + const tempDir = await makeTempDir(); + const localModule = path.join(Deno.cwd(), "installer", "testdata", "echo.ts"); + await install("echo_test", localModule, ["hello"], tempDir); + + const filePath = path.resolve(tempDir, "echo_test"); + const fileInfo = await stat(filePath); + assert(fileInfo.isFile()); + + const ps = run({ + args: [filePath + (path.isWindows ? ".cmd" : ""), "foo"], + stdout: "piped" + }); + + if (!ps.stdout) { + assert(!!ps.stdout, "There should have stdout."); + return; + } + + let thrown = false; + + try { + const b = await readAll(ps.stdout); + + const s = new TextDecoder("utf-8").decode(b); + + assertEquals(s, "hello, foo"); + } catch (err) { + console.error(err); + thrown = true; + } finally { + await remove(tempDir, { recursive: true }); + ps.close(); + } + + assert(!thrown, "It should not throw an error"); +}, true); // set true to install module in your real $HOME dir. + +installerTest(async function installAndMakesureItCanRun(): Promise<void> { + const tempDir = await makeTempDir(); + await install( + "echo_test", + "http://localhost:4500/installer/testdata/echo.ts", + ["hello"], + tempDir + ); + + const filePath = path.resolve(tempDir, "echo_test"); + const fileInfo = await stat(filePath); + assert(fileInfo.isFile()); + + const ps = run({ + args: [filePath + (path.isWindows ? ".cmd" : ""), "foo"], + stdout: "piped" + }); + + if (!ps.stdout) { + assert(!!ps.stdout, "There should have stdout."); + return; + } + + let thrown = false; + + try { + const b = await readAll(ps.stdout); + + const s = new TextDecoder("utf-8").decode(b); + + assertEquals(s, "hello, foo"); + } catch (err) { + console.error(err); + thrown = true; + } finally { + await remove(tempDir, { recursive: true }); + ps.close(); + } + + assert(!thrown, "It should not throw an error"); +}, true); // set true to install module in your real $HOME dir. + +installerTest(async function installAndMakesureArgsRight(): Promise<void> { + const tempDir = await makeTempDir(); + await install( + "args_test", + "http://localhost:4500/installer/testdata/args.ts", + ["arg1", "--flag1"], + tempDir + ); + + const filePath = path.resolve(tempDir, "args_test"); + const fileInfo = await stat(filePath); + assert(fileInfo.isFile()); + + const ps = run({ + args: [filePath + (path.isWindows ? ".cmd" : ""), "arg2", "--flag2"], + stdout: "piped" + }); + + if (!ps.stdout) { + assert(!!ps.stdout, "There should have stdout."); + return; + } + + let thrown = false; + + try { + const b = await readAll(ps.stdout); + + const s = new TextDecoder("utf-8").decode(b); + + const obj = JSON.parse(s); + + assertEquals(obj[0], "arg1"); + assertEquals(obj[1], "--flag1"); + assertEquals(obj[2], "arg2"); + assertEquals(obj[3], "--flag2"); + } catch (err) { + console.error(err); + thrown = true; + } finally { + await remove(tempDir, { recursive: true }); + ps.close(); + } + + assert(!thrown, "It should not throw an error"); +}, true); // set true to install module in your real $HOME dir. test(function testIsRemoteUrl(): void { assert(isRemoteUrl("https://deno.land/std/http/file_server.ts")); diff --git a/std/prettier/main_test.ts b/std/prettier/main_test.ts index 5214db8ef..16c19ed03 100644 --- a/std/prettier/main_test.ts +++ b/std/prettier/main_test.ts @@ -213,7 +213,6 @@ console.log([function foo() {}, function baz() {}, (a) => {}]); emptyDir(tempDir); }); -/* TODO(ry) Re-enable test test(async function testPrettierPrintToStdout(): Promise<void> { const tempDir = await Deno.makeTempDir(); await copy(testdata, tempDir, { overwrite: true }); @@ -226,20 +225,35 @@ test(async function testPrettierPrintToStdout(): Promise<void> { const { stdout } = await run([...cmd, file0]); // The source file will not change without `--write` flags. - assertEquals(await getSourceCode(file0), "console.log (0)" + EOL); + assertEquals( + await getSourceCode(file0), + `console.log (0) +` + ); // The output should be formatted code. - assertEquals(stdout, "console.log(0);" + EOL); + assertEquals( + stdout, + `console.log(0); +` + ); const { stdout: formattedCode } = await run([...cmd, file1]); // The source file will not change without `--write` flags. - assertEquals(await getSourceCode(file1), "console.log(0);" + EOL); + assertEquals( + await getSourceCode(file1), + `console.log(0); +` + ); // The output will be formatted code even it is the same as the source file's // content. - assertEquals(formattedCode, "console.log(0);" + EOL); + assertEquals( + formattedCode, + `console.log(0); +` + ); emptyDir(tempDir); }); -*/ test(async function testPrettierReadFromStdin(): Promise<void> { interface TestCase { |