Skip to content

Commit 4acb224

Browse files
committed
Rework config.ts & add unit test
1 parent 51d9a55 commit 4acb224

File tree

4 files changed

+95
-18
lines changed

4 files changed

+95
-18
lines changed

src/config.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,17 @@ const defaultConfig = {
1919
enterprise: true
2020
}
2121

22+
function toBooleanConfig(configValue?: string | boolean) {
23+
if (configValue && typeof configValue === 'string') {
24+
return (configValue === 'true')
25+
}
26+
return configValue
27+
}
28+
2229
const envConfig = {
2330
cookiePath: process.env.HMD_CLI_COOKIE_PATH || process.env.CMD_CLI_COOKIE_PATH,
2431
serverUrl: process.env.HMD_CLI_SERVER_URL || process.env.CMD_CLI_SERVER_URL,
25-
enterprise: (process.env.HMD_CLI_COOKIE_PATH || process.env.HMD_CLI_SERVER_URL)
32+
enterprise: (process.env.HMD_CLI_COOKIE_PATH || process.env.HMD_CLI_SERVER_URL) ? true : toBooleanConfig(process.env.HMD_CLI_ENTERPRISE)
2633
}
2734

2835
// look for a readable config file; we can merge it with the env.
@@ -62,6 +69,7 @@ ${err}
6269
// prefer environment config over file config
6370
const config = defaults(envConfig, readConfig, defaultConfig)
6471

72+
// !FIXME This branching never meets because we have defaultConfig
6573
if (!config.serverUrl) {
6674
throw new Error(`
6775

test/commands/hello.test.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

test/config.test.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import {expect} from '@oclif/test'
2+
import * as fs from 'fs-extra'
3+
import * as path from 'path'
4+
5+
import {tempDir} from './utils'
6+
7+
const requireConfig = () => require('../src/config').default
8+
9+
const cleanup = () => {
10+
// delete config module cache
11+
delete require.cache[require.resolve('../src/config')]
12+
13+
// reset env
14+
process.env = {}
15+
}
16+
17+
const setupConfigFile = () => {
18+
const configDir = tempDir()
19+
process.env.HMD_CLI_CONFIG_DIR = configDir
20+
return path.join(configDir, 'config.json')
21+
}
22+
23+
describe('Config test', function () {
24+
beforeEach(function () {
25+
cleanup()
26+
this.configFilePath = setupConfigFile()
27+
})
28+
29+
it('should throw no config error if config.json not found and no serverUrl set in env', function () {
30+
expect(requireConfig)
31+
.to.throw(new RegExp(`Configuration file at ${this.configFilePath} not readable`))
32+
})
33+
34+
it('should throw read error if config.json is not valid JSON', function () {
35+
fs.writeFileSync(this.configFilePath, '.', 'utf8')
36+
37+
expect(requireConfig)
38+
.to.throw(/Could not read JSON config file at/)
39+
})
40+
41+
it.skip('should throw error if no serverUrl is set', function () {
42+
fs.writeFileSync(this.configFilePath, '{}', 'utf8')
43+
44+
expect(requireConfig)
45+
.to.throw(/Please specify CodiMD server URL either/)
46+
})
47+
48+
it('should set enterprise to true if HMD_CLI_COOKIE_PATH is supplied', function () {
49+
process.env.HMD_CLI_COOKIE_PATH = tempDir()
50+
fs.writeFileSync(this.configFilePath, '{}', 'utf8')
51+
52+
const config = requireConfig()
53+
54+
expect(config.cookiePath).to.eq(process.env.HMD_CLI_COOKIE_PATH)
55+
expect(config.enterprise).to.eq(true)
56+
})
57+
58+
it('should set enterprise to true if HMD_CLI_SERVER_URL is supplied', function () {
59+
process.env.HMD_CLI_SERVER_URL = tempDir()
60+
fs.writeFileSync(this.configFilePath, '{}', 'utf8')
61+
62+
const config = requireConfig()
63+
64+
expect(config.serverUrl).to.eq(process.env.HMD_CLI_SERVER_URL)
65+
expect(config.enterprise).to.eq(true)
66+
})
67+
68+
it('should set enterprise with HMD_CLI_ENTERPRISE', function () {
69+
fs.writeFileSync(this.configFilePath, '{}', 'utf8')
70+
71+
process.env.HMD_CLI_ENTERPRISE = 'false'
72+
expect(requireConfig().enterprise).to.eq(false)
73+
74+
cleanup()
75+
76+
process.env.HMD_CLI_ENTERPRISE = 'true'
77+
expect(requireConfig().enterprise).to.eq(true)
78+
})
79+
})

test/utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as fs from 'fs-extra'
2+
import * as os from 'os'
3+
import * as path from 'path'
4+
5+
export function tempDir() {
6+
return fs.mkdtempSync(path.join(os.tmpdir(), 'foo-'))
7+
}

0 commit comments

Comments
 (0)