Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: checks existing file creation time #40

Merged
merged 1 commit into from
May 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/RollingFileWriteStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,22 @@ class RollingFileWriteStream extends Writable {
};

if (this.options.flags === 'a') {
this.state.currentSize = this._getExistingSize();
this._setExistingSizeAndDate();
}

debug(`create new file with no hot file. name=${this.justTheFile}, state=${JSON.stringify(this.state)}`);
this._renewWriteStream();

}

_getExistingSize() {
_setExistingSizeAndDate() {
try {
return fs.statSync(this.filename).size;
const stats = fs.statSync(this.filename);
this.state.currentSize = stats.size;
this.state.currentDate = stats.birthtime;
} catch (e) {
//file does not exist, that's fine - move along
return 0;
return;
}
}

Expand Down
32 changes: 31 additions & 1 deletion test/RollingFileWriteStream-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const mockFs = require('fs-extra');
const oldStatSync = mockFs.statSync
mockFs.statSync = fd => {
const result = oldStatSync(fd);
result.birthtime = fakedFsDate.valueOf();
result.birthtime = fakedFsDate;
return result;
}

Expand Down Expand Up @@ -730,6 +730,36 @@ describe('RollingFileWriteStream', () => {
});
});

describe('when old files exist with contents and rolling by date', () => {
const fileObj = generateTestFile();
let s;

before(done => {
fakeNow = new Date(2012, 8, 12, 10, 37, 11);
fs.ensureFileSync(fileObj.path);
fs.writeFileSync(fileObj.path, 'This was created Sept 12, 2012.\n');
fakeNow = new Date(2012, 8, 13, 10, 53, 12);
s = new RollingFileWriteStream(fileObj.path, { pattern: 'yyyy-MM-dd' });
s.write('It is now Sept 13, 2012.\n', 'utf8', done); // this should be in a new file.
});

after(() => {
s.end();
fs.removeSync(fileObj.dir);
});

it('should respect the existing file date', () => {
const files = fs.readdirSync(fileObj.dir);
const expectedFileList = [fileObj.base, fileObj.base + '.2012-09-12'];
files.length.should.equal(expectedFileList.length);
files.should.containDeep(expectedFileList);

fs.readFileSync(path.format(fileObj)).toString().should.equal('It is now Sept 13, 2012.\n');
fs.readFileSync(path.join(fileObj.dir, fileObj.base + '.2012-09-12')).toString().should.equal('This was created Sept 12, 2012.\n');

});
});

describe('when old files exist with contents and stream created with overwrite flag', () => {
const fileObj = generateTestFile();
let s;
Expand Down