Skip to content

Commit

Permalink
Implement dumpFiles and dumpFilenames.
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Jul 22, 2020
1 parent cbf83b5 commit eeb3dd8
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
24 changes: 24 additions & 0 deletions lib/run-result.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,30 @@ module.exports = class RunResult {
);
}

dumpFiles(...files) {
if (files.length === 0) {
this.env.sharedFs.each((file) => {
console.log(file.path);
if (file.contents) {
console.log(file.contents.toString('utf8'));
}
});
return this;
}

files.forEach((file) => {
console.log(this.fs.read(this._fileName(file)));
});
return this;
}

dumpFilenames() {
this.env.sharedFs.each((file) => {
console.log(file.path);
});
return this;
}

_fileName(filename) {
if (path.isAbsolute(filename)) {
return filename;
Expand Down
66 changes: 65 additions & 1 deletion test/run-result.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/* eslint-disable max-nested-callbacks */
'use strict';
const assert = require('assert');
const MemFs = require('mem-fs');
const MemFsEditor = require('mem-fs-editor');
const path = require('path');
const sinon = require('sinon');

const RunContext = require('../lib/run-context');
const RunResult = require('../lib/run-result');
Expand All @@ -26,7 +30,7 @@ describe('run-result', () => {
runResult = new RunResult(options);
});
it('loads fs option', () => {
assert.equal(runResult.cwd, cwd);
assert.equal(runResult.fs, fs);
});
it('loads cwd option', () => {
assert.equal(runResult.cwd, cwd);
Expand All @@ -50,6 +54,66 @@ describe('run-result', () => {
});
});
});
describe('#dumpFiles', () => {
let runResult;
let consoleMock;
beforeEach(() => {
const memFs = MemFs.create();
const memFsEditor = MemFsEditor.create(memFs);
runResult = new RunResult({
fs: memFsEditor,
cwd: process.cwd(),
env: {sharedFs: memFs}
});
consoleMock = sinon.stub(console, 'log');
runResult.fs.write(path.resolve('test.txt'), 'test content');
runResult.fs.write(path.resolve('test2.txt'), 'test2 content');
});
afterEach(() => {
consoleMock.restore();
});
it('dumps every file without an argument', () => {
runResult.dumpFiles();
assert.equal(consoleMock.callCount, 4);
assert.equal(consoleMock.getCall(0).args[0], path.resolve('test.txt'));
assert.equal(consoleMock.getCall(1).args[0], 'test content');
assert.equal(consoleMock.getCall(2).args[0], path.resolve('test2.txt'));
assert.equal(consoleMock.getCall(3).args[0], 'test2 content');
});
it('dumps a file with an argument', () => {
runResult.dumpFiles(path.resolve('test.txt'));
assert.equal(consoleMock.callCount, 1);
assert.equal(consoleMock.getCall(0).args[0], 'test content');
runResult.dumpFiles(path.resolve('test2.txt'));
assert.equal(consoleMock.callCount, 2);
assert.equal(consoleMock.getCall(1).args[0], 'test2 content');
});
});
describe('#dumpFilenames', () => {
let runResult;
let consoleMock;
beforeEach(() => {
const memFs = MemFs.create();
const memFsEditor = MemFsEditor.create(memFs);
runResult = new RunResult({
fs: memFsEditor,
cwd: process.cwd(),
env: {sharedFs: memFs}
});
consoleMock = sinon.stub(console, 'log');
runResult.fs.write(path.resolve('test.txt'), 'test content');
runResult.fs.write(path.resolve('test2.txt'), 'test2 content');
});
afterEach(() => {
consoleMock.restore();
});
it('dumps every filename', () => {
runResult.dumpFilenames();
assert.equal(consoleMock.callCount, 2);
assert.equal(consoleMock.getCall(0).args[0], path.resolve('test.txt'));
assert.equal(consoleMock.getCall(1).args[0], path.resolve('test2.txt'));
});
});
describe('#createContext', () => {
const newSettings = {newOnly: 'foo', overrided: 'newOverrided'};
const newEnvOptions = {newOnlyEnv: 'bar', overridedEnv: 'newOverridedEnv'};
Expand Down

0 comments on commit eeb3dd8

Please sign in to comment.