From 704a5e5006d1e2d09f137650ac07de6b478cbb09 Mon Sep 17 00:00:00 2001 From: trickypr Date: Sun, 26 Dec 2021 12:07:25 +1100 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Add=20tests=20for=20config?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jest.config.js | 3 +- src/index.ts | 4 +- src/log.ts | 2 + src/utils/config.test.ts | 83 ++++++++++++++++++++++++++++++++++++++++ src/utils/config.ts | 22 +++++++---- 5 files changed, 103 insertions(+), 11 deletions(-) create mode 100644 src/utils/config.test.ts diff --git a/jest.config.js b/jest.config.js index 59e21a9..d4f24f0 100644 --- a/jest.config.js +++ b/jest.config.js @@ -2,5 +2,6 @@ module.exports = { preset: 'ts-jest', testEnvironment: 'node', - testMatch: ['src/**/*.test.ts'], + testMatch: ['**/*.test.ts'], + rootDir: 'src', } diff --git a/src/index.ts b/src/index.ts index 3139d83..1076b51 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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' diff --git a/src/log.ts b/src/log.ts index bf62745..982c774 100644 --- a/src/log.ts +++ b/src/log.ts @@ -69,4 +69,6 @@ class Log { } } +export const log = new Log() + export default Log diff --git a/src/utils/config.test.ts b/src/utils/config.test.ts new file mode 100644 index 0000000..275ae8e --- /dev/null +++ b/src/utils/config.test.ts @@ -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) + }) +}) diff --git a/src/utils/config.ts b/src/utils/config.ts index 62a2661..b574b39 100644 --- a/src/utils/config.ts +++ b/src/utils/config.ts @@ -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)