-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
test.ts
76 lines (65 loc) · 1.99 KB
/
test.ts
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
// external
import { deepEqual, equal, errorEqual } from 'assert-helpers'
import kava from 'kava'
import safeps from 'safeps'
import filedirname from 'filedirname'
// builtin
import { resolve } from 'path'
// local
import { parse, stringify } from './index.js'
// prepare
const [file, dir] = filedirname()
const root = resolve(dir, '..')
const binPath = resolve(dir, 'bin.js')
// Test
kava.suite('envfile', function (suite, test) {
test('should work without comments', function (done) {
const command = `echo "a=1\\nb:2\\nc = 3\\nd : 4" | node ${binPath} env2json | node ${binPath} json2env`
// @ts-ignore
safeps.exec(command, { cwd: root }, function (err, stdout) {
errorEqual(err, null, 'no error to exist')
equal(stdout.trim(), 'a=1\nb=2\nc=3\nd=4', 'stdout to be as expected')
done()
})
})
test('comments should be ignored', function (done) {
const command = `echo "#comments with = are ignored\\na=1\\n" | node ${binPath} env2json | node ${binPath} json2env`
// @ts-ignore
safeps.exec(command, { cwd: root }, function (err, stdout) {
errorEqual(err, null, 'no error to exist')
equal(stdout.trim(), 'a=1', 'stdout to be as expected')
done()
})
})
test('quotes should be preserved and normalized', function (done) {
const str = `name="bob"\nplanet="earth"\nrace='human'`
const expected = {
name: 'bob',
planet: 'earth',
race: 'human',
}
const result = parse(str)
deepEqual(result, expected)
done()
})
test('line breaks inside quotes should be preserved on parse', function (done) {
const str = `name="bob\nmarley"\nplanet=earth`
const expected = {
name: 'bob\nmarley',
planet: 'earth',
}
const result = parse(str)
deepEqual(result, expected)
done()
})
test('line breaks inside quotes should be preserved on stringify', function (done) {
const input = {
name: 'bob\nmarley',
planet: 'earth',
}
const expected = `name="bob\\nmarley"\nplanet=earth\n`
const result = stringify(input)
deepEqual(result, expected)
done()
})
})