azure-pipelines-task-lib 4.4.0 → 5.0.0-preview.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.
@@ -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
- mockery.registerMock(modName, mod);
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 mockery
65
+ // determine whether to enable mocker
66
66
  if (!noMockTask || this._moduleCount) {
67
- mockery.enable({ warnOnUnregistered: false });
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
- mockery.registerMock('azure-pipelines-task-lib/task', tlm);
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.4.0",
3
+ "version": "5.0.0-preview.0",
4
4
  "description": "Azure Pipelines Task SDK",
5
5
  "main": "./task.js",
6
6
  "typings": "./task.d.ts",
@@ -28,7 +28,6 @@
28
28
  "homepage": "https://github.com/Microsoft/azure-pipelines-task-lib",
29
29
  "dependencies": {
30
30
  "minimatch": "3.0.5",
31
- "mockery": "^2.1.0",
32
31
  "q": "^1.5.1",
33
32
  "semver": "^5.1.0",
34
33
  "shelljs": "^0.8.5",
@@ -38,7 +37,6 @@
38
37
  "devDependencies": {
39
38
  "@types/minimatch": "3.0.3",
40
39
  "@types/mocha": "^9.1.1",
41
- "@types/mockery": "^1.4.29",
42
40
  "@types/node": "^16.11.39",
43
41
  "@types/q": "^1.5.4",
44
42
  "@types/semver": "^7.3.4",
package/task.d.ts CHANGED
@@ -58,7 +58,8 @@ export declare const setErrStream: typeof im._setErrStream;
58
58
  * from agent version 2.142.0 or higher (otherwise will no-op).
59
59
  * @returns void
60
60
  */
61
- export declare function setResult(result: TaskResult, message: string, done?: boolean): void;
61
+ export declare function setResult(result: TaskResult.Succeeded, message?: string, done?: boolean): void;
62
+ export declare function setResult(result: Exclude<TaskResult, 'Succeeded'>, message: string, done?: boolean): void;
62
63
  export declare const setResourcePath: typeof im._setResourcePath;
63
64
  export declare const loc: typeof im._loc;
64
65
  export declare const getVariable: typeof im._getVariable;
package/task.js CHANGED
@@ -63,22 +63,6 @@ var AgentHostedMode;
63
63
  //-----------------------------------------------------
64
64
  exports.setStdStream = im._setStdStream;
65
65
  exports.setErrStream = im._setErrStream;
66
- //-----------------------------------------------------
67
- // Results
68
- //-----------------------------------------------------
69
- /**
70
- * Sets the result of the task.
71
- * Execution will continue.
72
- * If not set, task will be Succeeded.
73
- * If multiple calls are made to setResult the most pessimistic call wins (Failed) regardless of the order of calls.
74
- *
75
- * @param result TaskResult enum of Succeeded, SucceededWithIssues, Failed, Cancelled or Skipped.
76
- * @param message A message which will be logged as an error issue if the result is Failed.
77
- * @param done Optional. Instructs the agent the task is done. This is helpful when child processes
78
- * may still be running and prevent node from fully exiting. This argument is supported
79
- * from agent version 2.142.0 or higher (otherwise will no-op).
80
- * @returns void
81
- */
82
66
  function setResult(result, message, done) {
83
67
  exports.debug('task result: ' + TaskResult[result]);
84
68
  // add an error issue