Skip to content

Commit 1bd9c06

Browse files
authored
Merge pull request ipfs#513 from ipfs/test/outside-of-mfs
test: add tests for cp, ls, mv, read outside of MFS
2 parents 0b1f8f5 + 5f0c424 commit 1bd9c06

File tree

5 files changed

+66
-10
lines changed

5 files changed

+66
-10
lines changed

SPEC/FILES.md

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
> The files API enables users to use the File System abstraction of IPFS. There are two Files API, one at the top level, the original `add`, `cat`, `get` and `ls`, and another behind the [`files`, also known as MFS](https://github.com/ipfs/specs/issues/98). [We are currently going through a revamping process of these APIs to make them more user-friendly](https://github.com/ipfs/interface-ipfs-core/issues/284).
44
5-
#### The Regular API
5+
#### The Regular API
66
The regular, top-level API for add, cat, get and ls Files on IPFS
77
- [add](#add)
88
- [addFromFs](#addfromfs)
@@ -19,8 +19,8 @@ The regular, top-level API for add, cat, get and ls Files on IPFS
1919
- [ls](#ls)
2020
- [lsPullStream](#lspullstream)
2121
- [lsReadableStream](#lsreadablestream)
22-
23-
#### The Files API
22+
23+
#### The Files API
2424
The Files API, aka MFS (Mutable File System)
2525

2626
_Explore the Mutable File System through interactive coding challenges in our [ProtoSchool tutorial](https://proto.school/#/mutable-file-system/)._
@@ -700,6 +700,7 @@ Where:
700700
- `from` is the path(s) of the source to copy. It might be:
701701
- An existing MFS path to a file or a directory (e.g. `/my-dir/my-file.txt`)
702702
- An IPFS path (e.g. `/ipfs/QmWGeRAEgtsHW3ec7U4qW2CyVy7eA2mFRVbk1nb24jFyks`)
703+
- A [CID][cid] instance (e.g. `new CID('QmWGeRAEgtsHW3ec7U4qW2CyVy7eA2mFRVbk1nb24jFyks')`)
703704
- `to` is the path of the destination to copy to
704705
- `options` is an optional Object that might contain the following keys:
705706
- `parents` is a Boolean value to decide whether or not to make the parent directories if they don't exist (default: false)
@@ -779,7 +780,10 @@ ipfs.files.mkdir('/my/beautiful/directory', (err) => {
779780

780781
Where:
781782

782-
- `path` is the path to the file or directory to stat
783+
- `path` is the path to the file or directory to stat. It might be:
784+
- An existing MFS path to a file or directory (e.g. `/my-dir/a.txt`)
785+
- An IPFS path (e.g. `/ipfs/QmWGeRAEgtsHW3ec7U4qW2CyVy7eA2mFRVbk1nb24jFyks`)
786+
- A [CID][cid] instance (e.g. `new CID('QmWGeRAEgtsHW3ec7U4qW2CyVy7eA2mFRVbk1nb24jFyks')`)
783787
- `options` is an optional Object that might contain the following keys:
784788
- `hash` is a Boolean value to return only the hash (default: false)
785789
- `size` is a Boolean value to return only the size (default: false)
@@ -862,7 +866,10 @@ ipfs.files.rm('/my/beautiful/directory', { recursive: true }, (err) => {
862866

863867
Where:
864868

865-
- `path` is the path of the file to read and must point to a file (and not a directory)
869+
- `path` is the path of the file to read and must point to a file (and not a directory). It might be:
870+
- An existing MFS path to a file (e.g. `/my-dir/a.txt`)
871+
- An IPFS path (e.g. `/ipfs/QmWGeRAEgtsHW3ec7U4qW2CyVy7eA2mFRVbk1nb24jFyks`)
872+
- A [CID][cid] instance (e.g. `new CID('QmWGeRAEgtsHW3ec7U4qW2CyVy7eA2mFRVbk1nb24jFyks')`)
866873
- `options` is an optional Object that might contain the following keys:
867874
- `offset` is an Integer with the byte offset to begin reading from (default: 0)
868875
- `length` is an Integer with the maximum number of bytes to read (default: Read to the end of stream)
@@ -890,7 +897,10 @@ ipfs.files.read('/hello-world', (error, buf) => {
890897

891898
Where:
892899

893-
- `path` is the path of the file to read and must point to a file (and not a directory)
900+
- `path` is the path of the file to read and must point to a file (and not a directory). It might be:
901+
- An existing MFS path to a file (e.g. `/my-dir/a.txt`)
902+
- An IPFS path (e.g. `/ipfs/QmWGeRAEgtsHW3ec7U4qW2CyVy7eA2mFRVbk1nb24jFyks`)
903+
- A [CID][cid] instance (e.g. `new CID('QmWGeRAEgtsHW3ec7U4qW2CyVy7eA2mFRVbk1nb24jFyks')`)
894904
- `options` is an optional Object that might contain the following keys:
895905
- `offset` is an Integer with the byte offset to begin reading from (default: 0)
896906
- `length` is an Integer with the maximum number of bytes to read (default: Read to the end of stream)
@@ -914,7 +924,10 @@ stream.on('data', (buf) => console.log(buf.toString('utf8')))
914924

915925
Where:
916926

917-
- `path` is the path of the file to read and must point to a file (and not a directory)
927+
- `path` is the path of the file to read and must point to a file (and not a directory). It might be:
928+
- An existing MFS path to a file (e.g. `/my-dir/a.txt`)
929+
- An IPFS path (e.g. `/ipfs/QmWGeRAEgtsHW3ec7U4qW2CyVy7eA2mFRVbk1nb24jFyks`)
930+
- A [CID][cid] instance (e.g. `new CID('QmWGeRAEgtsHW3ec7U4qW2CyVy7eA2mFRVbk1nb24jFyks')`)
918931
- `options` is an optional Object that might contain the following keys:
919932
- `offset` is an Integer with the byte offset to begin reading from (default: 0)
920933
- `length` is an Integer with the maximum number of bytes to read (default: Read to the end of stream)
@@ -1050,7 +1063,10 @@ ipfs.files.flush('/', (err) => {
10501063

10511064
Where:
10521065

1053-
- `path` is an optional string to show listing for (default: `/`)
1066+
- `path` is an optional string to show listing for (default: `/`). It might be:
1067+
- An existing MFS path to a directory (e.g. `/my-dir`)
1068+
- An IPFS path (e.g. `/ipfs/QmWGeRAEgtsHW3ec7U4qW2CyVy7eA2mFRVbk1nb24jFyks`)
1069+
- A [CID][cid] instance (e.g. `new CID('QmWGeRAEgtsHW3ec7U4qW2CyVy7eA2mFRVbk1nb24jFyks')`)
10541070
- `options` is an optional Object that might contain the following keys:
10551071
- `long` is a Boolean value to decide whether or not to populate `type`, `size` and `hash` (default: false)
10561072
- `cidBase` is which number base to use to format hashes - e.g. `base32`, `base64` etc (default: `base58btc`)
@@ -1087,7 +1103,10 @@ ipfs.files.ls('/screenshots', function (err, files) {
10871103

10881104
Where:
10891105

1090-
- `path` is an optional string to show listing for (default: `/`)
1106+
- `path` is an optional string to show listing for (default: `/`). It might be:
1107+
- An existing MFS path to a directory (e.g. `/my-dir`)
1108+
- An IPFS path (e.g. `/ipfs/QmWGeRAEgtsHW3ec7U4qW2CyVy7eA2mFRVbk1nb24jFyks`)
1109+
- A [CID][cid] instance (e.g. `new CID('QmWGeRAEgtsHW3ec7U4qW2CyVy7eA2mFRVbk1nb24jFyks')`)
10911110
- `options` is an optional Object that might contain the following keys:
10921111
- `long` is a Boolean value to decide whether or not to populate `type`, `size` and `hash` (default: false)
10931112
- `cidBase` is which number base to use to format hashes - e.g. `base32`, `base64` etc (default: `base58btc`)
@@ -1120,7 +1139,10 @@ stream.on('data', (file) => {
11201139

11211140
Where:
11221141

1123-
- `path` is an optional string to show listing for (default: `/`)
1142+
- `path` is an optional string to show listing for (default: `/`). It might be:
1143+
- An existing MFS path to a directory (e.g. `/my-dir`)
1144+
- An IPFS path (e.g. `/ipfs/QmWGeRAEgtsHW3ec7U4qW2CyVy7eA2mFRVbk1nb24jFyks`)
1145+
- A [CID][cid] instance (e.g. `new CID('QmWGeRAEgtsHW3ec7U4qW2CyVy7eA2mFRVbk1nb24jFyks')`)
11241146
- `options` is an optional Object that might contain the following keys:
11251147
- `long` is a Boolean value to decide whether or not to populate `type`, `size` and `hash` (default: false)
11261148
- `cidBase` is which number base to use to format hashes - e.g. `base32`, `base64` etc (default: `base58btc`)

src/files-mfs/cp.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
const series = require('async/series')
55
const hat = require('hat')
6+
const { fixtures } = require('../files-regular/utils')
67
const { getDescribe, getIt, expect } = require('../utils/mocha')
78

89
module.exports = (createCommon, options) => {
@@ -74,5 +75,13 @@ module.exports = (createCommon, options) => {
7475
done()
7576
})
7677
})
78+
79+
it('should copy from outside of mfs', async () => {
80+
const [hash] = ipfs.add(fixtures.smallFile.data)
81+
const testFilePath = `/${hat()}`
82+
await ipfs.files.cp(`/ipfs/${hash}`, testFilePath)
83+
const testFileData = await ipfs.files.read(testFilePath)
84+
expect(testFileData).to.eql(fixtures.smallFile.data)
85+
})
7786
})
7887
}

src/files-mfs/ls.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
const series = require('async/series')
55
const hat = require('hat')
6+
const { fixtures } = require('../files-regular/utils')
67
const { getDescribe, getIt, expect } = require('../utils/mocha')
78

89
module.exports = (createCommon, options) => {
@@ -92,6 +93,14 @@ module.exports = (createCommon, options) => {
9293
})
9394
})
9495

96+
it('should ls from outside of mfs', async () => {
97+
const testFileName = hat()
98+
const res = await ipfs.add({ path: `/test/${testFileName}`, content: fixtures.smallFile.data })
99+
const listing = await ipfs.files.ls('/ipfs/' + res[res.length - 1])
100+
expect(listing).to.have.length(1)
101+
expect(listing[0].name).to.equal(testFileName)
102+
})
103+
95104
it('should list an empty directory', async () => {
96105
const testDir = `/test-${hat()}`
97106
await ipfs.files.mkdir(testDir)

src/files-mfs/mv.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
const series = require('async/series')
55
const hat = require('hat')
6+
const { fixtures } = require('../files-regular/utils')
67
const { getDescribe, getIt, expect } = require('../utils/mocha')
78

89
module.exports = (createCommon, options) => {
@@ -76,5 +77,13 @@ module.exports = (createCommon, options) => {
7677
})
7778
})
7879
})
80+
81+
it('should move from outside of mfs', async () => {
82+
const [hash] = ipfs.add(fixtures.smallFile.data)
83+
const testFilePath = `/${hat()}`
84+
await ipfs.files.mv(`/ipfs/${hash}`, testFilePath)
85+
const testFileData = await ipfs.files.read(testFilePath)
86+
expect(testFileData).to.eql(fixtures.smallFile.data)
87+
})
7988
})
8089
}

src/files-mfs/read.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
const series = require('async/series')
55
const hat = require('hat')
6+
const { fixtures } = require('../files-regular/utils')
67
const { getDescribe, getIt, expect } = require('../utils/mocha')
78

89
module.exports = (createCommon, options) => {
@@ -58,5 +59,11 @@ module.exports = (createCommon, options) => {
5859
})
5960
})
6061
})
62+
63+
it('should read from outside of mfs', async () => {
64+
const [hash] = ipfs.add(fixtures.smallFile.data)
65+
const testFileData = await ipfs.files.read(`/ipfs/${hash}`)
66+
expect(testFileData).to.eql(fixtures.smallFile.data)
67+
})
6168
})
6269
}

0 commit comments

Comments
 (0)