Comparing version 1.0.6 to 1.0.7
@@ -36,7 +36,8 @@ { | ||
"globals": { | ||
"Promise": false, | ||
"ATAT_REGEX": false, | ||
"CR": false, | ||
"CRE": false, | ||
"CRS": false | ||
"CRS": false, | ||
"Promise": false | ||
} | ||
} |
@@ -8,3 +8,2 @@ 'use strict' | ||
var gitDiffAsync = function(str1, str2, options) { | ||
return new Promise(function(resolve) { | ||
@@ -11,0 +10,0 @@ var gitDiff = gitDiffSync(str1, str2, options) |
@@ -0,5 +1,8 @@ | ||
'use strict' | ||
var config = { | ||
testing: process.env.NODE_ENV === 'test' | ||
testing: process.env.NODE_ENV === 'test', | ||
gitDiffOptionsUrl: 'https://git-scm.com/docs/git-diff#_options' | ||
} | ||
module.exports = config |
@@ -7,4 +7,5 @@ 'use strict' | ||
forceFake: false, // git-diff | ||
noHeaders: false, // real | ||
save: false, // git-diff | ||
wordDiff: false // fake real | ||
}) |
@@ -7,8 +7,5 @@ 'use strict' | ||
function Color() { | ||
this.add = function(str, color) { | ||
/* istanbul ignore next */ | ||
if (!config.testing) str = chalk[color](str) | ||
return str | ||
@@ -15,0 +12,0 @@ } |
@@ -7,3 +7,2 @@ 'use strict' | ||
function gitDiffFake(str1, str2, options) { | ||
return (options.wordDiff) ? wordDiffFake(str1, str2, options) : lineDiffFake(str1, str2, options) | ||
@@ -10,0 +9,0 @@ } |
@@ -22,28 +22,40 @@ 'use strict' | ||
describe('line difference', function() { | ||
var testObjs = [{ | ||
testPrefix: 'line difference -', | ||
wordDiff: false, | ||
expected: imp.data.lineDiffFake, | ||
oneLinerExpected: imp.data.oneLinerLineDiffFake, | ||
endingsLinuxExpected: imp.data.endingsLinuxLineDiff, | ||
endingsWindowsExpected: imp.data.endingsWindowsLineDiff | ||
}, { | ||
testPrefix: 'word difference -', | ||
wordDiff: true, | ||
expected: imp.data.wordDiffFake, | ||
oneLinerExpected: imp.data.oneLinerWordDiffFake, | ||
endingsLinuxExpected: imp.data.endingsLinuxWordDiff, | ||
endingsWindowsExpected: imp.data.endingsWindowsWordDiff | ||
}] | ||
var expected = imp.data.lineDiffFakeVim | ||
imp.using(testObjs, function() { | ||
it('color', function() { | ||
var actual = gitDiffFake(str1, str2, {color: true, wordDiff: false}) | ||
imp.expect(actual).to.equal(expected) | ||
it('{testPrefix} color', function(testObj) { | ||
var actual = gitDiffFake(str1, str2, {color: true, wordDiff: testObj.wordDiff}) | ||
imp.expect(actual).to.equal(testObj.expected) | ||
imp.expect(imp.color.add).to.have.been.calledWith(imp.sinon.match.any, 'green') | ||
imp.expect(imp.color.add).to.have.been.calledWith(imp.sinon.match.any, 'red') | ||
imp.expect(imp.color.add).to.have.been.calledWith(imp.sinon.match.any, 'reset') | ||
}) | ||
it('no color', function() { | ||
var actual = gitDiffFake(str1, str2, {color: false, wordDiff: false}) | ||
imp.expect(actual).to.equal(expected) | ||
it('{testPrefix} no color', function(testObj) { | ||
var actual = gitDiffFake(str1, str2, {color: false, wordDiff: testObj.wordDiff}) | ||
imp.expect(actual).to.equal(testObj.expected) | ||
imp.expect(imp.color.add).to.have.not.been.called | ||
}) | ||
it('one liner', function() { | ||
var expected = imp.data.oneLinerLineDiffFake | ||
var actual = gitDiffFake('my first string', 'my second string', {color: false, wordDiff: false}) | ||
imp.expect(actual).to.equal(expected) | ||
it('{testPrefix} one liner', function(testObj) { | ||
var actual = gitDiffFake('my first string', 'my second string', {color: false, wordDiff: testObj.wordDiff}) | ||
imp.expect(actual).to.equal(testObj.oneLinerExpected) | ||
}) | ||
it('no difference', function() { | ||
var actual = gitDiffFake('', '', {color: true, wordDiff: false}) | ||
it('{testPrefix} no difference', function(testObj) { | ||
var actual = gitDiffFake('', '', {color: true, wordDiff: testObj.wordDiff}) | ||
imp.expect(actual).to.be.undefined | ||
@@ -53,57 +65,12 @@ imp.expect(imp.color.add).to.have.not.been.called | ||
it('line endings', function() { | ||
var expected, actual | ||
it('{testPrefix} line endings', function(testObj) { | ||
var actual | ||
expected = imp.data.endingsLinuxLineDiff | ||
actual = gitDiffFake('my first\nstring', 'my second\nstring', {color: false, wordDiff: false}) | ||
imp.expect(actual).to.equal(expected) | ||
actual = gitDiffFake('my first\nstring', 'my second\nstring', {color: false, wordDiff: testObj.wordDiff}) | ||
imp.expect(actual).to.equal(testObj.endingsLinuxExpected) | ||
expected = imp.data.endingsWindowsLineDiff | ||
actual = gitDiffFake('my first\r\nstring', 'my second\r\nstring', {color: false, wordDiff: false}) | ||
imp.expect(actual).to.equal(expected) | ||
actual = gitDiffFake('my first\r\nstring', 'my second\r\nstring', {color: false, wordDiff: testObj.wordDiff}) | ||
imp.expect(actual).to.equal(testObj.endingsWindowsExpected) | ||
}) | ||
}) | ||
describe('word difference', function() { | ||
var expected = imp.data.wordDiffFake | ||
it('color', function() { | ||
var actual = gitDiffFake(str1, str2, {color: true, wordDiff: true}) | ||
imp.expect(actual).to.equal(expected) | ||
imp.expect(imp.color.add).to.have.been.calledWith(imp.sinon.match.any, 'green') | ||
imp.expect(imp.color.add).to.have.been.calledWith(imp.sinon.match.any, 'red') | ||
imp.expect(imp.color.add).to.have.been.calledWith(imp.sinon.match.any, 'reset') | ||
}) | ||
it('no color', function() { | ||
var actual = gitDiffFake(str1, str2, {color: false, wordDiff: true}) | ||
imp.expect(actual).to.equal(expected) | ||
imp.expect(imp.color.add).to.have.not.been.called | ||
}) | ||
it('one liner', function() { | ||
var expected = imp.data.oneLinerWordDiffFake | ||
var actual = gitDiffFake('my first string', 'my second string', {color: false, wordDiff: true}) | ||
imp.expect(actual).to.equal(expected) | ||
}) | ||
it('no difference', function() { | ||
var actual = gitDiffFake('', '', {color: true, wordDiff: true}) | ||
imp.expect(actual).to.be.undefined | ||
imp.expect(imp.color.add).to.have.not.been.called | ||
}) | ||
it('line endings', function() { | ||
var expected, actual | ||
expected = imp.data.endingsLinuxWordDiff | ||
actual = gitDiffFake('my first\nstring', 'my second\nstring', {color: false, wordDiff: true}) | ||
imp.expect(actual).to.equal(expected) | ||
expected = imp.data.endingsWindowsWordDiff | ||
actual = gitDiffFake('my first\r\nstring', 'my second\r\nstring', {color: false, wordDiff: true}) | ||
imp.expect(actual).to.equal(expected) | ||
}) | ||
}) | ||
}) |
@@ -8,3 +8,2 @@ 'use strict' | ||
var gitDiffReal = function(str1, str2, options) { | ||
if (keepIt.real()) { | ||
@@ -11,0 +10,0 @@ return generateDiff(str1, str2, options) |
'use strict' | ||
var config = require('../../config') | ||
var gitDiffReal = require('./index') | ||
var imp = require('../../test/_js/testImports') | ||
var gitDiffReal = require('./index') | ||
var GREEN = '\u001b[32m' | ||
var RED = '\u001b[31m' | ||
var str1 = imp.data.str1 | ||
@@ -60,4 +62,4 @@ var str2 = imp.data.str2 | ||
var actual = gitDiffReal(str1, str2, {color: true, wordDiff: false}) | ||
imp.expect(actual).to.include(GREEN) | ||
imp.expect(actual).to.include(RED) | ||
imp.expect(actual).to.include(GREEN) | ||
}) | ||
@@ -67,7 +69,6 @@ | ||
if (testObj.stub) stub() | ||
var expected = imp.data.lineDiffRealVim | ||
var actual = gitDiffReal(str1, str2, {color: false, wordDiff: false}) | ||
imp.expect(actual).to.equal(expected) | ||
imp.expect(actual).to.equal(imp.data.lineDiffReal) | ||
imp.expect(actual).to.not.include(GREEN) | ||
imp.expect(actual).to.not.include(RED) | ||
imp.expect(actual).to.not.include(GREEN) | ||
}) | ||
@@ -77,5 +78,4 @@ | ||
if (testObj.stub) stub() | ||
var expected = imp.data.oneLinerLineDiffReal | ||
var actual = gitDiffReal('my first string', 'my second string', {color: false, wordDiff: false}) | ||
imp.expect(actual).to.equal(expected) | ||
imp.expect(actual).to.equal(imp.data.oneLinerLineDiffReal) | ||
}) | ||
@@ -85,3 +85,3 @@ | ||
if (testObj.stub) stub() | ||
var actual = gitDiffReal('', '', {wordDiff: false}) | ||
var actual = gitDiffReal('', '', {color: true, wordDiff: false}) | ||
imp.expect(actual).to.be.undefined | ||
@@ -104,4 +104,4 @@ }) | ||
var actual = gitDiffReal(str1, str2, {color: true, wordDiff: true}) | ||
imp.expect(actual).to.include(GREEN) | ||
imp.expect(actual).to.include(RED) | ||
imp.expect(actual).to.include(GREEN) | ||
}) | ||
@@ -111,7 +111,6 @@ | ||
if (testObj.stub) stub() | ||
var expected = imp.data.wordDiffReal | ||
var actual = gitDiffReal(str1, str2, {color: false, wordDiff: true}) | ||
imp.expect(actual).to.equal(expected) | ||
imp.expect(actual).to.equal(imp.data.wordDiffReal) | ||
imp.expect(actual).to.not.include(GREEN) | ||
imp.expect(actual).to.not.include(RED) | ||
imp.expect(actual).to.not.include(GREEN) | ||
}) | ||
@@ -121,5 +120,4 @@ | ||
if (testObj.stub) stub() | ||
var expected = imp.data.oneLinerWordDiffReal | ||
var actual = gitDiffReal('my first string', 'my second string', {color: false, wordDiff: true}) | ||
imp.expect(actual).to.equal(expected) | ||
imp.expect(actual).to.equal(imp.data.oneLinerWordDiffReal) | ||
}) | ||
@@ -129,3 +127,3 @@ | ||
if (testObj.stub) stub() | ||
var actual = gitDiffReal('', '', {wordDiff: true}) | ||
var actual = gitDiffReal('', '', {color: true, wordDiff: true}) | ||
imp.expect(actual).to.be.undefined | ||
@@ -147,5 +145,4 @@ }) | ||
if (testObj.stub) stub() | ||
var expected = imp.data.shortstatReal | ||
var actual = gitDiffReal(str1, str2, {color: false, flags: '--shortstat'}) | ||
imp.expect(actual).to.equal(expected) | ||
imp.expect(actual).to.equal(imp.data.shortstatReal) | ||
imp.expect(imp.loglevel.warn).to.have.not.been.called | ||
@@ -158,6 +155,6 @@ imp.expect(imp.loglevel.info).to.have.not.been.called | ||
var actual = gitDiffReal(str1, str2, {color: false, flags: '--oops'}) | ||
var expected = imp.data.lineDiffRealVim | ||
imp.expect(actual).to.equal(expected) | ||
imp.expect(actual).to.equal(imp.data.lineDiffReal) | ||
imp.expect(imp.loglevel.warn).to.have.been.calledWith('Ignoring invalid git diff options: --oops') | ||
imp.expect(imp.loglevel.info).to.have.been.calledWith('For valid git diff options refer to https://git-scm.com/docs/git-diff#_options') | ||
imp.expect(imp.loglevel.info).to.have.been.calledWith('For valid git diff options refer to ' + config.gitDiffOptionsUrl) | ||
imp.expect(imp.loglevel.info).to.have.not.been.calledWithMatch(/Using default git diff options/) | ||
}) | ||
@@ -169,7 +166,6 @@ | ||
var actual = gitDiffReal(str1, str2, {color: false, flags: '--oops'}) | ||
var expected = imp.data.shortstatReal | ||
imp.expect(actual).to.equal(expected) | ||
imp.expect(actual).to.equal(imp.data.shortstatReal) | ||
imp.expect(DEFAULTS.flags).to.equal('--shortstat') | ||
imp.expect(imp.loglevel.warn).to.have.been.calledWith('Ignoring invalid git diff options: --oops') | ||
imp.expect(imp.loglevel.info).to.have.been.calledWith('For valid git diff options refer to https://git-scm.com/docs/git-diff#_options') | ||
imp.expect(imp.loglevel.info).to.have.been.calledWith('For valid git diff options refer to ' + config.gitDiffOptionsUrl) | ||
imp.expect(imp.loglevel.info).to.have.been.calledWith('Using default git diff options: --shortstat') | ||
@@ -182,10 +178,32 @@ }) | ||
var actual = gitDiffReal(str1, str2, {color: false, flags: '--oops'}) | ||
var expected = imp.data.lineDiffRealVim | ||
imp.expect(actual).to.equal(expected) | ||
imp.expect(actual).to.equal(imp.data.lineDiffReal) | ||
imp.expect(DEFAULTS.flags).to.equal(null) | ||
imp.expect(imp.loglevel.warn).to.have.been.calledWith('Ignoring invalid git diff options: --oops') | ||
imp.expect(imp.loglevel.info).to.have.been.calledWith('For valid git diff options refer to https://git-scm.com/docs/git-diff#_options') | ||
imp.expect(imp.loglevel.info).to.not.have.been.calledWithMatch(/Using default git diff options/) | ||
imp.expect(imp.loglevel.info).to.have.been.calledWith('For valid git diff options refer to ' + config.gitDiffOptionsUrl) | ||
imp.expect(imp.loglevel.info).to.have.not.been.calledWithMatch(/Using default git diff options/) | ||
}) | ||
}) | ||
describe('no headers', function() { | ||
before(function(testObj) { | ||
if (!testObj.stub) { | ||
if (!imp.keepIt.real()) this.skip() | ||
} else { | ||
if (!imp.keepIt.realNoRepo()) this.skip() | ||
} | ||
}) | ||
it('{testPrefix} headers', function(testObj) { | ||
if (testObj.stub) stub() | ||
var actual = gitDiffReal(str1, str2, {color: false, noHeaders: false}) | ||
imp.expect(actual).to.startWith('@@') | ||
}) | ||
it('{testPrefix} no headers', function(testObj) { | ||
if (testObj.stub) stub() | ||
var actual = gitDiffReal(str1, str2, {color: false, noHeaders: true}) | ||
imp.expect(actual).to.not.startWith('@@') | ||
}) | ||
}) | ||
}) | ||
@@ -192,0 +210,0 @@ }) |
@@ -28,2 +28,3 @@ 'use strict' | ||
this.git = this.git || isGit() | ||
this.mktemp = this.mktemp || isMkTemp() | ||
this.printf = this.printf || isPrintf() | ||
@@ -36,3 +37,3 @@ | ||
this.realNoRepo = function() { | ||
return this.printf && this.git && isMkTemp() | ||
return this.printf && this.git && this.mktemp | ||
} | ||
@@ -39,0 +40,0 @@ } |
@@ -33,2 +33,6 @@ 'use strict' | ||
if (typeof options.noHeaders !== 'undefined') { | ||
options.noHeaders = toBoolean(options.noHeaders) | ||
} | ||
if (typeof options.save !== 'undefined') { | ||
@@ -53,2 +57,3 @@ options.save = toBoolean(options.save) | ||
DEFAULTS.forceFake = options.forceFake | ||
DEFAULTS.noHeaders = options.noHeaders | ||
DEFAULTS.wordDiff = options.wordDiff | ||
@@ -55,0 +60,0 @@ } |
@@ -49,2 +49,7 @@ 'use strict' | ||
it('flags is null', function() { | ||
actual = normaliseOptions({flags: null}) | ||
imp.expect(actual).to.eql(expected) | ||
}) | ||
it('flags is not a string', function() { | ||
@@ -66,2 +71,13 @@ actual = normaliseOptions({flags: 9}) | ||
it('noHeaders is a boolean', function() { | ||
actual = normaliseOptions({noHeaders: false}) | ||
imp.expect(actual).to.eql(expected) | ||
}) | ||
it('noHeaders is not a boolean', function() { | ||
expected.noHeaders = true | ||
actual = normaliseOptions({noHeaders: 9}) | ||
imp.expect(actual).to.eql(expected) | ||
}) | ||
it('save', function() { | ||
@@ -68,0 +84,0 @@ var SAVE = true |
@@ -6,9 +6,7 @@ 'use strict' | ||
function validate(str1, str2) { | ||
if (typeof str1 !== 'string' || typeof str2 !== 'string') { | ||
throw TypeError('Both inputs to ' + pkg.name + ' must be strings') | ||
throw TypeError(pkg.name + ' requires two strings') | ||
} | ||
} | ||
module.exports = validate |
@@ -21,3 +21,3 @@ 'use strict' | ||
validate(9, '') | ||
}).to.throw(TypeError, 'Both inputs to ' + pkg.name + ' must be strings') | ||
}).to.throw(TypeError, pkg.name + ' requires two strings') | ||
}) | ||
@@ -27,6 +27,6 @@ | ||
imp.expect(function() { | ||
validate('', {}) | ||
}).to.throw(TypeError, 'Both inputs to ' + pkg.name + ' must be strings') | ||
validate('') | ||
}).to.throw(TypeError, pkg.name + ' requires two strings') | ||
}) | ||
}) | ||
}) |
{ | ||
"name": "git-diff", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"description": "Returns the git diff of two strings", | ||
@@ -26,8 +26,17 @@ "author": "Daniel Lewis BSc (Hons)", | ||
"difference", | ||
"git-diff", | ||
"git-differ", | ||
"git-difference", | ||
"git diff", | ||
"git differ", | ||
"git difference", | ||
"string-diff", | ||
"string-differ", | ||
"string-difference", | ||
"string diff", | ||
"string differ", | ||
"string difference", | ||
"diff-tool", | ||
"differ-tool", | ||
"difference-tool", | ||
"diff tool", | ||
@@ -38,2 +47,4 @@ "differ tool", | ||
"difftool", | ||
"git-difftool", | ||
"git-diff-tool", | ||
"git difftool", | ||
@@ -47,20 +58,21 @@ "git diff tool" | ||
"shelljs": "^0.7.8", | ||
"shelljs.exec": "^1.1.3" | ||
"shelljs.exec": "^1.1.5" | ||
}, | ||
"devDependencies": { | ||
"chai": "^4.1.2", | ||
"chai-string": "^1.4.0", | ||
"coveralls": "2.11.16", | ||
"data-driven": "^1.3.0", | ||
"eslint": "^4.7.2", | ||
"eslint": "^4.8.0", | ||
"husky": "^0.14.3", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^3.5.3", | ||
"mocha": "^4.0.0", | ||
"npm-run-all": "^4.1.1", | ||
"readfile-go": "^1.0.3", | ||
"sinon": "^3.3.0", | ||
"sinon-chai": "^2.13.0" | ||
"readfile-go": "^1.0.4", | ||
"sinon": "^4.0.0", | ||
"sinon-chai": "^2.14.0" | ||
}, | ||
"engines": { | ||
"node": ">= 4" | ||
"node": ">= 4.0.0" | ||
} | ||
} |
171
README.md
@@ -17,2 +17,12 @@ # git-diff | ||
## Introduction | ||
git-diff will use ```git``` (if installed) and ```printf``` (if available) to get the real git diff of two strings. | ||
If either command is unavailable, git-diff instead returns a very good fake git diff. | ||
<br> | ||
## Usage | ||
@@ -22,24 +32,85 @@ | ||
git-diff takes 3 arguments, the **first string to diff**, the **second string to diff** and optionally a **color flag (boolean)** | ||
git-diff takes 3 arguments, the **old string** to diff, the **new string** to diff and optionally an [options object](#options-object) | ||
git-diff returns a string containing the difference | ||
git-diff returns the git diff or `undefined` where there is no difference. | ||
The color flag (defaults to false) indicates whether you want the return value to be colorized with [chalk](https://www.npmjs.com/package/chalk) | ||
An example to demonstrate usage: | ||
Examples to demonstrate usage follow. | ||
```javascript 1.5 | ||
var gitDiff = require('git-diff') | ||
var oldStr = 'fred\nis\nfunny\n' | ||
var newStr = 'paul\nis\nfunny\n' | ||
var actual = gitDiff(oldStr, newStr, {color: false}) | ||
expect(actual).to.equal('@@ -1,3 +1,3 @@\n-fred\n+paul\n is\n funny\n') | ||
``` | ||
With color: | ||
<br> | ||
## Options object | ||
Available options are: | ||
color | flags | forceFake | noHeaders | save | wordDiff | ||
Default options are: | ||
```javascript 1.5 | ||
var options = { | ||
color: true, // Add color to the git diff returned? | ||
flags: null, // A space separated string of git diff flags from https://git-scm.com/docs/git-diff#_options | ||
forceFake: false, // Do not try and get a real git diff, just get me a fake? Faster but may not be 100% accurate | ||
noHeaders: false, // Remove the ugly @@ -1,3 +1,3 @@ header? | ||
save: false, // Remember the options for next time? | ||
wordDiff: false // Get a word diff instead of a line diff? | ||
} | ||
``` | ||
Where options are not self explanatory, further assistance is given below. | ||
<br> | ||
#### **flags** (string | null) [![top](top.png)](#options-object) | ||
The flags option allows you to use any [git diff flags](https://git-scm.com/docs/git-diff#_options) | ||
It only applies to real git diffs and will not effect the returned git diff if it is fake. | ||
An example to illustrate: | ||
```javascript 1.5 | ||
var gitDiff = require('git-diff') | ||
var actual = gitDiff('a\nb\n', 'a\nc\n', {color: true}) | ||
expect(actual).to.equal('a\n-b\n+c\n') | ||
var oldStr = 'fred\n is \nfunny\n' | ||
var newStr = 'paul\nis\n funny \n' | ||
var actual = gitDiff(oldStr, newStr, {color: false, flags: '--diff-algorithm=minimal --ignore-all-space'}) | ||
expect(actual).to.equal('@@ -1,3 +1,3 @@\n-fred\n+paul\n is\n funny \n') | ||
``` | ||
Without color: | ||
<br> | ||
#### **forceFake** (boolean) [![top](top.png)](#options-object) | ||
git-diff will first try to use ```git``` and ```printf``` to get the real git diff of two strings. | ||
If it cannot, it instead returns a very good fake git diff. | ||
A fake git diff is faster to obtain but may not be 100% representative of a real git diff. | ||
The [flags](TODO) option is unavailable when faking and fake diffs never have a header. | ||
However, if a fake is good enough and speed is of the essence then you may want to force a fake git diff. | ||
The forceFake option allows you to do exactly that: | ||
```javascript 1.5 | ||
var gitDiff = require('git-diff') | ||
var actual = gitDiff('a\nb\n', 'a\nc\n') | ||
expect(actual).to.equal('a\n-b\n+c\n') | ||
var oldStr = 'fred\nis\nfunny\n' | ||
var newStr = 'paul\nis\nfunny\n' | ||
var actual = gitDiff(oldStr, newStr, {color: false, forceFake: true}) | ||
expect(actual).to.equal('-fred\n+paul\n is\n funny\n') | ||
``` | ||
@@ -51,2 +122,82 @@ | ||
#### **save** (boolean) [![top](top.png)](#options-object) | ||
Its annoying to keep passing the same options every time. | ||
git-diff, if instructed to do so, will remember previously used options for you. | ||
When the `{save: true}` option is used in a call to git-diff subsequent calls remember the options. | ||
```javascript 1.5 | ||
var gitDiff = require('git-diff') | ||
var oldStr = 'fred\nis\nfunny\n' | ||
var newStr = 'paul\nis\nfunny\n' | ||
var actual | ||
actual = gitDiff(oldStr, newStr, {color: false, save: true, wordDiff: true}) | ||
expect(actual).to.equal('@@ -1,3 +1,3 @@\n[-fred-]{+paul+}\nis\nfunny\n') | ||
actual = gitDiff(oldStr, newStr) | ||
expect(actual).to.equal('@@ -1,3 +1,3 @@\n[-fred-]{+paul+}\nis\nfunny\n') | ||
``` | ||
Here, the second call remembers that the color option is off and wordDiff is on. `{color: false}` and `{wordDiff: true}` are now the defaults. | ||
<br> | ||
#### **wordDiff** (boolean) [![top](top.png)](#options-object) | ||
This option allows you to choose between a line diff `{wordDiff: false}` and a word diff `{wordDiff: true}` | ||
console.logging the returned git-diff for both a line diff and a word diff produces: | ||
![Line diff v Word diff](diffs.png "Line diff v Word diff") | ||
<br> | ||
## FAQs | ||
Q: What is a real git diff? | ||
A: A real git diff refers to the actual diff output produced by git itself | ||
Q: What is a fake git diff? | ||
A: A fake git diff is a programmatic attempt to reproduce / mimic the diff output produced by git | ||
Q: How can I tell if the returned git diff is real or fake? | ||
A: If the @@ -1,3 +1,3 @@ header is present then the returned git diff is real | ||
If the header is absent then either the noHeaders option is on or the returned git diff is fake | ||
Q: Will my environment produce real or fake git diffs? | ||
A: Linux and mac have the ```printf``` command available. On Windows [git bash](https://git-for-windows.github.io) makes ```printf``` available. | ||
Where git is installed then any of these environments will produce a real git diff. | ||
<br> | ||
## Asynchronous execution | ||
git-diff exposes a promise based asynchronous solution: | ||
```javascript 1.5 | ||
var gitDiff = require('git-diff/async') | ||
var oldStr = 'fred\nis\nfunny\n' | ||
var newStr = 'paul\nis\nfunny\n' | ||
gitDiff(oldStr, newStr, {color: false}).then(function(actual) { | ||
expect(actual).to.equal('@@ -1,3 +1,3 @@\n-fred\n+paul\n is\n funny\n') | ||
}) | ||
``` | ||
<br> | ||
## Author says | ||
@@ -53,0 +204,0 @@ |
@@ -5,5 +5,5 @@ 'use strict' | ||
var normaliseOptions = require('./js/normaliseOptions') | ||
var gitDiffFake = require('./js/gitDiffFake') | ||
var gitDiffReal = require('./js/gitDiffReal') | ||
var normaliseOptions = require('./js/normaliseOptions') | ||
var validate = require('./js/validate') | ||
@@ -10,0 +10,0 @@ |
'use strict' | ||
// expect sinon | ||
var chai = require('chai') | ||
var chaiString = require('chai-string') | ||
var sinon = require('sinon') | ||
var sinonChai = require('sinon-chai') | ||
var expect = chai.expect | ||
chai.use(chaiString) | ||
chai.use(sinonChai) | ||
@@ -10,0 +11,0 @@ |
@@ -6,2 +6,3 @@ 'use strict' | ||
module.exports = Object.freeze({ | ||
endingsLinuxLineDiff: '-my first\n+my second\n string\n', | ||
@@ -11,7 +12,11 @@ endingsWindowsLineDiff: '-my first\r\n+my second\r\n string\r\n', | ||
endingsWindowsWordDiff: 'my [-first-]{+second+}\r\nstring\r\n', | ||
lineDiffFakeVim: readFileGo(__dirname + '/lineDiffVim.txt', { | ||
lineDiffFake: readFileGo(__dirname + '/lineDiffVim.txt', { | ||
throw: true, | ||
save: true | ||
}).replace(/^@@.+@@(?:\r?\n|\r)/, ''), | ||
lineDiffRealVim: readFileGo(__dirname + '/lineDiffVim.txt'), | ||
}).replace(ATAT_REGEX, '').replace(CR, ''), | ||
lineDiffReal: readFileGo(__dirname + '/lineDiffVim.txt'), | ||
wordDiffFake: readFileGo(__dirname + '/wordDiffFake.txt').replace(ATAT_REGEX, '').replace(CR, ''), | ||
wordDiffReal: readFileGo(__dirname + '/wordDiffReal.txt'), | ||
oneLinerLineDiffFake: '-my first string\n+my second string\n', | ||
@@ -21,7 +26,7 @@ oneLinerLineDiffReal: '@@ -1 +1 @@\n-my first string\n\\ No newline at end of file\n+my second string\n\\ No newline at end of file\n', | ||
oneLinerWordDiffReal: '@@ -1 +1 @@\nmy [-first-]{+second+} string\n', | ||
shortstatReal: readFileGo(__dirname + '/shortstatReal.txt'), | ||
str1: readFileGo(__dirname + '/str1.txt'), | ||
str2: readFileGo(__dirname + '/str2.txt'), | ||
wordDiffFake: readFileGo(__dirname + '/wordDiffFake.txt').replace(/^@@.+@@(?:\r?\n|\r)/, ''), | ||
wordDiffReal: readFileGo(__dirname + '/wordDiffReal.txt') | ||
str2: readFileGo(__dirname + '/str2.txt') | ||
}) |
@@ -1,37 +0,78 @@ | ||
// 'use strict' | ||
// | ||
// var imp = require('../_js/testImports') | ||
// | ||
// describe('examples', function() { | ||
// | ||
// var sandbox | ||
// | ||
// beforeEach(function() { | ||
// sandbox = imp.sinon.sandbox.create() | ||
// sandbox.spy(imp.color, 'add') | ||
// }) | ||
// | ||
// afterEach(function() { | ||
// sandbox.restore() | ||
// }) | ||
// | ||
// it('color', function() { | ||
// | ||
// var gitDiff = require('../../index') | ||
// var actual = gitDiff('a\nb\n', 'a\nc\n', true) | ||
// imp.expect(actual).to.equal('a\n-b\n+c\n') | ||
// | ||
// imp.expect(imp.color.add).to.have.been.calledWith(imp.sinon.match.any, 'green') | ||
// imp.expect(imp.color.add).to.have.been.calledWith(imp.sinon.match.any, 'red') | ||
// imp.expect(imp.color.add).to.have.been.calledWith(imp.sinon.match.any, 'grey') | ||
// }) | ||
// | ||
// it('no color', function() { | ||
// | ||
// var gitDiff = require('../../index') | ||
// var actual = gitDiff('a\nb\n', 'a\nc\n') | ||
// imp.expect(actual).to.equal('a\n-b\n+c\n') | ||
// | ||
// imp.expect(imp.color.add).to.have.not.been.called | ||
// }) | ||
// }) | ||
'use strict' | ||
var imp = require('../_js/testImports') | ||
describe('examples', function() { | ||
var sandbox | ||
before(function() { | ||
if (!imp.keepIt.real() && !imp.keepIt.realNoRepo()) this.skip() | ||
}) | ||
beforeEach(function() { | ||
delete require.cache[require.resolve('../../js/_shared/defaultOptions')] | ||
require('../../js/_shared/defaultOptions') | ||
sandbox = imp.sinon.sandbox.create() | ||
sandbox.spy(imp.color, 'add') | ||
}) | ||
afterEach(function() { | ||
sandbox.restore() | ||
}) | ||
it('usage', function() { | ||
var gitDiff = require('../../sync') | ||
var oldStr = 'fred\nis\nfunny\n' | ||
var newStr = 'paul\nis\nfunny\n' | ||
var actual = gitDiff(oldStr, newStr, {color: false}) | ||
imp.expect(actual).to.equal('@@ -1,3 +1,3 @@\n-fred\n+paul\n is\n funny\n') | ||
imp.expect(imp.color.add).to.have.not.been.called | ||
}) | ||
it('flags', function() { | ||
var gitDiff = require('../../sync') | ||
var oldStr = 'fred\n is \nfunny\n' | ||
var newStr = 'paul\nis\n funny \n' | ||
var actual = gitDiff(oldStr, newStr, {color: false, flags: '--diff-algorithm=minimal --ignore-all-space'}) | ||
imp.expect(actual).to.equal('@@ -1,3 +1,3 @@\n-fred\n+paul\n is\n funny \n') | ||
imp.expect(imp.color.add).to.have.not.been.called | ||
}) | ||
it('forceFake', function() { | ||
var gitDiff = require('../../sync') | ||
var oldStr = 'fred\nis\nfunny\n' | ||
var newStr = 'paul\nis\nfunny\n' | ||
var actual = gitDiff(oldStr, newStr, {color: false, forceFake: true}) | ||
imp.expect(actual).to.equal('-fred\n+paul\n is\n funny\n') | ||
imp.expect(imp.color.add).to.have.not.been.called | ||
}) | ||
it('save', function() { | ||
var gitDiff = require('../../sync') | ||
var oldStr = 'fred\nis\nfunny\n' | ||
var newStr = 'paul\nis\nfunny\n' | ||
var actual | ||
actual = gitDiff(oldStr, newStr, {color: false, save: true, wordDiff: true}) | ||
imp.expect(actual).to.equal('@@ -1,3 +1,3 @@\n[-fred-]{+paul+}\nis\nfunny\n') | ||
actual = gitDiff(oldStr, newStr) | ||
imp.expect(actual).to.equal('@@ -1,3 +1,3 @@\n[-fred-]{+paul+}\nis\nfunny\n') | ||
imp.expect(imp.color.add).to.have.not.been.called | ||
}) | ||
it('async', function(done) { | ||
var gitDiff = require('../../async') | ||
var oldStr = 'fred\nis\nfunny\n' | ||
var newStr = 'paul\nis\nfunny\n' | ||
gitDiff(oldStr, newStr, {color: false}).then(function(actual) { | ||
imp.expect(actual).to.equal('@@ -1,3 +1,3 @@\n-fred\n+paul\n is\n funny\n') | ||
imp.expect(imp.color.add).to.have.not.been.called | ||
done() | ||
}) | ||
}) | ||
}) |
'use strict' | ||
var gitDiffAsync = require('../../async') | ||
var imp = require('../_js/testImports') | ||
var gitDiffAsync = require('../../async') | ||
var pkg = require('../../package.json') | ||
@@ -30,9 +30,6 @@ | ||
it('force fake - color', function(done) { | ||
var expected = imp.data.lineDiffFakeVim | ||
gitDiffAsync(str1, str2, {forceFake: true}).then(function(actual) { | ||
imp.expect(actual).to.equal(expected) | ||
imp.expect(actual).to.equal(imp.data.lineDiffFake) | ||
imp.expect(imp.color.add).to.have.been.calledWith(imp.sinon.match.any, 'green') | ||
imp.expect(imp.color.add).to.have.been.calledWith(imp.sinon.match.any, 'red') | ||
imp.expect(imp.color.add).to.have.been.calledWith(imp.sinon.match.any, 'reset') | ||
done() | ||
@@ -47,6 +44,5 @@ }) | ||
it('str1 not a string', function(done) { | ||
gitDiffAsync(9, '').catch(function(err) { | ||
imp.expect(err).to.be.an.instanceof(TypeError) | ||
imp.expect(err.message).to.equal('Both inputs to ' + pkg.name + ' must be strings') | ||
imp.expect(err.message).to.equal(pkg.name + ' requires two strings') | ||
done() | ||
@@ -53,0 +49,0 @@ }) |
@@ -0,3 +1,4 @@ | ||
global.ATAT_REGEX = /^.*@@.+@@.*$/m | ||
global.CR = /(?:\r?\n)/ | ||
global.CRE = /(?:\r?\n)$/ | ||
global.CRS = '(?:\r?\n)' |
10
TODO.md
@@ -1,9 +0,7 @@ | ||
* Breakdown coverage | ||
* TODO | ||
* Test names | ||
* Update README / examples spec file | ||
* Tidy code | ||
* Packages up to date? | ||
* Update README | ||
* Update examples spec file | ||
async |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
131509
47
1216
215
12
1
Updatedshelljs.exec@^1.1.5