Skip to content

Commit 14b621c

Browse files
committed
test/style: remove or hide unused code in git.js, add tests
1 parent 1ec1737 commit 14b621c

10 files changed

+199
-47
lines changed

.eslintrc.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = {
1717
// Best Practices //
1818
//----------------//
1919
'default-case': 'warn',
20-
'dot-notation': ['error', { allowKeywords: false }],
20+
'dot-notation': 'warn',
2121
'guard-for-in': 'warn',
2222
'no-alert': 'error',
2323
'no-caller': 'error',

Gruntfile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ module.exports = function(grunt) {
279279
'bgShell:integrationTests',
280280
'sauce',
281281
'metrics',
282-
'publish:latest'
282+
'publish-to-aws'
283283
]);
284284
grunt.registerTask('on-file-change', [
285285
'build',

package-lock.json

+26-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"eslint-config-prettier": "^6.7.0",
4545
"eslint-plugin-compat": "^3.3.0",
4646
"eslint-plugin-es5": "^1.4.1",
47+
"fs-extra": "^8.1.0",
4748
"grunt": "^1.0.4",
4849
"grunt-babel": "^5.0.0",
4950
"grunt-bg-shell": "^2.3.3",
@@ -86,7 +87,7 @@
8687
"lint": "eslint --max-warnings 0 . ",
8788
"dtslint": "dtslint types",
8889
"test": "grunt",
89-
"extensive-tests-and-publish-to-aws": "grunt --stack extensive-tests-and-publish-to-aws",
90+
"extensive-tests-and-publish-to-aws": "npx mocha tasks/task-tests/ && grunt --stack extensive-tests-and-publish-to-aws",
9091
"integration-test": "grunt integration-tests",
9192
"--- combined tasks ---": "",
9293
"check-before-pull-request": "concurrently --kill-others-on-fail npm:lint npm:dtslint npm:check-format npm:test"

tasks/publish.js renamed to tasks/publish-to-aws.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ const semver = require('semver');
66
module.exports = function(grunt) {
77
const registerAsyncTask = createRegisterAsyncTaskFn(grunt);
88

9-
registerAsyncTask('publish:latest', async () => {
9+
registerAsyncTask('publish-to-aws', async () => {
1010
grunt.log.writeln('remotes: ' + (await git.remotes()));
1111
grunt.log.writeln('branches: ' + (await git.branches()));
1212

1313
const commitInfo = await git.commitInfo();
14-
grunt.log.writeln('tag: ' + commitInfo.tagName);
14+
grunt.log.writeln('tag: ', commitInfo.tagName);
1515

1616
const suffixes = [];
1717

@@ -22,7 +22,7 @@ module.exports = function(grunt) {
2222
}
2323

2424
// Publish tags by their tag-name
25-
if (commitInfo.tagName && semver.valid(commitInfo.tagName)) {
25+
if (commitInfo.tagName != null && semver.valid(commitInfo.tagName)) {
2626
suffixes.push('-' + commitInfo.tagName);
2727
}
2828

tasks/task-tests/.eslintrc.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
extends: '../../.eslintrc.js',
3+
env: {
4+
mocha: true
5+
},
6+
parserOptions: {
7+
ecmaVersion: 2018
8+
}
9+
};

tasks/task-tests/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Use `mocha tasks/task-tests` to run these tests

tasks/task-tests/git.test.js

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
const os = require('os');
2+
const path = require('path');
3+
const fs = require('fs-extra');
4+
const chai = require('chai');
5+
chai.use(require('dirty-chai'));
6+
7+
const git = require('../util/git');
8+
9+
const expect = chai.expect;
10+
11+
const tmpBaseDir = path.join(os.tmpdir(), 'handlebars-task-tests');
12+
const tmpDir = path.join(tmpBaseDir, Date.now().toString(36));
13+
const remoteDir = path.join(tmpDir, 'remote-repo');
14+
const cloneDir = path.join(tmpDir, 'clone-repo');
15+
const oldCwd = process.cwd();
16+
17+
describe('utils/git', function() {
18+
beforeEach(async function() {
19+
await fs.remove(tmpDir);
20+
await createRepositoryThatActsAsRemote();
21+
process.chdir(tmpDir);
22+
await git.git('clone', 'remote-repo', 'clone-repo');
23+
process.chdir(cloneDir);
24+
});
25+
26+
async function createRepositoryThatActsAsRemote() {
27+
await fs.mkdirp(remoteDir);
28+
process.chdir(remoteDir);
29+
30+
await git.git('init');
31+
await fs.writeFile('testfile.txt', 'Testfile');
32+
await git.add('testfile.txt');
33+
await git.commit('commit message');
34+
}
35+
36+
afterEach(function() {
37+
process.chdir(oldCwd);
38+
});
39+
40+
describe('the "remotes"-function', function() {
41+
it('should list all remotes', async function() {
42+
await git.git('remote', 'set-url', 'origin', 'https://test.org/test');
43+
await git.git('remote', 'add', 'second-remote', 'https://test.org/test2');
44+
45+
const result = await git.remotes();
46+
47+
expect(result.trim().split('\n')).to.deep.equal([
48+
'origin\thttps://test.org/test (fetch)',
49+
'origin\thttps://test.org/test (push)',
50+
'second-remote\thttps://test.org/test2 (fetch)',
51+
'second-remote\thttps://test.org/test2 (push)'
52+
]);
53+
});
54+
});
55+
56+
describe('the "branches"-function', function() {
57+
it('should list all branches', async function() {
58+
await git.git('branch', 'test');
59+
await git.git('branch', 'test2');
60+
61+
const result = await git.branches();
62+
expect(result.trim().split('\n')).to.deep.equal([
63+
'* master',
64+
' test',
65+
' test2',
66+
' remotes/origin/HEAD -> origin/master',
67+
' remotes/origin/master'
68+
]);
69+
});
70+
});
71+
72+
describe('the "commitInfo"-function', function() {
73+
it('should list head and master sha', async function() {
74+
const result = await git.commitInfo();
75+
expect(result.masterSha).to.equal(result.headSha);
76+
expect(result.masterSha).to.match(/^[0-9a-f]+$/);
77+
expect(result.headSha).to.match(/^[0-9a-f]+$/);
78+
});
79+
80+
it('should have "isMaster=true" if the master branch is checked out', async function() {
81+
const result = await git.commitInfo();
82+
expect(result.isMaster).to.be.true();
83+
});
84+
85+
it('should have "isMaster=true" if the current commit is the last commit of the master branch', async function() {
86+
await git.git('checkout', '-b', 'new-branch');
87+
88+
const result = await git.commitInfo();
89+
expect(result.isMaster).to.be.true();
90+
});
91+
92+
it('should have "isMaster=false" if the current commit is NOT the last commit of the master branch', async function() {
93+
await git.git('checkout', '-b', 'new-branch');
94+
fs.writeFile('new-file.txt', 'new-file');
95+
await git.add('new-file.txt');
96+
await git.commit('added new file');
97+
98+
const result = await git.commitInfo();
99+
expect(result.isMaster).to.be.false();
100+
});
101+
102+
it('should show the current tag', async function() {
103+
await git.git('tag', 'test-tag');
104+
const result = await git.commitInfo();
105+
expect(result.tagName).to.be.equal('test-tag');
106+
});
107+
108+
it('should show a version tag rather than standard tags', async function() {
109+
await git.git('tag', 'test-tag');
110+
await git.git('tag', 'v1.2');
111+
await git.git('tag', 'test-tag2');
112+
const result = await git.commitInfo();
113+
expect(result.tagName).to.be.equal('v1.2');
114+
});
115+
116+
it('should show no tag if there is no tag', async function() {
117+
const result = await git.commitInfo();
118+
expect(result.tagName).to.be.null();
119+
});
120+
});
121+
});

tasks/task-tests/mocha.opts

Whitespace-only changes.

tasks/util/git.js

+35-36
Original file line numberDiff line numberDiff line change
@@ -2,63 +2,62 @@ const childProcess = require('child_process');
22

33
module.exports = {
44
async remotes() {
5-
return git('remotes', '-v');
5+
return git('remote', '-v');
66
},
77
async branches() {
88
return git('branch', '-a');
99
},
10-
async clean() {
11-
const stdout = git('diff-index', '--name-only', 'HEAD', '--');
12-
return stdout === '';
13-
},
1410
async commitInfo() {
15-
const headSha = await this.headSha();
16-
const masterSha = await this.masterSha();
11+
const headSha = await getHeadSha();
12+
const masterSha = await getMasterSha();
1713
return {
1814
headSha,
1915
masterSha,
20-
tagName: await this.tagName(),
16+
tagName: await getTagName(),
2117
isMaster: headSha === masterSha
2218
};
2319
},
24-
async headSha() {
25-
const stdout = await git(' rev-parse', '--short', 'HEAD');
26-
return stdout.trim();
27-
},
28-
async masterSha() {
29-
try {
30-
const stdout = await git('rev-parse', '--short', 'origin/master');
31-
return stdout.trim();
32-
} catch (error) {
33-
if (/Needed a single revision/.test(error.message)) {
34-
// Master was not checked out but in this case, so we know we are not master. We can ignore this
35-
return '';
36-
}
37-
throw error;
38-
}
39-
},
40-
4120
async add(path) {
4221
return git('add', '-f', path);
4322
},
4423
async commit(message) {
4524
return git('commit', '--message', message);
4625
},
47-
async tag(name) {
48-
return git('tag', '-a', `--message=${name}`, name);
49-
},
50-
async tagName() {
51-
const stdout = await git('tag', '-l', '--points-at', 'HEAD');
26+
git // visible for testing
27+
};
5228

53-
const tags = stdout.trim().split(/\n|\r\n/);
54-
const versionTags = tags.filter(tag => /^v/.test(tag));
29+
async function getHeadSha() {
30+
const stdout = await git('rev-parse', '--short', 'HEAD');
31+
return stdout.trim();
32+
}
5533

56-
if (versionTags[0] != null) {
57-
return versionTags;
34+
async function getMasterSha() {
35+
try {
36+
const stdout = await git('rev-parse', '--short', 'origin/master');
37+
return stdout.trim();
38+
} catch (error) {
39+
if (/Needed a single revision/.test(error.message)) {
40+
// Master was not checked out but in this case, so we know we are not master. We can ignore this
41+
return '';
5842
}
59-
return tags[0];
43+
throw error;
6044
}
61-
};
45+
}
46+
47+
async function getTagName() {
48+
const stdout = await git('tag', '-l', '--points-at', 'HEAD');
49+
const trimmedStdout = stdout.trim();
50+
if (trimmedStdout === '') {
51+
return null; // there is no tag
52+
}
53+
54+
const tags = trimmedStdout.split(/\n|\r\n/);
55+
const versionTags = tags.filter(tag => /^v/.test(tag));
56+
if (versionTags[0] != null) {
57+
return versionTags[0];
58+
}
59+
return tags[0];
60+
}
6261

6362
async function git(...args) {
6463
return new Promise((resolve, reject) =>

0 commit comments

Comments
 (0)