-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
69 lines (60 loc) · 1.86 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
69
import fs from 'fs'
import path from 'path'
import test from 'ava'
import del from 'del'
import uuid from 'uuid'
import fn from '.'
test.before(() => {
process.chdir(__dirname)
})
test.beforeEach(t => {
t.context.targetPath = path.resolve(uuid.v4())
t.context.targetFilePath = path.resolve(t.context.targetPath, 'target')
t.context.output = 'target'
t.context.src = uuid.v4()
t.context.dest = uuid.v4()
})
test.afterEach.always(t => {
Object.keys(t.context).forEach(p => del.sync(t.context[p]))
})
test('run', t => {
const { targetPath, targetFilePath, output } = t.context
fs.mkdirSync(targetPath)
fs.writeFileSync(targetFilePath, 'unicorn', 'utf8')
fn(output, { dirPath: targetPath })
t.is(fs.readFileSync(output, 'utf8'), 'unicorn')
})
test('from not found', t => {
const { src } = t.context
fn(src)
t.is(fs.readFileSync(src, 'utf8'), '')
})
test('--add', t => {
const { src, dest } = t.context
fs.writeFileSync(src, '')
fn(src, { add: true, dirPath: dest })
t.is(
fs.readFileSync(path.resolve(dest, path.basename(src)), 'utf8'),
fs.readFileSync(src, 'utf8')
)
})
test('--overwrite=true', t => {
const { targetPath, targetFilePath, output } = t.context
fs.mkdirSync(targetPath)
fs.writeFileSync(targetFilePath, 'overwrite', 'utf-8')
fs.writeFileSync(output, 'unicorn')
fn(output, { dirPath: targetPath, overwrite: true })
t.is(fs.readFileSync(output, 'utf-8'), 'overwrite')
})
test('--overwrite=false', t => {
const { targetPath, targetFilePath, output } = t.context
fs.mkdirSync(targetPath)
fs.writeFileSync(targetFilePath, 'overwrite', 'utf-8')
fs.writeFileSync(output, 'unicorn')
fn(output, { dirPath: targetPath, overwrite: false })
t.is(fs.readFileSync(output, 'utf-8'), 'unicorn')
})
test('throw err', t => {
const err = t.throws(() => fn(23), TypeError)
t.is(err.message, 'Expected a string, got number')
})