Skip to content

Commit

Permalink
test(config): include tests for config loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmad Nassri committed Dec 14, 2020
1 parent 5333af2 commit 67a38ec
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 8 deletions.
16 changes: 8 additions & 8 deletions action/lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ import yaml from 'js-yaml'

// default value is passed from workflow
export default function ({ workspace, inputs }) {
const configPath = path.join(workspace, inputs.config)

let config
const configPath = path.join(workspace || '', inputs.config || '.github/auto-merge.yml')

// read auto-merge.yml to determine what should be merged
if (fs.existsSync(configPath)) {
// parse .github/auto-merge.yml
const configYaml = fs.readFileSync(configPath, 'utf8')
config = yaml.safeLoad(configYaml)
const config = yaml.safeLoad(configYaml)
core.info('loaded merge config: \n' + configYaml)
} else {
// or convert the input "target" to the equivalent config
config = [{ match: { dependency_type: 'all', update_type: `semver:${inputs.target}` } }]
core.info('using workflow\'s "target": \n' + yaml.safeDump(config))

return config
}

// or convert the input "target" to the equivalent config
const config = [{ match: { dependency_type: 'all', update_type: `semver:${inputs.target}` } }]
core.info('using workflow\'s "target": \n' + yaml.safeDump(config))

return config
}
1 change: 1 addition & 0 deletions action/test/fixtures/.github/auto-merge.yml
3 changes: 3 additions & 0 deletions action/test/fixtures/config-valid.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- match:
dependency_type: development
update_type: semver:minor
58 changes: 58 additions & 0 deletions action/test/parse/config-load.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// packages
import tap from 'tap'
import sinon from 'sinon'

import core from '@actions/core'

// module
import config from '../../lib/config.js'

import path from 'path'
const __dirname = path.resolve()

const workspace = `${__dirname}/test/fixtures/`

tap.test('input.config --> default', async assert => {
assert.plan(2)

sinon.stub(core, 'info') // silence output on terminal

const expected = [{ match: { dependency_type: 'development', update_type: 'semver:minor' } }]

const result = config({ workspace, inputs: { } })

assert.match(core.info.getCall(-1)?.firstArg, 'loaded merge config')
assert.match(result, expected)

core.info.restore()
})

tap.test('input.config --> custom', async assert => {
assert.plan(2)

sinon.stub(core, 'info') // silence output on terminal

const expected = [{ match: { dependency_type: 'development', update_type: 'semver:minor' } }]

const result = config({ workspace, inputs: { config: 'config-valid.yml' } })

assert.match(core.info.getCall(-1)?.firstArg, 'loaded merge config')
assert.match(result, expected)

core.info.restore()
})

tap.test('input.config --> no file', async assert => {
assert.plan(2)

sinon.stub(core, 'info') // silence output on terminal

const expected = [{ match: { dependency_type: 'all', update_type: 'semver:patch' } }]

const result = config({ inputs: { target: 'patch' } })

assert.match(core.info.getCall(-1)?.firstArg, 'using workflow\'s "target":')
assert.match(result, expected)

core.info.restore()
})
File renamed without changes.

0 comments on commit 67a38ec

Please sign in to comment.