Skip to content

Commit

Permalink
feat: allow --config with absolute path [gh-0] (#589)
Browse files Browse the repository at this point in the history
* feat: allow --config with absolute path [gh-0]

* chore: use path.resolve instead checking isAbsolute() [gh-0]

* chore: return real OS path from splitConfigFilePath [gh-0]

* debug: add console.info() for tests [gh-0]

* debug: remove console.info() for tests [gh-0]
  • Loading branch information
Nahuel Alejandro Ramos authored Mar 16, 2023
1 parent c326b6a commit 59acfb2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
8 changes: 4 additions & 4 deletions packages/cli/e2e/__tests__/test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ describe('test', () => {
accountId: config.get('accountId'),
directory: path.join(__dirname, 'fixtures', 'test-project'),
})
expect(result.status).toBe(0)
expect(result.stdout).not.toContain('File extension type example')
expect(result.stdout).toContain(secretEnv)
expect(result.status).toBe(0)
})

it('Should include only one check', () => {
Expand All @@ -24,9 +24,9 @@ describe('test', () => {
accountId: config.get('accountId'),
directory: path.join(__dirname, 'fixtures', 'test-project'),
})
expect(result.status).toBe(0)
expect(result.stdout).toContain('Show SECRET_ENV value')
expect(result.stdout).toContain('1 passed, 1 total')
expect(result.status).toBe(0)
})

it('Should use different config file', () => {
Expand All @@ -36,9 +36,9 @@ describe('test', () => {
accountId: config.get('accountId'),
directory: path.join(__dirname, 'fixtures', 'test-project'),
})
expect(result.status).toBe(0)
expect(result.stdout).toContain('Show SECRET_ENV value')
expect(result.stdout).toContain('1 passed, 1 total')
expect(result.status).toBe(0)
})

it('Should fail with config file not found', () => {
Expand All @@ -58,8 +58,8 @@ describe('test', () => {
accountId: config.get('accountId'),
directory: path.join(__dirname, 'fixtures', 'test-project'),
})
expect(result.status).toBe(0)
expect(result.stdout).toContain('0 total')
expect(result.status).toBe(0)
})

it('Should terminate with error when JS/TS throws error', () => {
Expand Down
43 changes: 33 additions & 10 deletions packages/cli/src/services/__tests__/checkly-config-loader.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as path from 'path'
import { loadChecklyConfig } from '../checkly-config-loader'
import { pathToPosix, splitConfigFilePath } from '../util'
import { splitConfigFilePath } from '../util'

describe('loadChecklyConfig()', () => {
it('config file should export an object', async () => {
Expand All @@ -26,15 +26,16 @@ describe('loadChecklyConfig()', () => {
}
})
it('config TS file should export an object', async () => {
const configFile = './fixtures/configs/good-config.ts'
const filename = 'good-config.ts'
const configFile = `./fixtures/configs/${filename}`
const { configDirectory, configFilenames } = splitConfigFilePath(configFile)

expect(configFilenames).toEqual(['good-config.ts'])
expect(configDirectory).toEqual(pathToPosix(path.dirname(path.join(process.cwd(), configFile))))
expect(configFilenames).toEqual([filename])
expect(configDirectory).toEqual(path.dirname(path.join(process.cwd(), configFile)))

const {
config,
} = await loadChecklyConfig(path.join(__dirname, 'fixtures', 'configs'), ['good-config.ts'])
} = await loadChecklyConfig(path.join(__dirname, 'fixtures', 'configs'), [filename])

expect(config).toMatchObject({
checks: {
Expand All @@ -45,16 +46,38 @@ describe('loadChecklyConfig()', () => {
},
})
})
it('config TS file should export an object', async () => {
const configFile = './fixtures/configs/good-config.js'
it('config JS file should export an object', async () => {
const filename = 'good-config.js'
const configFile = `./fixtures/configs/${filename}`
const { configDirectory, configFilenames } = splitConfigFilePath(configFile)

expect(configFilenames).toEqual(['good-config.js'])
expect(configDirectory).toEqual(pathToPosix(path.dirname(path.join(process.cwd(), configFile))))
expect(configFilenames).toEqual([filename])
expect(configDirectory).toEqual(path.dirname(path.join(process.cwd(), configFile)))

const {
config,
} = await loadChecklyConfig(path.join(__dirname, 'fixtures', 'configs'), [filename])

expect(config).toMatchObject({
checks: {
checkMatch: '**/*.check.ts',
browserChecks: {
testMatch: '**/__checks__/*.spec.ts',
},
},
})
})
it('config from absolute path', async () => {
const filename = 'good-config.ts'
const configFile = `./fixtures/configs/${filename}`
const { configDirectory, configFilenames } = splitConfigFilePath(path.join(process.cwd(), configFile))

expect(configFilenames).toEqual([filename])
expect(configDirectory).toEqual(path.dirname(path.join(process.cwd(), configFile)))

const {
config,
} = await loadChecklyConfig(path.join(__dirname, 'fixtures', 'configs'), ['good-config.js'])
} = await loadChecklyConfig(path.join(__dirname, 'fixtures', 'configs'), [filename])

expect(config).toMatchObject({
checks: {
Expand Down
11 changes: 9 additions & 2 deletions packages/cli/src/services/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,15 @@ export function pathToPosix (relPath: string): string {
}

export function splitConfigFilePath (configFile?: string): { configDirectory: string, configFilenames?: string[] } {
if (configFile) {
const cwd = path.resolve(path.dirname(configFile))
return {
configDirectory: cwd,
configFilenames: [path.basename(configFile)],
}
}
return {
configDirectory: configFile ? pathToPosix(path.join(process.cwd(), path.dirname(configFile))) : process.cwd(),
configFilenames: configFile ? [path.basename(configFile)] : undefined,
configDirectory: process.cwd(),
configFilenames: undefined,
}
}

0 comments on commit 59acfb2

Please sign in to comment.