azure-pipelines-task-lib: Unable to mock Windows in `getPlatform` call
Environment
azure-pipelines-task-lib version: 2.9.3
Issue Description
When mocking getPlatform you can’t set a response that indicates Windows.
The mock answers handler returns null if the value of the mock answer isn’t truthy. The Platform enum has Windows as the first entry which means the value of Platform.Windows is zero.
Here’s the rendered JS:
/** Platforms supported by our build agent */
var Platform;
(function (Platform) {
Platform[Platform["Windows"] = 0] = "Windows";
Platform[Platform["MacOS"] = 1] = "MacOS";
Platform[Platform["Linux"] = 2] = "Linux";
})(Platform = exports.Platform || (exports.Platform = {}));
What that means is if you have answers defined:
const answers: ma.TaskLibAnswers = {
'getPlatform': {
'getPlatform': tl.Platform.Windows,
},
};
runner.setAnswers(answers);
Then you’ll see something like this in your debug output:
##vso[task.debug]looking up mock answers for "getPlatform", key '"getPlatform"'
##vso[task.debug]mock response not found
This becomes troublesome for code like this:
export function setConsoleCodePage(): void {
if (tl.getPlatform() === tl.Platform.Windows) {
tl.execSync(path.resolve(process.env.windir as string, 'system32', 'chcp.com'), ['65001']);
}
}
You can work around this by being slightly less explicit:
export function setConsoleCodePage(): void {
if (!tl.getPlatform()) {
tl.execSync(path.resolve(process.env.windir as string, 'system32', 'chcp.com'), ['65001']);
}
}
but it’d be nice if the mocking didn’t drop non-truthy answers.
Hypothetically, this may also drop other non-truthy answers, too but I see things like the checkPath mock, where you’d potentially want to say false to simulate a missing file, handle the falsy values directly.
Expected behaviour
I expect the mock for getPlatform to return 0 (Windows) when configured to mock that value.
Actual behaviour
The mock returns null and indicates no mock is set up for getPlatform.
Steps to reproduce
- Create a simple task that just does
tl.debug(`Platform: ${tl.getPlatform()}`); - Set up a test that tries to mock the
getPlatformcall. Set the value to0to indicate Windows. - Run the test with the
TASK_TEST_TRACEenvironment variable set. - Inspect the output and notice the value is
nullrather than0. Also notice the mock logging indicates there’s no answer rather than an answer indicating0.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 18 (3 by maintainers)
Verified, this fixes it. Thanks!
The change has been published to npm. Please check when you can. I’ll close ticket for now, please feel free to reopen it if something is wrong or ping me at my email v-ikuleshov@microsoft.com