packager-worker-for-buck: refactor and fix source map output
Reviewed By: davidaurelio Differential Revision: D6385199 fbshipit-source-id: f104f7b000dde131b57b671d14d4ec4e0d30d7a2
This commit is contained in:
parent
b9e7006cc6
commit
7fd5aa84a1
|
@ -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) => {
|
||||
|
@ -332,26 +359,34 @@ fs.createReadStream.mockImplementation(filepath => {
|
|||
});
|
||||
});
|
||||
|
||||
fs.createWriteStream.mockImplementation(file => {
|
||||
fs.createWriteStream.mockImplementation(filePath => {
|
||||
let node;
|
||||
try {
|
||||
node = getToNode(dirname(file));
|
||||
} finally {
|
||||
if (typeof node === 'object') {
|
||||
const writeStream = new stream.Writable({
|
||||
write(chunk) {
|
||||
write(chunk, encoding, callback) {
|
||||
this.__chunks.push(chunk);
|
||||
callback();
|
||||
},
|
||||
final(callback) {
|
||||
node[path.basename(filePath)] = this.__chunks.join('');
|
||||
callback();
|
||||
},
|
||||
});
|
||||
writeStream.__file = file;
|
||||
writeStream.__file = filePath;
|
||||
writeStream.__chunks = [];
|
||||
writeStream.end = jest.fn(writeStream.end);
|
||||
fs.createWriteStream.mock.returned.push(writeStream);
|
||||
try {
|
||||
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;
|
||||
} else {
|
||||
throw new Error('Cannot open file ' + file);
|
||||
}
|
||||
}
|
||||
});
|
||||
fs.createWriteStream.mock.returned = [];
|
||||
|
||||
|
|
|
@ -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');
|
||||
|
@ -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!');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue