Skip to content

Commit 7f90035

Browse files
committed
Breaking: Append path to history in constructor (closes #95)
1 parent bf8a2c9 commit 7f90035

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ function File(file) {
3030
this.contents = file.contents || null;
3131

3232
// Replay path history to ensure proper normalization and trailing sep
33-
var history = file.path ? [file.path] : file.history || [];
33+
var history = Array.prototype.slice.call(file.history || []);
34+
if (file.path) {
35+
history.push(file.path);
36+
}
3437
this.history = [];
3538
history.forEach(function(path) {
3639
self.path = path;

test/File.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,52 @@ describe('File', function() {
182182
]);
183183
}
184184
});
185+
186+
it('appends path to history if both exist and different from last', function(done) {
187+
var p = path.normalize('/test/baz/test.coffee');
188+
var history = [
189+
path.normalize('/test/bar/test.coffee'),
190+
path.normalize('/test/foo/test.coffee'),
191+
];
192+
var file = new File({ path: p, history: history });
193+
194+
var expectedHistory = history.concat(p);
195+
196+
file.path.should.equal(path.normalize('/test/baz/test.coffee'));
197+
file.history.should.eql(expectedHistory);
198+
done();
199+
});
200+
201+
it('does not append path to history if both exist and same as last', function(done) {
202+
var history = [
203+
path.normalize('/test/bar/test.coffee'),
204+
path.normalize('/test/foo/test.coffee'),
205+
path.normalize('/test/baz/test.coffee'),
206+
];
207+
var file = new File({ path: history[history.length - 1], history: history });
208+
209+
file.path.should.equal(path.normalize('/test/baz/test.coffee'));
210+
file.history.should.eql(history);
211+
done();
212+
});
213+
214+
it('does not mutate history array passed in', function(done) {
215+
var p = path.normalize('/test/baz/test.coffee');
216+
var history = [
217+
path.normalize('/test/bar/test.coffee'),
218+
path.normalize('/test/foo/test.coffee'),
219+
];
220+
var historyCopy = Array.prototype.slice.call(history);
221+
var file = new File({ path: p, history: history });
222+
223+
var expectedHistory = history.concat(p);
224+
225+
file.path.should.equal(path.normalize('/test/baz/test.coffee'));
226+
file.history.should.eql(expectedHistory);
227+
history.should.eql(historyCopy);
228+
done();
229+
});
230+
185231
});
186232

187233
describe('isBuffer()', function() {

0 commit comments

Comments
 (0)