mirror of
https://github.com/zen-browser/surfer.git
synced 2025-07-07 17:05: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 = {
|
||||
preset: 'ts-jest',
|
||||
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
|
||||
// messages from occurring
|
||||
import Log from './log'
|
||||
export const log = new Log()
|
||||
import { log as logInited } from './log'
|
||||
export const log = logInited
|
||||
|
||||
import chalk from 'chalk'
|
||||
import commander, { Command } from 'commander'
|
||||
|
|
|
@ -69,4 +69,6 @@ class Log {
|
|||
}
|
||||
}
|
||||
|
||||
export const log = new 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 { join } from 'path'
|
||||
|
||||
import { log } from '..'
|
||||
import { log } from '../log'
|
||||
|
||||
export const projectDir = process.cwd()
|
||||
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',
|
||||
vendor: 'Unknown',
|
||||
appId: 'unknown.appid',
|
||||
|
@ -107,23 +107,29 @@ export function hasConfig(): boolean {
|
|||
return existsSync(configPath)
|
||||
}
|
||||
|
||||
export function getConfig(): Config {
|
||||
export function rawConfig(): string {
|
||||
const configExists = hasConfig()
|
||||
|
||||
let fileContents = '{}'
|
||||
let fileParsed: Config
|
||||
let contents = '{}'
|
||||
|
||||
if (!configExists) {
|
||||
if (configExists) {
|
||||
contents = readFileSync(configPath, 'utf8')
|
||||
} else {
|
||||
if (!hasWarnedAboutConfig) {
|
||||
log.warning(
|
||||
`Config file not found at ${configPath}. It is recommended to create one by running |melon setup-project|`
|
||||
)
|
||||
hasWarnedAboutConfig = true
|
||||
}
|
||||
} else {
|
||||
fileContents = readFileSync(configPath).toString()
|
||||
}
|
||||
|
||||
return contents
|
||||
}
|
||||
|
||||
export function getConfig(): Config {
|
||||
const fileContents = rawConfig()
|
||||
let fileParsed: Config
|
||||
|
||||
try {
|
||||
// Try to parse the contents of the file. May not be valid JSON
|
||||
fileParsed = JSON.parse(fileContents)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue