-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathpreuninstall.ts
65 lines (53 loc) · 2.61 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
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";
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.isTrue(argsPassedToSpawn.indexOf(expectedPathToCliExecutable) !== -1, `The spawned args must contain path to TNS.
Expected path is: ${expectedPathToCliExecutable}, current args are: ${argsPassedToSpawn}.`);
assert.isTrue(argsPassedToSpawn.indexOf("dev-preuninstall") !== -1, `The spawned args must contain the name of the preuninstall command.
Expected path is: ${expectedPathToCliExecutable}, current args are: ${argsPassedToSpawn}.`);
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("ensure package.json has correct preuninstall script", () => {
const packageJsonData = require("../package.json");
assert.equal(packageJsonData.scripts.preuninstall, "node preuninstall.js");
});
});