Skip to content

[bug fix] Fix unavailable public variables when creating tests #717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 224 commits into
base: master
Choose a base branch
from

Conversation

bartier
Copy link

@bartier bartier commented Feb 18, 2021

Closes #716

Currently, it's not possible to create public mock variables using the function setVariableName('key', 'value', false) from a TaskMockRunner. This PR attempts to fix this, so it handles the public task variables when VariableInfo[] should be returned by the function getVariables().

Example:

Consider this test idea:

let tmr: tmrm.TaskMockRunner = new tmrm.TaskMockRunner(taskPath);
tmr.setVariableName('FOO', 'BAR', false); // this FOO variable isn't available when we call  getVariables() in the task
tmr.setVariableName('EGG', 'BAR', true);
tmr.run();

If our task needs to access a public variable using getVariables, it only gets the secrets (in this example only EGG).

import tl = require("azure-pipelines-task-lib/task")

async function run() {
  let vars = tl.getVariables()

  console.log(tl.getVariables()) // FOO will not be available here, only EGG will be available
}

run()

I added a test to validate this case, so I hope this PR could be merged as soon as possible =)

TingluoHuang and others added 30 commits August 29, 2017 10:39
…ent-version

Add TFS 2017 Update 3 agent version.
Deep Dive on Building Custom Build or Deploy Tasks
* Add task.json schema file

* Allow any "connectedService:" type of input

* Added the "id" field
Properly escape property value to handle non string types
* Error on multiline secret

* v2.3.0
@ghost
Copy link

ghost commented Feb 18, 2021

CLA assistant check
All CLA requirements met.

@bartier
Copy link
Author

bartier commented Feb 22, 2021

@egor-bryzgalov Could you please have a look at this? I'm not sure if this PR contains the required stuff to be merged :D

@egor-bryzgalov egor-bryzgalov requested review from a team and damccorm February 24, 2021 08:17
@bartier
Copy link
Author

bartier commented Mar 8, 2021

@damccorm If there is anything I can clarify in the review process I am willing to help =)

@bartier bartier requested a review from anatolybolshakov June 1, 2021 15:20
@@ -243,7 +243,11 @@ export function _getVariable(name: string): string | undefined {
}
else {
// get the public value
varval = process.env[key];
if (_startsWith(key, 'VSTS_TASKVARIABLE_')) {
varval = _knownVariableMap[key].value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add null check here - so undefined value will be returned for this case, but not an exception being thrown? (the same as for process.env[key])

@anatolybolshakov
Copy link
Contributor

@bartier I'm still not confident about necessity of these changes - why we can't just use TaskMockRunner.setAnswers method to provide mock for GetVariables task-lib method?

@bartier
Copy link
Author

bartier commented Jun 17, 2021

@anatolybolshakov Could you please give an example of using setAnswers to achieve that? I'm not sure what field of TaskLibAnswers it would be used.

@anatolybolshakov
Copy link
Contributor

@bartier oh I see - mock-task does not support mocking for getVariables method at the moment and just uses original method.
Probably we could extend mock-task - to be able to set up variables - let us take a look at it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How can I mock a pipeline variable in my tests?