|
| 1 | +import { readFileSync } from 'node:fs' |
| 2 | +import path from 'node:path' |
| 3 | +import { describe, expect, test } from 'vitest' |
| 4 | +import { writeFixtures } from '../../tests/utils.ts' |
| 5 | +import { detectIndentation, writeJsonFile } from './json.ts' |
| 6 | + |
| 7 | +describe('writeJsonFile', () => { |
| 8 | + test('creates a new file when it does not exist', async (context) => { |
| 9 | + const { testDir } = await writeFixtures(context, { 'placeholder.txt': '' }) |
| 10 | + const filePath = path.join(testDir, 'new.json') |
| 11 | + writeJsonFile(filePath, { foo: 'bar' }) |
| 12 | + expect(readFileSync(filePath, 'utf8')).toBe('{\n "foo": "bar"\n}') |
| 13 | + }) |
| 14 | + |
| 15 | + test('does not rewrite when keys are reordered but content is deeply equal', async (context) => { |
| 16 | + const original = '{"b":1,\n"a":2}' |
| 17 | + const { testDir } = await writeFixtures(context, { 'pkg.json': original }) |
| 18 | + const filePath = path.join(testDir, 'pkg.json') |
| 19 | + writeJsonFile(filePath, { a: 2, b: 1 }) |
| 20 | + expect(readFileSync(filePath, 'utf8')).toBe(original) |
| 21 | + }) |
| 22 | + |
| 23 | + test('does not rewrite when content is identical', async (context) => { |
| 24 | + const original = '{\t"foo":"bar"\n }' |
| 25 | + const { testDir } = await writeFixtures(context, { 'pkg.json': original }) |
| 26 | + const filePath = path.join(testDir, 'pkg.json') |
| 27 | + writeJsonFile(filePath, { foo: 'bar' }) |
| 28 | + expect(readFileSync(filePath, 'utf8')).toBe(original) |
| 29 | + }) |
| 30 | + |
| 31 | + test('updates the file when content changes', async (context) => { |
| 32 | + const { testDir } = await writeFixtures(context, { |
| 33 | + 'pkg.json': '{\n "foo": "bar"\n}', |
| 34 | + }) |
| 35 | + const filePath = path.join(testDir, 'pkg.json') |
| 36 | + writeJsonFile(filePath, { foo: 'baz' }) |
| 37 | + expect(readFileSync(filePath, 'utf8')).toBe('{\n "foo": "baz"\n}') |
| 38 | + }) |
| 39 | + |
| 40 | + test('preserves tab indentation', async (context) => { |
| 41 | + const { testDir } = await writeFixtures(context, { |
| 42 | + 'pkg.json': '{\n\t"foo": "bar"\n}', |
| 43 | + }) |
| 44 | + const filePath = path.join(testDir, 'pkg.json') |
| 45 | + writeJsonFile(filePath, { foo: 'baz' }) |
| 46 | + expect(readFileSync(filePath, 'utf8')).toBe('{\n\t"foo": "baz"\n}') |
| 47 | + }) |
| 48 | + |
| 49 | + test('preserves 4-space indentation', async (context) => { |
| 50 | + const { testDir } = await writeFixtures(context, { |
| 51 | + 'pkg.json': '{\n "foo": "bar"\n}', |
| 52 | + }) |
| 53 | + const filePath = path.join(testDir, 'pkg.json') |
| 54 | + writeJsonFile(filePath, { foo: 'baz' }) |
| 55 | + expect(readFileSync(filePath, 'utf8')).toBe('{\n "foo": "baz"\n}') |
| 56 | + }) |
| 57 | + |
| 58 | + test('preserves CRLF line endings', async (context) => { |
| 59 | + const { testDir } = await writeFixtures(context, { |
| 60 | + 'pkg.json': '{\r\n "foo": "bar"\r\n}', |
| 61 | + }) |
| 62 | + const filePath = path.join(testDir, 'pkg.json') |
| 63 | + writeJsonFile(filePath, { foo: 'baz' }) |
| 64 | + expect(readFileSync(filePath, 'utf8')).toBe('{\r\n "foo": "baz"\r\n}') |
| 65 | + }) |
| 66 | + |
| 67 | + test('preserves trailing newline', async (context) => { |
| 68 | + const { testDir } = await writeFixtures(context, { |
| 69 | + 'pkg.json': '{\n "foo": "bar"\n}\n', |
| 70 | + }) |
| 71 | + const filePath = path.join(testDir, 'pkg.json') |
| 72 | + writeJsonFile(filePath, { foo: 'baz' }) |
| 73 | + expect(readFileSync(filePath, 'utf8')).toBe('{\n "foo": "baz"\n}\n') |
| 74 | + }) |
| 75 | +}) |
| 76 | + |
| 77 | +describe('detectIndent', () => { |
| 78 | + test('two spaces', ({ expect }) => { |
| 79 | + expect(detectIndentation(stringifyJson(2))).toBe(2) |
| 80 | + }) |
| 81 | + test('four spaces', ({ expect }) => { |
| 82 | + expect(detectIndentation(stringifyJson(4))).toBe(4) |
| 83 | + }) |
| 84 | + test('tab', ({ expect }) => { |
| 85 | + expect(detectIndentation(stringifyJson('\t'))).toBe('\t') |
| 86 | + }) |
| 87 | + test('empty', ({ expect }) => { |
| 88 | + expect(detectIndentation('')).toBe(2) |
| 89 | + }) |
| 90 | + test('empty line', ({ expect }) => { |
| 91 | + expect(detectIndentation('{\n\n "foo": 42 }')).toBe(2) |
| 92 | + }) |
| 93 | +}) |
| 94 | + |
| 95 | +function stringifyJson(indentation: string | number): string { |
| 96 | + const contents = JSON.stringify({ foo: 42 }, null, indentation) |
| 97 | + return contents |
| 98 | +} |
0 commit comments