Skip to content
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

Add powershell tests #11606

Merged
merged 9 commits into from
Oct 22, 2019
Merged
69 changes: 69 additions & 0 deletions Tasks/PowerShellV2/Tests/L0.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import assert = require('assert');
import os = require('os');
import path = require('path');
import * as ttm from 'vsts-task-lib/mock-test';
Copy link
Contributor

Choose a reason for hiding this comment

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

at some point we should upgrade to azure-pipelines-task-lib


describe('PowerShell Suite', function () {
this.timeout(60000);

function runValidations(validator: () => void, tr, done) {
try {
validator();
done();
}
catch (error) {
console.log("STDERR", tr.stderr);
console.log("STDOUT", tr.stdout);
done(error);
}
}

it('Runs an inline script correctly', (done: MochaDone) => {
this.timeout(5000);

let tp: string = path.join(__dirname, 'L0Inline.js');
let tr: ttm.MockTestRunner = new ttm.MockTestRunner(tp);

tr.run();

runValidations(() => {
assert(tr.succeeded, 'PowerShell should have succeeded.');
assert(tr.stderr.length === 0, 'PowerShell should not have written to stderr');
assert(tr.stdout.indexOf(`Writing \ufeff$ErrorActionPreference = 'Stop'${os.EOL}Write-Host "my script output" to temp/path/fileName.ps1`) > 0, 'PowerShell should have written the script to a file');
assert(tr.stdout.indexOf('my script output') > 0,'PowerShell should have correctly run the script');
}, tr, done);
});

it('Runs a checked in script correctly', (done: MochaDone) => {
this.timeout(5000);

let tp: string = path.join(__dirname, 'L0External.js');
let tr: ttm.MockTestRunner = new ttm.MockTestRunner(tp);

tr.run();

runValidations(() => {
assert(tr.succeeded, 'PowerShell should have succeeded.');
assert(tr.stderr.length === 0, 'PowerShell should not have written to stderr');
assert(tr.stdout.indexOf(`Writing \ufeff$ErrorActionPreference = 'Stop'${os.EOL}. 'path/to/script.ps1' to temp/path/fileName.ps1`) > 0, 'PowerShell should have written the script to a file');
assert(tr.stdout.indexOf('my script output') > 0,'PowerShell should have correctly run the script');
}, tr, done);
});

it('Adds arguments to the script', (done: MochaDone) => {
this.timeout(5000);

let tp: string = path.join(__dirname, 'L0Args.js');
let tr: ttm.MockTestRunner = new ttm.MockTestRunner(tp);

tr.run();

runValidations(() => {
assert(tr.succeeded, 'PowerShell should have succeeded.');
assert(tr.stderr.length === 0, 'PowerShell should not have written to stderr');
assert(tr.stdout.indexOf(`Writing \ufeff$ErrorActionPreference = 'Stop'${os.EOL}. 'path/to/script.ps1' myCustomArg to temp/path/fileName.ps1`) > 0, 'PowerShell should have written the script to a file');
assert(tr.stdout.indexOf('my script output') > 0,'PowerShell should have correctly run the script');
}, tr, done);
});

});
79 changes: 79 additions & 0 deletions Tasks/PowerShellV2/Tests/L0Args.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import ma = require('vsts-task-lib/mock-answer');
import tmrm = require('vsts-task-lib/mock-run');
import path = require('path');

let taskPath = path.join(__dirname, '..', 'powershell.js');
let tmr: tmrm.TaskMockRunner = new tmrm.TaskMockRunner(taskPath);

tmr.setInput('targetType', 'filepath');
tmr.setInput('filePath', 'path/to/script.ps1');
tmr.setInput('workingDirectory', '/fakecwd');
tmr.setInput('ignoreLASTEXITCODE', 'true');
tmr.setInput('arguments', 'myCustomArg');

//Create assertAgent and getVariable mocks, support not added in this version of task-lib
const tl = require('vsts-task-lib/mock-task');
const tlClone = Object.assign({}, tl);
tlClone.getVariable = function(variable: string) {
if (variable.toLowerCase() == 'agent.tempdirectory') {
return 'temp/path';
}
return null;
};
tlClone.assertAgent = function(variable: string) {
return;
};
tmr.registerMock('vsts-task-lib/mock-task', tlClone);

// Mock task-lib
let a: ma.TaskLibAnswers = <ma.TaskLibAnswers>{
'checkPath' : {
'/fakecwd' : true,
'path/to/powershell': true,
'temp/path': true
},
'which': {
'powershell': 'path/to/powershell'
},
'assertAgent': {
'2.115.0': true
},
'exec': {
'path/to/powershell --noprofile --norc -c pwd': {
"code": 0,
"stdout": "temp/path"
},
"path/to/powershell -NoLogo -NoProfile -NonInteractive -Command . 'temp\\path\\fileName.ps1'": {
"code": 0,
"stdout": "my script output"
},
"path/to/powershell -NoLogo -NoProfile -NonInteractive -Command . 'temp/path/fileName.ps1'": {
"code": 0,
"stdout": "my script output"
}
},
'stats': {
'path/to/script.ps1': {
isFile() {
return true;
}
}
}
};
tmr.setAnswers(a);

