Skip to content

Commit

Permalink
test/style: remove or hide unused code in git.js, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nknapp committed Dec 14, 2019
1 parent 1ec1737 commit 14b621c
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = {
// Best Practices //
//----------------//
'default-case': 'warn',
'dot-notation': ['error', { allowKeywords: false }],
'dot-notation': 'warn',
'guard-for-in': 'warn',
'no-alert': 'error',
'no-caller': 'error',
Expand Down
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ module.exports = function(grunt) {
'bgShell:integrationTests',
'sauce',
'metrics',
'publish:latest'
'publish-to-aws'
]);
grunt.registerTask('on-file-change', [
'build',
Expand Down
31 changes: 26 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"eslint-config-prettier": "^6.7.0",
"eslint-plugin-compat": "^3.3.0",
"eslint-plugin-es5": "^1.4.1",
"fs-extra": "^8.1.0",
"grunt": "^1.0.4",
"grunt-babel": "^5.0.0",
"grunt-bg-shell": "^2.3.3",
Expand Down Expand Up @@ -86,7 +87,7 @@
"lint": "eslint --max-warnings 0 . ",
"dtslint": "dtslint types",
"test": "grunt",
"extensive-tests-and-publish-to-aws": "grunt --stack extensive-tests-and-publish-to-aws",
"extensive-tests-and-publish-to-aws": "npx mocha tasks/task-tests/ && grunt --stack extensive-tests-and-publish-to-aws",
"integration-test": "grunt integration-tests",
"--- combined tasks ---": "",
"check-before-pull-request": "concurrently --kill-others-on-fail npm:lint npm:dtslint npm:check-format npm:test"
Expand Down
6 changes: 3 additions & 3 deletions tasks/publish.js → tasks/publish-to-aws.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ const semver = require('semver');
module.exports = function(grunt) {
const registerAsyncTask = createRegisterAsyncTaskFn(grunt);

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

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

const suffixes = [];

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

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

Expand Down
9 changes: 9 additions & 0 deletions tasks/task-tests/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
extends: '../../.eslintrc.js',
env: {
mocha: true
},
parserOptions: {
ecmaVersion: 2018
}
};
1 change: 1 addition & 0 deletions tasks/task-tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use `mocha tasks/task-tests` to run these tests
121 changes: 121 additions & 0 deletions tasks/task-tests/git.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
const os = require('os');
const path = require('path');
const fs = require('fs-extra');
const chai = require('chai');
chai.use(require('dirty-chai'));

const git = require('../util/git');

const expect = chai.expect;

const tmpBaseDir = path.join(os.tmpdir(), 'handlebars-task-tests');
const tmpDir = path.join(tmpBaseDir, Date.now().toString(36));
const remoteDir = path.join(tmpDir, 'remote-repo');
const cloneDir = path.join(tmpDir, 'clone-repo');
const oldCwd = process.cwd();

describe('utils/git', function() {
beforeEach(async function() {
await fs.remove(tmpDir);
await createRepositoryThatActsAsRemote();
process.chdir(tmpDir);
await git.git('clone', 'remote-repo', 'clone-repo');
process.chdir(cloneDir);
});

async function createRepositoryThatActsAsRemote() {
await fs.mkdirp(remoteDir);
process.chdir(remoteDir);

await git.git('init');
await fs.writeFile('testfile.txt', 'Testfile');
await git.add('testfile.txt');
await git.commit('commit message');
}

afterEach(function() {
process.chdir(oldCwd);
});

describe('the "remotes"-function', function() {
it('should list all remotes', async function() {
await git.git('remote', 'set-url', 'origin', 'https://test.org/test');
await git.git('remote', 'add', 'second-remote', 'https://test.org/test2');

const result = await git.remotes();

expect(result.trim().split('\n')).to.deep.equal([
'origin\thttps://test.org/test (fetch)',
'origin\thttps://test.org/test (push)',
'second-remote\thttps://test.org/test2 (fetch)',
'second-remote\thttps://test.org/test2 (push)'
]);
});
});

describe('the "branches"-function', function() {
it('should list all branches', async function() {
await git.git('branch', 'test');
await git.git('branch', 'test2');

const result = await git.branches();
expect(result.trim().split('\n')).to.deep.equal([
'* master',
' test',
' test2',
' remotes/origin/HEAD -> origin/master',
' remotes/origin/master'
]);
});
});

describe('the "commitInfo"-function', function() {
it('should list head and master sha', async function() {
const result = await git.commitInfo();
expect(result.masterSha).to.equal(result.headSha);
expect(result.masterSha).to.match(/^[0-9a-f]+$/);
expect(result.headSha).to.match(/^[0-9a-f]+$/);
});

it('should have "isMaster=true" if the master branch is checked out', async function() {
const result = await git.commitInfo();
expect(result.isMaster).to.be.true();
});

it('should have "isMaster=true" if the current commit is the last commit of the master branch', async function() {
await git.git('checkout', '-b', 'new-branch');

const result = await git.commitInfo();
expect(result.isMaster).to.be.true();
});

it('should have "isMaster=false" if the current commit is NOT the last commit of the master branch', async function() {
await git.git('checkout', '-b', 'new-branch');
fs.writeFile('new-file.txt', 'new-file');
await git.add('new-file.txt');
await git.commit('added new file');

const result = await git.commitInfo();
expect(result.isMaster).to.be.false();
});

it('should show the current tag', async function() {
await git.git('tag', 'test-tag');
const result = await git.commitInfo();
expect(result.tagName).to.be.equal('test-tag');
});

it('should show a version tag rather than standard tags', async function() {
await git.git('tag', 'test-tag');
await git.git('tag', 'v1.2');
await git.git('tag', 'test-tag2');
const result = await git.commitInfo();
expect(result.tagName).to.be.equal('v1.2');
});

it('should show no tag if there is no tag', async function() {
const result = await git.commitInfo();
expect(result.tagName).to.be.null();
});
});
});
Empty file added tasks/task-tests/mocha.opts
Empty file.
71 changes: 35 additions & 36 deletions tasks/util/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,62 @@ const childProcess = require('child_process');

module.exports = {
async remotes() {
return git('remotes', '-v');
return git('remote', '-v');
},
async branches() {
return git('branch', '-a');
},
async clean() {
const stdout = git('diff-index', '--name-only', 'HEAD', '--');
return stdout === '';
},
async commitInfo() {
const headSha = await this.headSha();
const masterSha = await this.masterSha();
const headSha = await getHeadSha();
const masterSha = await getMasterSha();
return {
headSha,
masterSha,
tagName: await this.tagName(),
tagName: await getTagName(),
isMaster: headSha === masterSha
};
},
async headSha() {
const stdout = await git(' rev-parse', '--short', 'HEAD');
return stdout.trim();
},
async masterSha() {
try {
const stdout = await git('rev-parse', '--short', 'origin/master');
return stdout.trim();
} catch (error) {
if (/Needed a single revision/.test(error.message)) {
// Master was not checked out but in this case, so we know we are not master. We can ignore this
return '';
}
throw error;
}
},

async add(path) {
return git('add', '-f', path);
},
async commit(message) {
return git('commit', '--message', message);
},
async tag(name) {
return git('tag', '-a', `--message=${name}`, name);
},
async tagName() {
const stdout = await git('tag', '-l', '--points-at', 'HEAD');
git // visible for testing
};

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

if (versionTags[0] != null) {
return versionTags;
async function getMasterSha() {
try {
const stdout = await git('rev-parse', '--short', 'origin/master');
return stdout.trim();
} catch (error) {
if (/Needed a single revision/.test(error.message)) {
// Master was not checked out but in this case, so we know we are not master. We can ignore this
return '';
}
return tags[0];
throw error;
}
};
}

async function getTagName() {
const stdout = await git('tag', '-l', '--points-at', 'HEAD');
const trimmedStdout = stdout.trim();
if (trimmedStdout === '') {
return null; // there is no tag
}

const tags = trimmedStdout.split(/\n|\r\n/);
const versionTags = tags.filter(tag => /^v/.test(tag));
if (versionTags[0] != null) {
return versionTags[0];
}
return tags[0];
}

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

0 comments on commit 14b621c

Please sign in to comment.