-
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
Users/madhurig/rubytestswithrunner #7369
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
@@ -1,163 +1,75 @@ | ||
import * as assert from 'assert'; | ||
import { EOL } from 'os'; | ||
import * as path from 'path'; | ||
|
||
import * as mockery from 'mockery'; | ||
import * as mockTask from 'vsts-task-lib/mock-task'; | ||
import * as useRubyVersion from '../userubyversion'; | ||
|
||
/** Reload the unit under test to use mocks that have been registered. */ | ||
function reload(): typeof useRubyVersion { | ||
return require('../userubyversion'); | ||
} | ||
import * as ttm from 'vsts-task-lib/mock-test'; | ||
|
||
describe('UseRubyVersion L0 Suite', function () { | ||
this.timeout(parseInt(process.env.TASK_TEST_TIMEOUT) || 2000); | ||
|
||
before(function () { | ||
mockery.enable({ | ||
useCleanCache: true, | ||
warnOnUnregistered: false | ||
}); | ||
this.timeout(parseInt(process.env.TASK_TEST_TIMEOUT) || 20000); | ||
before(() => { | ||
}); | ||
|
||
after(function () { | ||
mockery.disable(); | ||
after(() => { | ||
}); | ||
|
||
afterEach(function () { | ||
mockery.deregisterAll(); | ||
mockery.resetCache(); | ||
}); | ||
it('finds version in cache in Linux', function (done: MochaDone) { | ||
this.timeout(1000); | ||
|
||
let tp: string = path.join(__dirname, 'L0FindVersionInLinuxCache.js'); | ||
let tr: ttm.MockTestRunner = new ttm.MockTestRunner(tp); | ||
|
||
tr.run(); | ||
|
||
it('finds version in cache in Linux', async function () { | ||
const buildVariables: { [key: string]: string } = {}; | ||
const mockTaskRun = { | ||
setVariable: (variable: string, value: string) => { | ||
buildVariables[variable] = value; | ||
}, | ||
getVariable: (variable: string) => buildVariables[variable] | ||
}; | ||
mockery.registerMock('vsts-task-lib/task', Object.assign({}, mockTask, mockTaskRun)); | ||
mockery.registerMock('fs', { | ||
symlinkSync: () => {}, | ||
unlinkSync: () => {}, | ||
existsSync: () => {} | ||
}); | ||
|
||
let mockPath = ''; | ||
const toolPath = path.join('/', 'Ruby', '2.5.4'); | ||
mockery.registerMock('vsts-task-tool-lib/tool', { | ||
findLocalTool: () => toolPath, | ||
prependPath: (s: string) => { | ||
mockPath = s + ':' + mockPath; | ||
} | ||
}); | ||
const uut = reload(); | ||
const parameters = { | ||
versionSpec: '= 2.5', | ||
addToPath: false | ||
}; | ||
|
||
assert.strictEqual(buildVariables['rubyLocation'], undefined); | ||
|
||
await uut.useRubyVersion(parameters, uut.Platform.Linux); | ||
assert.strictEqual(buildVariables['rubyLocation'], path.join(toolPath, 'bin')); | ||
assert(tr.succeeded, 'task should have succeeded'); | ||
assert(tr.stdout.includes("task.setvariable variable=rubyLocation"), 'variable was not set as expected'); | ||
assert(tr.stdout.includes(path.join('/', 'Ruby', '2.5.4', 'bin')), 'ruby location is not set as expected'); | ||
|
||
done(); | ||
}); | ||
|
||
it('rejects version not in cache', async function (done: MochaDone) { | ||
mockery.registerMock('vsts-task-lib/task', mockTask); | ||
mockery.registerMock('vsts-task-tool-lib/tool', { | ||
findLocalTool: () => null, | ||
findLocalToolVersions: () => ['2.7.13'] | ||
}); | ||
|
||
const uut = reload(); | ||
const parameters = { | ||
versionSpec: '3.x', | ||
addToPath: false | ||
}; | ||
|
||
try { | ||
await uut.useRubyVersion(parameters, uut.Platform.Linux); | ||
done(new Error('should not have succeeded')); | ||
} catch (e) { | ||
const expectedMessage = [ | ||
'loc_mock_VersionNotFound 3.x', | ||
'loc_mock_ListAvailableVersions', | ||
'2.7.13' | ||
].join(EOL); | ||
|
||
assert.strictEqual(e.message, expectedMessage); | ||
done(); | ||
} | ||
|
||
it('rejects version not in cache', function (done: MochaDone) { | ||
this.timeout(1000); | ||
|
||
let tp: string = path.join(__dirname, 'L0RejectVersionNotInCache.js'); | ||
let tr: ttm.MockTestRunner = new ttm.MockTestRunner(tp); | ||
|
||
tr.run(); | ||
|
||
assert(tr.failed, 'task should have failed'); | ||
assert(tr.stdout.includes('loc_mock_VersionNotFound 3.x'), 'error message not as expected'); | ||
assert(tr.stdout.includes('loc_mock_ListAvailableVersions'), 'list of available versions is not printed as expected'); | ||
assert(tr.stdout.includes('2.7.13'), 'list of available versions is not printed as expected'); | ||
|
||
done(); | ||
}); | ||
|
||
it('sets PATH correctly on Linux', async function () { | ||
const buildVariables: { [key: string]: string } = { 'PATH': '' }; | ||
const mockTaskRun = { | ||
setVariable: (variable: string, value: string) => { | ||
buildVariables[variable] = value; | ||
}, | ||
getVariable: (variable: string) => buildVariables[variable] | ||
}; | ||
mockery.registerMock('vsts-task-lib/task', Object.assign({}, mockTask, mockTaskRun)); | ||
mockery.registerMock('fs', { | ||
symlinkSync: () => {}, | ||
unlinkSync: () => {}, | ||
existsSync: () => {} | ||
}); | ||
|
||
let mockPath = ''; | ||
const toolPath = path.join('/', 'Ruby', '2.4.4'); | ||
mockery.registerMock('vsts-task-tool-lib/tool', { | ||
findLocalTool: () => toolPath, | ||
prependPath: (s: string) => { | ||
mockPath = s + ':' + mockPath; | ||
} | ||
}); | ||
|
||
const uut = reload(); | ||
const parameters = { | ||
versionSpec: '2.4', | ||
addToPath: true | ||
}; | ||
|
||
await uut.useRubyVersion(parameters, uut.Platform.Linux); | ||
assert.strictEqual(`${path.join(toolPath, 'bin')}:`, mockPath); | ||
it('sets PATH correctly on Linux', function (done: MochaDone) { | ||
this.timeout(1000); | ||
|
||
let tp: string = path.join(__dirname, 'L0SetPathOnLinux.js'); | ||
let tr: ttm.MockTestRunner = new ttm.MockTestRunner(tp); | ||
|
||
tr.run(); | ||
|
||
assert(tr.succeeded, 'task should have succeeded'); | ||
assert(tr.stdout.includes("task.setvariable variable=rubyLocation"), 'variable was not set as expected'); | ||
assert(tr.stdout.includes(path.join('/', 'Ruby', '2.4.4', 'bin')), 'ruby location is not set as expected'); | ||
assert(tr.stdout.includes('##vso[task.prependpath]' + path.join('/', 'Ruby', '2.4.4', 'bin')), 'ruby tool location was not added to PATH as expected'); | ||
done(); | ||
}); | ||
|
||
it('sets PATH correctly on Windows', async function () { | ||
const buildVariables: { [key: string]: string } = { 'PATH': '' }; | ||
const mockTaskRun = { | ||
setVariable: (variable: string, value: string) => { | ||
buildVariables[variable] = value; | ||
}, | ||
getVariable: (variable: string) => buildVariables[variable] | ||
}; | ||
mockery.registerMock('vsts-task-lib/task', Object.assign({}, mockTask, mockTaskRun)); | ||
mockery.registerMock('fs', { | ||
symlinkSync: () => {}, | ||
unlinkSync: () => {}, | ||
existsSync: () => {} | ||
}); | ||
|
||
let mockPath = ''; | ||
const toolPath = path.join('/', 'Ruby', '2.4.4'); | ||
mockery.registerMock('vsts-task-tool-lib/tool', { | ||
findLocalTool: () => toolPath, | ||
prependPath: (s: string) => { | ||
mockPath = s + ';' + mockPath; | ||
} | ||
}); | ||
|
||
const uut = reload(); | ||
const parameters = { | ||
versionSpec: '2.4', | ||
addToPath: true | ||
}; | ||
|
||
await uut.useRubyVersion(parameters, uut.Platform.Windows); | ||
assert.strictEqual(`${path.join(toolPath, 'bin')};`, mockPath); | ||
it('sets PATH correctly on Windows', function (done: MochaDone) { | ||
this.timeout(1000); | ||
|
||
let tp: string = path.join(__dirname, 'L0SetPathOnWindows.js'); | ||
let tr: ttm.MockTestRunner = new ttm.MockTestRunner(tp); | ||
|
||
tr.run(); | ||
|
||
assert(tr.succeeded, 'task should have succeeded'); | ||
assert(tr.stdout.includes("task.setvariable variable=rubyLocation"), 'variable was not set as expected'); | ||
assert(tr.stdout.includes(path.join('/', 'Ruby', '2.4.4', 'bin')), 'ruby location is not set as expected'); | ||
assert(tr.stdout.includes('##vso[task.prependpath]' + path.join('/', 'Ruby', '2.4.4', 'bin')), 'ruby tool location was not added to PATH as expected'); | ||
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,31 @@ | ||
import * as fs from 'fs'; | ||
import * as os from 'os'; | ||
import * as path from 'path'; | ||
import * as tmrm from 'vsts-task-lib/mock-run'; | ||
|
||
let taskPath = path.join(__dirname, '..', 'main.js'); | ||
let tr: tmrm.TaskMockRunner = new tmrm.TaskMockRunner(taskPath); | ||
|
||
tr.setInput('versionSpec', '2.5'); | ||
tr.setInput('addToPath', 'false'); | ||
|
||
tr.registerMock('vsts-task-tool-lib/tool', { | ||
findLocalTool: () => path.join('/', 'Ruby', '2.5.4') | ||
}); | ||
|
||
tr.registerMock('fs', { | ||
symlinkSync: () => { }, | ||
unlinkSync: () => { }, | ||
existsSync: () => { return true; }, | ||
statSync: fs.statSync, | ||
writeFileSync: fs.writeFileSync, | ||
readFileSync: fs.readFileSync | ||
}); | ||
|
||
tr.registerMock('os', { | ||
type: () => { return 'linux'; }, | ||
EOL: os.EOL | ||
}); | ||
|
||
tr.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,32 @@ | ||
import * as fs from 'fs'; | ||
import * as os from 'os'; | ||
import * as path from 'path'; | ||
import * as tmrm from 'vsts-task-lib/mock-run'; | ||
|
||
let taskPath = path.join(__dirname, '..', 'main.js'); | ||
let tr: tmrm.TaskMockRunner = new tmrm.TaskMockRunner(taskPath); | ||
|
||
tr.setInput('versionSpec', '3.x'); | ||
tr.setInput('addToPath', 'false'); | ||
|
||
tr.registerMock('vsts-task-tool-lib/tool', { | ||
findLocalTool: () => null, | ||
findLocalToolVersions: () => ['2.7.13'] | ||
}); | ||
|
||
tr.registerMock('fs', { | ||
symlinkSync: () => { }, | ||
unlinkSync: () => { }, | ||
existsSync: () => { return false; }, | ||
statSync: fs.statSync, | ||
writeFileSync: fs.writeFileSync, | ||
readFileSync: fs.readFileSync | ||
}); | ||
|
||
tr.registerMock('os', { | ||
type: () => { return 'linux'; }, | ||
EOL: os.EOL | ||
}); | ||
|
||
tr.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,34 @@ | ||
import * as fs from 'fs'; | ||
import * as os from 'os'; | ||
import * as path from 'path'; | ||
import * as tmrm from 'vsts-task-lib/mock-run'; | ||
|
||
let taskPath = path.join(__dirname, '..', 'main.js'); | ||
let tr: tmrm.TaskMockRunner = new tmrm.TaskMockRunner(taskPath); | ||
|
||
tr.setInput('versionSpec', '2.4'); | ||
tr.setInput('addToPath', 'true'); | ||
|
||
tr.registerMock('vsts-task-tool-lib/tool', { | ||
findLocalTool: () => path.join('/', 'Ruby', '2.4.4'), | ||
prependPath: (s: string) => { | ||
console.log('##vso[task.prependpath]' + s); | ||
} | ||
}); | ||
|
||
tr.registerMock('fs', { | ||
symlinkSync: () => { }, | ||
unlinkSync: () => { }, | ||
existsSync: () => { return true; }, | ||
statSync: () => fs.statSync, | ||
writeFileSync: () => fs.writeFileSync, | ||
readFileSync: () => fs.readFileSync | ||
}); | ||
|
||
tr.registerMock('os', { | ||
type: () => { return 'linux'; }, | ||
EOL: () => os.EOL | ||
}); | ||
|
||
tr.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,35 @@ | ||
import * as fs from 'fs'; | ||
import * as os from 'os'; | ||
import * as path from 'path'; | ||
import * as tmrm from 'vsts-task-lib/mock-run'; | ||
|
||
let taskPath = path.join(__dirname, '..', 'main.js'); | ||
let tr: tmrm.TaskMockRunner = new tmrm.TaskMockRunner(taskPath); | ||
|
||
tr.setInput('versionSpec', '2.4'); | ||
tr.setInput('addToPath', 'true'); | ||
|
||
tr.registerMock('vsts-task-tool-lib/tool', { | ||
findLocalTool: () => path.join('/', 'Ruby', '2.4.4'), | ||
prependPath: (s: string) => { | ||
console.log('##vso[task.prependpath]' + s); | ||
} | ||
}); | ||
|
||
tr.registerMock('fs', { | ||
symlinkSync: () => { }, | ||
unlinkSync: () => { }, | ||
existsSync: () => { return true; }, | ||
statSync: fs.statSync, | ||
writeFileSync: fs.writeFileSync, | ||
readFileSync: fs.readFileSync | ||
}); | ||
|
||
tr.registerMock('os', { | ||
type: () => { return 'win32'; }, | ||
EOL: os.EOL | ||
}); | ||
|
||
|
||
tr.run(); | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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.
I think statSync, writeFileSync, readFileSync must not be used. The mocks here wouldn't work as expected if called -- they're functions that are returning a function object :)
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.
They need to be mocked, but should fall through.