Skip to content

Commit

Permalink
Only add to mem-fs when the file has changed. (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima authored Aug 25, 2021
1 parent 4d21170 commit 5a8285a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
17 changes: 16 additions & 1 deletion __tests__/commit-file-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const filesystem = require('fs');
const os = require('os');
const path = require('path');
const memFs = require('mem-fs');
const sinon = require('sinon');
const editor = require('..');

const rmSync = filesystem.rmSync || filesystem.rmdirSync;
Expand All @@ -12,15 +13,19 @@ describe('#commit()', () => {
const outputRoot = path.join(os.tmpdir(), 'mem-fs-editor-test');
const outputDir = path.join(outputRoot, 'output');
const filename = path.join(outputDir, 'file.txt');
const filenameNew = path.join(outputDir, 'file-new.txt');

let store;
let fs;

beforeEach(() => {
store = memFs.create();
fs = editor.create(store);
sinon.spy(store, 'add');

fs = editor.create(store);
fs.write(filename, 'foo');

expect(store.add.callCount).toEqual(1);
});

afterEach(() => {
Expand All @@ -31,4 +36,14 @@ describe('#commit()', () => {
await fs.commitFileAsync(store.get(filename));
expect(filesystem.readFileSync(filename).toString()).toEqual('foo');
});

it('adds non existing file to store', async () => {
await fs.commitFileAsync({path: filenameNew, contents: Buffer.from('bar'), state: 'modified'});
expect(store.add.callCount).toEqual(2);
});

it('doesn\'t readd same file to store', async () => {
await fs.commitFileAsync(store.get(filename));
expect(store.add.callCount).toEqual(1);
});
});
15 changes: 15 additions & 0 deletions __tests__/write.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const path = require('path');
const sinon = require('sinon');
const editor = require('..');
const memFs = require('mem-fs');

Expand All @@ -10,6 +11,8 @@ describe('#write()', () => {

beforeEach(() => {
store = memFs.create();
sinon.spy(store, 'add');

fs = editor.create(store);
});

Expand All @@ -36,4 +39,16 @@ describe('#write()', () => {
expect(fs.read(filepath)).toBe(contents);
expect(fs.store.get(filepath).state).toBe('modified');
});

it('doesn\'t re-add an identical file that already exist in memory', () => {
const filepath = path.join(__dirname, 'fixtures/file-a.txt');
const contents = 'some text';
fs.write(filepath, contents);
expect(store.add.callCount).toBe(1);
expect(fs.read(filepath)).toBe(contents);
expect(fs.store.get(filepath).state).toBe('modified');

fs.write(filepath, contents);
expect(store.add.callCount).toBe(1);
});
});
6 changes: 5 additions & 1 deletion lib/actions/commit-file-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ async function remove(file) {
}

module.exports = async function (file) {
this.store.add(file);
const existingFile = this.store.get(file.path);
if (!existingFile || existingFile !== file) {
this.store.add(file);
}

if (isFileStateModified(file)) {
setCommittedFile(file);
await write(file);
Expand Down
18 changes: 13 additions & 5 deletions lib/actions/write.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const assert = require('assert');
const {setModifiedFileState} = require('../state');
const {isFileStateModified, setModifiedFileState} = require('../state');

module.exports = function (filepath, contents, stat) {
assert(
Expand All @@ -10,10 +10,18 @@ module.exports = function (filepath, contents, stat) {
);

const file = this.store.get(filepath);
setModifiedFileState(file);
file.contents = Buffer.isBuffer(contents) ? contents : Buffer.from(contents);
file.stat = stat;
this.store.add(file);
const newContents = Buffer.isBuffer(contents) ? contents : Buffer.from(contents);
if (
!isFileStateModified(file)
|| !Buffer.isBuffer(file.contents)
|| !newContents.equals(file.contents)
|| (stat !== undefined && file.stat !== stat)
) {
setModifiedFileState(file);
file.contents = newContents;
file.stat = stat;
this.store.add(file);
}

return file.contents.toString();
};

0 comments on commit 5a8285a

Please sign in to comment.