Skip to content

Commit

Permalink
packager-worker-for-buck: refactor and fix source map output
Browse files Browse the repository at this point in the history
Reviewed By: davidaurelio

Differential Revision: D6385199

fbshipit-source-id: f104f7b000dde131b57b671d14d4ec4e0d30d7a2
  • Loading branch information
Jean Lauliac authored and facebook-github-bot committed Nov 22, 2017
1 parent b9e7006 commit 7fd5aa8
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 16 deletions.
67 changes: 51 additions & 16 deletions local-cli/__mocks__/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,33 @@ fs.writeFileSync.mockImplementation((filePath, content, options) => {
node[path.basename(filePath)] = content;
});

const openFds = new Map();
let nextFd = 3;

fs.openSync.mockImplementation((filePath, flags) => {
const dirPath = path.dirname(filePath);
const node = getToNode(dirPath);
if (!isDirNode(node)) {
throw fsError('ENOTDIR', 'not a directory: ' + dirPath);
}
node[path.basename(filePath)] = '';
openFds.set(nextFd, {filePath, flags, node});
return nextFd++;
});

fs.writeSync.mockImplementation((fd, str) => {
invariant(typeof str === 'string', 'only strings supported');
const data = openFds.get(fd);
if (data == null || data.flags !== 'w') {
throw fsError('EBADF', 'bad file descriptor, write');
}
data.node[path.basename(data.filePath)] += str;
});

fs.closeSync.mockImplementation(fd => {
openFds.delete(fd);
});

fs.mkdir.mockImplementation(asyncify(fs.mkdirSync));

fs.mkdirSync.mockImplementation((dirPath, mode) => {
Expand Down Expand Up @@ -332,26 +359,34 @@ fs.createReadStream.mockImplementation(filepath => {
});
});

fs.createWriteStream.mockImplementation(file => {
fs.createWriteStream.mockImplementation(filePath => {
let node;
const writeStream = new stream.Writable({
write(chunk, encoding, callback) {
this.__chunks.push(chunk);
callback();
},
final(callback) {
node[path.basename(filePath)] = this.__chunks.join('');
callback();
},
});
writeStream.__file = filePath;
writeStream.__chunks = [];
writeStream.end = jest.fn(writeStream.end);
fs.createWriteStream.mock.returned.push(writeStream);
try {
node = getToNode(dirname(file));
} finally {
if (typeof node === 'object') {
const writeStream = new stream.Writable({
write(chunk) {
this.__chunks.push(chunk);
},
});
writeStream.__file = file;
writeStream.__chunks = [];
writeStream.end = jest.fn(writeStream.end);
fs.createWriteStream.mock.returned.push(writeStream);
return writeStream;
} else {
throw new Error('Cannot open file ' + file);
const dirPath = dirname(filePath);
node = getToNode(dirPath);
if (!isDirNode(node)) {
throw fsError('ENOTDIR', 'not a directory: ' + dirPath);
}
// Truncate the file on opening.
node[path.basename(filePath)] = '';
} catch (error) {
process.nextTick(() => writeStream.emit('error', error));
}
return writeStream;
});
fs.createWriteStream.mock.returned = [];

Expand Down
28 changes: 28 additions & 0 deletions local-cli/__tests__/fs-mock-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ jest.mock('fs');
const fs = require('fs');

describe('fs mock', () => {
beforeEach(() => {
(fs: $FlowFixMe).mock.clear();
});

describe('writeFileSync()', () => {
it('stores content correctly', () => {
fs.writeFileSync('/test', 'foobar', 'utf8');
Expand Down Expand Up @@ -55,4 +59,28 @@ describe('fs mock', () => {
expect(content).toEqual('foobar');
});
});

describe('createWriteStream()', () => {
it('writes content', done => {
const stream = fs.createWriteStream('/test');
stream.write('hello, ');
stream.write('world');
stream.end('!');
process.nextTick(() => {
const content = fs.readFileSync('/test', 'utf8');
expect(content).toEqual('hello, world!');
done();
});
});
});

describe('writeSync()', () => {
it('writes content', () => {
const fd = fs.openSync('/test', 'w');
fs.writeSync(fd, 'hello, world!');
fs.closeSync(fd);
const content = fs.readFileSync('/test', 'utf8');
expect(content).toEqual('hello, world!');
});
});
});

0 comments on commit 7fd5aa8

Please sign in to comment.