Skip to content

Commit

Permalink
path get/set for recording path change gulpjs#19
Browse files Browse the repository at this point in the history
  • Loading branch information
popomore committed Jun 28, 2014
1 parent 83bd747 commit ca8a7bd
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
18 changes: 18 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,22 @@ Object.defineProperty(File.prototype, 'relative', {
}
});

Object.defineProperty(File.prototype, 'path', {
get: function() {
return this._path;
},
set: function(path) {
if (!this.history) {
this.history = [];
}

// record history only when path changed
if (path !== this.history[this.history.length - 1]) {
this.history.push(path);
}

this._path = path;
}
});

module.exports = File;
47 changes: 44 additions & 3 deletions test/File.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('File', function() {
done();
});
});

describe('isBuffer()', function() {
it('should return true when the contents are a Buffer', function(done) {
var val = new Buffer("test");
Expand Down Expand Up @@ -382,7 +382,7 @@ describe('File', function() {
process.nextTick(done);
});
});

describe('inspect()', function() {
it('should return correct format when no contents and no path', function(done) {
var file = new File();
Expand Down Expand Up @@ -445,7 +445,7 @@ describe('File', function() {
done();
});
});

describe('contents get/set', function() {
it('should work with Buffer', function(done) {
var val = new Buffer("test");
Expand Down Expand Up @@ -537,4 +537,45 @@ describe('File', function() {
});
});

describe('path get/set', function() {

it('should record history when instantiation', function() {
var file = new File({
cwd: '/',
path: '/test/test.coffee'
});

file.path.should.eql('/test/test.coffee');
file.history.should.eql(['/test/test.coffee']);
});

it('should record history when path change', function() {
var file = new File({
cwd: '/',
path: '/test/test.coffee'
});

file.path = '/test/test.js';
file.path.should.eql('/test/test.js');
file.history.should.eql(['/test/test.coffee', '/test/test.js']);

file.path = '/test/test.coffee';
file.path.should.eql('/test/test.coffee');
file.history.should.eql(['/test/test.coffee', '/test/test.js', '/test/test.coffee']);
});

it('should not record history when set the same path', function() {
var file = new File({
cwd: '/',
path: '/test/test.coffee'
});

file.path = '/test/test.coffee';
file.path = '/test/test.coffee';
file.path.should.eql('/test/test.coffee');
file.history.should.eql(['/test/test.coffee']);
});

});

});

0 comments on commit ca8a7bd

Please sign in to comment.