-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest.js
68 lines (56 loc) · 1.65 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const { test } = require('tap')
const crypto = require('crypto')
const fastFolderSize = require('.')
const fastFolderSizeSync = require('./sync')
test('callback', async t => {
await t.test('folder size is larger than 0', t => {
fastFolderSize('.', (err, bytes) => {
t.error(err)
t.ok(Number.isFinite(bytes))
t.ok(bytes > 0)
t.end()
})
})
await t.test('folder size is correct', t => {
const writtenBytes = 8 * 1024
const testdirName = t.testdir({
whatever: crypto.randomBytes(writtenBytes),
})
fastFolderSize(testdirName, (err, bytes) => {
t.error(err)
console.log('real size:', writtenBytes, 'found size:', bytes)
t.ok(bytes >= writtenBytes)
t.ok(bytes <= writtenBytes * 1.5)
t.end()
})
})
await t.test('should be able to cancel the operation', t => {
const controller = new AbortController()
fastFolderSize('.', { signal: controller.signal }, (err, bytes) => {
t.ok(err)
t.equal(err.name, 'AbortError')
t.notOk(bytes)
t.end()
})
controller.abort()
})
})
test('sync', async t => {
await t.test('sync: folder size is larger than 0', t => {
const bytes = fastFolderSizeSync('.')
t.ok(Number.isFinite(bytes))
t.ok(bytes > 0)
t.end()
})
await t.test('sync: folder size is correct', t => {
const writtenBytes = 8 * 1024
const testdirName = t.testdir({
whatever: crypto.randomBytes(writtenBytes),
})
const bytes = fastFolderSizeSync(testdirName)
console.log('real size:', writtenBytes, 'found size:', bytes)
t.ok(bytes >= writtenBytes)
t.ok(bytes <= writtenBytes * 1.5)
t.end()
})
})