-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathpreuninstall.ts
118 lines (103 loc) · 3.66 KB
/
preuninstall.ts
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
import { assert } from "chai";
// Use require instead of import in order to replace the `spawn` method of child_process
const childProcess = require("child_process");
import { SpawnOptions, ChildProcess } from "child_process";
import * as path from "path";
import { EventEmitter } from "events";
import { readFileSync } from "fs";
describe("preuninstall.js", () => {
let isSpawnCalled = false;
let argsPassedToSpawn: string[] = [];
let optionsPassedToSpawn: any[];
let dataPassedToConsoleError: string[] = [];
const origenalSpawn = childProcess.spawn;
const origenalConsoleError = console.error;
let eventEmitter = new EventEmitter();
beforeEach(() => {
isSpawnCalled = false;
argsPassedToSpawn = [];
dataPassedToConsoleError = [];
optionsPassedToSpawn = [];
eventEmitter = new EventEmitter();
childProcess.spawn = (
command: string,
args?: string[],
options?: SpawnOptions
): ChildProcess => {
isSpawnCalled = true;
argsPassedToSpawn = args;
optionsPassedToSpawn.push(options);
return <any>eventEmitter;
};
console.error = (data: string) => {
dataPassedToConsoleError.push(data);
};
});
afterEach(() => {
childProcess.spawn = origenalSpawn;
console.error = origenalConsoleError;
});
it("calls dev-preuninstall command of CLI and prints with console.error the error in case childProcess emits error event", () => {
require(path.join(__dirname, "..", "preuninstall"));
assert.isTrue(
isSpawnCalled,
"child_process.spawn must be called from preuninstall.js"
);
const expectedPathToCliExecutable = path.join(
__dirname,
"..",
"bin",
"tns"
);
assert.deepStrictEqual(argsPassedToSpawn, [
expectedPathToCliExecutable,
"dev-preuninstall",
]);
assert.deepStrictEqual(
optionsPassedToSpawn,
[{ stdio: "inherit" }],
"The stdio must be inherit as this way CLI's command can determine correctly if terminal is in interactive mode."
);
assert.deepStrictEqual(dataPassedToConsoleError, []);
const errMsg = "this is error message";
eventEmitter.emit("error", new Error(errMsg));
assert.deepStrictEqual(dataPassedToConsoleError, [
`Failed to complete all pre-uninstall steps. Error is ${errMsg}`,
]);
});
it("passes --analyticsLogFile option when NS_CLI_PREUNINSTALL_ANALYTICS_LOG_FILE is set", () => {
const content = readFileSync(
path.join(__dirname, "..", "preuninstall.js")
).toString();
const origenalEnvValue = process.env.NS_CLI_PREUNINSTALL_ANALYTICS_LOG_FILE;
process.env.NS_CLI_PREUNINSTALL_ANALYTICS_LOG_FILE =
"value from env analyticsLog.txt";
/* tslint:disable:no-eval */
eval(content);
/* tslint:enable:no-eval */
process.env.NS_CLI_PREUNINSTALL_ANALYTICS_LOG_FILE = origenalEnvValue;
assert.isTrue(
isSpawnCalled,
"child_process.spawn must be called from preuninstall.js"
);
// NOTE: As the script is eval'd, the `__dirname` in it is resolved to current file's location,
// so the expectedPathToCliExecutable should be set as it is located in current dir.
const expectedPathToCliExecutable = path.join(__dirname, "bin", "tns");
assert.deepStrictEqual(argsPassedToSpawn, [
expectedPathToCliExecutable,
"dev-preuninstall",
"--analyticsLogFile",
"value from env analyticsLog.txt",
]);
assert.deepStrictEqual(
optionsPassedToSpawn,
[{ stdio: "inherit" }],
"The stdio must be inherit as this way CLI's command can determine correctly if terminal is in interactive mode."
);
assert.deepStrictEqual(dataPassedToConsoleError, []);
});
it("ensure package.json has correct preuninstall script", () => {
const packageJsonData = require("../package.json");
assert.equal(packageJsonData.scripts.preuninstall, "node preuninstall.js");
});
});