From a546a5b6c7efedd7d24c54e5ae4136eae688b317 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 7 Dec 2018 18:40:20 +0000 Subject: [PATCH] test: convert tests to async/await License: MIT Signed-off-by: achingbrain --- test/cp.spec.js | 221 +++++++++++----------- test/flush.spec.js | 13 +- test/ls.spec.js | 179 +++++++++--------- test/mkdir.spec.js | 121 ++++++------ test/mv.spec.js | 137 +++++++------- test/read.spec.js | 137 +++++++------- test/rm.spec.js | 232 ++++++++++++----------- test/stat.spec.js | 186 +++++++++---------- test/write.spec.js | 446 +++++++++++++++++++++++---------------------- 9 files changed, 822 insertions(+), 850 deletions(-) diff --git a/test/cp.spec.js b/test/cp.spec.js index 1291815ed0..e75dc32401 100644 --- a/test/cp.spec.js +++ b/test/cp.spec.js @@ -10,158 +10,147 @@ const { createShardedDirectory } = require('./helpers') -describe('cp', function () { +describe('cp', () => { let mfs - before(() => { - return createMfs() - .then(instance => { - mfs = instance - }) + before(async () => { + mfs = await createMfs() }) - it('refuses to copy files without arguments', () => { - return mfs.cp() - .then(() => { - throw new Error('No error was thrown for missing files') - }) - .catch(error => { - expect(error.message).to.contain('Please supply at least one source') - }) + it('refuses to copy files without arguments', async () => { + try { + await mfs.cp() + throw new Error('No error was thrown for missing files') + } catch (err) { + expect(err.message).to.contain('Please supply at least one source') + } }) - it('refuses to copy files without files', () => { - return mfs.cp('/destination') - .then(() => { - throw new Error('No error was thrown for missing files') - }) - .catch(error => { - expect(error.message).to.contain('Please supply at least one source') - }) + it('refuses to copy files without files', async () => { + try { + await mfs.cp('/destination') + throw new Error('No error was thrown for missing files') + } catch (err) { + expect(err.message).to.contain('Please supply at least one source') + } }) - it('refuses to copy files without files even with options', () => { - return mfs.cp('/destination', {}) - .then(() => { - throw new Error('No error was thrown for missing files') - }) - .catch(error => { - expect(error.message).to.contain('Please supply at least one source') - }) + it('refuses to copy files without files even with options', async () => { + try { + await mfs.cp('/destination', {}) + throw new Error('No error was thrown for missing files') + } catch (err) { + expect(err.message).to.contain('Please supply at least one source') + } }) - it('refuses to copy a file to a non-existent directory', () => { - return mfs.cp('/i-do-not-exist', '/output') - .then(() => { - throw new Error('No error was thrown for a non-existent file') - }) - .catch(error => { - expect(error.message).to.contain('does not exist') - }) + it('refuses to copy a file to a non-existent directory', async () => { + try { + await mfs.cp('/i-do-not-exist', '/output') + throw new Error('No error was thrown for a non-existent file') + } catch (err) { + expect(err.message).to.contain('does not exist') + } }) - it('refuses to copy files to an exsting file', () => { + it('refuses to copy files to an exsting file', async () => { const source = `/source-file-${Math.random()}.txt` const destination = `/dest-file-${Math.random()}.txt` - return mfs.write(source, bufferStream(100), { + await mfs.write(source, bufferStream(100), { create: true }) - .then(() => mfs.write(destination, bufferStream(100), { - create: true - })) - .then(() => mfs.cp(source, destination)) - .then(() => { - throw new Error('No error was thrown for a non-existent file') - }) - .catch(error => { - expect(error.message).to.contain('directory already has entry by that name') - }) + await mfs.write(destination, bufferStream(100), { + create: true + }) + + try { + await mfs.cp(source, destination) + throw new Error('No error was thrown for a non-existent file') + } catch (err) { + expect(err.message).to.contain('directory already has entry by that name') + } }) - it('refuses to copy a file to itself', () => { + it('refuses to copy a file to itself', async () => { const source = `/source-file-${Math.random()}.txt` - return mfs.write(source, bufferStream(100), { + await mfs.write(source, bufferStream(100), { create: true }) - .then(() => mfs.cp(source, source)) - .then(() => { - throw new Error('No error was thrown for a non-existent file') - }) - .catch(error => { - expect(error.message).to.contain('directory already has entry by that name') - }) + + try { + await mfs.cp(source, source) + throw new Error('No error was thrown for a non-existent file') + } catch (err) { + expect(err.message).to.contain('directory already has entry by that name') + } }) - it('copies a file to new location', () => { + it('copies a file to new location', async () => { const source = `/source-file-${Math.random()}.txt` const destination = `/dest-file-${Math.random()}.txt` let data = Buffer.alloc(0) - return mfs.write(source, bufferStream(500, { + await mfs.write(source, bufferStream(500, { collector: (bytes) => { data = Buffer.concat([data, bytes]) } }), { create: true }) - .then(() => mfs.cp(source, destination)) - .then(() => mfs.read(destination)) - .then((buffer) => { - expect(buffer).to.deep.equal(data) - }) + + await mfs.cp(source, destination) + const buffer = await mfs.read(destination) + + expect(buffer).to.deep.equal(data) }) - it('copies a file to a pre-existing directory', () => { + it('copies a file to a pre-existing directory', async () => { const source = `/source-file-${Math.random()}.txt` const directory = `/dest-directory-${Math.random()}` const destination = `${directory}${source}` - return mfs.write(source, bufferStream(500), { + await mfs.write(source, bufferStream(500), { create: true }) - .then(() => mfs.mkdir(directory)) - .then(() => mfs.cp(source, directory)) - .then(() => mfs.stat(destination)) - .then((stats) => { - expect(stats.size).to.equal(500) - }) + await mfs.mkdir(directory) + await mfs.cp(source, directory) + + const stats = await mfs.stat(destination) + expect(stats.size).to.equal(500) }) - it('copies directories', () => { + it('copies directories', async () => { const source = `/source-directory-${Math.random()}` const destination = `/dest-directory-${Math.random()}` - return mfs.mkdir(source) - .then(() => mfs.cp(source, destination)) - .then(() => mfs.stat(destination)) - .then((stats) => { - expect(stats.type).to.equal('directory') - }) + await mfs.mkdir(source) + await mfs.cp(source, destination) + + const stats = await mfs.stat(destination) + expect(stats.type).to.equal('directory') }) - it('copies directories recursively', () => { + it('copies directories recursively', async () => { const directory = `/source-directory-${Math.random()}` const subDirectory = `/source-directory-${Math.random()}` const source = `${directory}${subDirectory}` const destination = `/dest-directory-${Math.random()}` - return mfs.mkdir(source, { + await mfs.mkdir(source, { parents: true }) - .then(() => mfs.cp(directory, destination)) - .then(() => mfs.stat(destination)) - .then((stats) => { - expect(stats.type).to.equal('directory') - }) - .then(() => mfs.stat(`${destination}/${subDirectory}`)) - .then((stats) => { - expect(stats.type).to.equal('directory') - }) + await mfs.cp(directory, destination) + + const stats = await mfs.stat(destination) + expect(stats.type).to.equal('directory') + + const subDirStats = await mfs.stat(`${destination}/${subDirectory}`) + expect(subDirStats.type).to.equal('directory') }) - it('copies multiple files to new location', () => { + it('copies multiple files to new location', async () => { const sources = [{ path: `/source-file-${Math.random()}.txt`, data: Buffer.alloc(0) @@ -171,42 +160,40 @@ describe('cp', function () { }] const destination = `/dest-dir-${Math.random()}` - return Promise.all( - sources.map(source => mfs.write(source.path, bufferStream(500, { + for (const source of sources) { + await mfs.write(source.path, bufferStream(500, { collector: (bytes) => { source.data = Buffer.concat([source.data, bytes]) } }), { create: true - })) - ) - .then(() => mfs.cp(sources[0].path, sources[1].path, destination, { - parents: true - })) - .then(() => Promise.all( - sources.map((source, index) => mfs.read(`${destination}${source.path}`) - .then((buffer) => { - expect(buffer).to.deep.equal(sources[index].data) - }) - ) - )) + }) + } + + await mfs.cp(sources[0].path, sources[1].path, destination, { + parents: true + }) + + for (const source of sources) { + const buffer = await mfs.read(`${destination}${source.path}`) + + expect(buffer).to.deep.equal(source.data) + } }) - it('copies files from ipfs paths', () => { + it('copies files from ipfs paths', async () => { const source = `/source-file-${Math.random()}.txt` const destination = `/dest-file-${Math.random()}.txt` - return mfs.write(source, bufferStream(100), { + await mfs.write(source, bufferStream(100), { create: true }) - .then(() => mfs.stat(source)) - .then((stats) => { - return mfs.cp(`/ipfs/${stats.hash}`, destination) - }) - .then(() => mfs.stat(destination)) - .then((stats) => { - expect(stats.size).to.equal(100) - }) + + const stats = await mfs.stat(source) + await mfs.cp(`/ipfs/${stats.hash}`, destination) + + const destinationStats = await mfs.stat(destination) + expect(destinationStats.size).to.equal(100) }) it('copies a sharded directory to a normal directory', async () => { diff --git a/test/flush.spec.js b/test/flush.spec.js index 5c1bdcb42d..d9176f1919 100644 --- a/test/flush.spec.js +++ b/test/flush.spec.js @@ -8,18 +8,15 @@ const { createMfs } = require('./helpers') -describe('flush', function () { +describe('flush', () => { let mfs - before(() => { - return createMfs() - .then(instance => { - mfs = instance - }) + before(async () => { + mfs = await createMfs() }) - it('flushes the root node', () => { - return mfs.flush() + it('flushes the root node', async () => { + await mfs.flush() }) it('throws a error when trying to flush non-existent directories', async () => { diff --git a/test/ls.spec.js b/test/ls.spec.js index ac6e4d6a73..07cbef3dfa 100644 --- a/test/ls.spec.js +++ b/test/ls.spec.js @@ -15,14 +15,11 @@ const { createShardedDirectory } = require('./helpers') -describe('ls', function () { +describe('ls', () => { let mfs - before(() => { - return createMfs() - .then(instance => { - mfs = instance - }) + before(async () => { + mfs = await createMfs() }) const methods = [{ @@ -79,7 +76,7 @@ describe('ls', function () { }] methods.forEach(method => { - describe(`ls ${method.name}`, function () { + describe(`ls ${method.name}`, () => { it('lists the root directory by default', async () => { const fileName = `small-file-${Math.random()}.txt` const content = Buffer.from('Hello world') @@ -93,136 +90,130 @@ describe('ls', function () { expect(files.find(file => file.name === fileName)).to.be.ok() }) - it('refuses to lists files with an empty path', () => { - return method.ls('') - .then((result) => method.collect(result)) - .then(() => { - throw new Error('No error was thrown for an empty path') - }) - .catch(error => { - expect(error.message).to.contain('paths must not be empty') - }) + it('refuses to lists files with an empty path', async () => { + try { + await method.collect(await method.ls('')) + throw new Error('No error was thrown for an empty path') + } catch (err) { + expect(err.message).to.contain('paths must not be empty') + } }) - it('refuses to lists files with an invalid path', () => { - return method.ls('not-valid') - .then((result) => method.collect(result)) - .then(() => { - throw new Error('No error was thrown for an empty path') - }) - .catch(error => { - expect(error.message).to.contain('paths must start with a leading /') - }) + it('refuses to lists files with an invalid path', async () => { + try { + await method.collect(await method.ls('not-valid')) + throw new Error('No error was thrown for an empty path') + } catch (err) { + expect(err.message).to.contain('paths must start with a leading /') + } }) - it('lists files in a directory', () => { + it('lists files in a directory', async () => { const dirName = `dir-${Math.random()}` const fileName = `small-file-${Math.random()}.txt` const content = Buffer.from('Hello world') - return mfs.write(`/${dirName}/${fileName}`, content, { + await mfs.write(`/${dirName}/${fileName}`, content, { create: true, parents: true }) - .then(() => method.ls(`/${dirName}`, {})) - .then((result) => method.collect(result)) - .then(files => { - expect(files.length).to.equal(1) - expect(files[0].name).to.equal(fileName) - expect(files[0].type).to.equal(FILE_TYPES.file) - expect(files[0].size).to.equal(0) - expect(files[0].hash).to.equal('') - }) + + const stream = await method.ls(`/${dirName}`, {}) + const files = await method.collect(stream) + + expect(files.length).to.equal(1) + expect(files[0].name).to.equal(fileName) + expect(files[0].type).to.equal(FILE_TYPES.file) + expect(files[0].size).to.equal(0) + expect(files[0].hash).to.equal('') }) - it('lists files in a directory with meta data', () => { + it('lists files in a directory with meta data', async () => { const dirName = `dir-${Math.random()}` const fileName = `small-file-${Math.random()}.txt` const content = Buffer.from('Hello world') - return mfs.write(`/${dirName}/${fileName}`, content, { + await mfs.write(`/${dirName}/${fileName}`, content, { create: true, parents: true }) - .then(() => method.ls(`/${dirName}`, { - long: true - })) - .then((result) => method.collect(result)) - .then(files => { - expect(files.length).to.equal(1) - expect(files[0].name).to.equal(fileName) - expect(files[0].type).to.equal(FILE_TYPES.file) - expect(files[0].size).to.equal(content.length) - }) + + const stream = await method.ls(`/${dirName}`, { + long: true + }) + const files = await method.collect(stream) + + expect(files.length).to.equal(1) + expect(files[0].name).to.equal(fileName) + expect(files[0].type).to.equal(FILE_TYPES.file) + expect(files[0].size).to.equal(content.length) }) - it('lists a file', () => { + it('lists a file', async () => { const fileName = `small-file-${Math.random()}.txt` const content = Buffer.from('Hello world') - return mfs.write(`/${fileName}`, content, { + await mfs.write(`/${fileName}`, content, { create: true }) - .then(() => method.ls(`/${fileName}`)) - .then((result) => method.collect(result)) - .then(files => { - expect(files.length).to.equal(1) - expect(files[0].name).to.equal(fileName) - expect(files[0].type).to.equal(FILE_TYPES.file) - expect(files[0].size).to.equal(0) - expect(files[0].hash).to.equal('') - }) + + const stream = await method.ls(`/${fileName}`) + const files = await method.collect(stream) + + expect(files.length).to.equal(1) + expect(files[0].name).to.equal(fileName) + expect(files[0].type).to.equal(FILE_TYPES.file) + expect(files[0].size).to.equal(0) + expect(files[0].hash).to.equal('') }) - it('lists a file with meta data', () => { + it('lists a file with meta data', async () => { const fileName = `small-file-${Math.random()}.txt` const content = Buffer.from('Hello world') - return mfs.write(`/${fileName}`, content, { + await mfs.write(`/${fileName}`, content, { create: true }) - .then(() => method.ls(`/${fileName}`, { - long: true - })) - .then((result) => method.collect(result)) - .then(files => { - expect(files.length).to.equal(1) - expect(files[0].name).to.equal(fileName) - expect(files[0].type).to.equal(FILE_TYPES.file) - expect(files[0].size).to.equal(content.length) - }) + const stream = await method.ls(`/${fileName}`, { + long: true + }) + const files = await method.collect(stream) + + expect(files.length).to.equal(1) + expect(files[0].name).to.equal(fileName) + expect(files[0].type).to.equal(FILE_TYPES.file) + expect(files[0].size).to.equal(content.length) }) - it('lists a file with a base32 hash', () => { + it('lists a file with a base32 hash', async () => { const fileName = `small-file-${Math.random()}.txt` const content = Buffer.from('Hello world') - return mfs.write(`/${fileName}`, content, { + await mfs.write(`/${fileName}`, content, { create: true }) - .then(() => method.ls(`/${fileName}`, { - long: true, - cidBase: 'base32' - })) - .then((result) => method.collect(result)) - .then(files => { - expect(files.length).to.equal(1) - expect(files[0].name).to.equal(fileName) - expect(files[0].type).to.equal(FILE_TYPES.file) - expect(files[0].size).to.equal(content.length) - expect(files[0].hash.startsWith('b')).to.equal(true) - }) + + const stream = await method.ls(`/${fileName}`, { + long: true, + cidBase: 'base32' + }) + const files = await method.collect(stream) + + expect(files.length).to.equal(1) + expect(files[0].name).to.equal(fileName) + expect(files[0].type).to.equal(FILE_TYPES.file) + expect(files[0].size).to.equal(content.length) + expect(files[0].hash.startsWith('b')).to.equal(true) }) - it('fails to list non-existent file', () => { - return method.ls('/i-do-not-exist') - .then((result) => method.collect(result)) - .then(() => { - throw new Error('No error was thrown for a non-existent file') - }) - .catch(error => { - expect(error.message).to.contain('does not exist') - }) + it('fails to list non-existent file', async () => { + try { + const stream = await method.ls('/i-do-not-exist') + await method.collect(stream) + throw new Error('No error was thrown for a non-existent file') + } catch (err) { + expect(err.message).to.contain('does not exist') + } }) it('lists a sharded directory contents', async () => { diff --git a/test/mkdir.spec.js b/test/mkdir.spec.js index 8d08e10e70..34860a1b00 100644 --- a/test/mkdir.spec.js +++ b/test/mkdir.spec.js @@ -11,99 +11,104 @@ const { createShardedDirectory } = require('./helpers') -describe('mkdir', function () { +describe('mkdir', () => { let mfs - before(() => { - return createMfs() - .then(instance => { - mfs = instance - }) + before(async () => { + mfs = await createMfs() }) - it('requires a directory', (done) => { - mfs.mkdir('', (error) => { - expect(error.message).to.contain('no path given') - - done() - }) + it('requires a directory', async () => { + try { + await mfs.mkdir('') + throw new Error('No error was thrown when creating an directory with an empty path') + } catch (err) { + expect(err.message).to.contain('no path given') + } }) - it('refuses to create a directory without a leading slash', (done) => { - mfs.mkdir('foo', (error) => { - expect(error.message).to.contain('paths must start with a leading /') - - done() - }) + it('refuses to create a directory without a leading slash', async () => { + try { + await mfs.mkdir('foo') + throw new Error('No error was thrown when creating an directory with no leading slash') + } catch (err) { + expect(err.message).to.contain('paths must start with a leading /') + } }) - it('refuses to recreate the root directory when -p is false', (done) => { - mfs.mkdir('/', { - parents: false - }, (error) => { - expect(error.message).to.contain("cannot create directory '/'") - - done() - }) + it('refuses to recreate the root directory when -p is false', async () => { + try { + await mfs.mkdir('/', { + parents: false + }) + throw new Error('No error was thrown when creating the root directory without -p') + } catch (err) { + expect(err.message).to.contain("cannot create directory '/'") + } }) - it('refuses to create a nested directory when -p is false', () => { - return mfs.mkdir('/foo/bar/baz', { - parents: false - }) - .catch(error => { - expect(error.message).to.contain('does not exist') + it('refuses to create a nested directory when -p is false', async () => { + try { + await mfs.mkdir('/foo/bar/baz', { + parents: false }) + throw new Error('No error was thrown when creating intermediate directories without -p') + } catch (err) { + expect(err.message).to.contain('does not exist') + } }) - it('creates a directory', () => { + it('creates a directory', async () => { const path = '/foo' - return mfs.mkdir(path, {}) - .then(() => mfs.ls(path)) - .then((files) => { - expect(files.length).to.equal(0) - }) + await mfs.mkdir(path, {}) + + const stats = await mfs.stat(path) + expect(stats.type).to.equal('directory') + + const files = await mfs.ls(path) + expect(files.length).to.equal(0) }) - it('refuses to create a directory that already exists', () => { + it('refuses to create a directory that already exists', async () => { const path = '/qux/quux/quuux' - return mfs.mkdir(path, { + await mfs.mkdir(path, { parents: true }) - .then(() => mfs.mkdir(path, { + + try { + await mfs.mkdir(path, { parents: false - })) - .then(() => { - throw new Error('Did not refuse to create a path that already exists') - }) - .catch((error) => { - expect(error.message).to.contain('file already exists') }) + throw new Error('Did not refuse to create a path that already exists') + } catch (err) { + expect(err.message).to.contain('file already exists') + } }) - it('does not error when creating a directory that already exists and parents is true', () => { + it('does not error when creating a directory that already exists and parents is true', async () => { const path = '/qux/quux/quuux' - return mfs.mkdir(path, { + await mfs.mkdir(path, { + parents: true + }) + + await mfs.mkdir(path, { parents: true }) - .then(() => mfs.mkdir(path, { - parents: true - })) }) - it('creates a nested directory when -p is true', function () { + it('creates a nested directory when -p is true', async () => { const path = '/foo/bar/baz' - return mfs.mkdir(path, { + await mfs.mkdir(path, { parents: true }) - .then(() => mfs.ls(path)) - .then((files) => { - expect(files.length).to.equal(0) - }) + + const files = await mfs.ls(path) + + expect(files.length).to.equal(0) }) it('creates a nested directory with a different CID version to the parent', async () => { diff --git a/test/mv.spec.js b/test/mv.spec.js index 2232426d7c..7e9641fa5e 100644 --- a/test/mv.spec.js +++ b/test/mv.spec.js @@ -10,108 +10,101 @@ const { createShardedDirectory } = require('./helpers') -describe('mv', function () { +describe('mv', () => { let mfs - before(() => { - return createMfs() - .then(instance => { - mfs = instance - }) + before(async () => { + mfs = await createMfs() }) - it('refuses to move files without arguments', () => { - return mfs.mv() - .then(() => { - throw new Error('No error was thrown for missing files') - }) - .catch(error => { - expect(error.message).to.contain('Please supply at least one source') - }) + it('refuses to move files without arguments', async () => { + try { + await mfs.mv() + throw new Error('No error was thrown for missing files') + } catch (err) { + expect(err.message).to.contain('Please supply at least one source') + } }) - it('refuses to move files without enough arguments', () => { - return mfs.mv('/destination') - .then(() => { - throw new Error('No error was thrown for missing files') - }) - .catch(error => { - expect(error.message).to.contain('Please supply at least one source') - }) + it('refuses to move files without enough arguments', async () => { + try { + await mfs.mv('/destination') + throw new Error('No error was thrown for missing files') + } catch (err) { + expect(err.message).to.contain('Please supply at least one source') + } }) - it('moves a file', () => { + it('moves a file', async () => { const source = `/source-file-${Math.random()}.txt` const destination = `/dest-file-${Math.random()}.txt` let data = Buffer.alloc(0) - return mfs.write(source, bufferStream(500, { + await mfs.write(source, bufferStream(500, { collector: (bytes) => { data = Buffer.concat([data, bytes]) } }), { create: true }) - .then(() => mfs.mv(source, destination)) - .then(() => mfs.read(destination)) - .then((buffer) => { - expect(buffer).to.deep.equal(data) - }) - .then(() => mfs.stat(source)) - .then(() => { - throw new Error('File was copied but not removed') - }) - .catch(error => { - expect(error.message).to.contain('does not exist') - }) + await mfs.mv(source, destination) + + const buffer = await mfs.read(destination) + expect(buffer).to.deep.equal(data) + + try { + await mfs.stat(source) + throw new Error('File was copied but not removed') + } catch (err) { + expect(err.message).to.contain('does not exist') + } }) - it('moves a directory', () => { + it('moves a directory', async () => { const source = `/source-directory-${Math.random()}` const destination = `/dest-directory-${Math.random()}` - return mfs.mkdir(source) - .then(() => mfs.mv(source, destination, { - recursive: true - })) - .then(() => mfs.stat(destination)) - .then((stats) => { - expect(stats.type).to.equal('directory') - }) - .then(() => mfs.stat(source)) - .then(() => { - throw new Error('Directory was copied but not removed') - }) - .catch(error => { - expect(error.message).to.contain('does not exist') - }) + await mfs.mkdir(source) + await mfs.mv(source, destination, { + recursive: true + }) + const stats = await mfs.stat(destination) + + expect(stats.type).to.equal('directory') + + try { + await mfs.stat(source) + throw new Error('Directory was copied but not removed') + } catch (err) { + expect(err.message).to.contain('does not exist') + } }) - it('moves directories recursively', () => { + it('moves directories recursively', async () => { const directory = `source-directory-${Math.random()}` const subDirectory = `/source-directory-${Math.random()}` const source = `/${directory}${subDirectory}` const destination = `/dest-directory-${Math.random()}` - return mfs.mkdir(source) - .then(() => mfs.mv(`/${directory}`, destination, { - recursive: true - })) - .then(() => mfs.stat(destination)) - .then((stats) => { - expect(stats.type).to.equal('directory') - }) - .then(() => mfs.stat(`${destination}${subDirectory}`)) - .then((stats) => { - expect(stats.type).to.equal('directory') - }) - .then(() => mfs.stat(source)) - .then(() => { - throw new Error('Directory was copied but not removed') - }) - .catch(error => { - expect(error.message).to.contain('does not exist') - }) + await mfs.mkdir(source, { + parents: true + }) + await mfs.mv(`/${directory}`, destination, { + recursive: true + }) + + const stats = await mfs.stat(destination) + expect(stats.type).to.equal('directory') + + const subDirectoryStats = await mfs.stat(`${destination}${subDirectory}`) + expect(subDirectoryStats.type).to.equal('directory') + + try { + await mfs.stat(source) + throw new Error('Directory was copied but not removed') + } catch (err) { + expect(err.message).to.contain('does not exist') + } }) it('moves a sharded directory to a normal directory', async () => { diff --git a/test/read.spec.js b/test/read.spec.js index b77486d3ec..64fce9c44d 100644 --- a/test/read.spec.js +++ b/test/read.spec.js @@ -14,15 +14,12 @@ const { createShardedDirectory } = require('./helpers') -describe('read', function () { +describe('read', () => { let mfs let smallFile = loadFixture(path.join('test', 'fixtures', 'small-file.txt')) - before(() => { - return createMfs() - .then(instance => { - mfs = instance - }) + before(async () => { + mfs = await createMfs() }) const methods = [{ @@ -40,9 +37,9 @@ describe('read', function () { return new Promise((resolve, reject) => { pull( stream, - collect((error, buffers) => { - if (error) { - return reject(error) + collect((err, buffers) => { + if (err) { + return reject(err) } resolve(Buffer.concat(buffers)) @@ -67,135 +64,147 @@ describe('read', function () { resolve(data) }) - stream.on('error', (error) => { - reject(error) + stream.on('error', (err) => { + reject(err) }) }) } }] methods.forEach(method => { - describe(`read ${method.name}`, function () { - it('reads a small file', () => { + describe(`read ${method.name}`, () => { + it('reads a small file', async () => { const filePath = '/small-file.txt' - return mfs.write(filePath, smallFile, { + await mfs.write(filePath, smallFile, { create: true }) - .then(() => method.read(filePath)) - .then((result) => method.collect(result)) - .then((buffer) => { - expect(buffer).to.deep.equal(smallFile) - }) + + const result = await method.read(filePath) + const buffer = await method.collect(result) + expect(buffer).to.deep.equal(smallFile) }) - it('reads a file with an offset', () => { + it('reads a file with an offset', async () => { const path = `/some-file-${Math.random()}.txt` let data = Buffer.alloc(0) const offset = 10 - return mfs.write(path, bufferStream(100, { + await mfs.write(path, bufferStream(100, { collector: (bytes) => { data = Buffer.concat([data, bytes]) } }), { create: true }) - .then(() => method.read(path, { - offset - })) - .then((result) => method.collect(result)) - .then((buffer) => expect(buffer).to.deep.equal(data.slice(offset))) + + const result = await method.read(path, { + offset + }) + const buffer = await method.collect(result) + + expect(buffer).to.deep.equal(data.slice(offset)) }) - it('reads a file with a length', () => { + it('reads a file with a length', async () => { const path = `/some-file-${Math.random()}.txt` let data = Buffer.alloc(0) const length = 10 - return mfs.write(path, bufferStream(100, { + await mfs.write(path, bufferStream(100, { collector: (bytes) => { data = Buffer.concat([data, bytes]) } }), { create: true }) - .then(() => method.read(path, { - length - })) - .then((result) => method.collect(result)) - .then((buffer) => expect(buffer).to.deep.equal(data.slice(0, length))) + + const result = await method.read(path, { + length + }) + const buffer = await method.collect(result) + + expect(buffer).to.deep.equal(data.slice(0, length)) }) - it('reads a file with a legacy count argument', () => { + it('reads a file with a legacy count argument', async () => { const path = `/some-file-${Math.random()}.txt` let data = Buffer.alloc(0) const length = 10 - return mfs.write(path, bufferStream(100, { + await mfs.write(path, bufferStream(100, { collector: (bytes) => { data = Buffer.concat([data, bytes]) } }), { create: true }) - .then(() => method.read(path, { - count: length - })) - .then((result) => method.collect(result)) - .then((buffer) => expect(buffer).to.deep.equal(data.slice(0, length))) + + const result = await method.read(path, { + count: length + }) + const buffer = await method.collect(result) + + expect(buffer).to.deep.equal(data.slice(0, length)) }) - it('reads a file with an offset and a length', () => { + it('reads a file with an offset and a length', async () => { const path = `/some-file-${Math.random()}.txt` let data = Buffer.alloc(0) const offset = 10 const length = 10 - return mfs.write(path, bufferStream(100, { + await mfs.write(path, bufferStream(100, { collector: (bytes) => { data = Buffer.concat([data, bytes]) } }), { create: true }) - .then(() => method.read(path, { - offset, - length - })) - .then((result) => method.collect(result)) - .then((buffer) => expect(buffer).to.deep.equal(data.slice(offset, offset + length))) + + const result = await method.read(path, { + offset, + length + }) + const buffer = await method.collect(result) + + expect(buffer).to.deep.equal(data.slice(offset, offset + length)) }) - it('reads a file with an offset and a legacy count argument', () => { + it('reads a file with an offset and a legacy count argument', async () => { const path = `/some-file-${Math.random()}.txt` let data = Buffer.alloc(0) const offset = 10 const length = 10 - return mfs.write(path, bufferStream(100, { + await mfs.write(path, bufferStream(100, { collector: (bytes) => { data = Buffer.concat([data, bytes]) } }), { create: true }) - .then(() => method.read(path, { - offset, - count: length - })) - .then((result) => method.collect(result)) - .then((buffer) => expect(buffer).to.deep.equal(data.slice(offset, offset + length))) + + const result = await method.read(path, { + offset, + count: length + }) + + const buffer = await method.collect(result) + + expect(buffer).to.deep.equal(data.slice(offset, offset + length)) }) - it('refuses to read a directory', () => { + it('refuses to read a directory', async () => { const path = '/' - return method.read(path) - .then((stream) => method.collect(stream)) - .catch(error => { - expect(error.message).to.contain('was not a file') - }) + try { + const result = await method.read(path) + await method.collect(result) + throw new Error('Should have errored on trying to read a directory') + } catch (err) { + expect(err.message).to.contain('was not a file') + } }) it('refuses to read a non-existent file', async () => { @@ -203,8 +212,8 @@ describe('read', function () { const stream = await method.read(`/file-${Math.random()}.txt`) await method.collect(stream) throw new Error('Should have errored on non-existent file') - } catch (error) { - expect(error.message).to.contain('does not exist') + } catch (err) { + expect(err.message).to.contain('does not exist') } }) diff --git a/test/rm.spec.js b/test/rm.spec.js index 8b797b31ae..08725ca5d7 100644 --- a/test/rm.spec.js +++ b/test/rm.spec.js @@ -15,176 +15,172 @@ const { FILE_SEPARATOR } = require('../src/core/utils') -describe('rm', function () { +describe('rm', () => { let mfs - before(() => { - return createMfs() - .then(instance => { - mfs = instance - }) + before(async () => { + mfs = await createMfs() }) - it('refuses to remove files without arguments', () => { - return mfs.rm() - .then(() => { - throw new Error('No error was thrown for missing paths') - }) - .catch(error => { - expect(error.message).to.contain('Please supply at least one path to remove') - }) + it('refuses to remove files without arguments', async () => { + try { + await mfs.rm() + throw new Error('No error was thrown for missing paths') + } catch (err) { + expect(err.message).to.contain('Please supply at least one path to remove') + } }) - it('refuses to remove the root path', () => { - return mfs.rm(FILE_SEPARATOR) - .then(() => { - throw new Error('No error was thrown for missing paths') - }) - .catch(error => { - expect(error.message).to.contain('Cannot delete root') - }) + it('refuses to remove the root path', async () => { + try { + await mfs.rm(FILE_SEPARATOR) + throw new Error('No error was thrown for missing paths') + } catch (err) { + expect(err.message).to.contain('Cannot delete root') + } }) - it('refuses to remove a directory without the recursive flag', () => { + it('refuses to remove a directory without the recursive flag', async () => { const path = `/directory-${Math.random()}` - return mfs.mkdir(path) - .then(() => mfs.rm(path)) - .then(() => { - throw new Error('No error was thrown for missing recursive flag') - }) - .catch(error => { - expect(error.message).to.contain(`${path} is a directory, use -r to remove directories`) - }) + await mfs.mkdir(path) + + try { + await mfs.rm(path) + throw new Error('No error was thrown for missing recursive flag') + } catch (err) { + expect(err.message).to.contain(`${path} is a directory, use -r to remove directories`) + } }) it('refuses to remove a non-existent file', async () => { try { await mfs.rm(`/file-${Math.random()}`) throw new Error('No error was thrown for non-existent file') - } catch (error) { - expect(error.message).to.contain('does not exist') + } catch (err) { + expect(err.message).to.contain('does not exist') } }) - it('removes a file', () => { + it('removes a file', async () => { const file = `/some-file-${Math.random()}.txt` - return mfs.write(file, bufferStream(100), { + await mfs.write(file, bufferStream(100), { create: true, parents: true }) - .then(() => mfs.rm(file, { - recursive: true - })) - .then(() => mfs.stat(file)) - .then(() => { - throw new Error('File was not removed') - }) - .catch(error => { - expect(error.message).to.contain('does not exist') - }) + + await mfs.rm(file, { + recursive: true + }) + + try { + await mfs.stat(file) + throw new Error('File was not removed') + } catch (err) { + expect(err.message).to.contain('does not exist') + } }) - it('removes multiple files', () => { + it('removes multiple files', async () => { const file1 = `/some-file-${Math.random()}.txt` const file2 = `/some-file-${Math.random()}.txt` - return mfs.write(file1, bufferStream(100), { + await mfs.write(file1, bufferStream(100), { + create: true, + parents: true + }) + await mfs.write(file2, bufferStream(100), { create: true, parents: true }) - .then(() => mfs.write(file2, bufferStream(100), { - create: true, - parents: true - })) - .then(() => mfs.rm(file1, file2, { - recursive: true - })) - .then(() => mfs.stat(file1)) - .then(() => { - throw new Error('File #1 was not removed') - }) - .catch(error => { - expect(error.message).to.contain('does not exist') - }) - .then(() => mfs.stat(file2)) - .then(() => { - throw new Error('File #2 was not removed') - }) - .catch(error => { - expect(error.message).to.contain('does not exist') - }) + await mfs.rm(file1, file2, { + recursive: true + }) + + try { + await mfs.stat(file1) + throw new Error('File #1 was not removed') + } catch (err) { + expect(err.message).to.contain('does not exist') + } + + try { + await mfs.stat(file2) + throw new Error('File #2 was not removed') + } catch (err) { + expect(err.message).to.contain('does not exist') + } }) - it('removes a directory', () => { + it('removes a directory', async () => { const directory = `/directory-${Math.random()}` - return mfs.mkdir(directory) - .then(() => mfs.rm(directory, { - recursive: true - })) - .then(() => mfs.stat(directory)) - .then(() => { - throw new Error('Directory was not removed') - }) - .catch(error => { - expect(error.message).to.contain('does not exist') - }) + await mfs.mkdir(directory) + await mfs.rm(directory, { + recursive: true + }) + + try { + await mfs.stat(directory) + throw new Error('Directory was not removed') + } catch (err) { + expect(err.message).to.contain('does not exist') + } }) - it('recursively removes a directory', () => { + it('recursively removes a directory', async () => { const directory = `/directory-${Math.random()}` const subdirectory = `/directory-${Math.random()}` const path = `${directory}${subdirectory}` - return mfs.mkdir(path, { + await mfs.mkdir(path, { parents: true }) - .then(() => mfs.rm(directory, { - recursive: true - })) - .then(() => mfs.ls(subdirectory)) - .then(() => { - throw new Error('File was not removed') - }) - .catch(error => { - expect(error.message).to.contain('does not exist') - }) - .then(() => mfs.ls(directory)) - .then(() => { - throw new Error('Directory was not removed') - }) - .catch(error => { - expect(error.message).to.contain('does not exist') - }) + await mfs.rm(directory, { + recursive: true + }) + + try { + await mfs.ls(subdirectory) + throw new Error('File was not removed') + } catch (err) { + expect(err.message).to.contain('does not exist') + } + + try { + await mfs.ls(directory) + throw new Error('Directory was not removed') + } catch (err) { + expect(err.message).to.contain('does not exist') + } }) - it('recursively removes a directory with files in', () => { + it('recursively removes a directory with files in', async () => { const directory = `directory-${Math.random()}` const file = `/${directory}/some-file-${Math.random()}.txt` - return mfs.write(file, bufferStream(100), { + await mfs.write(file, bufferStream(100), { create: true, parents: true }) - .then(() => mfs.rm(`/${directory}`, { - recursive: true - })) - .then(() => mfs.stat(file)) - .then(() => { - throw new Error('File was not removed') - }) - .catch(error => { - expect(error.message).to.contain('does not exist') - }) - .then(() => mfs.stat(`/${directory}`)) - .then(() => { - throw new Error('Directory was not removed') - }) - .catch(error => { - expect(error.message).to.contain('does not exist') - }) + await mfs.rm(`/${directory}`, { + recursive: true + }) + + try { + await mfs.stat(file) + throw new Error('File was not removed') + } catch (err) { + expect(err.message).to.contain('does not exist') + } + + try { + await mfs.stat(`/${directory}`) + throw new Error('Directory was not removed') + } catch (err) { + expect(err.message).to.contain('does not exist') + } }) it('recursively removes a sharded directory inside a normal directory', async () => { diff --git a/test/stat.spec.js b/test/stat.spec.js index f2352e0c72..805ac0875c 100644 --- a/test/stat.spec.js +++ b/test/stat.spec.js @@ -14,154 +14,144 @@ const { EMPTY_DIRECTORY_HASH_BASE32 } = require('./helpers') -describe('stat', function () { +describe('stat', () => { let mfs let smallFile = loadFixture(path.join('test', 'fixtures', 'small-file.txt')) let largeFile = loadFixture(path.join('test', 'fixtures', 'large-file.jpg')) - before(() => { - return createMfs() - .then(instance => { - mfs = instance - }) + before(async () => { + mfs = await createMfs() }) - it('refuses to stat files with an empty path', () => { - return mfs.stat('') - .then(() => { - throw new Error('No error was thrown for an empty path') - }) - .catch(error => { - expect(error.message).to.contain('paths must not be empty') - }) + it('refuses to stat files with an empty path', async () => { + try { + await mfs.stat('') + throw new Error('No error was thrown for an empty path') + } catch (err) { + expect(err.message).to.contain('paths must not be empty') + } }) - it('refuses to lists files with an invalid path', () => { - return mfs.stat('not-valid') - .then(() => { - throw new Error('No error was thrown for an empty path') - }) - .catch(error => { - expect(error.message).to.contain('paths must start with a leading /') - }) + it('refuses to lists files with an invalid path', async () => { + try { + await mfs.stat('not-valid') + throw new Error('No error was thrown for an empty path') + } catch (err) { + expect(err.message).to.contain('paths must start with a leading /') + } }) - it('fails to stat non-existent file', () => { - return mfs.stat('/i-do-not-exist') - .then(() => { - throw new Error('No error was thrown for a non-existent file') - }) - .catch(error => { - expect(error.message).to.contain('does not exist') - }) + it('fails to stat non-existent file', async () => { + try { + await mfs.stat('/i-do-not-exist') + throw new Error('No error was thrown for a non-existent file') + } catch (err) { + expect(err.message).to.contain('does not exist') + } }) - it('stats an empty directory', () => { + it('stats an empty directory', async () => { const path = `/directory-${Math.random()}` - return mfs.mkdir(path) - .then(() => mfs.stat(path)) - .then(stats => { - expect(stats.size).to.equal(0) - expect(stats.cumulativeSize).to.equal(4) - expect(stats.blocks).to.equal(0) - expect(stats.type).to.equal('directory') - }) + await mfs.mkdir(path) + + const stats = await mfs.stat(path) + expect(stats.size).to.equal(0) + expect(stats.cumulativeSize).to.equal(4) + expect(stats.blocks).to.equal(0) + expect(stats.type).to.equal('directory') }) - it('returns only a hash', () => { + it('returns only a hash', async () => { const path = `/directory-${Math.random()}` - return mfs.mkdir(path) - .then(() => mfs.stat(path, { - hash: true - })) - .then(stats => { - expect(Object.keys(stats).length).to.equal(1) - expect(stats.hash).to.equal(EMPTY_DIRECTORY_HASH) - }) + await mfs.mkdir(path) + + const stats = await mfs.stat(path, { + hash: true + }) + + expect(Object.keys(stats).length).to.equal(1) + expect(stats.hash).to.equal(EMPTY_DIRECTORY_HASH) }) - it('returns only a base32 hash', () => { + it('returns only a base32 hash', async () => { const path = `/directory-${Math.random()}` - return mfs.mkdir(path) - .then(() => mfs.stat(path, { - hash: true, - cidBase: 'base32' - })) - .then(stats => { - expect(Object.keys(stats).length).to.equal(1) - expect(stats.hash).to.equal(EMPTY_DIRECTORY_HASH_BASE32) - }) + await mfs.mkdir(path) + + const stats = await mfs.stat(path, { + hash: true, + cidBase: 'base32' + }) + + expect(Object.keys(stats).length).to.equal(1) + expect(stats.hash).to.equal(EMPTY_DIRECTORY_HASH_BASE32) }) - it('returns only the size', () => { + it('returns only the size', async () => { const path = `/directory-${Math.random()}` - return mfs.mkdir(path) - .then(() => mfs.stat(path, { - size: true - })) - .then(stats => { - expect(Object.keys(stats).length).to.equal(1) - expect(stats.size).to.equal(4) // protobuf size?! - }) + await mfs.mkdir(path) + + const stats = await mfs.stat(path, { + size: true + }) + + expect(Object.keys(stats).length).to.equal(1) + expect(stats.size).to.equal(4) // protobuf size?! }) - it.skip('computes how much of the DAG is local', () => { + it.skip('computes how much of the DAG is local', async () => { }) - it('stats a small file', () => { + it('stats a small file', async () => { const filePath = '/stat/small-file.txt' - return mfs.write(filePath, smallFile, { + await mfs.write(filePath, smallFile, { create: true, parents: true }) - .then(() => mfs.stat(filePath)) - .then((stats) => { - expect(stats.size).to.equal(smallFile.length) - expect(stats.cumulativeSize).to.equal(71) - expect(stats.blocks).to.equal(1) - expect(stats.type).to.equal('file') - }) + + const stats = await mfs.stat(filePath) + expect(stats.size).to.equal(smallFile.length) + expect(stats.cumulativeSize).to.equal(71) + expect(stats.blocks).to.equal(1) + expect(stats.type).to.equal('file') }) - it('stats a large file', () => { + it('stats a large file', async () => { const filePath = '/stat/large-file.txt' - return mfs.write(filePath, largeFile, { + await mfs.write(filePath, largeFile, { create: true, parents: true }) - .then(() => mfs.stat(filePath)) - .then((stats) => { - expect(stats.size).to.equal(largeFile.length) - expect(stats.cumulativeSize).to.equal(490800) - expect(stats.blocks).to.equal(2) - expect(stats.type).to.equal('file') - }) + + const stats = await mfs.stat(filePath) + expect(stats.size).to.equal(largeFile.length) + expect(stats.cumulativeSize).to.equal(490800) + expect(stats.blocks).to.equal(2) + expect(stats.type).to.equal('file') }) - it('stats a large file with base32', () => { + it('stats a large file with base32', async () => { const filePath = '/stat/large-file.txt' - return mfs.write(filePath, largeFile, { + await mfs.write(filePath, largeFile, { create: true, parents: true }) - .then(() => mfs.stat(filePath, { - cidBase: 'base32' - })) - .then((stats) => { - expect(stats.hash.startsWith('b')).to.equal(true) - expect(stats.size).to.equal(largeFile.length) - expect(stats.cumulativeSize).to.equal(490800) - expect(stats.blocks).to.equal(2) - expect(stats.type).to.equal('file') - }) + + const stats = await mfs.stat(filePath, { + cidBase: 'base32' + }) + expect(stats.hash.startsWith('b')).to.equal(true) + expect(stats.size).to.equal(largeFile.length) + expect(stats.cumulativeSize).to.equal(490800) + expect(stats.blocks).to.equal(2) + expect(stats.type).to.equal('file') }) it('stats a sharded directory', async () => { diff --git a/test/write.spec.js b/test/write.spec.js index 800de74242..41f44a9d1a 100644 --- a/test/write.spec.js +++ b/test/write.spec.js @@ -25,7 +25,7 @@ if (isNode) { fs = require('fs') } -describe('write', function () { +describe('write', () => { let mfs let smallFile = loadFixture(path.join('test', 'fixtures', 'small-file.txt')) let largeFile = loadFixture(path.join('test', 'fixtures', 'large-file.jpg')) @@ -68,81 +68,81 @@ describe('write', function () { }) } - before(() => { - return createMfs() - .then(instance => { - mfs = instance - }) + before(async () => { + mfs = await createMfs() }) - it('explodes if it cannot convert content to a pull stream', () => { - return mfs.write('/foo', -1, { - create: true - }) - .then(() => expect(false).to.equal(true)) - .catch((error) => { - expect(error.message).to.contain('Don\'t know how to convert -1 into a pull stream source') + it('explodes if it cannot convert content to a pull stream', async () => { + try { + await mfs.write('/foo', -1, { + create: true }) + throw new Error('Did not fail to convert -1 into a pull stream source') + } catch (err) { + expect(err.message).to.contain('Don\'t know how to convert -1 into a pull stream source') + } }) - it('explodes if given an invalid path', () => { - return mfs.write('foo', null, { - create: true - }) - .then(() => expect(false).to.equal(true)) - .catch((error) => { - expect(error.message).to.contain('paths must start with a leading /') + it('explodes if given an invalid path', async () => { + try { + await mfs.write('foo', null, { + create: true }) + throw new Error('Did not object to invalid paths') + } catch (err) { + expect(err.message).to.contain('paths must start with a leading /') + } }) - it('explodes if given a negtive offset', () => { - return mfs.write('/foo.txt', Buffer.from('foo'), { - offset: -1 - }) - .then(() => expect(false).to.equal(true)) - .catch((error) => { - expect(error.message).to.contain('cannot have negative write offset') + it('explodes if given a negtive offset', async () => { + try { + await mfs.write('/foo.txt', Buffer.from('foo'), { + offset: -1 }) + throw new Error('Did not object to negative write offset') + } catch (err) { + expect(err.message).to.contain('cannot have negative write offset') + } }) - it('explodes if given a negative length', () => { - return mfs.write('/foo.txt', Buffer.from('foo'), { - length: -1 - }) - .then(() => expect(false).to.equal(true)) - .catch((error) => { - expect(error.message).to.contain('cannot have negative byte count') + it('explodes if given a negative length', async () => { + try { + await mfs.write('/foo.txt', Buffer.from('foo'), { + length: -1 }) + throw new Error('Did not object to negative byte count') + } catch (err) { + expect(err.message).to.contain('cannot have negative byte count') + } }) - it('creates a zero length file when passed a zero length', () => { - return mfs.write('/foo.txt', Buffer.from('foo'), { + it('creates a zero length file when passed a zero length', async () => { + await mfs.write('/foo.txt', Buffer.from('foo'), { length: 0, create: true }) - .then(() => mfs.ls('/', { - long: true - })) - .then((files) => { - expect(files.length).to.equal(1) - expect(files[0].name).to.equal('foo.txt') - expect(files[0].size).to.equal(0) - }) + + const files = await mfs.ls('/', { + long: true + }) + + expect(files.length).to.equal(1) + expect(files[0].name).to.equal('foo.txt') + expect(files[0].size).to.equal(0) }) - it('writes a small file using a buffer', () => { + it('writes a small file using a buffer', async () => { const filePath = `/small-file-${Math.random()}.txt` - return mfs.write(filePath, smallFile, { + await mfs.write(filePath, smallFile, { create: true }) - .then(() => mfs.stat(filePath)) - .then((stats) => { - expect(stats.size).to.equal(smallFile.length) - }) + const stats = await mfs.stat(filePath) + + expect(stats.size).to.equal(smallFile.length) }) - it('writes a small file using a path (Node only)', function () { + it('writes a small file using a path (Node only)', async function () { if (!isNode) { return this.skip() } @@ -150,16 +150,16 @@ describe('write', function () { const filePath = `/small-file-${Math.random()}.txt` const pathToFile = path.resolve(path.join(__dirname, 'fixtures', 'small-file.txt')) - return mfs.write(filePath, pathToFile, { + await mfs.write(filePath, pathToFile, { create: true }) - .then(() => mfs.stat(filePath)) - .then((stats) => { - expect(stats.size).to.equal(smallFile.length) - }) + + const stats = await mfs.stat(filePath) + + expect(stats.size).to.equal(smallFile.length) }) - it('writes part of a small file using a path (Node only)', function () { + it('writes part of a small file using a path (Node only)', async function () { if (!isNode) { return this.skip() } @@ -167,17 +167,17 @@ describe('write', function () { const filePath = `/small-file-${Math.random()}.txt` const pathToFile = path.resolve(path.join(__dirname, 'fixtures', 'small-file.txt')) - return mfs.write(filePath, pathToFile, { + await mfs.write(filePath, pathToFile, { create: true, length: 2 }) - .then(() => mfs.stat(filePath)) - .then((stats) => { - expect(stats.size).to.equal(2) - }) + + const stats = await mfs.stat(filePath) + + expect(stats.size).to.equal(2) }) - it('writes a small file using a Node stream (Node only)', function () { + it('writes a small file using a Node stream (Node only)', async function () { if (!isNode) { return this.skip() } @@ -186,28 +186,28 @@ describe('write', function () { const pathToFile = path.resolve(path.join(__dirname, 'fixtures', 'small-file.txt')) const stream = fs.createReadStream(pathToFile) - return mfs.write(filePath, stream, { + await mfs.write(filePath, stream, { create: true }) - .then(() => mfs.stat(filePath)) - .then((stats) => { - expect(stats.size).to.equal(smallFile.length) - }) + + const stats = await mfs.stat(filePath) + + expect(stats.size).to.equal(smallFile.length) }) - it('writes a small file using a pull stream source', function () { + it('writes a small file using a pull stream source', async function () { const filePath = `/small-file-${Math.random()}.txt` - return mfs.write(filePath, values([smallFile]), { + await mfs.write(filePath, values([smallFile]), { create: true }) - .then(() => mfs.stat(filePath)) - .then((stats) => { - expect(stats.size).to.equal(smallFile.length) - }) + + const stats = await mfs.stat(filePath) + + expect(stats.size).to.equal(smallFile.length) }) - it('writes a small file using an HTML5 Blob (Browser only)', function () { + it('writes a small file using an HTML5 Blob (Browser only)', async function () { if (!global.Blob) { return this.skip() } @@ -215,13 +215,13 @@ describe('write', function () { const filePath = `/small-file-${Math.random()}.txt` const blob = new global.Blob([smallFile.buffer.slice(smallFile.byteOffset, smallFile.byteOffset + smallFile.byteLength)]) - return mfs.write(filePath, blob, { + await mfs.write(filePath, blob, { create: true }) - .then(() => mfs.stat(filePath)) - .then((stats) => { - expect(stats.size).to.equal(smallFile.length) - }) + + const stats = await mfs.stat(filePath) + + expect(stats.size).to.equal(smallFile.length) }) it('writes a small file with an escaped slash in the title', async () => { @@ -238,48 +238,46 @@ describe('write', function () { try { await mfs.stat('/small-\\') throw new Error('Created path section before escape as directory') - } catch (error) { - expect(error.message).to.include('does not exist') + } catch (err) { + expect(err.message).to.include('does not exist') } }) - it('writes a deeply nested small file', () => { + it('writes a deeply nested small file', async () => { const filePath = '/foo/bar/baz/qux/quux/garply/small-file.txt' - return mfs.write(filePath, smallFile, { + await mfs.write(filePath, smallFile, { create: true, parents: true }) - .then(() => mfs.stat(filePath)) - .then((stats) => { - expect(stats.size).to.equal(smallFile.length) - }) + + const stats = await mfs.stat(filePath) + + expect(stats.size).to.equal(smallFile.length) }) - it('refuses to write to a file in a folder that does not exist', () => { + it('refuses to write to a file in a folder that does not exist', async () => { const filePath = `/${Math.random()}/small-file.txt` - return mfs.write(filePath, smallFile, { - create: true - }) - .then(() => { - throw new Error('Writing a file to a non-existent folder without the --parents flag should have failed') - }) - .catch((error) => { - expect(error.message).to.contain('does not exist') + try { + await mfs.write(filePath, smallFile, { + create: true }) + throw new Error('Writing a file to a non-existent folder without the --parents flag should have failed') + } catch (err) { + expect(err.message).to.contain('does not exist') + } }) - it('refuses to write to a file that does not exist', () => { + it('refuses to write to a file that does not exist', async () => { const filePath = `/small-file-${Math.random()}.txt` - return mfs.write(filePath, smallFile) - .then(() => { - throw new Error('Writing a file to a non-existent file without the --create flag should have failed') - }) - .catch((error) => { - expect(error.message).to.contain('file does not exist') - }) + try { + await mfs.write(filePath, smallFile) + throw new Error('Writing a file to a non-existent file without the --create flag should have failed') + } catch (err) { + expect(err.message).to.contain('file does not exist') + } }) it('refuses to write to a path that has a file in it', async () => { @@ -295,178 +293,184 @@ describe('write', function () { }) throw new Error('Writing a path with a file in it should have failed') - } catch (error) { - expect(error.message).to.contain('Not a directory') + } catch (err) { + expect(err.message).to.contain('Not a directory') } }) runTest(({ type, path, content }) => { - it(`limits how many bytes to write to a file (${type})`, () => { - return mfs.write(path, content, { + it(`limits how many bytes to write to a file (${type})`, async () => { + await mfs.write(path, content, { create: true, parents: true, length: 2 }) - .then(() => mfs.read(path)) - .then((buffer) => { - expect(buffer.length).to.equal(2) - }) + + const buffer = await mfs.read(path) + expect(buffer.length).to.equal(2) }) }) runTest(({ type, path, content, contentSize }) => { - it(`overwrites start of a file without truncating (${type})`, () => { + it(`overwrites start of a file without truncating (${type})`, async () => { const newContent = Buffer.from('Goodbye world') - return mfs.write(path, content, { + await mfs.write(path, content, { create: true }) - .then(() => mfs.write(path, newContent)) - .then(() => mfs.stat(path)) - .then((stats) => expect(stats.size).to.equal(contentSize)) - .then(() => mfs.read(path, { - offset: 0, - length: newContent.length - })) - .then((buffer) => expect(buffer).to.deep.equal(newContent)) + await mfs.write(path, newContent) + + const stats = await mfs.stat(path) + expect(stats.size).to.equal(contentSize) + + const buffer = await mfs.read(path, { + offset: 0, + length: newContent.length + }) + expect(buffer).to.deep.equal(newContent) }) }) runTest(({ type, path, content, contentSize }) => { - it(`pads the start of a new file when an offset is specified (${type})`, () => { + it(`pads the start of a new file when an offset is specified (${type})`, async () => { const offset = 10 - return mfs.write(path, content, { + await mfs.write(path, content, { offset, create: true }) - .then(() => mfs.stat(path)) - .then((stats) => { - expect(stats.size).to.equal(offset + contentSize) - }) - .then(() => mfs.read(path, { - offset: 0, - length: offset - })) - .then((buffer) => { - expect(buffer).to.deep.equal(Buffer.alloc(offset, 0)) - }) + + const stats = await mfs.stat(path) + expect(stats.size).to.equal(offset + contentSize) + + const buffer = await mfs.read(path, { + offset: 0, + length: offset + }) + expect(buffer).to.deep.equal(Buffer.alloc(offset, 0)) }) }) runTest(({ type, path, content, contentSize }) => { - it(`expands a file when an offset is specified (${type})`, () => { + it(`expands a file when an offset is specified (${type})`, async () => { const offset = contentSize - 1 const newContent = Buffer.from('Oh hai!') - return mfs.write(path, content, { + await mfs.write(path, content, { create: true }) - .then(() => mfs.write(path, newContent, { - offset - })) - .then(() => mfs.stat(path)) - .then((stats) => expect(stats.size).to.equal(contentSize + newContent.length - 1)) - .then(() => mfs.read(path, { - offset - })) - .then((buffer) => expect(buffer).to.deep.equal(newContent)) + + await mfs.write(path, newContent, { + offset + }) + + const stats = await mfs.stat(path) + expect(stats.size).to.equal(contentSize + newContent.length - 1) + + const buffer = await mfs.read(path, { + offset + }) + expect(buffer).to.deep.equal(newContent) }) }) runTest(({ type, path, content, contentSize }) => { - it(`expands a file when an offset is specified and the offset is longer than the file (${type})`, () => { + it(`expands a file when an offset is specified and the offset is longer than the file (${type})`, async () => { const offset = contentSize + 5 const newContent = Buffer.from('Oh hai!') - return mfs.write(path, content, { + await mfs.write(path, content, { create: true }) - .then(() => mfs.write(path, newContent, { - offset - })) - .then(() => mfs.stat(path)) - .then((stats) => expect(stats.size).to.equal(newContent.length + offset)) - .then(() => mfs.read(path, { - offset: offset - 5 - })) - .then((buffer) => { - expect(buffer).to.deep.equal(Buffer.concat([Buffer.from([0, 0, 0, 0, 0]), newContent])) - }) + await mfs.write(path, newContent, { + offset + }) + + const stats = await mfs.stat(path) + expect(stats.size).to.equal(newContent.length + offset) + + const buffer = await mfs.read(path, { + offset: offset - 5 + }) + expect(buffer).to.deep.equal(Buffer.concat([Buffer.from([0, 0, 0, 0, 0]), newContent])) }) }) runTest(({ type, path, content }) => { - it(`truncates a file after writing (${type})`, () => { + it(`truncates a file after writing (${type})`, async () => { const newContent = Buffer.from('Oh hai!') - return mfs.write(path, content, { + await mfs.write(path, content, { create: true }) - .then(() => mfs.write(path, newContent, { - truncate: true - })) - .then(() => mfs.stat(path)) - .then((stats) => expect(stats.size).to.equal(newContent.length)) - .then(() => mfs.read(path)) - .then((buffer) => expect(buffer).to.deep.equal(newContent)) + await mfs.write(path, newContent, { + truncate: true + }) + + const stats = await mfs.stat(path) + expect(stats.size).to.equal(newContent.length) + + const buffer = await mfs.read(path) + expect(buffer).to.deep.equal(newContent) }) }) runTest(({ type, path, content }) => { - it(`truncates a file after writing with a stream (${type})`, () => { + it(`truncates a file after writing with a stream (${type})`, async () => { const newContent = Buffer.from('Oh hai!') const stream = values([newContent]) - return mfs.write(path, content, { + await mfs.write(path, content, { create: true }) - .then(() => mfs.write(path, stream, { - truncate: true - })) - .then(() => mfs.stat(path)) - .then((stats) => expect(stats.size).to.equal(newContent.length)) - .then(() => mfs.read(path)) - .then((buffer) => expect(buffer).to.deep.equal(newContent)) + await mfs.write(path, stream, { + truncate: true + }) + + const stats = await mfs.stat(path) + expect(stats.size).to.equal(newContent.length) + + const buffer = await mfs.read(path) + expect(buffer).to.deep.equal(newContent) }) }) runTest(({ type, path, content }) => { - it(`truncates a file after writing with a stream with an offset (${type})`, () => { + it(`truncates a file after writing with a stream with an offset (${type})`, async () => { const offset = 100 const newContent = Buffer.from('Oh hai!') const stream = values([newContent]) - return mfs.write(path, content, { + await mfs.write(path, content, { create: true }) - .then(() => mfs.write(path, stream, { - truncate: true, - offset - })) - .then(() => mfs.stat(path)) - .then((stats) => expect(stats.size).to.equal(offset + newContent.length)) + await mfs.write(path, stream, { + truncate: true, + offset + }) + + const stats = await mfs.stat(path) + expect(stats.size).to.equal(offset + newContent.length) }) }) runTest(({ type, path, content }) => { - it(`writes a file with raw blocks for newly created leaf nodes (${type})`, () => { - return mfs.write(path, content, { + it(`writes a file with raw blocks for newly created leaf nodes (${type})`, async () => { + await mfs.write(path, content, { create: true, rawLeaves: true }) - .then(() => mfs.stat(path)) - .then((stats) => collectLeafCids(mfs, stats.hash)) - .then((cids) => { - const rawNodes = cids - .filter(cid => cid.codec === 'raw') - expect(rawNodes).to.not.be.empty() - }) + const stats = await mfs.stat(path) + const cids = await collectLeafCids(mfs, stats.hash) + const rawNodes = cids + .filter(cid => cid.codec === 'raw') + + expect(rawNodes).to.not.be.empty() }) }) - it('supports concurrent writes', function () { + it('supports concurrent writes', async function () { const files = [] for (let i = 0; i < 10; i++) { @@ -476,23 +480,22 @@ describe('write', function () { }) } - return Promise.all( + await Promise.all( files.map(({ name, source }) => mfs.write(`/concurrent/${name}`, source, { create: true, parents: true })) ) - .then(() => mfs.ls('/concurrent')) - .then(listing => { - expect(listing.length).to.equal(files.length) - listing.forEach(listedFile => { - expect(files.find(file => file.name === listedFile.name)) - }) - }) + const listing = await mfs.ls('/concurrent') + expect(listing.length).to.equal(files.length) + + listing.forEach(listedFile => { + expect(files.find(file => file.name === listedFile.name)) + }) }) - it('rewrites really big files', function () { + it('rewrites really big files', async function () { let expectedBytes = Buffer.alloc(0) let originalBytes = Buffer.alloc(0) const initialStream = bufferStream(1024 * 300, { @@ -508,27 +511,28 @@ describe('write', function () { const fileName = `/rewrite/file-${Math.random()}.txt` - return mfs.write(fileName, initialStream, { + await mfs.write(fileName, initialStream, { create: true, parents: true }) - .then(() => mfs.write(fileName, newDataStream, { - offset: 0 - })) - .then(() => mfs.read(fileName)) - .then(actualBytes => { - for (var i = 0; i < expectedBytes.length; i++) { - if (expectedBytes[i] !== actualBytes[i]) { - if (originalBytes[i] === actualBytes[i]) { - throw new Error(`Bytes at index ${i} were not overwritten - expected ${expectedBytes[i]} actual ${originalBytes[i]}`) - } - - throw new Error(`Bytes at index ${i} not equal - expected ${expectedBytes[i]} actual ${actualBytes[i]}`) - } + + await mfs.write(fileName, newDataStream, { + offset: 0 + }) + + const actualBytes = await mfs.read(fileName) + + for (var i = 0; i < expectedBytes.length; i++) { + if (expectedBytes[i] !== actualBytes[i]) { + if (originalBytes[i] === actualBytes[i]) { + throw new Error(`Bytes at index ${i} were not overwritten - expected ${expectedBytes[i]} actual ${originalBytes[i]}`) } - expect(actualBytes).to.deep.equal(expectedBytes) - }) + throw new Error(`Bytes at index ${i} not equal - expected ${expectedBytes[i]} actual ${actualBytes[i]}`) + } + } + + expect(actualBytes).to.deep.equal(expectedBytes) }) it('shards a large directory when writing too many links to it', async () => {