Skip to content

Commit

Permalink
add helper function
Browse files Browse the repository at this point in the history
Signed-off-by: Jeffrey Tang <[email protected]>
  • Loading branch information
JeffreyDallas committed Sep 18, 2024
1 parent d1f7857 commit f296aa5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
13 changes: 2 additions & 11 deletions src/core/config_manager.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions src/core/helpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)) {

Check warning on line 364 in src/core/helpers.mjs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/core/helpers.mjs#L364

The application dynamically constructs file or path information.
const yamlData = fs.readFileSync(yamlFile, 'utf8')

Check warning on line 365 in src/core/helpers.mjs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/core/helpers.mjs#L365

The application dynamically constructs file or path information.
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)

Check warning on line 376 in src/core/helpers.mjs

View check run for this annotation

Codecov / codecov/patch

src/core/helpers.mjs#L376

Added line #L376 was not covered by tests
}
}
13 changes: 6 additions & 7 deletions test/unit/core/config_manager.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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({})
Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit f296aa5

Please sign in to comment.