From a8bc46c3a6b7664fe30ee9bf4f133b933cf2f343 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Mon, 22 Jan 2018 19:11:21 +0000 Subject: [PATCH 01/11] Update FILES.md --- SPEC/FILES.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/SPEC/FILES.md b/SPEC/FILES.md index 5f808307..af24a3d4 100644 --- a/SPEC/FILES.md +++ b/SPEC/FILES.md @@ -539,6 +539,20 @@ pull( A great source of [examples][] can be found in the tests for this API. +--- + +# MFS (WIP) + +ipfs.files.cp +ipfs.files.ls +ipfs.files.mkdir +ipfs.files.stat +ipfs.files.rm +ipfs.files.read +ipfs.files.write +ipfs.files.mv +ipfs.files.flush(path, [callback]) + [examples]: https://github.com/ipfs/interface-ipfs-core/blob/master/src/files.js [b]: https://www.npmjs.com/package/buffer [rs]: https://www.npmjs.com/package/readable-stream From 5d287c214483568ed52e0fcee5d6b68fe520d157 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Mon, 22 Jan 2018 19:46:42 +0000 Subject: [PATCH 02/11] Update FILES.md --- SPEC/FILES.md | 98 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 89 insertions(+), 9 deletions(-) diff --git a/SPEC/FILES.md b/SPEC/FILES.md index af24a3d4..6a6e3ccb 100644 --- a/SPEC/FILES.md +++ b/SPEC/FILES.md @@ -543,15 +543,95 @@ A great source of [examples][] can be found in the tests for this API. # MFS (WIP) -ipfs.files.cp -ipfs.files.ls -ipfs.files.mkdir -ipfs.files.stat -ipfs.files.rm -ipfs.files.read -ipfs.files.write -ipfs.files.mv -ipfs.files.flush(path, [callback]) +#### `cp` + +> Copy files. + +##### `Go` **WIP** + +##### `JavaScript` - ipfs.files.cp([from, to], [callback]) + +TODO + +#### `ls` + +> Get directory contents. + +##### `Go` **WIP** + +##### `JavaScript` - ipfs.files.ls(path, [options, callback]) + +TODO + +#### `mkdir` + +> Make a directory. + +##### `Go` **WIP** + +##### `JavaScript` - ipfs.files.mkdir(path, [options, callback]) + +TODO + +#### `stat` + +> Get file or directory status. + +##### `Go` **WIP** + +##### `JavaScript` - ipfs.files.stat(path, [options, callback]) + +TODO + +#### `rm` + +> Remove a file or directory. + +##### `Go` **WIP** + +##### `JavaScript` - ipfs.files.rm(path, [options, callback]) + +TODO + +#### `read` + +> Read a file. + +##### `Go` **WIP** + +##### `JavaScript` - ipfs.files.read(path, [options, callback]) + +TODO + +#### `write` + +> Write to a file. + +##### `Go` **WIP** + +##### `JavaScript` - ipfs.files.write(path, [options, callback]) + +TODO + +#### `mv` + +> Move files. + +##### `Go` **WIP** + +##### `JavaScript` - ipfs.files.cp([from, to], [callback]) + +TODO + +#### `flush` + +> Flush a given path's data to the disk + +##### `Go` **WIP** + +##### `JavaScript` - ipfs.files.cp(path, [callback]) + +TODO [examples]: https://github.com/ipfs/interface-ipfs-core/blob/master/src/files.js [b]: https://www.npmjs.com/package/buffer From 79363a962d08b529810071e4e376aa78d2027fae Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Mon, 22 Jan 2018 19:59:28 +0000 Subject: [PATCH 03/11] Update FILES.md --- SPEC/FILES.md | 106 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 100 insertions(+), 6 deletions(-) diff --git a/SPEC/FILES.md b/SPEC/FILES.md index 6a6e3ccb..e4e0db59 100644 --- a/SPEC/FILES.md +++ b/SPEC/FILES.md @@ -551,7 +551,24 @@ A great source of [examples][] can be found in the tests for this API. ##### `JavaScript` - ipfs.files.cp([from, to], [callback]) -TODO +Where: + +- `from` is the path of the source object to copy. +- `to` is the path of the destination object to copy to. + +`callback` must follow the `function (err) {}` signature, where `err` is an Error if the operation was not successful. + +If no `callback` is passed, a promise is returned. + +**Example:** + +```JavaScript +ipfs.files.cp(['/src-file', '/dst-file'], (err) => { + if (err) { + console.error(err) + } +}) +``` #### `ls` @@ -571,7 +588,25 @@ TODO ##### `JavaScript` - ipfs.files.mkdir(path, [options, callback]) -TODO +Where: + +- `path` is the path to the directory to make. +- `options` is an optional Object that might contain the following keys: + - `parents` is a Boolean value to decide whether or not to make the parent directories if they don't exist. + +`callback` must follow the `function (err) {}` signature, where `err` is an Error if the operation was not successful. + +If no `callback` is passed, a promise is returned. + +**Example:** + +```JavaScript +ipfs.files.mkdir('/my/beautiful/directory', (err) => { + if (err) { + console.error(err) + } +}) +``` #### `stat` @@ -591,7 +626,33 @@ TODO ##### `JavaScript` - ipfs.files.rm(path, [options, callback]) -TODO +Where: + +- `path` is the path of the object to remove. +- `options` is an optional Object that might contain the following keys: + - `recursive` is a Boolean value to decide whether or not to remove directories recursively. + +`callback` must follow the `function (err) {}` signature, where `err` is an Error if the operation was not successful. + +If no `callback` is passed, a promise is returned. + +**Example:** + +```JavaScript +// To remove a file +ipfs.files.mkdir('/my/beautiful/file.txt', (err) => { + if (err) { + console.error(err) + } +}) + +// To remove a directory +ipfs.files.mkdir('/my/beautiful/directory', { recursive: true }, (err) => { + if (err) { + console.error(err) + } +}) +``` #### `read` @@ -621,7 +682,24 @@ TODO ##### `JavaScript` - ipfs.files.cp([from, to], [callback]) -TODO +Where: + +- `from` is the path of the source object to move. +- `to` is the path of the destination object to move to. + +`callback` must follow the `function (err) {}` signature, where `err` is an Error if the operation was not successful. + +If no `callback` is passed, a promise is returned. + +**Example:** + +```JavaScript +ipfs.files.mv(['/src-file', '/dst-file'], (err) => { + if (err) { + console.error(err) + } +}) +``` #### `flush` @@ -629,9 +707,25 @@ TODO ##### `Go` **WIP** -##### `JavaScript` - ipfs.files.cp(path, [callback]) +##### `JavaScript` - ipfs.files.cp([path, callback]) -TODO +Where: + +- `path` is the path to flush. Default is `/`. + +`callback` must follow the `function (err) {}` signature, where `err` is an Error if the operation was not successful. + +If no `callback` is passed, a promise is returned. + +**Example:** + +```JavaScript +ipfs.files.flush('/', (err) => { + if (err) { + console.error(err) + } +}) +``` [examples]: https://github.com/ipfs/interface-ipfs-core/blob/master/src/files.js [b]: https://www.npmjs.com/package/buffer From c20ae1a3f4f0b9c90da27b2e57eb221e37372d9e Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Mon, 22 Jan 2018 20:08:08 +0000 Subject: [PATCH 04/11] Update FILES.md --- SPEC/FILES.md | 53 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 13 deletions(-) diff --git a/SPEC/FILES.md b/SPEC/FILES.md index e4e0db59..3ee4fb18 100644 --- a/SPEC/FILES.md +++ b/SPEC/FILES.md @@ -539,13 +539,9 @@ pull( A great source of [examples][] can be found in the tests for this API. ---- - -# MFS (WIP) - #### `cp` -> Copy files. +> Mutable File System specific. Copy files. ##### `Go` **WIP** @@ -572,7 +568,7 @@ ipfs.files.cp(['/src-file', '/dst-file'], (err) => { #### `ls` -> Get directory contents. +> Mutable File System specific. Get directory contents. ##### `Go` **WIP** @@ -582,7 +578,7 @@ TODO #### `mkdir` -> Make a directory. +> Mutable File System specific. Make a directory. ##### `Go` **WIP** @@ -610,13 +606,44 @@ ipfs.files.mkdir('/my/beautiful/directory', (err) => { #### `stat` -> Get file or directory status. +> Mutable File System specific. Get file or directory status. ##### `Go` **WIP** ##### `JavaScript` - ipfs.files.stat(path, [options, callback]) -TODO +Where: + +- `path` is the path to the directory to make. +- `options` is an optional Object that might contain the following keys: + - `hash` is a Boolean value to return only the hash. + - `size` is a Boolean value to return only the size. + +`callback` must follow the `function (err, stat) {}` signature, where `err` is an Error if the operation was not successful and `stat` is an Object with the following keys: + +- `hash` is a string with the hash. +- `size` is an integer with the size in Bytes. +- `cumulativeSize` is an integer with the cumulative size in Bytes. +- `blocks` is an integer indicating the number of blocks. +- `type` is a string that can be either `directory` or `file`. + +If no `callback` is passed, a promise is returned. + +**Example:** + +```JavaScript +ipfs.files.stat('/file.txt', (err, stats) => { + console.log(stats) +}) + +// { +// hash: 'QmXmJBmnYqXVuicUfn9uDCC8kxCEEzQpsAbeq1iJvLAmVs', +// size: 60, +// cumulativeSize: 118, +// blocks: 1, +// type: 'file' +// } +``` #### `rm` @@ -656,7 +683,7 @@ ipfs.files.mkdir('/my/beautiful/directory', { recursive: true }, (err) => { #### `read` -> Read a file. +> Mutable File System specific. Read a file. ##### `Go` **WIP** @@ -666,7 +693,7 @@ TODO #### `write` -> Write to a file. +> Mutable File System specific. Write to a file. ##### `Go` **WIP** @@ -676,7 +703,7 @@ TODO #### `mv` -> Move files. +> Mutable File System specific. Move files. ##### `Go` **WIP** @@ -703,7 +730,7 @@ ipfs.files.mv(['/src-file', '/dst-file'], (err) => { #### `flush` -> Flush a given path's data to the disk +> Mutable File System specific. Flush a given path's data to the disk ##### `Go` **WIP** From a62686ff8834dc3f7ac7bc4fde1c302bdf8dfc01 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Mon, 22 Jan 2018 20:14:15 +0000 Subject: [PATCH 05/11] Update FILES.md --- SPEC/FILES.md | 57 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/SPEC/FILES.md b/SPEC/FILES.md index 3ee4fb18..a1417610 100644 --- a/SPEC/FILES.md +++ b/SPEC/FILES.md @@ -566,16 +566,6 @@ ipfs.files.cp(['/src-file', '/dst-file'], (err) => { }) ``` -#### `ls` - -> Mutable File System specific. Get directory contents. - -##### `Go` **WIP** - -##### `JavaScript` - ipfs.files.ls(path, [options, callback]) - -TODO - #### `mkdir` > Mutable File System specific. Make a directory. @@ -689,7 +679,26 @@ ipfs.files.mkdir('/my/beautiful/directory', { recursive: true }, (err) => { ##### `JavaScript` - ipfs.files.read(path, [options, callback]) -TODO +Where: + +- `path` is the path of the object to read. +- `options` is an optional Object that might contain the following keys: + - `offset` is an Integer with the byte offset to begin reading from. + - `count` is an Integer with the maximum number of bytes to read. + +`callback` must follow the `function (err, buf) {}` signature, where `err` is an Error if the operation was not successful and `buf` is a Buffer with the contents of `path`. + +If no `callback` is passed, a promise is returned. + +**Example:** + +```JavaScript +ipfs.files.read('/hello-world', (err, buf) => { + console.log(buf.toString()) +}) + +// Hello, World! +``` #### `write` @@ -697,9 +706,31 @@ TODO ##### `Go` **WIP** -##### `JavaScript` - ipfs.files.write(path, [options, callback]) +##### `JavaScript` - ipfs.files.write(path, content, [options, callback]) + +Where: + +- `path` is the path of the object to write. +- `content` can be: + - a Buffer instance. + - a Path (caveat: will only work in Node.js). +- `options` is an optional Object that might contain the following keys: + - `offset` is an Integer with the byte offset to begin writing at. + - `create` is a Boolean to indicate to create the file if it doesn't exist. + - `truncate` is a Boolean to indicate if the file should be truncated to size 0 before writing. + - `count` is an Integer with the maximum number of bytes to read. + +`callback` must follow the `function (err) {}` signature, where `err` is an Error if the operation was not successful. + +If no `callback` is passed, a promise is returned. + +**Example:** -TODO +```JavaScript +ipfs.files.write('/hello-world', Buffer.from('Hello, world!'), (err) => { + console.log(err) +}) +``` #### `mv` From 9dcb7132737810f220e87a40aa627f231d650329 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Tue, 23 Jan 2018 10:15:04 +0000 Subject: [PATCH 06/11] Update FILES.md --- SPEC/FILES.md | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/SPEC/FILES.md b/SPEC/FILES.md index a1417610..3ad4baf9 100644 --- a/SPEC/FILES.md +++ b/SPEC/FILES.md @@ -387,6 +387,10 @@ pull( A great source of [examples][] can be found in the tests for this API. +### Mutable File System specific + +The Mutable File System (MFS) is a peer-to-peer file system where every file and directory can be addressed by an hash. + #### `ls` > Lists a directory from IPFS that is addressed by a valid IPFS Path. @@ -541,7 +545,7 @@ A great source of [examples][] can be found in the tests for this API. #### `cp` -> Mutable File System specific. Copy files. +> Copy files. ##### `Go` **WIP** @@ -568,7 +572,7 @@ ipfs.files.cp(['/src-file', '/dst-file'], (err) => { #### `mkdir` -> Mutable File System specific. Make a directory. +> Make a directory. ##### `Go` **WIP** @@ -596,7 +600,7 @@ ipfs.files.mkdir('/my/beautiful/directory', (err) => { #### `stat` -> Mutable File System specific. Get file or directory status. +> Get file or directory status. ##### `Go` **WIP** @@ -673,7 +677,7 @@ ipfs.files.mkdir('/my/beautiful/directory', { recursive: true }, (err) => { #### `read` -> Mutable File System specific. Read a file. +> Read a file. ##### `Go` **WIP** @@ -702,7 +706,7 @@ ipfs.files.read('/hello-world', (err, buf) => { #### `write` -> Mutable File System specific. Write to a file. +> Write to a file. ##### `Go` **WIP** @@ -734,7 +738,7 @@ ipfs.files.write('/hello-world', Buffer.from('Hello, world!'), (err) => { #### `mv` -> Mutable File System specific. Move files. +> Move files. ##### `Go` **WIP** @@ -761,7 +765,7 @@ ipfs.files.mv(['/src-file', '/dst-file'], (err) => { #### `flush` -> Mutable File System specific. Flush a given path's data to the disk +> Flush a given path's data to the disk ##### `Go` **WIP** From 9457fe61012e155a25dd032c85a8ae98174706cc Mon Sep 17 00:00:00 2001 From: David Dias Date: Tue, 23 Jan 2018 17:53:00 -0800 Subject: [PATCH 07/11] update wording --- SPEC/FILES.md | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/SPEC/FILES.md b/SPEC/FILES.md index 3ad4baf9..c4d90895 100644 --- a/SPEC/FILES.md +++ b/SPEC/FILES.md @@ -387,9 +387,6 @@ pull( A great source of [examples][] can be found in the tests for this API. -### Mutable File System specific - -The Mutable File System (MFS) is a peer-to-peer file system where every file and directory can be addressed by an hash. #### `ls` @@ -431,7 +428,7 @@ If no `callback` is passed, a promise is returned. ```JavaScript const validCID = 'QmQ2r6iMNpky5f1m4cnm3Yqw8VSvjuKpTcK1X7dBR1LkJF' -ipfs.files.ls(validCID, function (err, files) { +ipfs.ls(validCID, function (err, files) { files.forEach((file) => { console.log(file.path) }) @@ -543,6 +540,15 @@ pull( A great source of [examples][] can be found in the tests for this API. +-------------------------------------------------------------------------------------------------- +-------------------------------------------------------------------------------------------------- + +Mutable File System +=================== + +The Mutable File System (MFS) is a virtual file system on top of IPFS that exposes a Unix like API over a virtual directory. It enables users to write and read from paths without having to worry about updating the graph. It enables things like [ipfs-blob-store](https://github.com/ipfs/ipfs-blob-store) to exist. + + #### `cp` > Copy files. From ee69b6360ccaa49fc7865028ca8b3df77a09600d Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Wed, 24 Jan 2018 14:55:07 +0000 Subject: [PATCH 08/11] Add files.ls3 --- SPEC/FILES.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/SPEC/FILES.md b/SPEC/FILES.md index c4d90895..c476d1df 100644 --- a/SPEC/FILES.md +++ b/SPEC/FILES.md @@ -795,6 +795,42 @@ ipfs.files.flush('/', (err) => { }) ``` +#### `ls` + +> List directories in the local mutable namespace. + +##### `Go` **WIP** + +##### `JavaScript` - ipfs.files.ls([path, options, callback]) + +Where: + +- `path` is the path to show listing for. Defaults to `/`. +- `options` is an optional Object that might contain the following keys: + - `l` is a Boolean value o use long listing format. + +`callback` must follow `function (err, files) {}` signature, where `err` is an error if the operation was not successful. `files` is an array containing Objects that contain the following keys: + +- `name` which is the file's name. +- `type` which i the object's type (`directory` or `file`). +- `size` the size of the file in bytes. +- `hash` the hash of the file. + +If no `callback` is passed, a promise is returned. + +**Example:** + +```JavaScript +ipfs.files.ls('/screenshots', function (err, files) { + files.forEach((file) => { + console.log(file.name) + }) +}) + +// 2018-01-22T18:08:46.775Z.png +// 2018-01-22T18:08:49.184Z.png +``` + [examples]: https://github.com/ipfs/interface-ipfs-core/blob/master/src/files.js [b]: https://www.npmjs.com/package/buffer [rs]: https://www.npmjs.com/package/readable-stream From b7f95e03f7e405d68ef94d6b368cdcafcf1c934b Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Thu, 25 Jan 2018 09:34:46 +0000 Subject: [PATCH 09/11] Tests --- SPEC/FILES.md | 4 +- src/files.js | 395 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 396 insertions(+), 3 deletions(-) diff --git a/SPEC/FILES.md b/SPEC/FILES.md index c476d1df..ce5ac4ef 100644 --- a/SPEC/FILES.md +++ b/SPEC/FILES.md @@ -748,7 +748,7 @@ ipfs.files.write('/hello-world', Buffer.from('Hello, world!'), (err) => { ##### `Go` **WIP** -##### `JavaScript` - ipfs.files.cp([from, to], [callback]) +##### `JavaScript` - ipfs.files.mv([from, to], [callback]) Where: @@ -775,7 +775,7 @@ ipfs.files.mv(['/src-file', '/dst-file'], (err) => { ##### `Go` **WIP** -##### `JavaScript` - ipfs.files.cp([path, callback]) +##### `JavaScript` - ipfs.files.flush([path, callback]) Where: diff --git a/src/files.js b/src/files.js index 41725156..8131fe7b 100644 --- a/src/files.js +++ b/src/files.js @@ -24,6 +24,7 @@ module.exports = (common) => { this.timeout(40 * 1000) let ipfs + let withGo function fixture (path) { return loadFixture(__dirname, path, 'interface-ipfs-core') @@ -60,7 +61,10 @@ module.exports = (common) => { factory.spawnNode((err, node) => { expect(err).to.not.exist() ipfs = node - done() + node.id((err, id) => { + withGo = id.agentVersion.startsWith('go-ipfs') + done() + }) }) }) }) @@ -980,5 +984,394 @@ module.exports = (common) => { ) }) }) + + describe('.mkdir', function () { + it('make directory on root', (done) => { + if (!withGo) { + console.log('Only supported by go-ipfs yet') + return done() + } + + ipfs.files.mkdir('/test', (err) => { + expect(err).to.not.exist() + done() + }) + }) + + it('make directory and its parents', (done) => { + if (!withGo) { + console.log('Only supported by go-ipfs yet') + return done() + } + + ipfs.files.mkdir('/test/lv1/lv2', { p: true }, (err) => { + expect(err).to.not.exist() + done() + }) + }) + + it('make already existent directory', (done) => { + if (!withGo) { + console.log('Only supported by go-ipfs yet') + return done() + } + + ipfs.files.mkdir('/', (err) => { + expect(err).to.exist() + done() + }) + }) + }) + + describe('.write', function () { + it('expect error', (done) => { + if (!withGo) { + console.log('Only supported by go-ipfs yet') + return done() + } + + ipfs.files.write('/test/a', Buffer.from('Hello, world!'), (err) => { + expect(err).to.exist() + done() + }) + }) + + it('expect no error', (done) => { + if (!withGo) { + console.log('Only supported by go-ipfs yet') + return done() + } + + ipfs.files.write('/test/a', Buffer.from('Hello, world!'), {create: true}, (err) => { + expect(err).to.not.exist() + done() + }) + }) + }) + + describe('.cp', function () { + it('copy file, expect error', (done) => { + if (!withGo) { + console.log('Only supported by go-ipfs yet') + return done() + } + + ipfs.files.cp(['/test/c', '/test/b'], (err) => { + expect(err).to.exist() + done() + }) + }) + + it('copy file, expect no error', (done) => { + if (!withGo) { + console.log('Only supported by go-ipfs yet') + return done() + } + + ipfs.files.cp(['/test/a', '/test/b'], (err) => { + expect(err).to.not.exist() + done() + }) + }) + + it('copy dir, expect error', (done) => { + if (!withGo) { + console.log('Only supported by go-ipfs yet') + return done() + } + + ipfs.files.cp(['/test/lv1/lv3', '/test/lv1/lv4'], (err) => { + expect(err).to.exist() + done() + }) + }) + + it('copy dir, expect no error', (done) => { + if (!withGo) { + console.log('Only supported by go-ipfs yet') + return done() + } + + ipfs.files.cp(['/test/lv1/lv2', '/test/lv1/lv3'], (err) => { + expect(err).to.not.exist() + done() + }) + }) + }) + + describe('.mv', function () { + it('move file, expect error', (done) => { + if (!withGo) { + console.log('Only supported by go-ipfs yet') + return done() + } + + ipfs.files.mv(['/test/404', '/test/a'], (err) => { + expect(err).to.exist() + done() + }) + }) + + it('move file, expect no error', (done) => { + if (!withGo) { + console.log('Only supported by go-ipfs yet') + return done() + } + + ipfs.files.mv(['/test/a', '/test/c'], (err) => { + expect(err).to.not.exist() + done() + }) + }) + + it('move dir, expect error', (done) => { + if (!withGo) { + console.log('Only supported by go-ipfs yet') + return done() + } + + ipfs.files.mv(['/test/lv1/404', '/test/lv1'], (err) => { + expect(err).to.exist() + done() + }) + }) + + it('move dir, expect no error', (done) => { + if (!withGo) { + console.log('Only supported by go-ipfs yet') + return done() + } + + ipfs.files.mv(['/test/lv1/lv2', '/test/lv1/lv4'], (err) => { + expect(err).to.not.exist() + done() + }) + }) + }) + + describe('.rm', function () { + it('remove file, expect error', (done) => { + if (!withGo) { + console.log('Only supported by go-ipfs yet') + return done() + } + + ipfs.files.rm('/test/a', (err) => { + expect(err).to.exist() + done() + }) + }) + + it('remove file, expect no error', (done) => { + if (!withGo) { + console.log('Only supported by go-ipfs yet') + return done() + } + + ipfs.files.rm('/test/c', (err) => { + expect(err).to.not.exist() + done() + }) + }) + + it('remove dir, expect error', (done) => { + if (!withGo) { + console.log('Only supported by go-ipfs yet') + return done() + } + + ipfs.files.rm('/test/lv1/lv4', (err) => { + expect(err).to.exist() + done() + }) + }) + + it('remove dir, expect no error', (done) => { + if (!withGo) { + console.log('Only supported by go-ipfs yet') + return done() + } + + ipfs.files.rm('/test/lv1/lv4', {recursive: true}, (err) => { + expect(err).to.not.exist() + done() + }) + }) + }) + + describe('.stat', function () { + it('stat not found, expect error', (done) => { + if (!withGo) { + console.log('Only supported by go-ipfs yet') + return done() + } + + ipfs.files.stat('/test/404', (err) => { + expect(err).to.exist() + done() + }) + }) + + it('stat file', (done) => { + if (!withGo) { + console.log('Only supported by go-ipfs yet') + return done() + } + + ipfs.files.stat('/test/b', (err, stat) => { + expect(err).to.not.exist() + expect(stat).to.eql({ + type: 'file', + blocks: 1, + size: 13, + hash: 'QmcZojhwragQr5qhTeFAmELik623Z21e3jBTpJXoQ9si1T', + cumulativeSize: 71 + }) + done() + }) + }) + + it('stat dir', (done) => { + if (!withGo) { + console.log('Only supported by go-ipfs yet') + return done() + } + + ipfs.files.stat('/test', (err, stat) => { + expect(err).to.not.exist() + expect(stat).to.eql({ + type: 'directory', + blocks: 2, + size: 0, + hash: 'QmVrkkNurBCeJvPRohW5JTvJG4AxGrFg7FnmsZZUS6nJto', + cumulativeSize: 216 + }) + done() + }) + }) + }) + + describe('.read', function () { + it('read not found, expect error', (done) => { + if (!withGo) { + console.log('Only supported by go-ipfs yet') + return done() + } + + ipfs.files.read('/test/404', (err, buf) => { + expect(err).to.exist() + expect(buf).to.not.exist() + done() + }) + }) + + it('read file', (done) => { + if (!withGo) { + console.log('Only supported by go-ipfs yet') + return done() + } + + ipfs.files.read('/test/b', (err, buf) => { + expect(err).to.not.exist() + expect(buf).to.eql(Buffer.from('Hello, world!')) + done() + }) + }) + }) + + describe('.ls', function () { + it('ls not found, expect error', (done) => { + if (!withGo) { + console.log('Only supported by go-ipfs yet') + return done() + } + + ipfs.files.ls('/test/404', (err, info) => { + expect(err).to.exist() + expect(info).to.not.exist() + done() + }) + }) + + it('ls directory', (done) => { + if (!withGo) { + console.log('Only supported by go-ipfs yet') + return done() + } + + ipfs.files.ls('/test', (err, info) => { + expect(err).to.not.exist() + expect(info).to.eql([ + { name: 'b', type: 0, size: 0, hash: '' }, + { name: 'lv1', type: 0, size: 0, hash: '' } + ]) + done() + }) + }) + + it('ls -l directory', (done) => { + if (!withGo) { + console.log('Only supported by go-ipfs yet') + return done() + } + + ipfs.files.ls('/test', { l: true }, (err, info) => { + expect(err).to.not.exist() + expect(info).to.eql([ + { + name: 'b', + type: 0, + size: 13, + hash: 'QmcZojhwragQr5qhTeFAmELik623Z21e3jBTpJXoQ9si1T' + }, + { + name: 'lv1', + type: 1, + size: 0, + hash: 'QmaSPtNHYKPjNjQnYX9pdu5ocpKUQEL3itSz8LuZcoW6J5' + } + ]) + done() + }) + }) + }) + + describe('.flush', function () { + it('flush not found, expect error', (done) => { + if (!withGo) { + console.log('Only supported by go-ipfs yet') + return done() + } + + ipfs.files.flush('/test/404', (err) => { + expect(err).to.exist() + done() + }) + }) + + it('flush root', (done) => { + if (!withGo) { + console.log('Only supported by go-ipfs yet') + return done() + } + + ipfs.files.flush((err) => { + expect(err).to.not.exist() + done() + }) + }) + + it('flush specific dir', (done) => { + if (!withGo) { + console.log('Only supported by go-ipfs yet') + return done() + } + + ipfs.files.flush('/test', (err) => { + expect(err).to.not.exist() + done() + }) + }) + }) }) } From 9cf59aeb7f1b35ec5c6f0c624bdfa2ef730fe37e Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Thu, 25 Jan 2018 18:16:01 +0000 Subject: [PATCH 10/11] refactor: move mfs tests to other file --- src/files.js | 397 +---------------------------------------------- src/mfs.js | 427 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 434 insertions(+), 390 deletions(-) create mode 100644 src/mfs.js diff --git a/src/files.js b/src/files.js index 8131fe7b..4f8bd912 100644 --- a/src/files.js +++ b/src/files.js @@ -18,8 +18,9 @@ const through = require('through2') const path = require('path') const bl = require('bl') const isNode = require('detect-node') +const mfs = require('./mfs') -module.exports = (common) => { +const tests = (common) => { describe('.files', function () { this.timeout(40 * 1000) @@ -984,394 +985,10 @@ module.exports = (common) => { ) }) }) - - describe('.mkdir', function () { - it('make directory on root', (done) => { - if (!withGo) { - console.log('Only supported by go-ipfs yet') - return done() - } - - ipfs.files.mkdir('/test', (err) => { - expect(err).to.not.exist() - done() - }) - }) - - it('make directory and its parents', (done) => { - if (!withGo) { - console.log('Only supported by go-ipfs yet') - return done() - } - - ipfs.files.mkdir('/test/lv1/lv2', { p: true }, (err) => { - expect(err).to.not.exist() - done() - }) - }) - - it('make already existent directory', (done) => { - if (!withGo) { - console.log('Only supported by go-ipfs yet') - return done() - } - - ipfs.files.mkdir('/', (err) => { - expect(err).to.exist() - done() - }) - }) - }) - - describe('.write', function () { - it('expect error', (done) => { - if (!withGo) { - console.log('Only supported by go-ipfs yet') - return done() - } - - ipfs.files.write('/test/a', Buffer.from('Hello, world!'), (err) => { - expect(err).to.exist() - done() - }) - }) - - it('expect no error', (done) => { - if (!withGo) { - console.log('Only supported by go-ipfs yet') - return done() - } - - ipfs.files.write('/test/a', Buffer.from('Hello, world!'), {create: true}, (err) => { - expect(err).to.not.exist() - done() - }) - }) - }) - - describe('.cp', function () { - it('copy file, expect error', (done) => { - if (!withGo) { - console.log('Only supported by go-ipfs yet') - return done() - } - - ipfs.files.cp(['/test/c', '/test/b'], (err) => { - expect(err).to.exist() - done() - }) - }) - - it('copy file, expect no error', (done) => { - if (!withGo) { - console.log('Only supported by go-ipfs yet') - return done() - } - - ipfs.files.cp(['/test/a', '/test/b'], (err) => { - expect(err).to.not.exist() - done() - }) - }) - - it('copy dir, expect error', (done) => { - if (!withGo) { - console.log('Only supported by go-ipfs yet') - return done() - } - - ipfs.files.cp(['/test/lv1/lv3', '/test/lv1/lv4'], (err) => { - expect(err).to.exist() - done() - }) - }) - - it('copy dir, expect no error', (done) => { - if (!withGo) { - console.log('Only supported by go-ipfs yet') - return done() - } - - ipfs.files.cp(['/test/lv1/lv2', '/test/lv1/lv3'], (err) => { - expect(err).to.not.exist() - done() - }) - }) - }) - - describe('.mv', function () { - it('move file, expect error', (done) => { - if (!withGo) { - console.log('Only supported by go-ipfs yet') - return done() - } - - ipfs.files.mv(['/test/404', '/test/a'], (err) => { - expect(err).to.exist() - done() - }) - }) - - it('move file, expect no error', (done) => { - if (!withGo) { - console.log('Only supported by go-ipfs yet') - return done() - } - - ipfs.files.mv(['/test/a', '/test/c'], (err) => { - expect(err).to.not.exist() - done() - }) - }) - - it('move dir, expect error', (done) => { - if (!withGo) { - console.log('Only supported by go-ipfs yet') - return done() - } - - ipfs.files.mv(['/test/lv1/404', '/test/lv1'], (err) => { - expect(err).to.exist() - done() - }) - }) - - it('move dir, expect no error', (done) => { - if (!withGo) { - console.log('Only supported by go-ipfs yet') - return done() - } - - ipfs.files.mv(['/test/lv1/lv2', '/test/lv1/lv4'], (err) => { - expect(err).to.not.exist() - done() - }) - }) - }) - - describe('.rm', function () { - it('remove file, expect error', (done) => { - if (!withGo) { - console.log('Only supported by go-ipfs yet') - return done() - } - - ipfs.files.rm('/test/a', (err) => { - expect(err).to.exist() - done() - }) - }) - - it('remove file, expect no error', (done) => { - if (!withGo) { - console.log('Only supported by go-ipfs yet') - return done() - } - - ipfs.files.rm('/test/c', (err) => { - expect(err).to.not.exist() - done() - }) - }) - - it('remove dir, expect error', (done) => { - if (!withGo) { - console.log('Only supported by go-ipfs yet') - return done() - } - - ipfs.files.rm('/test/lv1/lv4', (err) => { - expect(err).to.exist() - done() - }) - }) - - it('remove dir, expect no error', (done) => { - if (!withGo) { - console.log('Only supported by go-ipfs yet') - return done() - } - - ipfs.files.rm('/test/lv1/lv4', {recursive: true}, (err) => { - expect(err).to.not.exist() - done() - }) - }) - }) - - describe('.stat', function () { - it('stat not found, expect error', (done) => { - if (!withGo) { - console.log('Only supported by go-ipfs yet') - return done() - } - - ipfs.files.stat('/test/404', (err) => { - expect(err).to.exist() - done() - }) - }) - - it('stat file', (done) => { - if (!withGo) { - console.log('Only supported by go-ipfs yet') - return done() - } - - ipfs.files.stat('/test/b', (err, stat) => { - expect(err).to.not.exist() - expect(stat).to.eql({ - type: 'file', - blocks: 1, - size: 13, - hash: 'QmcZojhwragQr5qhTeFAmELik623Z21e3jBTpJXoQ9si1T', - cumulativeSize: 71 - }) - done() - }) - }) - - it('stat dir', (done) => { - if (!withGo) { - console.log('Only supported by go-ipfs yet') - return done() - } - - ipfs.files.stat('/test', (err, stat) => { - expect(err).to.not.exist() - expect(stat).to.eql({ - type: 'directory', - blocks: 2, - size: 0, - hash: 'QmVrkkNurBCeJvPRohW5JTvJG4AxGrFg7FnmsZZUS6nJto', - cumulativeSize: 216 - }) - done() - }) - }) - }) - - describe('.read', function () { - it('read not found, expect error', (done) => { - if (!withGo) { - console.log('Only supported by go-ipfs yet') - return done() - } - - ipfs.files.read('/test/404', (err, buf) => { - expect(err).to.exist() - expect(buf).to.not.exist() - done() - }) - }) - - it('read file', (done) => { - if (!withGo) { - console.log('Only supported by go-ipfs yet') - return done() - } - - ipfs.files.read('/test/b', (err, buf) => { - expect(err).to.not.exist() - expect(buf).to.eql(Buffer.from('Hello, world!')) - done() - }) - }) - }) - - describe('.ls', function () { - it('ls not found, expect error', (done) => { - if (!withGo) { - console.log('Only supported by go-ipfs yet') - return done() - } - - ipfs.files.ls('/test/404', (err, info) => { - expect(err).to.exist() - expect(info).to.not.exist() - done() - }) - }) - - it('ls directory', (done) => { - if (!withGo) { - console.log('Only supported by go-ipfs yet') - return done() - } - - ipfs.files.ls('/test', (err, info) => { - expect(err).to.not.exist() - expect(info).to.eql([ - { name: 'b', type: 0, size: 0, hash: '' }, - { name: 'lv1', type: 0, size: 0, hash: '' } - ]) - done() - }) - }) - - it('ls -l directory', (done) => { - if (!withGo) { - console.log('Only supported by go-ipfs yet') - return done() - } - - ipfs.files.ls('/test', { l: true }, (err, info) => { - expect(err).to.not.exist() - expect(info).to.eql([ - { - name: 'b', - type: 0, - size: 13, - hash: 'QmcZojhwragQr5qhTeFAmELik623Z21e3jBTpJXoQ9si1T' - }, - { - name: 'lv1', - type: 1, - size: 0, - hash: 'QmaSPtNHYKPjNjQnYX9pdu5ocpKUQEL3itSz8LuZcoW6J5' - } - ]) - done() - }) - }) - }) - - describe('.flush', function () { - it('flush not found, expect error', (done) => { - if (!withGo) { - console.log('Only supported by go-ipfs yet') - return done() - } - - ipfs.files.flush('/test/404', (err) => { - expect(err).to.exist() - done() - }) - }) - - it('flush root', (done) => { - if (!withGo) { - console.log('Only supported by go-ipfs yet') - return done() - } - - ipfs.files.flush((err) => { - expect(err).to.not.exist() - done() - }) - }) - - it('flush specific dir', (done) => { - if (!withGo) { - console.log('Only supported by go-ipfs yet') - return done() - } - - ipfs.files.flush('/test', (err) => { - expect(err).to.not.exist() - done() - }) - }) - }) }) } + +module.exports = (common) => { + tests(common) + mfs(common) +} diff --git a/src/mfs.js b/src/mfs.js new file mode 100644 index 00000000..dc8f00e9 --- /dev/null +++ b/src/mfs.js @@ -0,0 +1,427 @@ +/* eslint-env mocha */ +/* eslint max-nested-callbacks: ["error", 8] */ + +'use strict' + +const chai = require('chai') +const dirtyChai = require('dirty-chai') +const expect = chai.expect +chai.use(dirtyChai) + +module.exports = (common) => { + describe('.files (MFS Specific)', function () { + this.timeout(40 * 1000) + + let ipfs + let withGo + + before(function (done) { + // CI takes longer to instantiate the daemon, so we need to increase the + // timeout for the before step + this.timeout(60 * 1000) + + common.setup((err, factory) => { + expect(err).to.not.exist() + factory.spawnNode((err, node) => { + expect(err).to.not.exist() + ipfs = node + node.id((err, id) => { + withGo = id.agentVersion.startsWith('go-ipfs') + done() + }) + }) + }) + }) + + after((done) => common.teardown(done)) + + describe('.mkdir', function () { + it('make directory on root', (done) => { + if (!withGo) { + console.log('Not supported in js-ipfs yet') + return done() + } + + ipfs.files.mkdir('/test', (err) => { + expect(err).to.not.exist() + done() + }) + }) + + it('make directory and its parents', (done) => { + if (!withGo) { + console.log('Not supported in js-ipfs yet') + return done() + } + + ipfs.files.mkdir('/test/lv1/lv2', { p: true }, (err) => { + expect(err).to.not.exist() + done() + }) + }) + + it('make already existent directory', (done) => { + if (!withGo) { + console.log('Not supported in js-ipfs yet') + return done() + } + + ipfs.files.mkdir('/', (err) => { + expect(err).to.exist() + done() + }) + }) + }) + + describe('.write', function () { + it('expect error', (done) => { + if (!withGo) { + console.log('Not supported in js-ipfs yet') + return done() + } + + ipfs.files.write('/test/a', Buffer.from('Hello, world!'), (err) => { + expect(err).to.exist() + done() + }) + }) + + it('expect no error', (done) => { + if (!withGo) { + console.log('Not supported in js-ipfs yet') + return done() + } + + ipfs.files.write('/test/a', Buffer.from('Hello, world!'), {create: true}, (err) => { + expect(err).to.not.exist() + done() + }) + }) + }) + + describe('.cp', function () { + it('copy file, expect error', (done) => { + if (!withGo) { + console.log('Not supported in js-ipfs yet') + return done() + } + + ipfs.files.cp(['/test/c', '/test/b'], (err) => { + expect(err).to.exist() + done() + }) + }) + + it('copy file, expect no error', (done) => { + if (!withGo) { + console.log('Not supported in js-ipfs yet') + return done() + } + + ipfs.files.cp(['/test/a', '/test/b'], (err) => { + expect(err).to.not.exist() + done() + }) + }) + + it('copy dir, expect error', (done) => { + if (!withGo) { + console.log('Not supported in js-ipfs yet') + return done() + } + + ipfs.files.cp(['/test/lv1/lv3', '/test/lv1/lv4'], (err) => { + expect(err).to.exist() + done() + }) + }) + + it('copy dir, expect no error', (done) => { + if (!withGo) { + console.log('Not supported in js-ipfs yet') + return done() + } + + ipfs.files.cp(['/test/lv1/lv2', '/test/lv1/lv3'], (err) => { + expect(err).to.not.exist() + done() + }) + }) + }) + + describe('.mv', function () { + it('move file, expect error', (done) => { + if (!withGo) { + console.log('Not supported in js-ipfs yet') + return done() + } + + ipfs.files.mv(['/test/404', '/test/a'], (err) => { + expect(err).to.exist() + done() + }) + }) + + it('move file, expect no error', (done) => { + if (!withGo) { + console.log('Not supported in js-ipfs yet') + return done() + } + + ipfs.files.mv(['/test/a', '/test/c'], (err) => { + expect(err).to.not.exist() + done() + }) + }) + + it('move dir, expect error', (done) => { + if (!withGo) { + console.log('Not supported in js-ipfs yet') + return done() + } + + ipfs.files.mv(['/test/lv1/404', '/test/lv1'], (err) => { + expect(err).to.exist() + done() + }) + }) + + it('move dir, expect no error', (done) => { + if (!withGo) { + console.log('Not supported in js-ipfs yet') + return done() + } + + ipfs.files.mv(['/test/lv1/lv2', '/test/lv1/lv4'], (err) => { + expect(err).to.not.exist() + done() + }) + }) + }) + + describe('.rm', function () { + it('remove file, expect error', (done) => { + if (!withGo) { + console.log('Not supported in js-ipfs yet') + return done() + } + + ipfs.files.rm('/test/a', (err) => { + expect(err).to.exist() + done() + }) + }) + + it('remove file, expect no error', (done) => { + if (!withGo) { + console.log('Not supported in js-ipfs yet') + return done() + } + + ipfs.files.rm('/test/c', (err) => { + expect(err).to.not.exist() + done() + }) + }) + + it('remove dir, expect error', (done) => { + if (!withGo) { + console.log('Not supported in js-ipfs yet') + return done() + } + + ipfs.files.rm('/test/lv1/lv4', (err) => { + expect(err).to.exist() + done() + }) + }) + + it('remove dir, expect no error', (done) => { + if (!withGo) { + console.log('Not supported in js-ipfs yet') + return done() + } + + ipfs.files.rm('/test/lv1/lv4', {recursive: true}, (err) => { + expect(err).to.not.exist() + done() + }) + }) + }) + + describe('.stat', function () { + it('stat not found, expect error', (done) => { + if (!withGo) { + console.log('Not supported in js-ipfs yet') + return done() + } + + ipfs.files.stat('/test/404', (err) => { + expect(err).to.exist() + done() + }) + }) + + it('stat file', (done) => { + if (!withGo) { + console.log('Not supported in js-ipfs yet') + return done() + } + + ipfs.files.stat('/test/b', (err, stat) => { + expect(err).to.not.exist() + expect(stat).to.eql({ + type: 'file', + blocks: 1, + size: 13, + hash: 'QmcZojhwragQr5qhTeFAmELik623Z21e3jBTpJXoQ9si1T', + cumulativeSize: 71 + }) + done() + }) + }) + + it('stat dir', (done) => { + if (!withGo) { + console.log('Not supported in js-ipfs yet') + return done() + } + + ipfs.files.stat('/test', (err, stat) => { + expect(err).to.not.exist() + expect(stat).to.eql({ + type: 'directory', + blocks: 2, + size: 0, + hash: 'QmVrkkNurBCeJvPRohW5JTvJG4AxGrFg7FnmsZZUS6nJto', + cumulativeSize: 216 + }) + done() + }) + }) + }) + + describe('.read', function () { + it('read not found, expect error', (done) => { + if (!withGo) { + console.log('Not supported in js-ipfs yet') + return done() + } + + ipfs.files.read('/test/404', (err, buf) => { + expect(err).to.exist() + expect(buf).to.not.exist() + done() + }) + }) + + it('read file', (done) => { + if (!withGo) { + console.log('Not supported in js-ipfs yet') + return done() + } + + ipfs.files.read('/test/b', (err, buf) => { + expect(err).to.not.exist() + expect(buf).to.eql(Buffer.from('Hello, world!')) + done() + }) + }) + }) + + describe('.ls', function () { + it('ls not found, expect error', (done) => { + if (!withGo) { + console.log('Not supported in js-ipfs yet') + return done() + } + + ipfs.files.ls('/test/404', (err, info) => { + expect(err).to.exist() + expect(info).to.not.exist() + done() + }) + }) + + it('ls directory', (done) => { + if (!withGo) { + console.log('Not supported in js-ipfs yet') + return done() + } + + ipfs.files.ls('/test', (err, info) => { + expect(err).to.not.exist() + expect(info).to.eql([ + { name: 'b', type: 0, size: 0, hash: '' }, + { name: 'lv1', type: 0, size: 0, hash: '' } + ]) + done() + }) + }) + + it('ls -l directory', (done) => { + if (!withGo) { + console.log('Not supported in js-ipfs yet') + return done() + } + + ipfs.files.ls('/test', { l: true }, (err, info) => { + expect(err).to.not.exist() + expect(info).to.eql([ + { + name: 'b', + type: 0, + size: 13, + hash: 'QmcZojhwragQr5qhTeFAmELik623Z21e3jBTpJXoQ9si1T' + }, + { + name: 'lv1', + type: 1, + size: 0, + hash: 'QmaSPtNHYKPjNjQnYX9pdu5ocpKUQEL3itSz8LuZcoW6J5' + } + ]) + done() + }) + }) + }) + + describe('.flush', function () { + it('flush not found, expect error', (done) => { + if (!withGo) { + console.log('Not supported in js-ipfs yet') + return done() + } + + ipfs.files.flush('/test/404', (err) => { + expect(err).to.exist() + done() + }) + }) + + it('flush root', (done) => { + if (!withGo) { + console.log('Not supported in js-ipfs yet') + return done() + } + + ipfs.files.flush((err) => { + expect(err).to.not.exist() + done() + }) + }) + + it('flush specific dir', (done) => { + if (!withGo) { + console.log('Not supported in js-ipfs yet') + return done() + } + + ipfs.files.flush('/test', (err) => { + expect(err).to.not.exist() + done() + }) + }) + }) + }) +} From 75a7647e412465725a987748e1388b9d7b38e306 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Thu, 25 Jan 2018 18:17:39 +0000 Subject: [PATCH 11/11] remove useless var --- src/files.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/files.js b/src/files.js index 4f8bd912..6815a62b 100644 --- a/src/files.js +++ b/src/files.js @@ -25,7 +25,6 @@ const tests = (common) => { this.timeout(40 * 1000) let ipfs - let withGo function fixture (path) { return loadFixture(__dirname, path, 'interface-ipfs-core') @@ -62,10 +61,7 @@ const tests = (common) => { factory.spawnNode((err, node) => { expect(err).to.not.exist() ipfs = node - node.id((err, id) => { - withGo = id.agentVersion.startsWith('go-ipfs') - done() - }) + done() }) }) })