Skip to content

Commit d6a9cab

Browse files
committed
added tests for build util
1 parent 5bf7d35 commit d6a9cab

File tree

2 files changed

+51
-10
lines changed

2 files changed

+51
-10
lines changed

lib/utils/sanitize.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const getUser = {
3333
const getRepo = {
3434
name: 'repo',
3535
type: 'input',
36-
message: 'Enter the repo name',
36+
message: 'Enter the repository name',
3737
};
3838

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

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

61-
if (/(.*):(.*)\/(.*)/.test(slug)) {
62-
return [slug.match(/(.*):(.*)\/(.*)/).slice(1), input[0]];
63-
}
61+
if (/(.*):(.*)\/(.*)/.test(slug)) {
62+
return [slug.match(/(.*):(.*)\/(.*)/).slice(1), input[0]];
6463
}
65-
64+
}
65+
6666
throw new InvalidInputError(`Invalid input. Please run build-stats --help to documention for the tool.`, input);
6767
}

lib/utils/tests/builds.test.ts

+44-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,53 @@ import fixtures from 'fixturez';
22
import * as builds from '../builds';
33
const f = fixtures(__dirname);
44

5+
test('builds.getAllDownloadsSlugs', async () => {
6+
const cwd = f.copy('with-builds');
7+
const downloadedSlugs = await builds.getAllDownloadedSlugs(cwd);
8+
expect(downloadedSlugs.length).toBe(2);
9+
expect(downloadedSlugs).toEqual(expect.arrayContaining([
10+
'bitbucket:atlassian/build-stats',
11+
'travis:boltpkg/bolt'
12+
]));
13+
});
14+
515
test('builds.getBuildDir()', async () => {
616
let cwd = f.copy('testRepo');
717
let dirPath = await builds.getBuildDir(cwd, 'bitbucket', 'test', 'test-repo');
818
expect(dirPath).toMatch(/testRepo\/\.data\/bitbucket\/test\/test-repo\/builds/);
919
});
1020

11-
test.todo('builds.getHistory()');
12-
test.todo('builds.findLongest()');
13-
test.todo('builds.toTimeRanges()');
21+
test('builds.getHistory()',async () => {
22+
const cwd = f.copy('with-builds');
23+
const history = await builds.getHistory(cwd, 'bitbucket', 'atlassian', 'build-stats', {
24+
branch: '*',
25+
result: '*'
26+
});
27+
expect(history.length).toBe(10);
28+
});
29+
test('builds.findLongest()',async () => {
30+
const cwd = f.copy('with-builds');
31+
const allBuilds = await builds.getHistory(cwd, 'bitbucket', 'atlassian', 'build-stats', {
32+
branch: '*',
33+
result: '*'
34+
});
35+
36+
const longestBuild = builds.findLongest(allBuilds);
37+
expect(longestBuild.id).toBe('7')
38+
});
39+
40+
test('builds.toTimeRanges()',async () => {
41+
const cwd = f.copy('with-builds');
42+
const allBuilds = await builds.getHistory(cwd, 'bitbucket', 'atlassian', 'build-stats', {
43+
branch: '*',
44+
result: '*'
45+
});
46+
47+
const ranges = builds.toTimeRanges(allBuilds, {
48+
period: 365, // period of 365 days/ 1 year
49+
last: 100 // last 100 years
50+
});
51+
52+
// One of the fixtures build is from 1900, so it should be excluded
53+
expect(ranges[0].length).toBe(9);
54+
});

0 commit comments

Comments
 (0)