This repository was archived by the owner on Jun 28, 2019. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathtest.js
More file actions
87 lines (66 loc) · 1.9 KB
/
Copy pathtest.js
File metadata and controls
87 lines (66 loc) · 1.9 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const inbox = 'Inbox'
const ms = 250
var crypto = require('crypto')
var fs = require('fs')
var path = require('path')
var test = require('tape')
var mkdirp = require('mkdirp')
var Folder = require('./index.js')
var folderName = 'test-' + crypto.randomBytes(16).toString('hex')
var appdir = path.join(require('os').homedir(), folderName)
var testdir = path.join(appdir, inbox)
var testfile = path.join(testdir, 'test.txt')
var EventEmitter = require('events')
var folder
test('setup', function (t) {
mkdirp.sync(appdir)
folder = Folder({
appdir,
subfolders: { inbox }
})
var stat = fs.statSync(testdir)
t.ok(stat && stat.isDirectory(), 'subfolder has been created')
t.ok(folder.watcher instanceof EventEmitter, 'watcher created')
t.end()
})
test('should emit events', function (t) {
t.plan(3)
folder.on('ready', async function () {
folder.on('inbox:add', function (filepath) {
t.ok(filepath === testfile, 'add event emitted')
})
folder.on('inbox:change', function (filepath) {
t.ok(filepath === testfile, 'change event emitted')
})
folder.on('inbox:remove', function (filepath) {
t.ok(filepath === testfile, 'remove event emitted')
})
await sleep(ms)
fs.writeFileSync(testfile, 'abc')
await sleep(ms)
fs.appendFileSync(testfile, 'abc', { flags: 'a+' })
await sleep(ms)
fs.unlinkSync(testfile)
})
})
test('should return full subfolder path', function (t) {
t.plan(1)
var expected = testdir
var actual = folder.subfolder('inbox')
t.equal(actual, expected)
})
test('teardown', function (t) {
folder.close()
t.ok(Object.entries(folder.watchers).every(w => !(w instanceof EventEmitter)), 'no more watchers')
fs.rmdirSync(testdir)
fs.rmdirSync(appdir)
t.end()
})
function sleep (t) {
return new Promise((resolve) => {
var timeout = setTimeout(() => {
clearTimeout(timeout)
resolve()
}, t)
})
}