diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-09-05 11:04:18 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-09-05 11:04:18 +0200 |
commit | a14ca3e268e95a7eab59fb205b41da7331d57631 (patch) | |
tree | 9c84b2cbd561345335fca3e26af961b2ea23d8ec /lib/fs | |
parent | 9c071dade573aa6990878006f83c89b6065a1395 (diff) | |
download | gitbook-a14ca3e268e95a7eab59fb205b41da7331d57631.zip gitbook-a14ca3e268e95a7eab59fb205b41da7331d57631.tar.gz gitbook-a14ca3e268e95a7eab59fb205b41da7331d57631.tar.bz2 |
Switch to lerna
Diffstat (limited to 'lib/fs')
-rw-r--r-- | lib/fs/__tests__/mock.js | 82 | ||||
-rw-r--r-- | lib/fs/mock.js | 95 | ||||
-rw-r--r-- | lib/fs/node.js | 42 |
3 files changed, 0 insertions, 219 deletions
diff --git a/lib/fs/__tests__/mock.js b/lib/fs/__tests__/mock.js deleted file mode 100644 index 04bd46a..0000000 --- a/lib/fs/__tests__/mock.js +++ /dev/null @@ -1,82 +0,0 @@ -var createMockFS = require('../mock'); - -describe('MockFS', function() { - var fs = createMockFS({ - 'README.md': 'Hello World', - 'SUMMARY.md': '# Summary', - 'folder': { - 'test.md': 'Cool', - 'folder2': { - 'hello.md': 'Hello', - 'world.md': 'World' - } - } - }); - - describe('exists', function() { - it('must return true for a file', function() { - return fs.exists('README.md') - .then(function(result) { - expect(result).toBeTruthy(); - }); - }); - - it('must return false for a non existing file', function() { - return fs.exists('README_NOTEXISTS.md') - .then(function(result) { - expect(result).toBeFalsy(); - }); - }); - - it('must return true for a directory', function() { - return fs.exists('folder') - .then(function(result) { - expect(result).toBeTruthy(); - }); - }); - - it('must return true for a deep file', function() { - return fs.exists('folder/test.md') - .then(function(result) { - expect(result).toBeTruthy(); - }); - }); - - it('must return true for a deep file (2)', function() { - return fs.exists('folder/folder2/hello.md') - .then(function(result) { - expect(result).toBeTruthy(); - }); - }); - }); - - describe('readAsString', function() { - it('must return content for a file', function() { - return fs.readAsString('README.md') - .then(function(result) { - expect(result).toBe('Hello World'); - }); - }); - - it('must return content for a deep file', function() { - return fs.readAsString('folder/test.md') - .then(function(result) { - expect(result).toBe('Cool'); - }); - }); - }); - - describe('readDir', function() { - it('must return content for a directory', function() { - return fs.readDir('./') - .then(function(files) { - expect(files.size).toBe(3); - expect(files.includes('README.md')).toBeTruthy(); - expect(files.includes('SUMMARY.md')).toBeTruthy(); - expect(files.includes('folder/')).toBeTruthy(); - }); - }); - }); -}); - - diff --git a/lib/fs/mock.js b/lib/fs/mock.js deleted file mode 100644 index 784c533..0000000 --- a/lib/fs/mock.js +++ /dev/null @@ -1,95 +0,0 @@ -var path = require('path'); -var is = require('is'); -var Buffer = require('buffer').Buffer; -var Immutable = require('immutable'); - -var FS = require('../models/fs'); -var error = require('../utils/error'); - -/** - Create a fake filesystem for unit testing GitBook. - - @param {Map<String:String|Map>} -*/ -function createMockFS(files) { - files = Immutable.fromJS(files); - var mtime = new Date(); - - function getFile(filePath) { - var parts = path.normalize(filePath).split(path.sep); - return parts.reduce(function(list, part, i) { - if (!list) return null; - - var file; - - if (!part || part === '.') file = list; - else file = list.get(part); - - if (!file) return null; - - if (is.string(file)) { - if (i === (parts.length - 1)) return file; - else return null; - } - - return file; - }, files); - } - - function fsExists(filePath) { - return Boolean(getFile(filePath) !== null); - } - - function fsReadFile(filePath) { - var file = getFile(filePath); - if (!is.string(file)) { - throw error.FileNotFoundError({ - filename: filePath - }); - } - - return new Buffer(file, 'utf8'); - } - - function fsStatFile(filePath) { - var file = getFile(filePath); - if (!file) { - throw error.FileNotFoundError({ - filename: filePath - }); - } - - return { - mtime: mtime - }; - } - - function fsReadDir(filePath) { - var dir = getFile(filePath); - if (!dir || is.string(dir)) { - throw error.FileNotFoundError({ - filename: filePath - }); - } - - return dir - .map(function(content, name) { - if (!is.string(content)) { - name = name + '/'; - } - - return name; - }) - .valueSeq(); - } - - return FS.create({ - root: '', - fsExists: fsExists, - fsReadFile: fsReadFile, - fsStatFile: fsStatFile, - fsReadDir: fsReadDir - }); -} - -module.exports = createMockFS; diff --git a/lib/fs/node.js b/lib/fs/node.js deleted file mode 100644 index dfe9fae..0000000 --- a/lib/fs/node.js +++ /dev/null @@ -1,42 +0,0 @@ -var path = require('path'); -var Immutable = require('immutable'); -var fresh = require('fresh-require'); - -var fs = require('../utils/fs'); -var FS = require('../models/fs'); - -function fsReadDir(folder) { - return fs.readdir(folder) - .then(function(files) { - files = Immutable.List(files); - - return files - .map(function(file) { - if (file == '.' || file == '..') return; - - var stat = fs.statSync(path.join(folder, file)); - if (stat.isDirectory()) file = file + path.sep; - return file; - }) - .filter(function(file) { - return Boolean(file); - }); - }); -} - -function fsLoadObject(filename) { - return fresh(filename, require); -} - -module.exports = function createNodeFS(root) { - return FS.create({ - root: root, - - fsExists: fs.exists, - fsReadFile: fs.readFile, - fsStatFile: fs.stat, - fsReadDir: fsReadDir, - fsLoadObject: fsLoadObject, - fsReadAsStream: fs.readStream - }); -}; |