1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
const { 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";
import * as fs from "../fs/mod.ts";
let fileServer: Deno.Process;
const isWindows = Deno.platform.os === "win";
// 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());
if (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/http/file_server.ts %*
) ELSE (
@SETLOCAL
@SET PATHEXT=%PATHEXT:;.TS;=;%
deno run http://localhost:4500/http/file_server.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/http/file_server.ts "$@"
ret=$?
else
deno run http://localhost:4500/http/file_server.ts "$@"
ret=$?
fi
exit $ret
`
/* eslint-enable max-len */
);
});
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");
if (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/http/file_server.ts --foobar %*
) ELSE (
@SETLOCAL
@SET PATHEXT=%PATHEXT:;.TS;=;%
deno run --allow-net --allow-read http://localhost:4500/http/file_server.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/http/file_server.ts --foobar "$@"
ret=$?
else
deno run --allow-net --allow-read http://localhost:4500/http/file_server.ts --foobar "$@"
ret=$?
fi
exit $ret
`
/* eslint-enable max-len */
);
});
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");
assert(!(await fs.exists(filePath)));
assert(!(await fs.exists(filePath + ".cmd")));
});
installerTest(async function uninstallNonExistentModule(): Promise<void> {
await assertThrowsAsync(
async (): Promise<void> => {
await uninstall("file_server");
},
Error,
"file_server not found"
);
});
runIfMain(import.meta);
|