// Mock fs
const fs = require('fs');
const fsClone = Object.assign({}, fs);
fsClone.writeFile = function(filePath, contents, options) {
// Normalize to linux paths for logs we check
console.log(`Writing ${contents} to ${filePath.replace(/\\/g, '/')}`);
}
tmr.registerMock('fs', fsClone);

// Mock uuidv4
tmr.registerMock('uuid/v4', function () {
return 'fileName';
});

tmr.run();
78 changes: 78 additions & 0 deletions Tasks/PowerShellV2/Tests/L0External.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import ma = require('vsts-task-lib/mock-answer');
import tmrm = require('vsts-task-lib/mock-run');
import path = require('path');

let taskPath = path.join(__dirname, '..', 'powershell.js');
let tmr: tmrm.TaskMockRunner = new tmrm.TaskMockRunner(taskPath);

tmr.setInput('targetType', 'filepath');
tmr.setInput('filePath', 'path/to/script.ps1');
tmr.setInput('workingDirectory', '/fakecwd');
tmr.setInput('ignoreLASTEXITCODE', 'true');

//Create assertAgent and getVariable mocks, support not added in this version of task-lib
const tl = require('vsts-task-lib/mock-task');
const tlClone = Object.assign({}, tl);
tlClone.getVariable = function(variable: string) {
if (variable.toLowerCase() == 'agent.tempdirectory') {
return 'temp/path';
}
return null;
};
tlClone.assertAgent = function(variable: string) {
return;
};
tmr.registerMock('vsts-task-lib/mock-task', tlClone);

// Mock task-lib
let a: ma.TaskLibAnswers = <ma.TaskLibAnswers>{
'checkPath' : {
'/fakecwd' : true,
'path/to/powershell': true,
'temp/path': true
},
'which': {
'powershell': 'path/to/powershell'
},
'assertAgent': {
'2.115.0': true
},
'exec': {
'path/to/powershell --noprofile --norc -c pwd': {
"code": 0,
"stdout": "temp/path"
},
"path/to/powershell -NoLogo -NoProfile -NonInteractive -Command . 'temp\\path\\fileName.ps1'": {
"code": 0,
"stdout": "my script output"
},
"path/to/powershell -NoLogo -NoProfile -NonInteractive -Command . 'temp/path/fileName.ps1'": {
"code": 0,
"stdout": "my script output"
}
},
'stats': {
'path/to/script.ps1': {
isFile() {
return true;
}
}
}
};
tmr.setAnswers(a);

// Mock fs
const fs = require('fs');
const fsClone = Object.assign({}, fs);
fsClone.writeFile = function(filePath, contents, options) {
// Normalize to linux paths for logs we check
console.log(`Writing ${contents} to ${filePath.replace(/\\/g, '/')}`);
}
tmr.registerMock('fs', fsClone);

// Mock uuidv4
tmr.registerMock('uuid/v4', function () {
return 'fileName';
});

tmr.run();
78 changes: 78 additions & 0 deletions Tasks/PowerShellV2/Tests/L0Inline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import ma = require('vsts-task-lib/mock-answer');
import tmrm = require('vsts-task-lib/mock-run');
import path = require('path');

let taskPath = path.join(__dirname, '..', 'powershell.js');
let tmr: tmrm.TaskMockRunner = new tmrm.TaskMockRunner(taskPath);

tmr.setInput('targetType', 'inline');
tmr.setInput('script', 'Write-Host "my script output"');
tmr.setInput('workingDirectory', '/fakecwd');
tmr.setInput('ignoreLASTEXITCODE', 'true');

//Create assertAgent and getVariable mocks, support not added in this version of task-lib
const tl = require('vsts-task-lib/mock-task');
const tlClone = Object.assign({}, tl);
tlClone.getVariable = function(variable: string) {
if (variable.toLowerCase() == 'agent.tempdirectory') {
return 'temp/path';
}
return null;
};
tlClone.assertAgent = function(variable: string) {
return;
};
tmr.registerMock('vsts-task-lib/mock-task', tlClone);

// Mock task-lib
let a: ma.TaskLibAnswers = <ma.TaskLibAnswers>{
'checkPath' : {
'/fakecwd' : true,
'path/to/powershell': true,
'temp/path': true
},
'which': {
'powershell': 'path/to/powershell'
},
'assertAgent': {
'2.115.0': true
},
'exec': {
'path/to/powershell --noprofile --norc -c pwd': {
"code": 0,
"stdout": "temp/path"
},
"path/to/powershell -NoLogo -NoProfile -NonInteractive -Command . 'temp\\path\\fileName.ps1'": {
"code": 0,
"stdout": "my script output"
},
"path/to/powershell -NoLogo -NoProfile -NonInteractive -Command . 'temp/path/fileName.ps1'": {
"code": 0,
"stdout": "my script output"
}
},
'stats': {
'path/to/script.ps1': {
isFile() {
return true;
}
}
}
};
tmr.setAnswers(a);

// Mock fs
const fs = require('fs');
const fsClone = Object.assign({}, fs);
fsClone.writeFile = function(filePath, contents, options) {
// Normalize to linux paths for logs we check
console.log(`Writing ${contents} to ${filePath.replace(/\\/g, '/')}`);
}
tmr.registerMock('fs', fsClone);

// Mock uuidv4
tmr.registerMock('uuid/v4', function () {
return 'fileName';
});

tmr.run();