diff --git a/package.json b/package.json index d1ed02e..5b13f19 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "lint": "eslint src tests", "lint:fix": "eslint --fix src tests", "test:manual": "parcel tests/index.html --out-dir tests/dist", + "test:migrations": "mocha tests/filesystems/migrations", "pretest": "npm run lint", "test": "npm run karma-mocha", "prebuild": "parcel build --global Filer src/index.js --no-minify --out-file filer.js", diff --git a/tests/filesystems/images/README.md b/tests/filesystems/images/README.md new file mode 100644 index 0000000..93c35b8 --- /dev/null +++ b/tests/filesystems/images/README.md @@ -0,0 +1,7 @@ +The following images are JSON filesystem images created with +`tools/fs-image.js`, and named with the filesystem dir used +as the source of the image, and the version of Filer used to +generate them: + +1. `tiny-fs.0.43.json` was created from `tests/filesystem/tiny-fs/` with https://github.com/filerjs/filer/blob/d66114e20c7f0235698d9933dbd90217ba86fa4e/dist/filer.min.js + diff --git a/tests/filesystems/images/tiny-fs.0.43.json b/tests/filesystems/images/tiny-fs.0.43.json new file mode 100644 index 0000000..62f34bd --- /dev/null +++ b/tests/filesystems/images/tiny-fs.0.43.json @@ -0,0 +1 @@ +{"00000000-0000-0000-0000-000000000000":{"id":"00000000-0000-0000-0000-000000000000","mode":"META","atime":1545098425757,"ctime":1545098425757,"mtime":1545098425757,"rnode":"A63180BA-9BCE-4245-86E8-8613726587CA"},"A63180BA-9BCE-4245-86E8-8613726587CA":{"id":"A63180BA-9BCE-4245-86E8-8613726587CA","mode":"DIRECTORY","size":0,"atime":1545098425770,"ctime":1545098425770,"mtime":1545098425770,"flags":[],"xattrs":{},"nlinks":1,"version":0,"nblocks":1,"data":"3A41160B-1F39-4162-85BD-473F16ABF35F"},"3A41160B-1F39-4162-85BD-473F16ABF35F":{"dir":{"id":"E1634C88-337D-4D1E-9155-81B099BC7E7C","type":"DIRECTORY"},"file.txt":{"id":"DD338D5C-34FF-4AF7-AFBB-C01E8214125F","type":"FILE"}},"E1634C88-337D-4D1E-9155-81B099BC7E7C":{"id":"E1634C88-337D-4D1E-9155-81B099BC7E7C","mode":"DIRECTORY","size":0,"atime":1545098425778,"ctime":1545098425778,"mtime":1545098425778,"flags":[],"xattrs":{},"nlinks":1,"version":0,"nblocks":1,"data":"C8BB5894-9B0C-4CDC-B014-3944FB40DA77"},"C8BB5894-9B0C-4CDC-B014-3944FB40DA77":{"file2.txt":{"id":"EEB56FEC-DABE-48BF-88A5-B9F37723A0BC","type":"FILE"}},"DD338D5C-34FF-4AF7-AFBB-C01E8214125F":{"id":"DD338D5C-34FF-4AF7-AFBB-C01E8214125F","mode":"FILE","size":16,"atime":1545098327877.7542,"ctime":1545098313562.6824,"mtime":1545098313562.6824,"flags":[],"xattrs":{},"nlinks":1,"version":1,"nblocks":1,"data":"BDEE2339-6992-4D8F-AFAA-81813C30592C"},"BDEE2339-6992-4D8F-AFAA-81813C30592C":{"type":"Buffer","data":[84,104,105,115,32,105,115,32,97,32,102,105,108,101,46,10]},"EEB56FEC-DABE-48BF-88A5-B9F37723A0BC":{"id":"EEB56FEC-DABE-48BF-88A5-B9F37723A0BC","mode":"FILE","size":23,"atime":1545098327877.7542,"ctime":1545098313562.6824,"mtime":1545098313562.6824,"flags":[],"xattrs":{},"nlinks":1,"version":1,"nblocks":1,"data":"62262B53-32BC-43F6-983B-4127302C2483"},"62262B53-32BC-43F6-983B-4127302C2483":{"type":"Buffer","data":[84,104,105,115,32,105,115,32,97,32,115,101,99,111,110,100,32,102,105,108,101,46,10]}} \ No newline at end of file diff --git a/tests/filesystems/migrations/from-0.43.test.js b/tests/filesystems/migrations/from-0.43.test.js new file mode 100644 index 0000000..6baadd0 --- /dev/null +++ b/tests/filesystems/migrations/from-0.43.test.js @@ -0,0 +1,92 @@ +const expect = require('chai').expect; +const Filer = require('../../../src'); +const SerializableMemoryProvider = require('../../lib/serializable-memory-provider'); +const nodeFs = require('fs'); +const nodePath = require('path'); + +describe('Migration tests from Filer 0.43 to current', () => { + + let filerFs; + + before(done => { + // Let the provider parse the JSON + const imagePath = nodePath.resolve(__dirname, '../images/tiny-fs.0.43.json'); + nodeFs.readFile(imagePath, 'utf8', (err, data) => { + if(err) throw err; + + new Filer.FileSystem({ + provider: new SerializableMemoryProvider('0.43', data) + }, (err, fs) => { + if(err) throw err; + + filerFs = fs; + done(); + }); + }); + }); + + it('should have a root directory', done => { + filerFs.stat('/', (err, stats) => { + if(err) throw err; + + expect(stats).to.be.an('object'); + expect(stats.isDirectory()).to.be.true; + done(); + }); + }); + + it('should have expected entries in root dir', done => { + filerFs.readdir('/', (err, entries) => { + if(err) throw err; + + expect(entries).to.be.an('array'); + expect(entries.length).to.equal(3); + expect(entries).to.contain('README.md'); + expect(entries).to.contain('file.txt'); + expect(entries).to.contain('dir'); + done(); + }); + }); + + it('should have correct contents for /file.txt (read as String)', done => { + const fileTxtPath = nodePath.resolve(__dirname, '../tiny-fs/file.txt'); + + nodeFs.readFile(fileTxtPath, 'utf8', (err, nodeData) => { + if(err) throw err; + + filerFs.readFile('/file.txt', 'utf8', (err, filerData) => { + if(err) throw err; + + expect(nodeData).to.equal(filerData); + done(); + }); + }); + }); + + it('should have expected entries in /dir', done => { + filerFs.readdir('/dir', (err, entries) => { + if(err) throw err; + + expect(entries).to.be.an('array'); + expect(entries.length).to.equal(1); + expect(entries).to.contain('file2.txt'); + done(); + }); + }); + + it('should have correct contents for /dir/file2.txt (read as Buffer)', done => { + const file2TxtPath = nodePath.resolve(__dirname, '../tiny-fs/dir/file2.txt'); + + nodeFs.readFile(file2TxtPath, null, (err, nodeData) => { + if(err) throw err; + + filerFs.readFile('/dir/file.txt', null, (err, filerData) => { + if(err) throw err; + + expect(nodeData).to.equal(filerData); + done(); + }); + }); + }); + +}); diff --git a/tests/filesystems/tiny-fs/dir/file2.txt b/tests/filesystems/tiny-fs/dir/file2.txt new file mode 100644 index 0000000..9af967a --- /dev/null +++ b/tests/filesystems/tiny-fs/dir/file2.txt @@ -0,0 +1 @@ +This is a second file. diff --git a/tests/filesystems/tiny-fs/file.txt b/tests/filesystems/tiny-fs/file.txt new file mode 100644 index 0000000..4dd1ef7 --- /dev/null +++ b/tests/filesystems/tiny-fs/file.txt @@ -0,0 +1 @@ +This is a file.