azure-pipelines-task-lib 4.6.1 → 4.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib-mocker.d.ts +17 -0
- package/lib-mocker.js +199 -0
- package/mock-run.js +5 -5
- package/package.json +2 -4
package/lib-mocker.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface MockOptions {
|
|
2
|
+
useCleanCache?: boolean;
|
|
3
|
+
warnOnReplace?: boolean;
|
|
4
|
+
warnOnUnregistered?: boolean;
|
|
5
|
+
}
|
|
6
|
+
export declare function enable(opts: MockOptions): void;
|
|
7
|
+
export declare function disable(): void;
|
|
8
|
+
export declare function resetCache(): void;
|
|
9
|
+
export declare function warnOnReplace(enable: boolean): void;
|
|
10
|
+
export declare function warnOnUnregistered(enable: boolean): void;
|
|
11
|
+
export declare function registerMock(mod: string, mock: any): void;
|
|
12
|
+
export declare function deregisterMock(mod: string): void;
|
|
13
|
+
export declare function deregisterAll(): void;
|
|
14
|
+
export declare function registerAllowable(mod: string): void;
|
|
15
|
+
export declare function registerAllowables(mods: string[]): void;
|
|
16
|
+
export declare function deregisterAllowable(mod: string): void;
|
|
17
|
+
export declare function deregisterAllowables(mods: any): void;
|
package/lib-mocker.js
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.deregisterAllowables = exports.deregisterAllowable = exports.registerAllowables = exports.registerAllowable = exports.deregisterAll = exports.deregisterMock = exports.registerMock = exports.warnOnUnregistered = exports.warnOnReplace = exports.resetCache = exports.disable = exports.enable = void 0;
|
|
4
|
+
var m = require('module');
|
|
5
|
+
;
|
|
6
|
+
var registeredMocks = {};
|
|
7
|
+
var registeredAllowables = new Set();
|
|
8
|
+
var originalLoader = null;
|
|
9
|
+
var originalCache = {};
|
|
10
|
+
var options = {};
|
|
11
|
+
var defaultOptions = {
|
|
12
|
+
useCleanCache: false,
|
|
13
|
+
warnOnReplace: true,
|
|
14
|
+
warnOnUnregistered: true
|
|
15
|
+
};
|
|
16
|
+
function _getEffectiveOptions(opts) {
|
|
17
|
+
var options = {};
|
|
18
|
+
Object.keys(defaultOptions).forEach(function (key) {
|
|
19
|
+
if (opts && opts.hasOwnProperty(key)) {
|
|
20
|
+
options[key] = opts[key];
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
options[key] = defaultOptions[key];
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
return options;
|
|
27
|
+
}
|
|
28
|
+
/*
|
|
29
|
+
* Loader function that used when hooking is enabled.
|
|
30
|
+
* if the requested module is registered as a mock, return the mock.
|
|
31
|
+
* otherwise, invoke the original loader + put warning in the output.
|
|
32
|
+
*/
|
|
33
|
+
function _hookedLoader(request, parent, isMain) {
|
|
34
|
+
if (!originalLoader) {
|
|
35
|
+
throw new Error("Loader has not been hooked");
|
|
36
|
+
}
|
|
37
|
+
if (registeredMocks.hasOwnProperty(request)) {
|
|
38
|
+
return registeredMocks[request];
|
|
39
|
+
}
|
|
40
|
+
if (!registeredAllowables.has(request) && options.warnOnUnregistered) {
|
|
41
|
+
console.warn("WARNING: loading non-allowed module: " + request);
|
|
42
|
+
}
|
|
43
|
+
return originalLoader(request, parent, isMain);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Remove references to modules in the cache from
|
|
47
|
+
* their parents' children.
|
|
48
|
+
*/
|
|
49
|
+
function _removeParentReferences() {
|
|
50
|
+
Object.keys(m._cache).forEach(function (k) {
|
|
51
|
+
var _a;
|
|
52
|
+
if (k.indexOf('\.node') === -1) {
|
|
53
|
+
// don't touch native modules, because they're special
|
|
54
|
+
var mod = m._cache[k];
|
|
55
|
+
var idx = (_a = mod === null || mod === void 0 ? void 0 : mod.parent) === null || _a === void 0 ? void 0 : _a.children.indexOf(mod);
|
|
56
|
+
if (idx > -1) {
|
|
57
|
+
mod.parent.children.splice(idx, 1);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
/*
|
|
63
|
+
* Starting in node 0.12 node won't reload native modules
|
|
64
|
+
* The reason is that native modules can register themselves to be loaded automatically
|
|
65
|
+
* This will re-populate the cache with the native modules that have not been mocked
|
|
66
|
+
*/
|
|
67
|
+
function _repopulateNative() {
|
|
68
|
+
Object.keys(originalCache).forEach(function (k) {
|
|
69
|
+
if (k.indexOf('\.node') > -1 && !m._cache[k]) {
|
|
70
|
+
m._cache[k] = originalCache[k];
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
/*
|
|
75
|
+
* Enable function, hooking the Node loader with options.
|
|
76
|
+
*/
|
|
77
|
+
function enable(opts) {
|
|
78
|
+
if (originalLoader) {
|
|
79
|
+
// Already hooked
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
options = _getEffectiveOptions(opts);
|
|
83
|
+
if (options.useCleanCache) {
|
|
84
|
+
originalCache = m._cache;
|
|
85
|
+
m._cache = {};
|
|
86
|
+
_repopulateNative();
|
|
87
|
+
}
|
|
88
|
+
originalLoader = m._load;
|
|
89
|
+
m._load = _hookedLoader;
|
|
90
|
+
}
|
|
91
|
+
exports.enable = enable;
|
|
92
|
+
/*
|
|
93
|
+
* Disables mock loading, reverting to normal 'require' behaviour.
|
|
94
|
+
*/
|
|
95
|
+
function disable() {
|
|
96
|
+
if (!originalLoader)
|
|
97
|
+
return;
|
|
98
|
+
if (options.useCleanCache) {
|
|
99
|
+
Object.keys(m._cache).forEach(function (k) {
|
|
100
|
+
if (k.indexOf('\.node') > -1 && !originalCache[k]) {
|
|
101
|
+
originalCache[k] = m._cache[k];
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
_removeParentReferences();
|
|
105
|
+
m._cache = originalCache;
|
|
106
|
+
originalCache = {};
|
|
107
|
+
}
|
|
108
|
+
m._load = originalLoader;
|
|
109
|
+
originalLoader = null;
|
|
110
|
+
}
|
|
111
|
+
exports.disable = disable;
|
|
112
|
+
/*
|
|
113
|
+
* If the clean cache option is in effect, reset the module cache to an empty
|
|
114
|
+
* state. Calling this function when the clean cache option is not in effect
|
|
115
|
+
* will have no ill effects, but will do nothing.
|
|
116
|
+
*/
|
|
117
|
+
function resetCache() {
|
|
118
|
+
if (options.useCleanCache && originalCache) {
|
|
119
|
+
_removeParentReferences();
|
|
120
|
+
m._cache = {};
|
|
121
|
+
_repopulateNative();
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
exports.resetCache = resetCache;
|
|
125
|
+
/*
|
|
126
|
+
* Enable or disable warnings to the console when previously registered mocks are replaced.
|
|
127
|
+
*/
|
|
128
|
+
function warnOnReplace(enable) {
|
|
129
|
+
options.warnOnReplace = enable;
|
|
130
|
+
}
|
|
131
|
+
exports.warnOnReplace = warnOnReplace;
|
|
132
|
+
/*
|
|
133
|
+
* Enable or disable warnings to the console when modules are loaded that have
|
|
134
|
+
* not been registered as a mock.
|
|
135
|
+
*/
|
|
136
|
+
function warnOnUnregistered(enable) {
|
|
137
|
+
options.warnOnUnregistered = enable;
|
|
138
|
+
}
|
|
139
|
+
exports.warnOnUnregistered = warnOnUnregistered;
|
|
140
|
+
/*
|
|
141
|
+
* Register a mock object for the specified module.
|
|
142
|
+
*/
|
|
143
|
+
function registerMock(mod, mock) {
|
|
144
|
+
if (options.warnOnReplace && registeredMocks.hasOwnProperty(mod)) {
|
|
145
|
+
console.warn("WARNING: Replacing existing mock for module: " + mod);
|
|
146
|
+
}
|
|
147
|
+
registeredMocks[mod] = mock;
|
|
148
|
+
}
|
|
149
|
+
exports.registerMock = registerMock;
|
|
150
|
+
/*
|
|
151
|
+
* Deregister a mock object for the specified module.
|
|
152
|
+
*/
|
|
153
|
+
function deregisterMock(mod) {
|
|
154
|
+
if (registeredMocks.hasOwnProperty(mod)) {
|
|
155
|
+
delete registeredMocks[mod];
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
exports.deregisterMock = deregisterMock;
|
|
159
|
+
/*
|
|
160
|
+
* Deregister all mocks.
|
|
161
|
+
*/
|
|
162
|
+
function deregisterAll() {
|
|
163
|
+
registeredMocks = {};
|
|
164
|
+
registeredAllowables = new Set();
|
|
165
|
+
}
|
|
166
|
+
exports.deregisterAll = deregisterAll;
|
|
167
|
+
/*
|
|
168
|
+
Register a module as 'allowed'.
|
|
169
|
+
This will allow the module to be loaded without mock otherwise a warning would be thrown.
|
|
170
|
+
*/
|
|
171
|
+
function registerAllowable(mod) {
|
|
172
|
+
registeredAllowables.add(mod);
|
|
173
|
+
}
|
|
174
|
+
exports.registerAllowable = registerAllowable;
|
|
175
|
+
/*
|
|
176
|
+
* Register an array of 'allowed' modules.
|
|
177
|
+
*/
|
|
178
|
+
function registerAllowables(mods) {
|
|
179
|
+
mods.forEach(function (mod) { return registerAllowable(mod); });
|
|
180
|
+
}
|
|
181
|
+
exports.registerAllowables = registerAllowables;
|
|
182
|
+
/*
|
|
183
|
+
* Deregister a module as 'allowed'.
|
|
184
|
+
*/
|
|
185
|
+
function deregisterAllowable(mod) {
|
|
186
|
+
if (registeredAllowables.hasOwnProperty(mod)) {
|
|
187
|
+
registeredAllowables.delete(mod);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
exports.deregisterAllowable = deregisterAllowable;
|
|
191
|
+
/*
|
|
192
|
+
* Deregister an array of modules as 'allowed'.
|
|
193
|
+
*/
|
|
194
|
+
function deregisterAllowables(mods) {
|
|
195
|
+
mods.forEach(function (mod) {
|
|
196
|
+
deregisterAllowable(mod);
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
exports.deregisterAllowables = deregisterAllowables;
|
package/mock-run.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TaskMockRunner = void 0;
|
|
4
|
-
var mockery = require("mockery");
|
|
5
4
|
var im = require("./internal");
|
|
5
|
+
var mocker = require("./lib-mocker");
|
|
6
6
|
var TaskMockRunner = /** @class */ (function () {
|
|
7
7
|
function TaskMockRunner(taskPath) {
|
|
8
8
|
this._exports = {};
|
|
@@ -40,7 +40,7 @@ var TaskMockRunner = /** @class */ (function () {
|
|
|
40
40
|
*/
|
|
41
41
|
TaskMockRunner.prototype.registerMock = function (modName, mod) {
|
|
42
42
|
this._moduleCount++;
|
|
43
|
-
|
|
43
|
+
mocker.registerMock(modName, mod);
|
|
44
44
|
};
|
|
45
45
|
/**
|
|
46
46
|
* Registers an override for a specific function on the mock "azure-pipelines-task-lib/task" instance.
|
|
@@ -62,9 +62,9 @@ var TaskMockRunner = /** @class */ (function () {
|
|
|
62
62
|
*/
|
|
63
63
|
TaskMockRunner.prototype.run = function (noMockTask) {
|
|
64
64
|
var _this = this;
|
|
65
|
-
// determine whether to enable
|
|
65
|
+
// determine whether to enable mocker
|
|
66
66
|
if (!noMockTask || this._moduleCount) {
|
|
67
|
-
|
|
67
|
+
mocker.enable({ warnOnUnregistered: false });
|
|
68
68
|
}
|
|
69
69
|
// answers and exports not compatible with "noMockTask" mode
|
|
70
70
|
if (noMockTask) {
|
|
@@ -82,7 +82,7 @@ var TaskMockRunner = /** @class */ (function () {
|
|
|
82
82
|
.forEach(function (key) {
|
|
83
83
|
tlm[key] = _this._exports[key];
|
|
84
84
|
});
|
|
85
|
-
|
|
85
|
+
mocker.registerMock('azure-pipelines-task-lib/task', tlm);
|
|
86
86
|
}
|
|
87
87
|
// run it
|
|
88
88
|
require(this._taskPath);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "azure-pipelines-task-lib",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.7.0",
|
|
4
4
|
"description": "Azure Pipelines Task SDK",
|
|
5
5
|
"main": "./task.js",
|
|
6
6
|
"typings": "./task.d.ts",
|
|
@@ -30,7 +30,6 @@
|
|
|
30
30
|
"adm-zip": "^0.5.10",
|
|
31
31
|
"deasync": "^0.1.28",
|
|
32
32
|
"minimatch": "3.0.5",
|
|
33
|
-
"mockery": "^2.1.0",
|
|
34
33
|
"nodejs-file-downloader": "^4.11.1",
|
|
35
34
|
"q": "^1.5.1",
|
|
36
35
|
"semver": "^5.1.0",
|
|
@@ -40,8 +39,7 @@
|
|
|
40
39
|
"devDependencies": {
|
|
41
40
|
"@types/minimatch": "3.0.3",
|
|
42
41
|
"@types/mocha": "^9.1.1",
|
|
43
|
-
"@types/
|
|
44
|
-
"@types/node": "^10.17.0",
|
|
42
|
+
"@types/node": "^16.11.39",
|
|
45
43
|
"@types/q": "^1.5.4",
|
|
46
44
|
"@types/semver": "^7.3.4",
|
|
47
45
|
"@types/shelljs": "^0.8.8",
|