-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Add powershell tests #11606
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
77d279c
Add powershell tests
a58e402
Merge branch 'master' into users/damccorm/powershellTests
daabe0e
Fix for linux
5b93a6f
Merge branch 'users/damccorm/powershellTests' of https://github.com/m…
22877e4
Fix for linux
b1c54d0
Merge branch 'master' into users/damccorm/powershellTests
8446978
Merge branch 'master' into users/damccorm/powershellTests
acfec05
Merge branch 'master' into users/damccorm/powershellTests
ba67811
Merge branch 'master' into users/damccorm/powershellTests
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
|
||
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); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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