-
Notifications
You must be signed in to change notification settings - Fork 278
/
Copy pathmock-run.ts
174 lines (156 loc) · 6.11 KB
/
mock-run.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
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
import { TaskLibAnswers } from './mock-answer';
import { SinonSandbox, createSandbox } from 'sinon';
import im = require('./internal');
import * as taskLib from './task';
export class TaskMockRunner {
constructor(taskPath: string) {
this._taskPath = taskPath;
this._sandbox = createSandbox();
}
_taskPath: string;
_answers: TaskLibAnswers | undefined;
_exports: {[key: string]: any} = { };
_moduleCount: number = 0;
private _sandbox: SinonSandbox;
public setInput(name: string, val: string) {
let key: string = im._getVariableKey(name);
process.env['INPUT_' + key] = val;
}
public setVariableName(name: string, val: string, isSecret?: boolean) {
let key: string = im._getVariableKey(name);
if (isSecret) {
process.env['SECRET_' + key] = val;
}
else {
process.env['VSTS_TASKVARIABLE_' + key] = val;
}
}
/**
* Register answers for the mock "azure-pipelines-task-lib/task" instance.
*
* @param answers Answers to be returned when the task lib functions are called.
*/
public setAnswers(answers: TaskLibAnswers) {
this._answers = answers;
}
/**
* Checks if a module name is valid for import, avoiding local module references.
*
* @param {string} modName - The name of the module to be checked.
* @returns {boolean} Returns true if the module name is valid, otherwise false.
*/
checkModuleName(modName: string): boolean {
if (modName.includes('.')) {
console.error(`ERROR: ${modName} is a local module. Cannot import it from task-lib. Please pass full path.`);
return false;
}
return true;
}
/**
* Checks if a method in a new module is mockable based on specified conditions.
*
* @param {object} newModule - The new module containing the method.
* @param {string} methodName - The name of the method to check.
* @param {object} oldModule - The original module from which the method might be inherited.
* @returns {boolean} Returns true if the method is mockable, otherwise false.
*/
checkIsMockable(newModule, methodName, oldModule) {
// Get the method from the newModule
const method = newModule[methodName];
// Check if the method exists and is not undefined
if (!newModule.hasOwnProperty(methodName) || typeof method === 'undefined') {
return false;
}
// Check if the method is a function
if (typeof method !== 'function') {
console.log(`WARNING: ${methodName} of ${newModule} is not a function. There is no option to replace getter/setter in this implementation. You can consider changing it.`);
return false;
}
// Check if the method is writable
const descriptor = Object.getOwnPropertyDescriptor(oldModule, methodName);
return descriptor && descriptor.writable !== false;
}
/**
* Registers a mock module, allowing the replacement of methods with mock implementations.
*
* @param {string} modName - The name of the module to be overridden.
* @param {object} modMock - The mock implementation of the module.
* @returns {void}
*/
public registerMock(modName: string, modMock: object): void {
this._moduleCount++;
let oldMod: object;
// Check if the module name is valid and can be imported
if (this.checkModuleName(modName)) {
oldMod = require(modName);
} else {
console.error(`ERROR: Cannot import ${modName}.`);
return;
}
// Iterate through methods in the old module and replace them with mock implementations
for (let method in oldMod) {
if (this.checkIsMockable(modMock, method, oldMod)) {
const replacement = modMock[method] || oldMod[method];
try {
this._sandbox.replace(oldMod, method, replacement);
} catch (error) {
console.error('ERROR: Cannot replace ${method} in ${oldMod} by ${replacement}. ${error.message}', );
}
}
}
}
/**
* Registers an override for a specific function on the mock "azure-pipelines-task-lib/task" instance.
* This can be used in conjunction with setAnswers(), for cases where additional runtime
* control is needed for a specific function.
*
* @param key Function or field to override.
* @param val Function or field value.
* @returns void
*/
public registerMockExport(key: string, val: any): void {
this._exports[key] = val;
}
/**
* Runs a task script.
*
* @param noMockTask Indicates whether to mock "azure-pipelines-task-lib/task". Default is to mock.
* @returns void
*/
public run(noMockTask?: boolean): void {
// answers and exports not compatible with "noMockTask" mode
if (noMockTask) {
if (this._answers || Object.keys(this._exports).length) {
throw new Error('setAnswers() and registerMockExport() is not compatible with "noMockTask" mode');
}
}
// register mock task lib
else {
let tlm = require('azure-pipelines-task-lib/mock-task');
if (this._answers) {
tlm.setAnswers(this._answers);
}
Object.keys(this._exports)
.forEach((key: string): void => {
tlm[key] = this._exports[key];
});
// With sinon we have to iterate through methods in the old module and replace them with mock implementations
let tlt = require('azure-pipelines-task-lib/task');
for (let method in tlt) {
if (tlm.hasOwnProperty(method)) {
this._sandbox.replace(tlt, method, tlm[method]);
}
}
}
// run it
require(this._taskPath);
}
/**
* Restores the sandboxed environment to its original state.
*
* @returns {void}
*/
public restore() {
this._sandbox.restore();
}
}