From ecec4319c4fda9bebc90216d5340442b2a5725df Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Mon, 11 Dec 2017 11:08:29 -0800 Subject: [PATCH] metro-buck: add e2e testing of command Reviewed By: davidaurelio Differential Revision: D6533842 fbshipit-source-id: b641559eb3085bac57ab3a1cc80a3f2f86b7ec92 --- local-cli/__mocks__/fs.js | 28 ++++------------------------ local-cli/__tests__/fs-mock-test.js | 11 +++++++++++ 2 files changed, 15 insertions(+), 24 deletions(-) diff --git a/local-cli/__mocks__/fs.js b/local-cli/__mocks__/fs.js index 9e5f6116d..9726dbe38 100644 --- a/local-cli/__mocks__/fs.js +++ b/local-cli/__mocks__/fs.js @@ -66,29 +66,7 @@ fs.readdir.mockImplementation((filepath, callback) => { return callback(null, Object.keys(node)); }); -fs.readFile.mockImplementation(function(filepath, encoding, callback) { - filepath = path.normalize(filepath); - callback = asyncCallback(callback); - if (arguments.length === 2) { - callback = encoding; - encoding = null; - } - - let node; - try { - node = getToNode(filepath); - if (isDirNode(node)) { - callback(new Error('Error readFile a dir: ' + filepath)); - } - if (node == null) { - return callback(Error('No such file: ' + filepath)); - } else { - return callback(null, node); - } - } catch (e) { - return callback(e); - } -}); +fs.readFile.mockImplementation(asyncify(fs.readFileSync)); fs.readFileSync.mockImplementation(function(filepath, encoding) { filepath = path.normalize(filepath); @@ -156,7 +134,9 @@ fs.mkdirSync.mockImplementation((dirPath, mode) => { if (!isDirNode(node)) { throw fsError('ENOTDIR', 'not a directory: ' + parentPath); } - node[path.basename(dirPath)] = {}; + if (node[path.basename(dirPath)] == null) { + node[path.basename(dirPath)] = {}; + } }); function fsError(code, message) { diff --git a/local-cli/__tests__/fs-mock-test.js b/local-cli/__tests__/fs-mock-test.js index 7e9bf07f7..4852106c6 100644 --- a/local-cli/__tests__/fs-mock-test.js +++ b/local-cli/__tests__/fs-mock-test.js @@ -64,6 +64,17 @@ describe('fs mock', () => { * comment and run Flow. */ expect(content).toEqual('foobar'); }); + + it('does not erase directories', () => { + fs.mkdirSync('/dir', 0o777); + fs.writeFileSync('/dir/test', 'foobar', 'utf8'); + fs.mkdirSync('/dir', 0o777); + const content = fs.readFileSync('/dir/test', 'utf8'); + /* $FlowFixMe(>=0.56.0 site=react_native_oss) This comment suppresses an + * error found when Flow v0.56 was deployed. To see the error delete this + * comment and run Flow. */ + expect(content).toEqual('foobar'); + }); }); describe('createWriteStream()', () => {