-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from kevinbarz/master
feat(option to save git.properties to a specific directory)
- Loading branch information
Showing
9 changed files
with
223 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,9 +11,13 @@ Node module that creates a git.properties file with information about the curren | |
|
||
```sh | ||
$ npm install -g node-git-info | ||
$ node-git-info | ||
$ node-git-info [options] | ||
|
||
-d, --directory Directory to save git.properties file to (directory must already exist). | ||
``` | ||
it will returns a file named ```git.properties```: | ||
It will save a file named ```git.properties```. If the directory option isn't passed, then default location for saving the git.properties file will be the current working directory of the Node.js process. | ||
|
||
Example output: | ||
```ini | ||
git.commit.id.abbrev=42954d1 | ||
git.commit.user.email[email protected] | ||
|
@@ -23,4 +27,4 @@ git.commit.message.short=first commit | |
git.commit.user.name=User Name | ||
git.branch=master | ||
git.commit.time=2016-11-20T11:48:42.000Z | ||
``` | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
#!/usr/bin/env node | ||
|
||
require('../lib/gitProperties'); | ||
require('../index'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict'; | ||
var gitProperties = require('./lib/gitProperties'); | ||
var commandLineArgs = require('command-line-args') | ||
|
||
// library's entry point | ||
var execute = function () { | ||
|
||
// define allows command line arguments when calling library | ||
const optionDefinitions = [ | ||
{name: 'directory', alias: 'd', type: String} | ||
] | ||
|
||
// convert command line arguments to object | ||
const options = commandLineArgs(optionDefinitions); | ||
|
||
// define callback function to call when git.properties file has been written or when an error doing so occurs. | ||
var callback = function (writeSuccess) { | ||
if (writeSuccess) { | ||
//exit with a 'success' code | ||
process.exit(0); | ||
} else { | ||
//exit with a 'failure' code | ||
process.exit(1); | ||
} | ||
} | ||
|
||
// write git.properties file | ||
gitProperties.write(options.directory, callback); | ||
} | ||
|
||
execute(); | ||
|
||
module.exports = execute; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,111 @@ | ||
'use strict' | ||
|
||
var fs = require('fs'); | ||
var properties = require('../../../lib/gitProperties'); | ||
var expect = require('chai').expect; | ||
var read = require( 'utils-fs-read-properties' ); | ||
var read = require('utils-fs-read-properties'); | ||
var appRootDir = require('app-root-dir').get(); | ||
var tmp = require('tmp'); | ||
var utils = require('./utils'); | ||
var mock = require('mock-fs'); | ||
|
||
function checkGitPropertiesFileHasExpectedData(filePath, done) { | ||
|
||
expect(fs.existsSync(filePath)).to.be.true; | ||
|
||
read(filePath, function (error, data) { | ||
expect(data['git.commit.id.abbrev']).to.not.be.undefined; | ||
expect(data['git.commit.user.email']).to.not.be.undefined; | ||
expect(data['git.commit.message.full']).to.not.be.undefined; | ||
expect(data['git.commit.id']).to.not.be.undefined; | ||
expect(data['git.commit.message.short']).to.not.be.undefined; | ||
expect(data['git.commit.user.name']).to.not.be.undefined; | ||
expect(data['git.branch']).to.not.be.undefined; | ||
expect(data['git.commit.time']).to.not.be.undefined; | ||
|
||
done(); | ||
}); | ||
} | ||
|
||
|
||
describe('executing write method', function () { | ||
|
||
describe('gitProperties', function() { | ||
const testSuiteLevelTimeout = 30 * 1000; | ||
const gitPropertiesFileName = 'git.properties'; | ||
// set higher than default test suite-level timeout in case of large files being writen/read. | ||
this.timeout(testSuiteLevelTimeout); | ||
|
||
it ('should creates a git.properties file', function() { | ||
properties(); | ||
var tmpTestOutputFolder; // tempory folder for writing git.properties file to. | ||
var gitPropertiesExpectedDefaultFileName = appRootDir + '/' + gitPropertiesFileName; | ||
|
||
expect(fs.existsSync('git.properties')).to.be.true; | ||
before(function (done) { | ||
|
||
read('git.properties', function(error, data) { | ||
expect(data['git.commit.id.abbrev']).to.not.be.undefined; | ||
expect(data['git.commit.user.email']).to.not.be.undefined; | ||
expect(data['git.commit.message.full']).to.not.be.undefined; | ||
expect(data['git.commit.id']).to.not.be.undefined; | ||
expect(data['git.commit.message.short']).to.not.be.undefined; | ||
expect(data['git.commit.user.name']).to.not.be.undefined; | ||
expect(data['git.branch']).to.not.be.undefined; | ||
expect(data['git.commit.time']).to.not.be.undefined; | ||
// runs before any tests are executed in this block | ||
|
||
tmp.setGracefulCleanup(); // cleanup the temporary files even when an uncaught exception occurs. | ||
|
||
tmp.dir({dir: appRootDir, unsafeCleanup : true}, function _tempDirCreated(err, path) { | ||
if (err) throw err; | ||
tmpTestOutputFolder = path; | ||
|
||
// now mock fs library | ||
const mockedGitProperties = "git.branch=master\ngit.commit.id.abbrev=1324324\ngit.commit.time=2016-11-18T13:16:39.000Z"; | ||
const mockedGitPropertiesPath = tmpTestOutputFolder + '/' + gitPropertiesFileName; | ||
var mockFileSystem = {}; | ||
mockFileSystem[gitPropertiesExpectedDefaultFileName] = mockedGitProperties; | ||
mockFileSystem[mockedGitPropertiesPath] = mockedGitProperties; | ||
mock(mockFileSystem); | ||
done() | ||
}); | ||
}) | ||
|
||
beforeEach(function () { | ||
// runs after each test in this block | ||
utils.deleteFilesRecursivelyByName(gitPropertiesFileName); // delete test generated files | ||
}); | ||
|
||
after(function () { | ||
// runs after all tests have executed in this block | ||
utils.deleteFilesRecursivelyByName(appRootDir, gitPropertiesFileName); // delete test generated files | ||
}); | ||
|
||
afterEach(function () { | ||
// restore the fs module (so that it is backed by the real file system) | ||
mock.restore(); | ||
}); | ||
|
||
it('should create a git.properties file', function (done) { | ||
|
||
var callback = function () { | ||
|
||
checkGitPropertiesFileHasExpectedData(gitPropertiesExpectedDefaultFileName, done); | ||
} | ||
|
||
properties.write(undefined, callback); | ||
}); | ||
|
||
}); | ||
it('should create a git.properties file in the destination directory given ', function (done) { | ||
|
||
properties.write(tmpTestOutputFolder); | ||
|
||
var callback = function () { | ||
var gitPropertiesDestination = tmpTestOutputFolder + '/' + gitPropertiesFileName; | ||
checkGitPropertiesFileHasExpectedData(gitPropertiesDestination, done); | ||
}; | ||
|
||
properties.write(tmpTestOutputFolder, callback); | ||
}); | ||
|
||
it('should return an error status if destination directory given does not exist', function (done) { | ||
|
||
var destinationPath = tmpTestOutputFolder + '/i_am_a_directory_that_does_not_exist'; | ||
|
||
var callback = function (writeSuccess) { | ||
expect(writeSuccess).to.be.false; | ||
var gitPropertiesDestination = destinationPath + '/' + gitPropertiesFileName; | ||
expect(fs.existsSync(gitPropertiesDestination)).to.be.false; | ||
done(); | ||
}; | ||
|
||
properties.write(destinationPath, callback); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict' | ||
var spawn = require('child_process').spawn; | ||
|
||
/* test utility methods */ | ||
|
||
exports.deleteFilesRecursivelyByName = function(appRootDir, fileName) { | ||
var shellSyntaxCommand = 'find ' + appRootDir + ' -name ' + fileName + ' -type f|xargs rm -f'; | ||
spawn('sh', ['-c', shellSyntaxCommand], {stdio: 'inherit'}); | ||
} | ||
|
||
/* end test utility methods */ |