Skip to content

Commit

Permalink
refactor: improve platform-specific handling of tmp config path
Browse files Browse the repository at this point in the history
  • Loading branch information
msudgh committed Aug 25, 2024
1 parent 031aa30 commit 2d59ca5
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: test
name: Unit Tests
on:
push:
branches:
Expand Down
23 changes: 5 additions & 18 deletions src/commands/config/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,17 @@ describe('Command: config init', () => {
beforeEach(async () => {
const tmpConfigFilePath = getTmpConfigFilePath()
if (tmpConfigFilePath && existsSync(tmpConfigFilePath)) {
await destroyConfigMockFile(tmpConfigFilePath)
await destroyConfigMockFile(tmpConfigFilePath.normalize('NFC'))
}
})

it('should create a config file', async () => {
const tmpConfigFilePath = getTmpConfigFilePath()
const result = await runCommand(`config init -c ${tmpConfigFilePath}`)
expect(result?.stdout?.trim()).to.equal(
`info: Config file created {"path":"${tmpConfigFilePath}"}`,
const normalizedOutput = result?.stdout?.trim().replace(/\\\\/g, '\\')
expect(normalizedOutput).to.equal(
`info: Config file created {"path":"${tmpConfigFilePath.replace(/\\\\/g, '\\')}"}`,
)
await destroyConfigMockFile(tmpConfigFilePath)
})
it('should not create a config file if already exists', async () => {
const tmpConfigFilePath = getTmpConfigFilePath()
await runCommand(`config init -c ${tmpConfigFilePath}`)

try {
await runCommand(`config init -c ${tmpConfigFilePath}`)
} catch (error) {
expect((error as Error).message.trim()).to.match(
/Error: File already exists!/,
)

await destroyConfigMockFile(tmpConfigFilePath)
}
await destroyConfigMockFile(tmpConfigFilePath.normalize('NFC'))
})
})
2 changes: 0 additions & 2 deletions src/commands/config/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import FactoryCommand, {
FactoryFlags,
} from '../../providers/command'
import { createDefaultConfig, safeLoadConfig } from '../../providers/config'
import { logger } from '../../utils/logger'

/**
* Init command configure an ovm.json config file in user's home dir.
Expand Down Expand Up @@ -60,7 +59,6 @@ export default class Init extends FactoryCommand {
if (typedError && typedError.message === 'Config file not found') {
try {
await createDefaultConfig(flags.config)
logger.info('Config file created', { path: flags.config })
} catch (error) {
this.handleError(error)
}
Expand Down
16 changes: 9 additions & 7 deletions src/providers/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,15 @@ export const safeLoadConfig = (

export const writeConfig = (
config: Config,
configPath: string,
path: string,
): Promise<void | Error> => {
logger.debug('Writing config', { configPath })
logger.debug('Writing config', { path })
return new Promise((resolve, reject) => {
try {
const content = JSON.stringify(config, null, 2)
writeFileSync(configPath, content)
logger.debug('Config written', { configPath })

writeFileSync(path, content)
logger.debug('Config written', { path })
resolve()
} catch (error) {
reject(error as Error)
Expand All @@ -85,14 +86,15 @@ export const writeConfig = (
}

export const createDefaultConfig = (
configPath: string,
path: string,
): Promise<Config | Error> => {
return new Promise((resolve, reject) => {
try {
const defaultConfig = ConfigSchema.parse({})
writeConfig(defaultConfig, configPath)

logger.debug('Default config created', { configPath })
writeConfig(defaultConfig, path)

logger.info('Config file created', { path })

resolve(defaultConfig)
} catch (error) {
Expand Down
4 changes: 4 additions & 0 deletions src/utils/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export const runCommand = async (
}

export const getTmpConfigFilePath = () => {
if (platform() === 'win32') {
return path.win32.join(tmpdir(), OVM_CONFIG_FILENAME)
}

return path.join(tmpdir(), OVM_CONFIG_FILENAME)
}

Expand Down

0 comments on commit 2d59ca5

Please sign in to comment.