|
| 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 | +}) |
0 commit comments