mirror of
https://github.com/zen-browser/surfer.git
synced 2025-07-07 08:55:33 +02:00
✅ Add tests for config
This commit is contained in:
parent
9fd97ec564
commit
704a5e5006
5 changed files with 103 additions and 11 deletions
|
@ -2,5 +2,6 @@
|
||||||
module.exports = {
|
module.exports = {
|
||||||
preset: 'ts-jest',
|
preset: 'ts-jest',
|
||||||
testEnvironment: 'node',
|
testEnvironment: 'node',
|
||||||
testMatch: ['src/**/*.test.ts'],
|
testMatch: ['**/*.test.ts'],
|
||||||
|
rootDir: 'src',
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
// Init the logger before literally anything else to stop really obscure error
|
// Init the logger before literally anything else to stop really obscure error
|
||||||
// messages from occurring
|
// messages from occurring
|
||||||
import Log from './log'
|
import { log as logInited } from './log'
|
||||||
export const log = new Log()
|
export const log = logInited
|
||||||
|
|
||||||
import chalk from 'chalk'
|
import chalk from 'chalk'
|
||||||
import commander, { Command } from 'commander'
|
import commander, { Command } from 'commander'
|
||||||
|
|
|
@ -69,4 +69,6 @@ class Log {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const log = new Log()
|
||||||
|
|
||||||
export default Log
|
export default Log
|
||||||
|
|
83
src/utils/config.test.ts
Normal file
83
src/utils/config.test.ts
Normal file
|
@ -0,0 +1,83 @@
|
||||||
|
import { existsSync, readFileSync, unlinkSync, writeFileSync } from 'fs'
|
||||||
|
import {
|
||||||
|
configPath,
|
||||||
|
defaultConfig,
|
||||||
|
getConfig,
|
||||||
|
hasConfig,
|
||||||
|
rawConfig,
|
||||||
|
} from './config'
|
||||||
|
|
||||||
|
function preserveExistingConfig() {
|
||||||
|
let configExists = false
|
||||||
|
let configContents = ''
|
||||||
|
|
||||||
|
beforeAll(() => {
|
||||||
|
if (existsSync(configPath)) {
|
||||||
|
configContents = readFileSync(configPath, 'utf8')
|
||||||
|
configExists = true
|
||||||
|
unlinkSync(configPath)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
afterAll(() => {
|
||||||
|
if (configExists) {
|
||||||
|
writeFileSync(configPath, configContents)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('hasConfig', () => {
|
||||||
|
preserveExistingConfig()
|
||||||
|
|
||||||
|
it('returns false when the config file does not exist', () =>
|
||||||
|
expect(hasConfig()).toBe(false))
|
||||||
|
|
||||||
|
it('returns true when the config file exists', () => {
|
||||||
|
writeFileSync(configPath, '{}')
|
||||||
|
expect(hasConfig()).toBe(true)
|
||||||
|
unlinkSync(configPath)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('rawConfig', () => {
|
||||||
|
preserveExistingConfig()
|
||||||
|
|
||||||
|
it('Returns "{}" when no config exists', () => expect(rawConfig()).toBe('{}'))
|
||||||
|
|
||||||
|
it('Returns the contents of the config file', () => {
|
||||||
|
writeFileSync(configPath, '{"test": "val"}')
|
||||||
|
expect(rawConfig()).toBe('{"test": "val"}')
|
||||||
|
unlinkSync(configPath)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('getConfig', () => {
|
||||||
|
preserveExistingConfig()
|
||||||
|
|
||||||
|
it('Returns the default config when none exists', () =>
|
||||||
|
expect(getConfig()).toEqual(defaultConfig))
|
||||||
|
|
||||||
|
it('Returns the default config when the config is empty', () => {
|
||||||
|
writeFileSync(configPath, '{}')
|
||||||
|
expect(getConfig()).toEqual(defaultConfig)
|
||||||
|
unlinkSync(configPath)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('Returns a merged config when there is a specified value', () => {
|
||||||
|
writeFileSync(configPath, '{"name": "val"}')
|
||||||
|
expect(getConfig()).toEqual({ ...defaultConfig, name: 'val' })
|
||||||
|
unlinkSync(configPath)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('Throws an error if there is invalid JSON', () => {
|
||||||
|
writeFileSync(configPath, '{invalid json')
|
||||||
|
expect(() => getConfig()).toThrowError()
|
||||||
|
unlinkSync(configPath)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('Throws an error if the product is invalid', () => {
|
||||||
|
writeFileSync(configPath, '{"version": {"product": "invalid"}}')
|
||||||
|
expect(() => getConfig()).toThrowError()
|
||||||
|
unlinkSync(configPath)
|
||||||
|
})
|
||||||
|
})
|
|
@ -5,7 +5,7 @@
|
||||||
import { existsSync, readFileSync } from 'fs'
|
import { existsSync, readFileSync } from 'fs'
|
||||||
import { join } from 'path'
|
import { join } from 'path'
|
||||||
|
|
||||||
import { log } from '..'
|
import { log } from '../log'
|
||||||
|
|
||||||
export const projectDir = process.cwd()
|
export const projectDir = process.cwd()
|
||||||
export const configPath = join(projectDir, 'melon.json')
|
export const configPath = join(projectDir, 'melon.json')
|
||||||
|
@ -79,7 +79,7 @@ export interface Config {
|
||||||
>
|
>
|
||||||
}
|
}
|
||||||
|
|
||||||
const defaultConfig: Config = {
|
export const defaultConfig: Config = {
|
||||||
name: 'Unknown melon build',
|
name: 'Unknown melon build',
|
||||||
vendor: 'Unknown',
|
vendor: 'Unknown',
|
||||||
appId: 'unknown.appid',
|
appId: 'unknown.appid',
|
||||||
|
@ -107,23 +107,29 @@ export function hasConfig(): boolean {
|
||||||
return existsSync(configPath)
|
return existsSync(configPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getConfig(): Config {
|
export function rawConfig(): string {
|
||||||
const configExists = hasConfig()
|
const configExists = hasConfig()
|
||||||
|
|
||||||
let fileContents = '{}'
|
let contents = '{}'
|
||||||
let fileParsed: Config
|
|
||||||
|
|
||||||
if (!configExists) {
|
if (configExists) {
|
||||||
|
contents = readFileSync(configPath, 'utf8')
|
||||||
|
} else {
|
||||||
if (!hasWarnedAboutConfig) {
|
if (!hasWarnedAboutConfig) {
|
||||||
log.warning(
|
log.warning(
|
||||||
`Config file not found at ${configPath}. It is recommended to create one by running |melon setup-project|`
|
`Config file not found at ${configPath}. It is recommended to create one by running |melon setup-project|`
|
||||||
)
|
)
|
||||||
hasWarnedAboutConfig = true
|
hasWarnedAboutConfig = true
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
fileContents = readFileSync(configPath).toString()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return contents
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getConfig(): Config {
|
||||||
|
const fileContents = rawConfig()
|
||||||
|
let fileParsed: Config
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Try to parse the contents of the file. May not be valid JSON
|
// Try to parse the contents of the file. May not be valid JSON
|
||||||
fileParsed = JSON.parse(fileContents)
|
fileParsed = JSON.parse(fileContents)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue