summaryrefslogtreecommitdiff
path: root/installer/test.ts
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2019-06-14 17:43:06 +0200
committerRyan Dahl <ry@tinyclouds.org>2019-06-14 08:43:06 -0700
commitd00a4beec40faa9d2ddb482ed152f00c36ae2f42 (patch)
tree39609a02ba29741d01eb1657a5871837b3ecbf54 /installer/test.ts
parent926594c53c600fd65f210806d7d6d841b02c3385 (diff)
feat: installer (denoland/deno_std#489)
Original: https://github.com/denoland/deno_std/commit/a3015be14195df46486a49e5c791afba4dfe084a
Diffstat (limited to 'installer/test.ts')
-rw-r--r--installer/test.ts122
1 files changed, 122 insertions, 0 deletions
diff --git a/installer/test.ts b/installer/test.ts
new file mode 100644
index 000000000..1b1aa4200
--- /dev/null
+++ b/installer/test.ts
@@ -0,0 +1,122 @@
+// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
+const { readFile, run, stat, makeTempDir, remove, env } = Deno;
+
+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";
+
+let fileServer: Deno.Process;
+
+// 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());
+
+ const fileBytes = await readFile(filePath);
+ const fileContents = new TextDecoder().decode(fileBytes);
+ assertEquals(
+ fileContents,
+ "#/bin/sh\ndeno http://localhost:4500/http/file_server.ts $@"
+ );
+});
+
+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");
+
+ const fileBytes = await readFile(filePath);
+ const fileContents = new TextDecoder().decode(fileBytes);
+ assertEquals(
+ fileContents,
+ "#/bin/sh\ndeno --allow-net --allow-read http://localhost:4500/http/file_server.ts --foobar $@"
+ );
+});
+
+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");
+
+ let thrown = false;
+ try {
+ await stat(filePath);
+ } catch (e) {
+ thrown = true;
+ assert(e instanceof Deno.DenoError);
+ assertEquals(e.kind, Deno.ErrorKind.NotFound);
+ }
+
+ assert(thrown);
+});
+
+installerTest(async function uninstallNonExistentModule(): Promise<void> {
+ await assertThrowsAsync(
+ async (): Promise<void> => {
+ await uninstall("file_server");
+ },
+ Error,
+ "file_server not found"
+ );
+});
+
+runIfMain(import.meta);