normalize line endings to prevent UNIX & Mac OSs to fail tests

This commit is contained in:
Ismaël Moret
2026-04-10 08:26:09 +00:00
parent ee856068e9
commit 0f2f84737d
3 changed files with 102 additions and 114 deletions

View File

@@ -1,41 +1,47 @@
import { execSync } from 'child_process'
import fs from 'fs-extra'
import { pathToFileURL } from 'node:url'
const ENV_VAR = 'cross-env SITES_DIR=tests/__data__/output/sites'
beforeEach(() => {
fs.emptyDirSync('tests/__data__/output')
fs.mkdirSync('tests/__data__/output/sites')
})
it('can create new site config from template', () => {
const cmd = `${ENV_VAR} npm run sites:init --- example.com`
const stdout = execSync(cmd, { encoding: 'utf8' })
if (process.env.DEBUG === 'true') console.log(cmd, stdout)
expect(exists('tests/__data__/output/sites/example.com')).toBe(true)
expect(exists('tests/__data__/output/sites/example.com/example.com.test.js')).toBe(true)
expect(exists('tests/__data__/output/sites/example.com/example.com.config.js')).toBe(true)
expect(exists('tests/__data__/output/sites/example.com/readme.md')).toBe(true)
expect(content('tests/__data__/output/sites/example.com/example.com.test.js')).toEqual(
content('tests/__data__/expected/sites_init/example.com.test.js')
)
expect(content('tests/__data__/output/sites/example.com/example.com.config.js')).toEqual(
content('tests/__data__/expected/sites_init/example.com.config.js')
)
expect(content('tests/__data__/output/sites/example.com/readme.md')).toEqual(
content('tests/__data__/expected/sites_init/readme.md')
)
})
function content(filepath: string) {
return fs.readFileSync(pathToFileURL(filepath), {
encoding: 'utf8'
})
}
function exists(filepath: string) {
return fs.existsSync(pathToFileURL(filepath))
}
import { execSync } from 'child_process'
import fs from 'fs-extra'
import { pathToFileURL } from 'node:url'
const ENV_VAR = 'cross-env SITES_DIR=tests/__data__/output/sites'
beforeEach(() => {
fs.emptyDirSync('tests/__data__/output')
fs.mkdirSync('tests/__data__/output/sites')
})
it('can create new site config from template', () => {
const cmd = `${ENV_VAR} npm run sites:init --- example.com`
const stdout = execSync(cmd, { encoding: 'utf8' })
if (process.env.DEBUG === 'true') console.log(cmd, stdout)
expect(exists('tests/__data__/output/sites/example.com')).toBe(true)
expect(exists('tests/__data__/output/sites/example.com/example.com.test.js')).toBe(true)
expect(exists('tests/__data__/output/sites/example.com/example.com.config.js')).toBe(true)
expect(exists('tests/__data__/output/sites/example.com/readme.md')).toBe(true)
expect(content('tests/__data__/output/sites/example.com/example.com.test.js')).toEqual(
content('tests/__data__/expected/sites_init/example.com.test.js')
)
expect(content('tests/__data__/output/sites/example.com/example.com.config.js')).toEqual(
content('tests/__data__/expected/sites_init/example.com.config.js')
)
expect(content('tests/__data__/output/sites/example.com/readme.md')).toEqual(
content('tests/__data__/expected/sites_init/readme.md')
)
})
function content(filepath: string) {
return normalizeLineEndings(
fs.readFileSync(pathToFileURL(filepath), {
encoding: 'utf8'
})
)
}
function normalizeLineEndings(data: string) {
return data.replace(/\r\n/g, '\n').replace(/\r/g, '\n')
}
function exists(filepath: string) {
return fs.existsSync(pathToFileURL(filepath))
}

View File

@@ -1,28 +1,32 @@
import { execSync } from 'child_process'
import fs from 'fs-extra'
import { pathToFileURL } from 'node:url'
const ENV_VAR = 'cross-env SITES_DIR=tests/__data__/input/sites_update/sites ROOT_DIR=tests/__data__/output'
beforeEach(() => {
fs.emptyDirSync('tests/__data__/output')
})
it('can update SITES.md', () => {
const cmd = `${ENV_VAR} npm run sites:update`
const stdout = execSync(cmd, { encoding: 'utf8' })
if (process.env.DEBUG === 'true') console.log(cmd, stdout)
expect(content('tests/__data__/output/SITES.md')).toEqual(
content('tests/__data__/expected/sites_update/SITES.md')
)
})
function content(filepath: string) {
const data = fs.readFileSync(pathToFileURL(filepath), {
encoding: 'utf8'
})
return JSON.stringify(data)
}
import { execSync } from 'child_process'
import fs from 'fs-extra'
import { pathToFileURL } from 'node:url'
const ENV_VAR = 'cross-env SITES_DIR=tests/__data__/input/sites_update/sites ROOT_DIR=tests/__data__/output'
beforeEach(() => {
fs.emptyDirSync('tests/__data__/output')
})
it('can update SITES.md', () => {
const cmd = `${ENV_VAR} npm run sites:update`
const stdout = execSync(cmd, { encoding: 'utf8' })
if (process.env.DEBUG === 'true') console.log(cmd, stdout)
expect(content('tests/__data__/output/SITES.md')).toEqual(
content('tests/__data__/expected/sites_update/SITES.md')
)
})
function content(filepath: string) {
const data = fs.readFileSync(pathToFileURL(filepath), {
encoding: 'utf8'
})
return JSON.stringify(normalizeLineEndings(data))
}
function normalizeLineEndings(data: string) {
return data.replace(/\r\n/g, '\n').replace(/\r/g, '\n')
}