Skip to content

Commit

Permalink
added tests for build util
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaymathur committed Apr 16, 2022
1 parent 5bf7d35 commit d6a9cab
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
14 changes: 7 additions & 7 deletions lib/utils/sanitize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const getUser = {
const getRepo = {
name: 'repo',
type: 'input',
message: 'Enter the repo name',
message: 'Enter the repository name',
};

const getSlug = (choices) => ({
Expand All @@ -55,13 +55,13 @@ export async function sanitizeInput(input): Promise<[Array<string>, string]> {
}

if (SUPPORTED_COMMANDS.indexOf(input[0]) !== -1) {
const slugs = await getAllDownloadedSlugs();
const { slug } = await inquirer.prompt([getSlug(slugs)]);
const downloadedSlugs = await getAllDownloadedSlugs();
const { slug } = await inquirer.prompt([getSlug(downloadedSlugs)]);

if (/(.*):(.*)\/(.*)/.test(slug)) {
return [slug.match(/(.*):(.*)\/(.*)/).slice(1), input[0]];
}
if (/(.*):(.*)\/(.*)/.test(slug)) {
return [slug.match(/(.*):(.*)\/(.*)/).slice(1), input[0]];
}

}

throw new InvalidInputError(`Invalid input. Please run build-stats --help to documention for the tool.`, input);
}
47 changes: 44 additions & 3 deletions lib/utils/tests/builds.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,53 @@ import fixtures from 'fixturez';
import * as builds from '../builds';
const f = fixtures(__dirname);

test('builds.getAllDownloadsSlugs', async () => {
const cwd = f.copy('with-builds');
const downloadedSlugs = await builds.getAllDownloadedSlugs(cwd);
expect(downloadedSlugs.length).toBe(2);
expect(downloadedSlugs).toEqual(expect.arrayContaining([
'bitbucket:atlassian/build-stats',
'travis:boltpkg/bolt'
]));
});

test('builds.getBuildDir()', async () => {
let cwd = f.copy('testRepo');
let dirPath = await builds.getBuildDir(cwd, 'bitbucket', 'test', 'test-repo');
expect(dirPath).toMatch(/testRepo\/\.data\/bitbucket\/test\/test-repo\/builds/);
});

test.todo('builds.getHistory()');
test.todo('builds.findLongest()');
test.todo('builds.toTimeRanges()');
test('builds.getHistory()',async () => {
const cwd = f.copy('with-builds');
const history = await builds.getHistory(cwd, 'bitbucket', 'atlassian', 'build-stats', {
branch: '*',
result: '*'
});
expect(history.length).toBe(10);
});
test('builds.findLongest()',async () => {
const cwd = f.copy('with-builds');
const allBuilds = await builds.getHistory(cwd, 'bitbucket', 'atlassian', 'build-stats', {
branch: '*',
result: '*'
});

const longestBuild = builds.findLongest(allBuilds);
expect(longestBuild.id).toBe('7')
});

test('builds.toTimeRanges()',async () => {
const cwd = f.copy('with-builds');
const allBuilds = await builds.getHistory(cwd, 'bitbucket', 'atlassian', 'build-stats', {
branch: '*',
result: '*'
});

const ranges = builds.toTimeRanges(allBuilds, {
period: 365, // period of 365 days/ 1 year
last: 100 // last 100 years
});

// One of the fixtures build is from 1900, so it should be excluded
expect(ranges[0].length).toBe(9);
});

0 comments on commit d6a9cab

Please sign in to comment.