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

Users/madhurig/rubytestswithrunner #7369

Merged
merged 4 commits into from
Jun 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
202 changes: 57 additions & 145 deletions Tasks/UseRubyVersionV0/Tests/L0.ts
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();
});
});
31 changes: 31 additions & 0 deletions Tasks/UseRubyVersionV0/Tests/L0FindVersionInLinuxCache.ts
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();

32 changes: 32 additions & 0 deletions Tasks/UseRubyVersionV0/Tests/L0RejectVersionNotInCache.ts
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();

34 changes: 34 additions & 0 deletions Tasks/UseRubyVersionV0/Tests/L0SetPathOnLinux.ts
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,
Copy link
Contributor

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 :)

Copy link
Contributor Author

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.

writeFileSync: () => fs.writeFileSync,
readFileSync: () => fs.readFileSync
});

tr.registerMock('os', {
type: () => { return 'linux'; },
EOL: () => os.EOL
});

tr.run();

35 changes: 35 additions & 0 deletions Tasks/UseRubyVersionV0/Tests/L0SetPathOnWindows.ts
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();

12 changes: 0 additions & 12 deletions Tasks/UseRubyVersionV0/Tests/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions Tasks/UseRubyVersionV0/Tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
},
"homepage": "https://github.com/microsoft/vsts-tasks#readme",
"devDependencies": {
"@types/mocha": "^2.2.48",
"@types/mockery": "^1.4.29",
"mockery": "^2.1.0"
"@types/mocha": "^2.2.48"
}
}