From f296aa507c90e4d4575808a91bbfcc0ceacb2566 Mon Sep 17 00:00:00 2001 From: Jeffrey Tang Date: Wed, 18 Sep 2024 16:14:46 -0500 Subject: [PATCH] add helper function Signed-off-by: Jeffrey Tang --- src/core/config_manager.mjs | 13 ++----------- src/core/helpers.mjs | 23 +++++++++++++++++++++++ test/unit/core/config_manager.test.mjs | 13 ++++++------- 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/core/config_manager.mjs b/src/core/config_manager.mjs index da1e9a751..0ca5ce130 100644 --- a/src/core/config_manager.mjs +++ b/src/core/config_manager.mjs @@ -23,6 +23,7 @@ import * as flags from '../commands/flags.mjs' import * as paths from 'path' import * as helpers from './helpers.mjs' import * as yaml from 'js-yaml' +import { yamlToObject } from './helpers.mjs' /** * ConfigManager cache command flag values so that user doesn't need to enter the same values repeatedly. @@ -50,17 +51,7 @@ export class ConfigManager { load () { try { if (fs.existsSync(this.cachedConfigFile)) { - const yamlData = fs.readFileSync(this.cachedConfigFile, 'utf8') - const configItems = yaml.load(yamlData) - let configMap = {} - // add profiles - for (const key in configItems) { - let config = configItems[key] - config = config || {} - configMap[key] = config - } - /** @type {Object} */ - this.config = configMap + this.config = yamlToObject(this.cachedConfigFile) } } catch (e) { throw new FullstackTestingError(`failed to initialize config manager: ${e.message}`, e) diff --git a/src/core/helpers.mjs b/src/core/helpers.mjs index 7c3d372cb..9dc8bdf5d 100644 --- a/src/core/helpers.mjs +++ b/src/core/helpers.mjs @@ -27,6 +27,7 @@ import { Templates } from './templates.mjs' import { HEDERA_HAPI_PATH, ROOT_CONTAINER, SOLO_LOGS_DIR } from './constants.mjs' import { constants } from './index.mjs' import { FileContentsQuery, FileId } from '@hashgraph/sdk' +import * as yaml from 'js-yaml' // cache current directory const CUR_FILE_DIR = paths.dirname(fileURLToPath(import.meta.url)) @@ -353,3 +354,25 @@ export function addDebugOptions (valuesArg, debugNodeId, index = 0) { } return valuesArg } + +/** + * Convert yaml file to object + * @param yamlFile + */ +export function yamlToObject (yamlFile) { + try { + if (fs.existsSync(yamlFile)) { + const yamlData = fs.readFileSync(yamlFile, 'utf8') + const configItems = yaml.load(yamlData) + const configMap = {} + for (const key in configItems) { + let config = configItems[key] + config = config || {} + configMap[key] = config + } + return configMap + } + } catch (e) { + throw new FullstackTestingError(`failed to convert yaml file ${yamlFile} to object: ${e.message}`, e) + } +} diff --git a/test/unit/core/config_manager.test.mjs b/test/unit/core/config_manager.test.mjs index 0c3a8dc8e..a506fe3c2 100644 --- a/test/unit/core/config_manager.test.mjs +++ b/test/unit/core/config_manager.test.mjs @@ -22,19 +22,19 @@ import * as flags from '../../../src/commands/flags.mjs' import fs from 'fs' import { testLogger } from '../../test_util.js' import * as helpers from '../../../src/core/helpers.mjs' +import { yamlToObject } from '../../../src/core/helpers.mjs' describe('ConfigManager', () => { it('should persist config', () => { const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'config-')) - const tmpFile = path.join(tmpDir, 'test.json') + const tmpFile = path.join(tmpDir, 'test.yaml') expect(fs.existsSync(tmpFile)).toBeFalsy() const cm = new ConfigManager(testLogger, tmpFile) cm.persist() expect(fs.existsSync(tmpFile)).toBeTruthy() - const configJSON = fs.readFileSync(tmpFile) - const cachedConfig = JSON.parse(configJSON.toString()) + const cachedConfig = yamlToObject(tmpFile) expect(cachedConfig.version).toStrictEqual(helpers.packageVersion()) expect(cachedConfig.flags).toStrictEqual({}) @@ -49,7 +49,7 @@ describe('ConfigManager', () => { describe('update values using argv', () => { const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'config-')) - const tmpFile = path.join(tmpDir, 'test.json') + const tmpFile = path.join(tmpDir, 'test.yaml') it('should update string flag value', () => { const cm = new ConfigManager(testLogger, tmpFile) @@ -114,7 +114,7 @@ describe('ConfigManager', () => { describe('should apply precedence', () => { const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'config-')) - const tmpFile = path.join(tmpDir, 'test.json') + const tmpFile = path.join(tmpDir, 'test.yaml') const aliases = {} aliases[flags.devMode.name] = [flags.devMode.name, flags.devMode.definition.alias] // mock @@ -221,8 +221,7 @@ describe('ConfigManager', () => { describe('handle argv overrides', () => { const configFilePath = process.cwd() + '/test/data/solo-test-2.config' const cm = new ConfigManager(testLogger, configFilePath) - const configJSON = fs.readFileSync(process.cwd() + '/test/data/solo-test-2.config') - const cachedConfig = JSON.parse(configJSON.toString()) + const cachedConfig = yamlToObject(process.cwd() + '/test/data/solo-test-2.config') it('override config using argv', () => { cm.load()