Skip to content

Commit

Permalink
Breaking: Append path to history in constructor (closes #95)
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Sep 27, 2016
1 parent 5b61311 commit 5cfeea3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ function File(file) {
this.contents = file.contents || null;

// Replay path history to ensure proper normalization and trailing sep
var history = file.path ? [file.path] : file.history || [];
var history = Array.prototype.slice.call(file.history || []);
if (file.path) {
history.push(file.path);
}
this.history = [];
history.forEach(function(path) {
self.path = path;
Expand Down
46 changes: 46 additions & 0 deletions test/File.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,52 @@ describe('File', function() {
]);
}
});

it('appends path to history if both exist and different from last', function(done) {
var p = path.normalize('/test/baz/test.coffee');
var history = [
path.normalize('/test/bar/test.coffee'),
path.normalize('/test/foo/test.coffee'),
];
var file = new File({ path: p, history: history });

var expectedHistory = history.concat(p);

file.path.should.equal(path.normalize('/test/baz/test.coffee'));
file.history.should.eql(expectedHistory);
done();
});

it('does not append path to history if both exist and same as last', function(done) {
var history = [
path.normalize('/test/bar/test.coffee'),
path.normalize('/test/foo/test.coffee'),
path.normalize('/test/baz/test.coffee'),
];
var file = new File({ path: history[history.length - 1], history: history });

file.path.should.equal(path.normalize('/test/baz/test.coffee'));
file.history.should.eql(history);
done();
});

it('does not mutate history array passed in', function(done) {
var p = path.normalize('/test/baz/test.coffee');
var history = [
path.normalize('/test/bar/test.coffee'),
path.normalize('/test/foo/test.coffee'),
];
var historyCopy = Array.prototype.slice.call(history);
var file = new File({ path: p, history: history });

var expectedHistory = history.concat(p);

file.path.should.equal(path.normalize('/test/baz/test.coffee'));
file.history.should.eql(expectedHistory);
history.should.eql(historyCopy);
done();
});

});

describe('isBuffer()', function() {
Expand Down

0 comments on commit 5cfeea3

Please sign in to comment.