Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
73b8041
commit 0e5f6ad
Showing
6 changed files
with
77 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,60 @@ | ||
import path from 'path'; | ||
import fs from 'fs'; | ||
import test from 'ava'; | ||
import tempfile from 'tempfile'; | ||
import m from '.'; | ||
import tempy from 'tempy'; | ||
import writeJsonFile from '.'; | ||
|
||
test('async', async t => { | ||
const tmp = path.join(tempfile(), 'foo'); | ||
await m(tmp, {foo: true}, {indent: 2}); | ||
t.is(fs.readFileSync(tmp, 'utf8'), '{\n "foo": true\n}\n'); | ||
const tempFile = tempy.file(); | ||
await writeJsonFile(tempFile, {foo: true}, {indent: 2}); | ||
t.is(fs.readFileSync(tempFile, 'utf8'), '{\n "foo": true\n}\n'); | ||
}); | ||
|
||
test('sync', t => { | ||
const tmp = path.join(tempfile(), 'foo'); | ||
m.sync(tmp, {foo: true}, {detectIndent: true, indent: 2}); | ||
t.is(fs.readFileSync(tmp, 'utf8'), '{\n "foo": true\n}\n'); | ||
const tempFile = tempy.file(); | ||
writeJsonFile.sync(tempFile, {foo: true}, {detectIndent: true, indent: 2}); | ||
t.is(fs.readFileSync(tempFile, 'utf8'), '{\n "foo": true\n}\n'); | ||
}); | ||
|
||
test('detect indent', async t => { | ||
const tmp = path.join(tempfile(), 'foo'); | ||
await m(tmp, {foo: true}, {indent: 2}); | ||
await m(tmp, {foo: true, bar: true, foobar: true}, {detectIndent: true}); | ||
t.is(fs.readFileSync(tmp, 'utf8'), '{\n "foo": true,\n "bar": true,\n "foobar": true\n}\n'); | ||
const tempFile = tempy.file(); | ||
await writeJsonFile(tempFile, {foo: true}, {indent: 2}); | ||
await writeJsonFile(tempFile, {foo: true, bar: true, foobar: true}, {detectIndent: true}); | ||
t.is(fs.readFileSync(tempFile, 'utf8'), '{\n "foo": true,\n "bar": true,\n "foobar": true\n}\n'); | ||
}); | ||
|
||
test('detect indent synchronously', t => { | ||
const tmp = path.join(tempfile(), 'foo'); | ||
m.sync(tmp, {foo: true}, {indent: 2}); | ||
m.sync(tmp, {foo: true, bar: true, foobar: true}, {detectIndent: true}); | ||
t.is(fs.readFileSync(tmp, 'utf8'), '{\n "foo": true,\n "bar": true,\n "foobar": true\n}\n'); | ||
const tempFile = tempy.file(); | ||
writeJsonFile.sync(tempFile, {foo: true}, {indent: 2}); | ||
writeJsonFile.sync(tempFile, {foo: true, bar: true, foobar: true}, {detectIndent: true}); | ||
t.is(fs.readFileSync(tempFile, 'utf8'), '{\n "foo": true,\n "bar": true,\n "foobar": true\n}\n'); | ||
}); | ||
|
||
test('fall back to default indent if file doesn\'t exist', async t => { | ||
const tmp = path.join(tempfile(), 'foo'); | ||
await m(tmp, {foo: true, bar: true, foobar: true}, {detectIndent: true}); | ||
t.is(fs.readFileSync(tmp, 'utf8'), '{\n\t"foo": true,\n\t"bar": true,\n\t"foobar": true\n}\n'); | ||
const tempFile = tempy.file(); | ||
await writeJsonFile(tempFile, {foo: true, bar: true, foobar: true}, {detectIndent: true}); | ||
t.is(fs.readFileSync(tempFile, 'utf8'), '{\n\t"foo": true,\n\t"bar": true,\n\t"foobar": true\n}\n'); | ||
}); | ||
|
||
test('async - {sortKeys: true}', async t => { | ||
const tmp = path.join(tempfile(), 'foo'); | ||
await m(tmp, {c: true, b: true, a: true}, {sortKeys: true}); | ||
t.is(fs.readFileSync(tmp, 'utf8'), '{\n\t"a": true,\n\t"b": true,\n\t"c": true\n}\n'); | ||
const tempFile = tempy.file(); | ||
await writeJsonFile(tempFile, {c: true, b: true, a: true}, {sortKeys: true}); | ||
t.is(fs.readFileSync(tempFile, 'utf8'), '{\n\t"a": true,\n\t"b": true,\n\t"c": true\n}\n'); | ||
}); | ||
|
||
test('async - {sortKeys: false}', async t => { | ||
const tmp = path.join(tempfile(), 'foo'); | ||
await m(tmp, {c: true, b: true, a: true}, {sortKeys: false}); | ||
t.is(fs.readFileSync(tmp, 'utf8'), '{\n\t"c": true,\n\t"b": true,\n\t"a": true\n}\n'); | ||
const tempFile = tempy.file(); | ||
await writeJsonFile(tempFile, {c: true, b: true, a: true}, {sortKeys: false}); | ||
t.is(fs.readFileSync(tempFile, 'utf8'), '{\n\t"c": true,\n\t"b": true,\n\t"a": true\n}\n'); | ||
}); | ||
|
||
test('async - `replacer` option', async t => { | ||
const tmp = path.join(tempfile(), 'foo'); | ||
await m(tmp, {foo: true, bar: true}, {replacer: ['foo']}); | ||
t.is(fs.readFileSync(tmp, 'utf8'), '{\n\t"foo": true\n}\n'); | ||
const tempFile = tempy.file(); | ||
await writeJsonFile(tempFile, {foo: true, bar: true}, {replacer: ['foo']}); | ||
t.is(fs.readFileSync(tempFile, 'utf8'), '{\n\t"foo": true\n}\n'); | ||
}); | ||
|
||
test('sync - `replacer` option', t => { | ||
const tmp = path.join(tempfile(), 'foo'); | ||
m.sync(tmp, {foo: true, bar: true}, {replacer: ['foo']}); | ||
t.is(fs.readFileSync(tmp, 'utf8'), '{\n\t"foo": true\n}\n'); | ||
const tempFile = tempy.file(); | ||
writeJsonFile.sync(tempFile, {foo: true, bar: true}, {replacer: ['foo']}); | ||
t.is(fs.readFileSync(tempFile, 'utf8'), '{\n\t"foo": true\n}\n'); | ||
}); |