git-patch-additions
Version:
Parse additions from git patches
205 lines (169 loc) • 5.88 kB
JavaScript
var m = require('mochainon');
var fs = require('fs');
var path = require('path');
var gitPatchAdditions = require('../lib/git-patch-additions');
var readPatch = function(name) {
var patchesPath = path.join(__dirname, 'patches');
return fs.readFileSync(path.join(patchesPath, name), {
encoding: 'utf8'
});
};
describe('Patch:', function() {
describe('.getAdditions()', function() {
describe('given a single addition in a single file', function() {
beforeEach(function() {
this.patch = readPatch('addition_single_file.patch');
});
it('should return the single addition', function() {
var additions = gitPatchAdditions.get(this.patch);
m.chai.expect(additions).to.deep.equal([
{
line: 2,
content: 'Hola World',
commit: 'e53787fc794ff85986479d85deddfabef0b3489e',
file: 'foo.txt'
}
]);
});
});
describe('given a single addition in the middle of a file', function() {
beforeEach(function() {
this.patch = readPatch('addition_single_file_middle.patch');
});
it('should return the single addition', function() {
var additions = gitPatchAdditions.get(this.patch);
m.chai.expect(additions).to.deep.equal([
{
line: 5,
content: '3. Third Item',
commit: 'e53787fc794ff85986479d85deddfabef0b3489e',
file: 'foo.txt'
}
]);
});
});
describe('given a single file patch without additions', function() {
beforeEach(function() {
this.patch = readPatch('deletion_single_file.patch');
});
it('should return an empty array', function() {
var additions = gitPatchAdditions.get(this.patch);
m.chai.expect(additions).to.deep.equal([]);
});
});
describe('given additions in multiple files', function() {
beforeEach(function() {
this.patch = readPatch('addition_multiple_files.patch');
});
it('should return all additions separated by file', function() {
var additions = gitPatchAdditions.get(this.patch);
m.chai.expect(additions).to.deep.equal([
{
line: 2,
content: 'Lorem ipsum dolor sit amet',
commit: 'e53787fc794ff85986479d85deddfabef0b3489e',
file: 'bar.txt'
},
{
line: 2,
content: 'Hola World',
commit: 'e53787fc794ff85986479d85deddfabef0b3489e',
file: 'foo.txt'
}
]);
});
});
describe('given real patches from checked pull requests', function() {
describe('given f4c9d76eaea972b2006550267d203e5897daf370', function() {
beforeEach(function() {
this.patch = readPatch('f4c9d76eaea972b2006550267d203e5897daf370.patch');
});
it('should parse the additions correctly', function() {
var additions = gitPatchAdditions.get(this.patch);
m.chai.expect(additions).to.deep.equal([
{
content: '1. First item.',
commit: 'f4c9d76eaea972b2006550267d203e5897daf370',
file: 'README.md',
line: 5
},
{
content: 'Lorem ipsum dolor sit amet.',
commit: 'f4c9d76eaea972b2006550267d203e5897daf370',
file: 'README.md',
line: 7
},
{
content: '2. Second item.',
commit: 'f4c9d76eaea972b2006550267d203e5897daf370',
file: 'README.md',
line: 9
},
{
content: 'Lorem ipsum dolor sit amet.',
commit: 'f4c9d76eaea972b2006550267d203e5897daf370',
file: 'README.md',
line: 11
},
{
content: 'The end.',
commit: 'f4c9d76eaea972b2006550267d203e5897daf370',
file: 'README.md',
line: 13
}
]);
});
});
describe('given 45441c22b36edf74df4ed890b363ff451fcf7df9', function() {
beforeEach(function() {
this.patch = readPatch('45441c22b36edf74df4ed890b363ff451fcf7df9.patch');
});
it('should parse the additions correctly', function() {
var additions = gitPatchAdditions.get(this.patch);
m.chai.expect(additions).to.deep.equal([
{
content: '3. Third item.',
commit: '45441c22b36edf74df4ed890b363ff451fcf7df9',
file: 'README.md',
line: 5
},
{
content: 'Foo bar',
commit: '45441c22b36edf74df4ed890b363ff451fcf7df9',
file: 'README.md',
line: 7
}
]);
});
});
describe('given eff03aaa6535bf87296901e3c2cf26b2243be84e', function() {
beforeEach(function() {
this.patch = readPatch('eff03aaa6535bf87296901e3c2cf26b2243be84e.patch');
});
it('should parse the additions correctly', function() {
var additions = gitPatchAdditions.get(this.patch);
m.chai.expect(additions).to.deep.equal([
{
content: '3. Third item.',
commit: '45441c22b36edf74df4ed890b363ff451fcf7df9',
file: 'README.md',
line: 5
},
{
content: 'Foo bar',
commit: '45441c22b36edf74df4ed890b363ff451fcf7df9',
file: 'README.md',
line: 7
},
{
content: 'Foo bar asdf',
commit: 'eff03aaa6535bf87296901e3c2cf26b2243be84e',
file: 'README.md',
line: 5
}
]);
});
});
});
});
});