Skip to content

Commit 87bf365

Browse files
committed
Added inquirer and tests for built util
1 parent dc40f19 commit 87bf365

File tree

6 files changed

+104
-6
lines changed

6 files changed

+104
-6
lines changed

.nvmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
16

lib/utils/builds.ts

+20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,25 @@ import * as times from './times';
44

55
const defaultBuildDir = path.join(__dirname, '../..');
66

7+
export async function getAllDownloadedSlugs(cwd = defaultBuildDir) {
8+
let allDownloadedSlugs = [];
9+
const pathToCache = path.join(cwd, '.data');
10+
11+
const allHosts = await fs.readDir(pathToCache);
12+
13+
await Promise.all(allHosts.map(async host => {
14+
const allUsers = await fs.readDir(path.join(pathToCache, host));
15+
await Promise.all(allUsers.map(async user => {
16+
const allRepos = await fs.readDir(path.join(pathToCache, host, user));
17+
allRepos.forEach(repo => {
18+
allDownloadedSlugs.push(`${host}:${user}/${repo}`);
19+
});
20+
}))
21+
}));
22+
23+
return allDownloadedSlugs;
24+
}
25+
726
export async function getBuildDir(cwd = defaultBuildDir, host, user, repo) {
827
const buildsDir = path.join(cwd, '.data', host, user, repo, 'builds');
928
await fs.mkdirp(buildsDir);
@@ -86,6 +105,7 @@ export async function getLastDownloadedBuildNumber(buildsDir) {
86105
}
87106

88107
module.exports = {
108+
getAllDownloadedSlugs,
89109
getBuildDir,
90110
getHistory,
91111
getLastDownloadedBuildNumber,

lib/utils/sanitize.ts

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import inquirer from 'inquirer';
2+
import { SUPPORTED_COMMANDS } from '../../index';
3+
import { InvalidInputError } from '../error';
4+
import { getAllDownloadedSlugs } from './builds';
5+
import flatten from 'lodash.flatten';
26

37
const getHost = {
48
name: 'host',
@@ -29,15 +33,35 @@ const getUser = {
2933
const getRepo = {
3034
name: 'repo',
3135
type: 'input',
32-
message: 'Enter the repo name',
36+
message: 'Enter the repository name',
3337
};
3438

39+
const getSlug = (choices) => ({
40+
name: 'slug',
41+
type: 'list',
42+
message: 'Select the repo:',
43+
choices
44+
})
45+
3546
export async function sanitizeInput(input): Promise<[Array<string>, string]> {
3647
if (/(.*):(.*)\/(.*)/.test(input[0])) {
3748
return [input[0].match(/(.*):(.*)\/(.*)/).slice(1), input[1]];
3849
}
3950

40-
const { host, user, repo } = await inquirer.prompt([getHost, getUser, getRepo]);
51+
if (input[0] === 'download') {
52+
const { host, user, repo } = await inquirer.prompt([getHost, getUser, getRepo]);
53+
54+
return [[host, user, repo], input[0]];
55+
}
4156

42-
return [[host, user, repo], input[0]];
57+
if (SUPPORTED_COMMANDS.indexOf(input[0]) !== -1) {
58+
const downloadedSlugs = await getAllDownloadedSlugs();
59+
const { slug } = await inquirer.prompt([getSlug(downloadedSlugs)]);
60+
61+
if (/(.*):(.*)\/(.*)/.test(slug)) {
62+
return [slug.match(/(.*):(.*)\/(.*)/).slice(1), input[0]];
63+
}
64+
}
65+
66+
throw new InvalidInputError(`Invalid input. Please run build-stats --help to documention for the tool.`, input);
4367
}

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+
});

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@
3939
"got": "^8.0.1",
4040
"inquirer": "^8.2.0",
4141
"left-pad": "^1.2.0",
42+
"lodash.flatten": "^4.4.0",
4243
"lodash.groupby": "^4.6.0",
4344
"lodash.pick": "^4.4.0",
45+
"lodash.without": "^4.4.0",
4446
"meow": "^v9.0.0",
4547
"mkdirp": "^0.5.1",
4648
"ora": "^5.4.1",

yarn.lock

+10
Original file line numberDiff line numberDiff line change
@@ -3108,6 +3108,11 @@ lodash.debounce@^4.0.8:
31083108
resolved "https://registry.yarnpkg.com/lodash.debounce/-/lodash.debounce-4.0.8.tgz#82d79bff30a67c4005ffd5e2515300ad9ca4d7af"
31093109
integrity sha1-gteb/zCmfEAF/9XiUVMArZyk168=
31103110

3111+
lodash.flatten@^4.4.0:
3112+
version "4.4.0"
3113+
resolved "https://registry.yarnpkg.com/lodash.flatten/-/lodash.flatten-4.4.0.tgz#f31c22225a9632d2bbf8e4addbef240aa765a61f"
3114+
integrity sha1-8xwiIlqWMtK7+OSt2+8kCqdlph8=
3115+
31113116
lodash.groupby@^4.6.0:
31123117
version "4.6.0"
31133118
resolved "https://registry.yarnpkg.com/lodash.groupby/-/lodash.groupby-4.6.0.tgz#0b08a1dcf68397c397855c3239783832df7403d1"
@@ -3123,6 +3128,11 @@ lodash.pick@^4.4.0:
31233128
resolved "https://registry.yarnpkg.com/lodash.pick/-/lodash.pick-4.4.0.tgz#52f05610fff9ded422611441ed1fc123a03001b3"
31243129
integrity sha1-UvBWEP/53tQiYRRB7R/BI6AwAbM=
31253130

3131+
lodash.without@^4.4.0:
3132+
version "4.4.0"
3133+
resolved "https://registry.yarnpkg.com/lodash.without/-/lodash.without-4.4.0.tgz#3cd4574a00b67bae373a94b748772640507b7aac"
3134+
integrity sha1-PNRXSgC2e643OpS3SHcmQFB7eqw=
3135+
31263136
lodash@^4.17.21, lodash@^4.7.0:
31273137
version "4.17.21"
31283138
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"

0 commit comments

Comments
 (0)