2019-06-14 11:43:06 -04:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-06-18 12:25:53 -04:00
|
|
|
const { run, stat, makeTempDir, remove, env } = Deno;
|
2019-06-14 11:43:06 -04:00
|
|
|
|
|
|
|
import { test, runIfMain, TestFunction } from "../testing/mod.ts";
|
|
|
|
import { assert, assertEquals, assertThrowsAsync } from "../testing/asserts.ts";
|
|
|
|
import { BufReader, EOF } from "../io/bufio.ts";
|
|
|
|
import { TextProtoReader } from "../textproto/mod.ts";
|
|
|
|
import { install, uninstall } from "./mod.ts";
|
|
|
|
import * as path from "../fs/path.ts";
|
2019-06-18 12:25:53 -04:00
|
|
|
import * as fs from "../fs/mod.ts";
|
2019-06-14 11:43:06 -04:00
|
|
|
|
|
|
|
let fileServer: Deno.Process;
|
2019-06-18 12:25:53 -04:00
|
|
|
const isWindows = Deno.platform.os === "win";
|
2019-06-14 11:43:06 -04:00
|
|
|
|
|
|
|
// copied from `http/file_server_test.ts`
|
|
|
|
async function startFileServer(): Promise<void> {
|
|
|
|
fileServer = run({
|
|
|
|
args: [
|
|
|
|
"deno",
|
|
|
|
"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 !== EOF && s.includes("server listening"));
|
|
|
|
}
|
|
|
|
|
|
|
|
function killFileServer(): void {
|
|
|
|
fileServer.close();
|
|
|
|
fileServer.stdout!.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
function installerTest(t: TestFunction): void {
|
|
|
|
const fn = async (): Promise<void> => {
|
|
|
|
await startFileServer();
|
|
|
|
const tempDir = await makeTempDir();
|
|
|
|
const envVars = env();
|
|
|
|
const originalHomeDir = envVars["HOME"];
|
|
|
|
envVars["HOME"] = tempDir;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await t();
|
|
|
|
} finally {
|
|
|
|
killFileServer();
|
|
|
|
await remove(tempDir, { recursive: true });
|
|
|
|
envVars["HOME"] = originalHomeDir;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
test(fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
installerTest(async function installBasic(): Promise<void> {
|
|
|
|
await install("file_srv", "http://localhost:4500/http/file_server.ts", []);
|
|
|
|
|
|
|
|
const { HOME } = env();
|
|
|
|
const filePath = path.resolve(HOME, ".deno/bin/file_srv");
|
|
|
|
const fileInfo = await stat(filePath);
|
|
|
|
assert(fileInfo.isFile());
|
|
|
|
|
2019-06-18 12:25:53 -04:00
|
|
|
if (isWindows) {
|
|
|
|
assertEquals(
|
|
|
|
await fs.readFileStr(filePath + ".cmd"),
|
2019-06-19 00:22:01 -04:00
|
|
|
/* eslint-disable max-len */
|
2019-06-18 12:25:53 -04:00
|
|
|
`% This executable is generated by Deno. Please don't modify it unless you know what it means. %
|
|
|
|
@IF EXIST "%~dp0\deno.exe" (
|
2019-06-19 10:16:34 -04:00
|
|
|
"%~dp0\deno.exe" "run" "http://localhost:4500/http/file_server.ts" %*
|
2019-06-18 12:25:53 -04:00
|
|
|
) ELSE (
|
|
|
|
@SETLOCAL
|
|
|
|
@SET PATHEXT=%PATHEXT:;.TS;=;%
|
2019-06-19 10:16:34 -04:00
|
|
|
"deno" "run" "http://localhost:4500/http/file_server.ts" %*
|
2019-06-18 12:25:53 -04:00
|
|
|
)
|
|
|
|
`
|
2019-06-19 00:22:01 -04:00
|
|
|
/* eslint-enable max-len */
|
2019-06-18 12:25:53 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-14 11:43:06 -04:00
|
|
|
assertEquals(
|
2019-06-18 12:25:53 -04:00
|
|
|
await fs.readFileStr(filePath),
|
2019-06-19 00:22:01 -04:00
|
|
|
/* eslint-disable max-len */
|
2019-06-18 12:25:53 -04:00
|
|
|
`#/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
|
2019-06-19 10:16:34 -04:00
|
|
|
"$basedir/deno" "run" "http://localhost:4500/http/file_server.ts" "$@"
|
2019-06-18 12:25:53 -04:00
|
|
|
ret=$?
|
|
|
|
else
|
2019-06-19 10:16:34 -04:00
|
|
|
"deno" "run" "http://localhost:4500/http/file_server.ts" "$@"
|
2019-06-18 12:25:53 -04:00
|
|
|
ret=$?
|
|
|
|
fi
|
|
|
|
exit $ret
|
|
|
|
`
|
2019-06-19 00:22:01 -04:00
|
|
|
/* eslint-enable max-len */
|
2019-06-14 11:43:06 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
installerTest(async function installWithFlags(): Promise<void> {
|
|
|
|
await install("file_server", "http://localhost:4500/http/file_server.ts", [
|
|
|
|
"--allow-net",
|
|
|
|
"--allow-read",
|
|
|
|
"--foobar"
|
|
|
|
]);
|
|
|
|
|
|
|
|
const { HOME } = env();
|
|
|
|
const filePath = path.resolve(HOME, ".deno/bin/file_server");
|
|
|
|
|
2019-06-18 12:25:53 -04:00
|
|
|
if (isWindows) {
|
|
|
|
assertEquals(
|
|
|
|
await fs.readFileStr(filePath + ".cmd"),
|
2019-06-19 00:22:01 -04:00
|
|
|
/* eslint-disable max-len */
|
2019-06-18 12:25:53 -04:00
|
|
|
`% This executable is generated by Deno. Please don't modify it unless you know what it means. %
|
|
|
|
@IF EXIST "%~dp0\deno.exe" (
|
2019-06-19 10:16:34 -04:00
|
|
|
"%~dp0\deno.exe" "run" "--allow-net" "--allow-read" "http://localhost:4500/http/file_server.ts" "--foobar" %*
|
2019-06-18 12:25:53 -04:00
|
|
|
) ELSE (
|
|
|
|
@SETLOCAL
|
|
|
|
@SET PATHEXT=%PATHEXT:;.TS;=;%
|
2019-06-19 10:16:34 -04:00
|
|
|
"deno" "run" "--allow-net" "--allow-read" "http://localhost:4500/http/file_server.ts" "--foobar" %*
|
2019-06-18 12:25:53 -04:00
|
|
|
)
|
|
|
|
`
|
2019-06-19 00:22:01 -04:00
|
|
|
/* eslint-enable max-len */
|
2019-06-18 12:25:53 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-14 11:43:06 -04:00
|
|
|
assertEquals(
|
2019-06-18 12:25:53 -04:00
|
|
|
await fs.readFileStr(filePath),
|
2019-06-19 00:22:01 -04:00
|
|
|
/* eslint-disable max-len */
|
2019-06-18 12:25:53 -04:00
|
|
|
`#/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
|
2019-06-19 10:16:34 -04:00
|
|
|
"$basedir/deno" "run" "--allow-net" "--allow-read" "http://localhost:4500/http/file_server.ts" "--foobar" "$@"
|
2019-06-18 12:25:53 -04:00
|
|
|
ret=$?
|
|
|
|
else
|
2019-06-19 10:16:34 -04:00
|
|
|
"deno" "run" "--allow-net" "--allow-read" "http://localhost:4500/http/file_server.ts" "--foobar" "$@"
|
2019-06-18 12:25:53 -04:00
|
|
|
ret=$?
|
|
|
|
fi
|
|
|
|
exit $ret
|
|
|
|
`
|
2019-06-19 00:22:01 -04:00
|
|
|
/* eslint-enable max-len */
|
2019-06-14 11:43:06 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
installerTest(async function uninstallBasic(): Promise<void> {
|
|
|
|
await install("file_server", "http://localhost:4500/http/file_server.ts", []);
|
|
|
|
|
|
|
|
const { HOME } = env();
|
|
|
|
const filePath = path.resolve(HOME, ".deno/bin/file_server");
|
|
|
|
|
|
|
|
await uninstall("file_server");
|
|
|
|
|
2019-06-18 12:25:53 -04:00
|
|
|
assert(!(await fs.exists(filePath)));
|
|
|
|
assert(!(await fs.exists(filePath + ".cmd")));
|
2019-06-14 11:43:06 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
installerTest(async function uninstallNonExistentModule(): Promise<void> {
|
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
|
|
|
await uninstall("file_server");
|
|
|
|
},
|
|
|
|
Error,
|
|
|
|
"file_server not found"
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
runIfMain(import.meta);
|