-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathpreuninstall.ts
82 lines (67 loc) · 3.5 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
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 originalSpawn = childProcess.spawn;
const originalConsoleError = 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 = originalSpawn;
console.error = originalConsoleError;
});
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.deepEqual(argsPassedToSpawn, [expectedPathToCliExecutable, "dev-preuninstall"]);
assert.deepEqual(optionsPassedToSpawn, [{ stdio: "inherit" }], "The stdio must be inherit as this way CLI's command can determine correctly if terminal is in interactive mode.");
assert.deepEqual(dataPassedToConsoleError, []);
const errMsg = "this is error message";
eventEmitter.emit("error", new Error(errMsg));
assert.deepEqual(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 originalEnvValue = 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 = originalEnvValue;
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.deepEqual(argsPassedToSpawn, [expectedPathToCliExecutable, "dev-preuninstall", "--analyticsLogFile", "value from env analyticsLog.txt"]);
assert.deepEqual(optionsPassedToSpawn, [{ stdio: "inherit" }], "The stdio must be inherit as this way CLI's command can determine correctly if terminal is in interactive mode.");
assert.deepEqual(dataPassedToConsoleError, []);
});
it("ensure package.json has correct preuninstall script", () => {
const packageJsonData = require("../package.json");
assert.equal(packageJsonData.scripts.preuninstall, "node preuninstall.js");
});
});