|
1 |
| -import path from 'path'; |
2 | 1 | import fs from 'fs';
|
3 | 2 | import test from 'ava';
|
4 |
| -import tempfile from 'tempfile'; |
5 |
| -import m from '.'; |
| 3 | +import tempy from 'tempy'; |
| 4 | +import writeJsonFile from '.'; |
6 | 5 |
|
7 | 6 | test('async', async t => {
|
8 |
| - const tmp = path.join(tempfile(), 'foo'); |
9 |
| - await m(tmp, {foo: true}, {indent: 2}); |
10 |
| - t.is(fs.readFileSync(tmp, 'utf8'), '{\n "foo": true\n}\n'); |
| 7 | + const tempFile = tempy.file(); |
| 8 | + await writeJsonFile(tempFile, {foo: true}, {indent: 2}); |
| 9 | + t.is(fs.readFileSync(tempFile, 'utf8'), '{\n "foo": true\n}\n'); |
11 | 10 | });
|
12 | 11 |
|
13 | 12 | test('sync', t => {
|
14 |
| - const tmp = path.join(tempfile(), 'foo'); |
15 |
| - m.sync(tmp, {foo: true}, {detectIndent: true, indent: 2}); |
16 |
| - t.is(fs.readFileSync(tmp, 'utf8'), '{\n "foo": true\n}\n'); |
| 13 | + const tempFile = tempy.file(); |
| 14 | + writeJsonFile.sync(tempFile, {foo: true}, {detectIndent: true, indent: 2}); |
| 15 | + t.is(fs.readFileSync(tempFile, 'utf8'), '{\n "foo": true\n}\n'); |
17 | 16 | });
|
18 | 17 |
|
19 | 18 | test('detect indent', async t => {
|
20 |
| - const tmp = path.join(tempfile(), 'foo'); |
21 |
| - await m(tmp, {foo: true}, {indent: 2}); |
22 |
| - await m(tmp, {foo: true, bar: true, foobar: true}, {detectIndent: true}); |
23 |
| - t.is(fs.readFileSync(tmp, 'utf8'), '{\n "foo": true,\n "bar": true,\n "foobar": true\n}\n'); |
| 19 | + const tempFile = tempy.file(); |
| 20 | + await writeJsonFile(tempFile, {foo: true}, {indent: 2}); |
| 21 | + await writeJsonFile(tempFile, {foo: true, bar: true, foobar: true}, {detectIndent: true}); |
| 22 | + t.is(fs.readFileSync(tempFile, 'utf8'), '{\n "foo": true,\n "bar": true,\n "foobar": true\n}\n'); |
24 | 23 | });
|
25 | 24 |
|
26 | 25 | test('detect indent synchronously', t => {
|
27 |
| - const tmp = path.join(tempfile(), 'foo'); |
28 |
| - m.sync(tmp, {foo: true}, {indent: 2}); |
29 |
| - m.sync(tmp, {foo: true, bar: true, foobar: true}, {detectIndent: true}); |
30 |
| - t.is(fs.readFileSync(tmp, 'utf8'), '{\n "foo": true,\n "bar": true,\n "foobar": true\n}\n'); |
| 26 | + const tempFile = tempy.file(); |
| 27 | + writeJsonFile.sync(tempFile, {foo: true}, {indent: 2}); |
| 28 | + writeJsonFile.sync(tempFile, {foo: true, bar: true, foobar: true}, {detectIndent: true}); |
| 29 | + t.is(fs.readFileSync(tempFile, 'utf8'), '{\n "foo": true,\n "bar": true,\n "foobar": true\n}\n'); |
31 | 30 | });
|
32 | 31 |
|
33 | 32 | test('fall back to default indent if file doesn\'t exist', async t => {
|
34 |
| - const tmp = path.join(tempfile(), 'foo'); |
35 |
| - await m(tmp, {foo: true, bar: true, foobar: true}, {detectIndent: true}); |
36 |
| - t.is(fs.readFileSync(tmp, 'utf8'), '{\n\t"foo": true,\n\t"bar": true,\n\t"foobar": true\n}\n'); |
| 33 | + const tempFile = tempy.file(); |
| 34 | + await writeJsonFile(tempFile, {foo: true, bar: true, foobar: true}, {detectIndent: true}); |
| 35 | + t.is(fs.readFileSync(tempFile, 'utf8'), '{\n\t"foo": true,\n\t"bar": true,\n\t"foobar": true\n}\n'); |
37 | 36 | });
|
38 | 37 |
|
39 | 38 | test('async - {sortKeys: true}', async t => {
|
40 |
| - const tmp = path.join(tempfile(), 'foo'); |
41 |
| - await m(tmp, {c: true, b: true, a: true}, {sortKeys: true}); |
42 |
| - t.is(fs.readFileSync(tmp, 'utf8'), '{\n\t"a": true,\n\t"b": true,\n\t"c": true\n}\n'); |
| 39 | + const tempFile = tempy.file(); |
| 40 | + await writeJsonFile(tempFile, {c: true, b: true, a: true}, {sortKeys: true}); |
| 41 | + t.is(fs.readFileSync(tempFile, 'utf8'), '{\n\t"a": true,\n\t"b": true,\n\t"c": true\n}\n'); |
43 | 42 | });
|
44 | 43 |
|
45 | 44 | test('async - {sortKeys: false}', async t => {
|
46 |
| - const tmp = path.join(tempfile(), 'foo'); |
47 |
| - await m(tmp, {c: true, b: true, a: true}, {sortKeys: false}); |
48 |
| - t.is(fs.readFileSync(tmp, 'utf8'), '{\n\t"c": true,\n\t"b": true,\n\t"a": true\n}\n'); |
| 45 | + const tempFile = tempy.file(); |
| 46 | + await writeJsonFile(tempFile, {c: true, b: true, a: true}, {sortKeys: false}); |
| 47 | + t.is(fs.readFileSync(tempFile, 'utf8'), '{\n\t"c": true,\n\t"b": true,\n\t"a": true\n}\n'); |
49 | 48 | });
|
50 | 49 |
|
51 | 50 | test('async - `replacer` option', async t => {
|
52 |
| - const tmp = path.join(tempfile(), 'foo'); |
53 |
| - await m(tmp, {foo: true, bar: true}, {replacer: ['foo']}); |
54 |
| - t.is(fs.readFileSync(tmp, 'utf8'), '{\n\t"foo": true\n}\n'); |
| 51 | + const tempFile = tempy.file(); |
| 52 | + await writeJsonFile(tempFile, {foo: true, bar: true}, {replacer: ['foo']}); |
| 53 | + t.is(fs.readFileSync(tempFile, 'utf8'), '{\n\t"foo": true\n}\n'); |
55 | 54 | });
|
56 | 55 |
|
57 | 56 | test('sync - `replacer` option', t => {
|
58 |
| - const tmp = path.join(tempfile(), 'foo'); |
59 |
| - m.sync(tmp, {foo: true, bar: true}, {replacer: ['foo']}); |
60 |
| - t.is(fs.readFileSync(tmp, 'utf8'), '{\n\t"foo": true\n}\n'); |
| 57 | + const tempFile = tempy.file(); |
| 58 | + writeJsonFile.sync(tempFile, {foo: true, bar: true}, {replacer: ['foo']}); |
| 59 | + t.is(fs.readFileSync(tempFile, 'utf8'), '{\n\t"foo": true\n}\n'); |
61 | 60 | });
|
0 commit comments