mirror of
https://github.com/status-im/react-native.git
synced 2025-01-14 03:26:07 +00:00
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;
|
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.mkdir.mockImplementation(asyncify(fs.mkdirSync));
|
||||||
|
|
||||||
fs.mkdirSync.mockImplementation((dirPath, mode) => {
|
fs.mkdirSync.mockImplementation((dirPath, mode) => {
|
||||||
@ -332,26 +359,34 @@ fs.createReadStream.mockImplementation(filepath => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
fs.createWriteStream.mockImplementation(file => {
|
fs.createWriteStream.mockImplementation(filePath => {
|
||||||
let node;
|
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 {
|
try {
|
||||||
node = getToNode(dirname(file));
|
const dirPath = dirname(filePath);
|
||||||
} finally {
|
node = getToNode(dirPath);
|
||||||
if (typeof node === 'object') {
|
if (!isDirNode(node)) {
|
||||||
const writeStream = new stream.Writable({
|
throw fsError('ENOTDIR', 'not a directory: ' + dirPath);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
// Truncate the file on opening.
|
||||||
|
node[path.basename(filePath)] = '';
|
||||||
|
} catch (error) {
|
||||||
|
process.nextTick(() => writeStream.emit('error', error));
|
||||||
}
|
}
|
||||||
|
return writeStream;
|
||||||
});
|
});
|
||||||
fs.createWriteStream.mock.returned = [];
|
fs.createWriteStream.mock.returned = [];
|
||||||
|
|
||||||
|
@ -24,6 +24,10 @@ jest.mock('fs');
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
|
||||||
describe('fs mock', () => {
|
describe('fs mock', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
(fs: $FlowFixMe).mock.clear();
|
||||||
|
});
|
||||||
|
|
||||||
describe('writeFileSync()', () => {
|
describe('writeFileSync()', () => {
|
||||||
it('stores content correctly', () => {
|
it('stores content correctly', () => {
|
||||||
fs.writeFileSync('/test', 'foobar', 'utf8');
|
fs.writeFileSync('/test', 'foobar', 'utf8');
|
||||||
@ -55,4 +59,28 @@ describe('fs mock', () => {
|
|||||||
expect(content).toEqual('foobar');
|
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…
x
Reference in New Issue
Block a user