Skip to content

Commit

Permalink
Move some tests to utils test file, re add other functions that might…
Browse files Browse the repository at this point in the history
… have been accidentally deleted, fix some errors with fsExtra
  • Loading branch information
MilapNaik committed Feb 29, 2024
1 parent c058dd6 commit 5859f91
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 128 deletions.
168 changes: 41 additions & 127 deletions src/RokuDeploy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,12 +631,12 @@ describe('index', () => {

it('computes absolute path for all operations', async () => {
const ensureDirPaths = [];
sinon.stub(rokuDeploy.fsExtra, 'ensureDir').callsFake((p) => {
sinon.stub(fsExtra, 'ensureDir').callsFake((p) => {
ensureDirPaths.push(p);
return Promise.resolve;
});
const copyPaths = [] as Array<{ src: string; dest: string }>;
sinon.stub(rokuDeploy.fsExtra as any, 'copy').callsFake((src, dest) => {
sinon.stub(fsExtra as any, 'copy').callsFake((src, dest) => {
copyPaths.push({ src: src as string, dest: dest as string });
return Promise.resolve();
});
Expand Down Expand Up @@ -828,10 +828,10 @@ describe('index', () => {
});

it('failure to close read stream does not crash', async () => {
const orig = rokuDeploy.fsExtra.createReadStream;
const orig = fsExtra.createReadStream;
//wrap the stream.close call so we can throw
sinon.stub(rokuDeploy.fsExtra, 'createReadStream').callsFake((pathLike) => {
const stream = orig.call(rokuDeploy.fsExtra, pathLike);
sinon.stub(fsExtra, 'createReadStream').callsFake((pathLike) => {
const stream = orig.call(fsExtra, pathLike);
const originalClose = stream.close;
stream.close = () => {
originalClose.call(stream);
Expand Down Expand Up @@ -1810,17 +1810,17 @@ describe('index', () => {
});
});
it('is resilient to file system errors', async () => {
let copy = rokuDeploy.fsExtra.copy;
let copy = fsExtra.copy;
let count = 0;

//mock writeFile so we can throw a few errors during the test
sinon.stub(rokuDeploy.fsExtra, 'copy').callsFake((...args) => {
sinon.stub(fsExtra, 'copy').callsFake((...args) => {
count += 1;
//fail a few times
if (count < 5) {
throw new Error('fake error thrown as part of the unit test');
} else {
return copy.apply(rokuDeploy.fsExtra, args);
return copy.apply(fsExtra, args);
}
});

Expand All @@ -1844,17 +1844,17 @@ describe('index', () => {
});

it('throws underlying error after the max fs error threshold is reached', async () => {
let copy = rokuDeploy.fsExtra.copy;
let copy = fsExtra.copy;
let count = 0;

//mock writeFile so we can throw a few errors during the test
sinon.stub(rokuDeploy.fsExtra, 'copy').callsFake((...args) => {
sinon.stub(fsExtra, 'copy').callsFake((...args) => {
count += 1;
//fail a few times
if (count < 15) {
throw new Error('fake error thrown as part of the unit test');
} else {
return copy.apply(rokuDeploy.fsExtra, args);
return copy.apply(fsExtra, args);
}
});

Expand Down Expand Up @@ -3127,125 +3127,10 @@ describe('index', () => {
});
});

describe('getDestPath', () => {
it('handles unrelated exclusions properly', () => {
expect(
rokuDeploy.getDestPath(
s`${rootDir}/components/comp1/comp1.brs`,
[
'**/*',
'!exclude.me'
],
rootDir
)
).to.equal(s`components/comp1/comp1.brs`);
});

it('finds dest path for top-level path', () => {
expect(
rokuDeploy.getDestPath(
s`${rootDir}/components/comp1/comp1.brs`,
['components/**/*'],
rootDir
)
).to.equal(s`components/comp1/comp1.brs`);
});

it('does not find dest path for non-matched top-level path', () => {
expect(
rokuDeploy.getDestPath(
s`${rootDir}/source/main.brs`,
['components/**/*'],
rootDir
)
).to.be.undefined;
});

it('excludes a file that is negated', () => {
expect(
rokuDeploy.getDestPath(
s`${rootDir}/source/main.brs`,
[
'source/**/*',
'!source/main.brs'
],
rootDir
)
).to.be.undefined;
});

it('excludes file from non-rootdir top-level pattern', () => {
expect(
rokuDeploy.getDestPath(
s`${rootDir}/../externalDir/source/main.brs`,
[
'!../externalDir/**/*'
],
rootDir
)
).to.be.undefined;
});

it('excludes a file that is negated in src;dest;', () => {
expect(
rokuDeploy.getDestPath(
s`${rootDir}/source/main.brs`,
[
'source/**/*',
{
src: '!source/main.brs'
}
],
rootDir
)
).to.be.undefined;
});

it('works for brighterscript files', () => {
let destPath = rokuDeploy.getDestPath(
util.standardizePath(`${cwd}/src/source/main.bs`),
[
'manifest',
'source/**/*.bs'
],
s`${cwd}/src`
);
expect(s`${destPath}`).to.equal(s`source/main.bs`);
});

it('excludes a file found outside the root dir', () => {
expect(
rokuDeploy.getDestPath(
s`${rootDir}/../source/main.brs`,
[
'../source/**/*'
],
rootDir
)
).to.be.undefined;
});
});

describe('normalizeRootDir', () => {
it('handles falsey values', () => {
expect(rokuDeploy.normalizeRootDir(null)).to.equal(cwd);
expect(rokuDeploy.normalizeRootDir(undefined)).to.equal(cwd);
expect(rokuDeploy.normalizeRootDir('')).to.equal(cwd);
expect(rokuDeploy.normalizeRootDir(' ')).to.equal(cwd);
expect(rokuDeploy.normalizeRootDir('\t')).to.equal(cwd);
});

it('handles non-falsey values', () => {
expect(rokuDeploy.normalizeRootDir(cwd)).to.equal(cwd);
expect(rokuDeploy.normalizeRootDir('./')).to.equal(cwd);
expect(rokuDeploy.normalizeRootDir('./testProject')).to.equal(path.join(cwd, 'testProject'));
});
});

describe('retrieveSignedPackage', () => {
let onHandler: any;
beforeEach(() => {
sinon.stub(rokuDeploy.fsExtra, 'ensureDir').callsFake(((pth: string, callback: (err: Error) => void) => {
sinon.stub(fsExtra, 'ensureDir').callsFake(((pth: string, callback: (err: Error) => void) => {
//do nothing, assume the dir gets created
}) as any);

Expand Down Expand Up @@ -3597,6 +3482,35 @@ describe('index', () => {
expect(getToFileIsResolved).to.be.true;
});
});

function mockDoGetRequest(body = '', statusCode = 200) {
return sinon.stub(rokuDeploy as any, 'doGetRequest').callsFake((params) => {
let results = { response: { statusCode: statusCode }, body: body };
rokuDeploy['checkRequest'](results);
return Promise.resolve(results);
});
}

function mockDoPostRequest(body = '', statusCode = 200) {
return sinon.stub(rokuDeploy as any, 'doPostRequest').callsFake((params) => {
let results = { response: { statusCode: statusCode }, body: body };
rokuDeploy['checkRequest'](results);
return Promise.resolve(results);
});
}

async function assertThrowsAsync(fn) {
let f = () => { };
try {
await fn();
} catch (e) {
f = () => {
throw e;
};
} finally {
assert.throws(f);
}
}
});

function getFakeResponseBody(messages: string): string {
Expand Down
117 changes: 116 additions & 1 deletion src/util.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { util, standardizePath as s } from './util';
import { expect } from 'chai';
import * as fsExtra from 'fs-extra';
import { tempDir, rootDir } from './testUtils.spec';
import { cwd, tempDir, rootDir } from './testUtils.spec';
import * as path from 'path';
import * as dns from 'dns';
import { createSandbox } from 'sinon';
Expand Down Expand Up @@ -336,6 +336,121 @@ describe('util', () => {
});
});

describe('normalizeRootDir', () => {
it('handles falsey values', () => {
expect(util.normalizeRootDir(null)).to.equal(cwd);
expect(util.normalizeRootDir(undefined)).to.equal(cwd);
expect(util.normalizeRootDir('')).to.equal(cwd);
expect(util.normalizeRootDir(' ')).to.equal(cwd);
expect(util.normalizeRootDir('\t')).to.equal(cwd);
});

it('handles non-falsey values', () => {
expect(util.normalizeRootDir(cwd)).to.equal(cwd);
expect(util.normalizeRootDir('./')).to.equal(cwd);
expect(util.normalizeRootDir('./testProject')).to.equal(path.join(cwd, 'testProject'));
});
});

describe('getDestPath', () => {
it('handles unrelated exclusions properly', () => {
expect(
util.getDestPath(
s`${rootDir}/components/comp1/comp1.brs`,
[
'**/*',
'!exclude.me'
],
rootDir
)
).to.equal(s`components/comp1/comp1.brs`);
});

it('finds dest path for top-level path', () => {
expect(
util.getDestPath(
s`${rootDir}/components/comp1/comp1.brs`,
['components/**/*'],
rootDir
)
).to.equal(s`components/comp1/comp1.brs`);
});

it('does not find dest path for non-matched top-level path', () => {
expect(
util.getDestPath(
s`${rootDir}/source/main.brs`,
['components/**/*'],
rootDir
)
).to.be.undefined;
});

it('excludes a file that is negated', () => {
expect(
util.getDestPath(
s`${rootDir}/source/main.brs`,
[
'source/**/*',
'!source/main.brs'
],
rootDir
)
).to.be.undefined;
});

it('excludes file from non-rootdir top-level pattern', () => {
expect(
util.getDestPath(
s`${rootDir}/../externalDir/source/main.brs`,
[
'!../externalDir/**/*'
],
rootDir
)
).to.be.undefined;
});

it('excludes a file that is negated in src;dest;', () => {
expect(
util.getDestPath(
s`${rootDir}/source/main.brs`,
[
'source/**/*',
{
src: '!source/main.brs'
}
],
rootDir
)
).to.be.undefined;
});

it('works for brighterscript files', () => {
let destPath = util.getDestPath(
util.standardizePath(`${cwd}/src/source/main.bs`),
[
'manifest',
'source/**/*.bs'
],
s`${cwd}/src`
);
expect(s`${destPath}`).to.equal(s`source/main.bs`);
});

it('excludes a file found outside the root dir', () => {
expect(
util.getDestPath(
s`${rootDir}/../source/main.brs`,
[
'../source/**/*'
],
rootDir
)
).to.be.undefined;
});
});

describe('getOptionsFromJson', () => {
beforeEach(() => {
fsExtra.ensureDirSync(rootDir);
Expand Down

0 comments on commit 5859f91

Please sign in to comment.