From 735a86c9054d6eeccd285cbc1200cb641a19b52f Mon Sep 17 00:00:00 2001 From: Marcelo Shima Date: Mon, 17 May 2021 20:08:43 -0300 Subject: [PATCH] Implement getStateSnapshot (#153) --- lib/run-result.js | 13 +++++++++++++ test/run-result.js | 37 ++++++++++++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/lib/run-result.js b/lib/run-result.js index dcf0e8d..e815742 100644 --- a/lib/run-result.js +++ b/lib/run-result.js @@ -62,6 +62,19 @@ class RunResult { return this.fs.dump(this.cwd, filter); } + /** + * Return an object with filenames with state. + * @param {Function} filter - parameter forwarded to mem-fs-editor#dump + * @returns {Object} + */ + getStateSnapshot(filter) { + const snapshot = this.getSnapshot(filter); + Object.values(snapshot).forEach((dump) => { + delete dump.contents; + }); + return snapshot; + } + /** * Prints files names and contents from mem-fs * @param {...string} files - Files to print or empty for entire mem-fs diff --git a/test/run-result.js b/test/run-result.js index f044cb4..115d5f7 100644 --- a/test/run-result.js +++ b/test/run-result.js @@ -129,9 +129,40 @@ describe('run-result', () => { runResult.fs.write(path.resolve('test2.txt'), 'test2 content'); }); it('should return every changed file', () => { - assert.equal(Object.keys(runResult.getSnapshot()).length, 2); - assert(runResult.getSnapshot()['test.txt']); - assert(runResult.getSnapshot()['test2.txt']); + assert.deepEqual(runResult.getSnapshot(), { + 'test.txt': { + contents: 'test content', + state: 'modified' + }, + 'test2.txt': { + contents: 'test2 content', + state: 'modified' + } + }); + }); + }); + describe('#getSnapshotState', () => { + let runResult; + beforeEach(() => { + const memFs = MemFs.create(); + const memFsEditor = MemFsEditor.create(memFs); + runResult = new RunResult({ + memFs, + fs: memFsEditor, + cwd: process.cwd() + }); + runResult.fs.write(path.resolve('test.txt'), 'test content'); + runResult.fs.write(path.resolve('test2.txt'), 'test2 content'); + }); + it('should return every changed file', () => { + assert.deepEqual(runResult.getStateSnapshot(), { + 'test.txt': { + state: 'modified' + }, + 'test2.txt': { + state: 'modified' + } + }); }); }); describe('#cleanup', () => {