Skip to content

Commit fbace2f

Browse files
authored
Merge pull request ipfs#33 from ipfs/convert-tests-to-async-await
test: convert tests to async/await
2 parents 30b58b8 + a546a5b commit fbace2f

File tree

9 files changed

+822
-850
lines changed

9 files changed

+822
-850
lines changed

test/cp.spec.js

Lines changed: 104 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -10,158 +10,147 @@ const {
1010
createShardedDirectory
1111
} = require('./helpers')
1212

13-
describe('cp', function () {
13+
describe('cp', () => {
1414
let mfs
1515

16-
before(() => {
17-
return createMfs()
18-
.then(instance => {
19-
mfs = instance
20-
})
16+
before(async () => {
17+
mfs = await createMfs()
2118
})
2219

23-
it('refuses to copy files without arguments', () => {
24-
return mfs.cp()
25-
.then(() => {
26-
throw new Error('No error was thrown for missing files')
27-
})
28-
.catch(error => {
29-
expect(error.message).to.contain('Please supply at least one source')
30-
})
20+
it('refuses to copy files without arguments', async () => {
21+
try {
22+
await mfs.cp()
23+
throw new Error('No error was thrown for missing files')
24+
} catch (err) {
25+
expect(err.message).to.contain('Please supply at least one source')
26+
}
3127
})
3228

33-
it('refuses to copy files without files', () => {
34-
return mfs.cp('/destination')
35-
.then(() => {
36-
throw new Error('No error was thrown for missing files')
37-
})
38-
.catch(error => {
39-
expect(error.message).to.contain('Please supply at least one source')
40-
})
29+
it('refuses to copy files without files', async () => {
30+
try {
31+
await mfs.cp('/destination')
32+
throw new Error('No error was thrown for missing files')
33+
} catch (err) {
34+
expect(err.message).to.contain('Please supply at least one source')
35+
}
4136
})
4237

43-
it('refuses to copy files without files even with options', () => {
44-
return mfs.cp('/destination', {})
45-
.then(() => {
46-
throw new Error('No error was thrown for missing files')
47-
})
48-
.catch(error => {
49-
expect(error.message).to.contain('Please supply at least one source')
50-
})
38+
it('refuses to copy files without files even with options', async () => {
39+
try {
40+
await mfs.cp('/destination', {})
41+
throw new Error('No error was thrown for missing files')
42+
} catch (err) {
43+
expect(err.message).to.contain('Please supply at least one source')
44+
}
5145
})
5246

53-
it('refuses to copy a file to a non-existent directory', () => {
54-
return mfs.cp('/i-do-not-exist', '/output')
55-
.then(() => {
56-
throw new Error('No error was thrown for a non-existent file')
57-
})
58-
.catch(error => {
59-
expect(error.message).to.contain('does not exist')
60-
})
47+
it('refuses to copy a file to a non-existent directory', async () => {
48+
try {
49+
await mfs.cp('/i-do-not-exist', '/output')
50+
throw new Error('No error was thrown for a non-existent file')
51+
} catch (err) {
52+
expect(err.message).to.contain('does not exist')
53+
}
6154
})
6255

63-
it('refuses to copy files to an exsting file', () => {
56+
it('refuses to copy files to an exsting file', async () => {
6457
const source = `/source-file-${Math.random()}.txt`
6558
const destination = `/dest-file-${Math.random()}.txt`
6659

67-
return mfs.write(source, bufferStream(100), {
60+
await mfs.write(source, bufferStream(100), {
6861
create: true
6962
})
70-
.then(() => mfs.write(destination, bufferStream(100), {
71-
create: true
72-
}))
73-
.then(() => mfs.cp(source, destination))
74-
.then(() => {
75-
throw new Error('No error was thrown for a non-existent file')
76-
})
77-
.catch(error => {
78-
expect(error.message).to.contain('directory already has entry by that name')
79-
})
63+
await mfs.write(destination, bufferStream(100), {
64+
create: true
65+
})
66+
67+
try {
68+
await mfs.cp(source, destination)
69+
throw new Error('No error was thrown for a non-existent file')
70+
} catch (err) {
71+
expect(err.message).to.contain('directory already has entry by that name')
72+
}
8073
})
8174

82-
it('refuses to copy a file to itself', () => {
75+
it('refuses to copy a file to itself', async () => {
8376
const source = `/source-file-${Math.random()}.txt`
8477

85-
return mfs.write(source, bufferStream(100), {
78+
await mfs.write(source, bufferStream(100), {
8679
create: true
8780
})
88-
.then(() => mfs.cp(source, source))
89-
.then(() => {
90-
throw new Error('No error was thrown for a non-existent file')
91-
})
92-
.catch(error => {
93-
expect(error.message).to.contain('directory already has entry by that name')
94-
})
81+
82+
try {
83+
await mfs.cp(source, source)
84+
throw new Error('No error was thrown for a non-existent file')
85+
} catch (err) {
86+
expect(err.message).to.contain('directory already has entry by that name')
87+
}
9588
})
9689

97-
it('copies a file to new location', () => {
90+
it('copies a file to new location', async () => {
9891
const source = `/source-file-${Math.random()}.txt`
9992
const destination = `/dest-file-${Math.random()}.txt`
10093
let data = Buffer.alloc(0)
10194

102-
return mfs.write(source, bufferStream(500, {
95+
await mfs.write(source, bufferStream(500, {
10396
collector: (bytes) => {
10497
data = Buffer.concat([data, bytes])
10598
}
10699
}), {
107100
create: true
108101
})
109-
.then(() => mfs.cp(source, destination))
110-
.then(() => mfs.read(destination))
111-
.then((buffer) => {
112-
expect(buffer).to.deep.equal(data)
113-
})
102+
103+
await mfs.cp(source, destination)
104+
const buffer = await mfs.read(destination)
105+
106+
expect(buffer).to.deep.equal(data)
114107
})
115108

116-
it('copies a file to a pre-existing directory', () => {
109+
it('copies a file to a pre-existing directory', async () => {
117110
const source = `/source-file-${Math.random()}.txt`
118111
const directory = `/dest-directory-${Math.random()}`
119112
const destination = `${directory}${source}`
120113

121-
return mfs.write(source, bufferStream(500), {
114+
await mfs.write(source, bufferStream(500), {
122115
create: true
123116
})
124-
.then(() => mfs.mkdir(directory))
125-
.then(() => mfs.cp(source, directory))
126-
.then(() => mfs.stat(destination))
127-
.then((stats) => {
128-
expect(stats.size).to.equal(500)
129-
})
117+
await mfs.mkdir(directory)
118+
await mfs.cp(source, directory)
119+
120+
const stats = await mfs.stat(destination)
121+
expect(stats.size).to.equal(500)
130122
})
131123

132-
it('copies directories', () => {
124+
it('copies directories', async () => {
133125
const source = `/source-directory-${Math.random()}`
134126
const destination = `/dest-directory-${Math.random()}`
135127

136-
return mfs.mkdir(source)
137-
.then(() => mfs.cp(source, destination))
138-
.then(() => mfs.stat(destination))
139-
.then((stats) => {
140-
expect(stats.type).to.equal('directory')
141-
})
128+
await mfs.mkdir(source)
129+
await mfs.cp(source, destination)
130+
131+
const stats = await mfs.stat(destination)
132+
expect(stats.type).to.equal('directory')
142133
})
143134

144-
it('copies directories recursively', () => {
135+
it('copies directories recursively', async () => {
145136
const directory = `/source-directory-${Math.random()}`
146137
const subDirectory = `/source-directory-${Math.random()}`
147138
const source = `${directory}${subDirectory}`
148139
const destination = `/dest-directory-${Math.random()}`
149140

150-
return mfs.mkdir(source, {
141+
await mfs.mkdir(source, {
151142
parents: true
152143
})
153-
.then(() => mfs.cp(directory, destination))
154-
.then(() => mfs.stat(destination))
155-
.then((stats) => {
156-
expect(stats.type).to.equal('directory')
157-
})
158-
.then(() => mfs.stat(`${destination}/${subDirectory}`))
159-
.then((stats) => {
160-
expect(stats.type).to.equal('directory')
161-
})
144+
await mfs.cp(directory, destination)
145+
146+
const stats = await mfs.stat(destination)
147+
expect(stats.type).to.equal('directory')
148+
149+
const subDirStats = await mfs.stat(`${destination}/${subDirectory}`)
150+
expect(subDirStats.type).to.equal('directory')
162151
})
163152

164-
it('copies multiple files to new location', () => {
153+
it('copies multiple files to new location', async () => {
165154
const sources = [{
166155
path: `/source-file-${Math.random()}.txt`,
167156
data: Buffer.alloc(0)
@@ -171,42 +160,40 @@ describe('cp', function () {
171160
}]
172161
const destination = `/dest-dir-${Math.random()}`
173162

174-
return Promise.all(
175-
sources.map(source => mfs.write(source.path, bufferStream(500, {
163+
for (const source of sources) {
164+
await mfs.write(source.path, bufferStream(500, {
176165
collector: (bytes) => {
177166
source.data = Buffer.concat([source.data, bytes])
178167
}
179168
}), {
180169
create: true
181-
}))
182-
)
183-
.then(() => mfs.cp(sources[0].path, sources[1].path, destination, {
184-
parents: true
185-
}))
186-
.then(() => Promise.all(
187-
sources.map((source, index) => mfs.read(`${destination}${source.path}`)
188-
.then((buffer) => {
189-
expect(buffer).to.deep.equal(sources[index].data)
190-
})
191-
)
192-
))
170+
})
171+
}
172+
173+
await mfs.cp(sources[0].path, sources[1].path, destination, {
174+
parents: true
175+
})
176+
177+
for (const source of sources) {
178+
const buffer = await mfs.read(`${destination}${source.path}`)
179+
180+
expect(buffer).to.deep.equal(source.data)
181+
}
193182
})
194183

195-
it('copies files from ipfs paths', () => {
184+
it('copies files from ipfs paths', async () => {
196185
const source = `/source-file-${Math.random()}.txt`
197186
const destination = `/dest-file-${Math.random()}.txt`
198187

199-
return mfs.write(source, bufferStream(100), {
188+
await mfs.write(source, bufferStream(100), {
200189
create: true
201190
})
202-
.then(() => mfs.stat(source))
203-
.then((stats) => {
204-
return mfs.cp(`/ipfs/${stats.hash}`, destination)
205-
})
206-
.then(() => mfs.stat(destination))
207-
.then((stats) => {
208-
expect(stats.size).to.equal(100)
209-
})
191+
192+
const stats = await mfs.stat(source)
193+
await mfs.cp(`/ipfs/${stats.hash}`, destination)
194+
195+
const destinationStats = await mfs.stat(destination)
196+
expect(destinationStats.size).to.equal(100)
210197
})
211198

212199
it('copies a sharded directory to a normal directory', async () => {

test/flush.spec.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,15 @@ const {
88
createMfs
99
} = require('./helpers')
1010

11-
describe('flush', function () {
11+
describe('flush', () => {
1212
let mfs
1313

14-
before(() => {
15-
return createMfs()
16-
.then(instance => {
17-
mfs = instance
18-
})
14+
before(async () => {
15+
mfs = await createMfs()
1916
})
2017

21-
it('flushes the root node', () => {
22-
return mfs.flush()
18+
it('flushes the root node', async () => {
19+
await mfs.flush()
2320
})
2421

2522
it('throws a error when trying to flush non-existent directories', async () => {

0 commit comments

Comments
 (0)