Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
Signed-off-by: Alana McKenzie <[email protected]>
  • Loading branch information
amckenzie132 committed Jan 25, 2025
1 parent fe62ec0 commit 12a0fa2
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ export default class SauceLabs {
}

// download and verify the Sauce Connect client
let scLoader = new SauceConnectLoader({version: sauceConnectVersion});
let scLoader = new SauceConnectLoader(sauceConnectVersion);
const isDownloaded = await scLoader.verifyAlreadyDownloaded();

if (!isDownloaded) {
Expand All @@ -317,7 +317,7 @@ export default class SauceLabs {
// downloaded version may differ from the input version, eg. if a partial version is given as input
// update scLoader if necessary
if (download.version != sauceConnectVersion) {
scLoader = new SauceConnectLoader({version: download.version});
scLoader = new SauceConnectLoader(download.version);
}
await scLoader.verifyAlreadyDownloaded({url: download.url});
}
Expand Down
8 changes: 2 additions & 6 deletions src/sauceConnectLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,9 @@ import fs from 'fs/promises';
import compressing from 'compressing';

export default class SauceConnectLoader {
constructor(options = {}) {
constructor(version) {
this.destDir = join(__dirname, 'sc-loader');
this.destSC = join(
__dirname,
'sc-loader',
`.sc-v${options.sauceConnectVersion}`
);
this.destSC = join(__dirname, 'sc-loader', `.sc-v${version}`);
let scBinary = 'sc';
if (getPlatform().startsWith('win')) {
scBinary += '.exe';
Expand Down
28 changes: 28 additions & 0 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,24 @@ describe('startSauceConnect', () => {
expect(err.message).toContain('code: 404 message: Invalid input');
});

it('should throw an error if there is an invalid response from the download API', async () => {
const logs = [];
const api = new SauceLabs({user: 'foo', key: 'bar'});
got.mockReturnValue(
Promise.resolve({
body: {}, // empty response
})
);
const err = await api
.startSauceConnect({
tunnelName: 'my-tunnel',
'proxy-tunnel': 'abc',
logger: (log) => logs.push(log),
})
.catch((err) => err);
expect(err.message).toBe('Failed to retrieve Sauce Connect download.');
});

it('should throw an error if the call to the download API failed', async () => {
const logs = [];
const api = new SauceLabs({user: 'foo', key: 'bar'});
Expand Down Expand Up @@ -626,6 +644,16 @@ describe('startSauceConnect', () => {
});
});

it('should fail with an invalid region', async () => {
const api = new SauceLabs({user: 'foo', key: 'bar', region: ''});
const res = await api
.startSauceConnect({
tunnelName: 'my-tunnel',
})
.catch((err) => err);
expect(res).toEqual(new Error(`Missing region`));
});

it('should fail when tunnelName is not given', async () => {
const api = new SauceLabs({user: 'foo', key: 'bar'});
const res = await api.startSauceConnect({}).catch((err) => err);
Expand Down
34 changes: 19 additions & 15 deletions tests/sauceConnectLoader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ describe('SauceConnectLoader', () => {
describe('constructor', () => {
test('should have .exe path for windows platform', () => {
jest.spyOn(utils, 'getPlatform').mockImplementation(() => 'windows');
const scl = new SauceConnectLoader({sauceConnectVersion: '1.2.3'});
const scl = new SauceConnectLoader('1.2.3');
expect(scl.path).toContain('exe');
});

test('should not have .exe path for windows platform', () => {
jest.spyOn(utils, 'getPlatform').mockImplementation(() => 'windows');
const scl = new SauceConnectLoader({sauceConnectVersion: '1.2.3'});
expect(scl.path).toContain('exe');
test('should not have .exe path for non-windows platform', () => {
jest.spyOn(utils, 'getPlatform').mockImplementation(() => 'linux');
const scl = new SauceConnectLoader('1.2.3');
expect(scl.path).not.toContain('exe');
});
});

Expand All @@ -34,9 +34,7 @@ describe('SauceConnectLoader', () => {
jest
.spyOn(fs.promises, 'stat')
.mockImplementation(() => Promise.resolve());
scl = new SauceConnectLoader({
sauceConnectVersion: '1.2.3',
});
scl = new SauceConnectLoader('1.2.3');
});

test('should not attempt to download anything', async () => {
Expand All @@ -50,9 +48,7 @@ describe('SauceConnectLoader', () => {
jest
.spyOn(fs.promises, 'stat')
.mockImplementation(() => Promise.reject({code: 'ENOENT'}));
scl = new SauceConnectLoader({
sauceConnectVersion: '1.2.3',
});
scl = new SauceConnectLoader('1.2.3');
});

test('should attempt to download', async () => {
Expand All @@ -72,9 +68,7 @@ describe('SauceConnectLoader', () => {
jest
.spyOn(fs.promises, 'stat')
.mockImplementation(() => Promise.reject(new Error()));
scl = new SauceConnectLoader({
sauceConnectVersion: '1.2.3',
});
scl = new SauceConnectLoader('1.2.3');
});

test('should reject', () => {
Expand All @@ -97,7 +91,7 @@ describe('SauceConnectLoader', () => {
jest
.spyOn(fs.promises, 'rename')
.mockImplementation(() => Promise.resolve());
scl = new SauceConnectLoader({sauceConnectVersion: '1.2.3'});
scl = new SauceConnectLoader('1.2.3');
mockHttpsGet = jest.spyOn(https, 'get');

mockCompressingLinux = jest
Expand Down Expand Up @@ -132,5 +126,15 @@ describe('SauceConnectLoader', () => {
expect(mockCompressingWinMac).toHaveBeenCalled();
});
});

describe('_download()', () => {
test('should handle download error', async () => {
const scl = new SauceConnectLoader('1.2.3');
const url = 'https://this-will-not-resolve.penguin';
``;
const err = await scl._download(url).catch((err) => err);
expect(err.message).toContain('this-will-not-resolve.penguin');
});
});
});
});

0 comments on commit 12a0fa2

Please sign in to comment.