test: Added tests for flows

releases/v3
Tycho Bokdam 2020-12-15 17:23:47 +01:00
parent 35206c5104
commit 289e972b9b
No known key found for this signature in database
GPG Key ID: A0FAE77C8CDF33C7
8 changed files with 431 additions and 232 deletions

View File

@ -1,5 +1,5 @@
name: 'Test the action'
on: [pull_request]
on: [ pull_request ]
jobs:
test-json:
@ -21,6 +21,7 @@ jobs:
uses: ./
env:
ENV: 'dont-use-git'
EXPECTED_TAG: 'v1.5.0'
with:
github-token: ${{ secrets.github_token }}
version-file: 'test-file.json'
@ -29,6 +30,12 @@ jobs:
run: |
echo "$(<test-file.json)"
- name: Test output
run: node ./test-output.js
env:
FILES: 'test-file.json'
EXPECTED_VERSION: '1.5.0'
test-json-new:
runs-on: ubuntu-latest
steps:
@ -48,6 +55,7 @@ jobs:
uses: ./
env:
ENV: 'dont-use-git'
EXPECTED_TAG: 'v0.1.0'
with:
github-token: ${{ secrets.github_token }}
version-file: 'test-file-new.json'
@ -56,6 +64,12 @@ jobs:
run: |
echo "$(<test-file-new.json)"
- name: Test output
run: node ./test-output.js
env:
FILES: 'test-file-new.json'
EXPECTED_VERSION: '0.1.0'
test-json-empty:
runs-on: ubuntu-latest
steps:
@ -77,12 +91,292 @@ jobs:
uses: ./
env:
ENV: 'dont-use-git'
EXPECTED_TAG: 'v0.1.0'
with:
github-token: ${{ secrets.github_token }}
version-file: './test-file-empty.json'
- run: echo "$(<./test-file-empty.json)"
- name: Test output
run: node ./test-output.js
env:
FILES: 'test-file-empty.json'
EXPECTED_VERSION: '0.1.0'
test-git:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
path: "./"
- run: npm ci --prod
- run: touch ./fake-file.log
- run: "git config --global user.email 'changelog@github.com'"
- run: "git config --global user.name 'Awesome Github action'"
- run: "git add . && git commit -m 'feat: Added fake file so version will be bumped'"
- name: Generate changelog
id: changelog
uses: ./
env:
ENV: 'dont-use-git'
EXPECTED_TAG: 'v0.1.0'
SKIPPED_COMMIT: true
with:
github-token: ${{ secrets.github_token }}
skip-commit: 'true'
test-git-fallback:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
path: "./"
- run: npm ci --prod
- run: touch ./fake-file.log
- run: "git config --global user.email 'changelog@github.com'"
- run: "git config --global user.name 'Awesome Github action'"
- run: "git add . && git commit -m 'feat: Added fake file so version will be bumped'"
- run: git tag | xargs git tag -d
- name: Generate changelog
id: changelog
uses: ./
env:
ENV: 'dont-use-git'
EXPECTED_TAG: 'v0.1.0'
SKIPPED_COMMIT: true
with:
github-token: ${{ secrets.github_token }}
skip-commit: 'true'
test-yaml:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
path: "./"
- run: npm ci --prod
- run: touch ./fake-file.log
- run: "git config --global user.email 'changelog@github.com'"
- run: "git config --global user.name 'Awesome Github action'"
- run: "git add . && git commit -m 'feat: Added fake file so version will be bumped'"
- name: Generate changelog
id: changelog
uses: ./
env:
ENV: 'dont-use-git'
EXPECTED_TAG: 'v9.5.0'
with:
github-token: ${{ secrets.github_token }}
version-file: 'test-file.yaml'
version-path: 'package.version'
- name: Show file
run: |
echo "$(<test-file.yaml)"
- name: Test output
run: node ./test-output.js
env:
FILES: 'test-file.yaml'
EXPECTED_VERSION: '9.5.0'
EXPECTED_VERSION_PATH: 'package.version'
test-yaml-new:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
path: "./"
- run: npm ci --prod
- run: touch ./fake-file.log
- run: "git config --global user.email 'changelog@github.com'"
- run: "git config --global user.name 'Awesome Github action'"
- run: "git add . && git commit -m 'feat: Added fake file so version will be bumped'"
- run: touch ./test-file-empty.yaml
- name: Generate changelog
id: changelog
uses: ./
env:
ENV: 'dont-use-git'
EXPECTED_TAG: 'v0.1.0'
with:
github-token: ${{ secrets.github_token }}
version-file: 'test-file-new.yaml'
version-path: 'package.version'
- name: Show file
run: |
echo "$(<test-file-new.yaml)"
- name: Test output
run: node ./test-output.js
env:
FILES: 'test-file-new.yaml'
EXPECTED_VERSION: '0.1.0'
EXPECTED_VERSION_PATH: 'package.version'
test-yaml-empty:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
path: "./"
- run: npm ci --prod
- run: touch ./fake-file.log
- run: "git config --global user.email 'changelog@github.com'"
- run: "git config --global user.name 'Awesome Github action'"
- run: "git add . && git commit -m 'feat: Added fake file so version will be bumped'"
- name: Generate changelog
id: changelog
uses: ./
env:
ENV: 'dont-use-git'
EXPECTED_TAG: 'v0.1.0'
with:
github-token: ${{ secrets.github_token }}
version-file: './test-file-empty.yaml'
version-path: 'package.version'
- name: Show file
run: |
echo "$(<test-file-empty.yaml)"
- name: Test output
run: node ./test-output.js
env:
FILES: 'test-file-empty.yaml'
EXPECTED_VERSION: '0.1.0'
EXPECTED_VERSION_PATH: 'package.version'
test-toml:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
path: "./"
- run: npm ci --prod
- run: touch ./fake-file.log
- run: "git config --global user.email 'changelog@github.com'"
- run: "git config --global user.name 'Awesome Github action'"
- run: "git add . && git commit -m 'feat: Added fake file so version will be bumped'"
- name: Generate changelog
id: changelog
uses: ./
env:
ENV: 'dont-use-git'
EXPECTED_TAG: 'v0.10.0'
with:
github-token: ${{ secrets.github_token }}
version-file: 'test-file.toml'
version-path: 'package.version'
- name: Show file
run: |
echo "$(<test-file.toml)"
- name: Test output
run: node ./test-output.js
env:
FILES: 'test-file.toml'
EXPECTED_VERSION: '0.10.0'
EXPECTED_VERSION_PATH: 'package.version'
test-toml-new:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
path: "./"
- run: npm ci --prod
- run: touch ./fake-file.log
- run: "git config --global user.email 'changelog@github.com'"
- run: "git config --global user.name 'Awesome Github action'"
- run: "git add . && git commit -m 'feat: Added fake file so version will be bumped'"
- name: Generate changelog
id: changelog
uses: ./
env:
ENV: 'dont-use-git'
EXPECTED_TAG: 'v0.1.0'
with:
github-token: ${{ secrets.github_token }}
version-file: 'test-file-new.toml'
version-path: 'package.version'
- name: Show file
run: |
echo "$(<test-file-new.toml)"
- name: Test output
run: node ./test-output.js
env:
FILES: 'test-file-new.toml'
EXPECTED_VERSION: '0.1.0'
EXPECTED_VERSION_PATH: 'package.version'
test-toml-empty:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
path: "./"
- run: npm ci --prod
- run: touch ./fake-file.log
- run: "git config --global user.email 'changelog@github.com'"
- run: "git config --global user.name 'Awesome Github action'"
- run: "git add . && git commit -m 'feat: Added fake file so version will be bumped'"
- run: touch ./test-file-empty.toml
- name: Generate changelog
id: changelog
uses: ./
env:
ENV: 'dont-use-git'
EXPECTED_TAG: 'v6.5.0'
with:
github-token: ${{ secrets.github_token }}
version-file: './test-file-empty.toml'
version-path: 'package.version'
fallback-version: '6.5.0'
- name: Show file
run: |
echo "$(<test-file-empty.toml)"
- name: Test output
run: node ./test-output.js
env:
FILES: 'test-file-empty.toml'
EXPECTED_VERSION: '6.5.0'
EXPECTED_VERSION_PATH: 'package.version'
test-pre-commit:
runs-on: ubuntu-latest
steps:
@ -134,6 +428,7 @@ jobs:
uses: ./
env:
ENV: 'dont-use-git'
EXPECTED_TAG: 'v1.0.100-alpha'
with:
github-token: ${{ secrets.github_token }}
pre-changelog-generation: './test-pre-changelog-generation.js'
@ -146,226 +441,6 @@ jobs:
- run: cat pre-changelog-generation.tag.test.json && echo ""
- run: cat ./test-file.toml
test-git:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
path: "./"
- run: npm ci --prod
- run: touch ./fake-file.log
- run: "git config --global user.email 'changelog@github.com'"
- run: "git config --global user.name 'Awesome Github action'"
- run: "git add . && git commit -m 'feat: Added fake file so version will be bumped'"
- name: Generate changelog
id: changelog
uses: ./
env:
ENV: 'dont-use-git'
with:
github-token: ${{ secrets.github_token }}
skip-commit: 'true'
test-git-fallback:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
path: "./"
- run: npm ci --prod
- run: touch ./fake-file.log
- run: "git config --global user.email 'changelog@github.com'"
- run: "git config --global user.name 'Awesome Github action'"
- run: "git add . && git commit -m 'feat: Added fake file so version will be bumped'"
- run: git tag | xargs git tag -d
- name: Generate changelog
id: changelog
uses: ./
env:
ENV: 'dont-use-git'
with:
github-token: ${{ secrets.github_token }}
skip-commit: 'true'
test-yaml:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
path: "./"
- run: npm ci --prod
- run: touch ./fake-file.log
- run: "git config --global user.email 'changelog@github.com'"
- run: "git config --global user.name 'Awesome Github action'"
- run: "git add . && git commit -m 'feat: Added fake file so version will be bumped'"
- name: Generate changelog
id: changelog
uses: ./
env:
ENV: 'dont-use-git'
with:
github-token: ${{ secrets.github_token }}
version-file: 'test-file.yaml'
version-path: 'package.version'
- name: Show file
run: |
echo "$(<test-file.yaml)"
test-yaml-new:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
path: "./"
- run: npm ci --prod
- run: touch ./fake-file.log
- run: "git config --global user.email 'changelog@github.com'"
- run: "git config --global user.name 'Awesome Github action'"
- run: "git add . && git commit -m 'feat: Added fake file so version will be bumped'"
- run: touch ./test-file-empty.yaml
- name: Generate changelog
id: changelog
uses: ./
env:
ENV: 'dont-use-git'
with:
github-token: ${{ secrets.github_token }}
version-file: 'test-file-new.yaml'
version-path: 'package.version'
- name: Show file
run: |
echo "$(<test-file-new.yaml)"
test-yaml-empty:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
path: "./"
- run: npm ci --prod
- run: touch ./fake-file.log
- run: "git config --global user.email 'changelog@github.com'"
- run: "git config --global user.name 'Awesome Github action'"
- run: "git add . && git commit -m 'feat: Added fake file so version will be bumped'"
- name: Generate changelog
id: changelog
uses: ./
env:
ENV: 'dont-use-git'
with:
github-token: ${{ secrets.github_token }}
version-file: './test-file-empty.yaml'
version-path: 'package.version'
- name: Show file
run: |
echo "$(<test-file-empty.yaml)"
test-toml:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
path: "./"
- run: npm ci --prod
- run: touch ./fake-file.log
- run: "git config --global user.email 'changelog@github.com'"
- run: "git config --global user.name 'Awesome Github action'"
- run: "git add . && git commit -m 'feat: Added fake file so version will be bumped'"
- name: Generate changelog
id: changelog
uses: ./
env:
ENV: 'dont-use-git'
with:
github-token: ${{ secrets.github_token }}
version-file: 'test-file.toml'
version-path: 'package.version'
- name: Show file
run: |
echo "$(<test-file.toml)"
test-toml-new:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
path: "./"
- run: npm ci --prod
- run: touch ./fake-file.log
- run: "git config --global user.email 'changelog@github.com'"
- run: "git config --global user.name 'Awesome Github action'"
- run: "git add . && git commit -m 'feat: Added fake file so version will be bumped'"
- name: Generate changelog
id: changelog
uses: ./
env:
ENV: 'dont-use-git'
with:
github-token: ${{ secrets.github_token }}
version-file: 'test-file-new.toml'
version-path: 'package.version'
- name: Show file
run: |
echo "$(<test-file-new.toml)"
test-toml-empty:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
path: "./"
- run: npm ci --prod
- run: touch ./fake-file.log
- run: "git config --global user.email 'changelog@github.com'"
- run: "git config --global user.name 'Awesome Github action'"
- run: "git add . && git commit -m 'feat: Added fake file so version will be bumped'"
- run: touch ./test-file-empty.toml
- name: Generate changelog
id: changelog
uses: ./
env:
ENV: 'dont-use-git'
with:
github-token: ${{ secrets.github_token }}
version-file: './test-file-empty.toml'
version-path: 'package.version'
- name: Show file
run: |
echo "$(<test-file-empty.toml)"
test-multiple-files:
runs-on: ubuntu-latest
steps:
@ -385,6 +460,7 @@ jobs:
uses: ./
env:
ENV: 'dont-use-git'
EXPECTED_TAG: 'v1.5.0'
with:
github-token: ${{ secrets.github_token }}
version-file: 'test-file.json, test-file.toml, test-file.yaml'
@ -392,9 +468,17 @@ jobs:
- name: Show files
run: |
echo "$(<test-file.json)"
echo ""
echo "$(<test-file.toml)"
echo ""
echo "$(<test-file.yaml)"
- name: Test output
run: node ./test-output.js
env:
FILES: 'test-file.json, test-file.toml, test-file.yaml'
EXPECTED_VERSION: '1.5.0, 0.1.0, 0.1.0'
test-config-file-path:
runs-on: ubuntu-latest
steps:
@ -414,7 +498,19 @@ jobs:
uses: ./
env:
ENV: 'dont-use-git'
EXPECTED_TAG: 'v0.1.0'
with:
github-token: ${{ secrets.github_token }}
skip-version-file: 'true'
config-file-path: './test-changelog.config.js'
- name: Test output
with:
GENERATED_CHANGELOG_OUTPUT: ${{ steps.changelog.outputs.clean_changelog }}
run: |
if grep -q "### New Features" "./CHANGELOG.md"; then
echo "Generated changelog file has config applied"
else
echo "Changelog config not applied" && exit 1
fi

View File

@ -1,5 +1,6 @@
const core = require('@actions/core')
const exec = require('@actions/exec')
const assert = require('assert')
const { GITHUB_REPOSITORY, GITHUB_REF, ENV } = process.env
@ -7,6 +8,8 @@ const branch = GITHUB_REF.replace('refs/heads/', '')
module.exports = new (class Git {
commandsRun = []
constructor() {
const githubToken = core.getInput('github-token', { required: true })
@ -16,10 +19,14 @@ module.exports = new (class Git {
const gitUserName = core.getInput('git-user-name')
const gitUserEmail = core.getInput('git-user-email')
// if the env is dont-use-git then we mock exec as we are testing a workflow locally
// if the env is dont-use-git then we mock exec as we are testing a workflow
if (ENV === 'dont-use-git') {
this.exec = (command) => {
console.log(`Skipping "git ${command}" because of test env`)
const fullCommand = `git ${command}`
console.log(`Skipping "${fullCommand}" because of test env`)
this.commandsRun.push(fullCommand)
}
}
@ -69,12 +76,11 @@ module.exports = new (class Git {
* Commit all changes
*
* @param message
* @param args
*
* @return {Promise<>}
*/
commit = (message, args = []) => (
this.exec(`commit -m "${message}" ${args.join(' ')}`)
commit = (message) => (
this.exec(`commit -m "${message}"`)
)
/**
@ -137,4 +143,34 @@ module.exports = new (class Git {
*/
createTag = (tag) => this.exec(`tag -a ${tag} -m "${tag}"`)
/**
* Validates the commands run
*/
testHistory = () => {
if (ENV === 'dont-use-git') {
const { EXPECTED_TAG, SKIPPED_COMMIT } = process.env
const expectedCommands = [
'git config user.name "Conventional Changelog Action"',
'git config user.email "conventional.changelog.action@github.com"',
'git remote set-url origin https://x-access-token:fake-token@github.com/TriPSs/conventional-changelog-action.git',
'git rev-parse --is-shallow-repository',
'git pull --tags --ff-only',
]
if (!SKIPPED_COMMIT) {
expectedCommands.push('git add .')
expectedCommands.push(`git commit -m "chore(release): ${EXPECTED_TAG}"`)
}
expectedCommands.push(`git tag -a ${EXPECTED_TAG} -m "${EXPECTED_TAG}"`)
expectedCommands.push(`git push origin ${branch} --follow-tags`)
assert.deepStrictEqual(
this.commandsRun,
expectedCommands,
)
}
}
})()

View File

@ -180,6 +180,16 @@ async function run() {
core.setOutput('version', newVersion)
core.setOutput('tag', gitTag)
core.setOutput('skipped', 'false')
try {
// If we are running in test mode we use this to validate everything still runs
git.testHistory()
} catch (error) {
console.error(error)
core.setFailed(error)
}
})
} catch (error) {
core.setFailed(error)

View File

@ -12,7 +12,7 @@ module.exports = new (class Git extends BaseVersioning {
gitSemverTags({
tagPrefix,
}, async (err, tags) => {
}, async(err, tags) => {
const currentVersion = tags.length > 0 ? tags.shift().replace(tagPrefix, '') : null
// Get the new version

View File

@ -12,7 +12,7 @@ module.exports = new (class Json extends BaseVersioning {
* @param {!string} releaseType - The type of release
* @return {*}
*/
bump = async (releaseType) => {
bump = async(releaseType) => {
// Read the file
const fileContent = this.read()

View File

@ -4,7 +4,7 @@ const toml = require('@iarna/toml')
const BaseVersioning = require('./base')
const bumpVersion = require('../helpers/bumpVersion')
module.exports = new (class Toml extends BaseVersioning{
module.exports = new (class Toml extends BaseVersioning {
/**
* Bumps the version in the package.json

View File

@ -4,7 +4,7 @@ const yaml = require('yaml')
const BaseVersioning = require('./base')
const bumpVersion = require('../helpers/bumpVersion')
module.exports = new (class Yaml extends BaseVersioning{
module.exports = new (class Yaml extends BaseVersioning {
/**
* Bumps the version in the package.json
@ -12,14 +12,14 @@ module.exports = new (class Yaml extends BaseVersioning{
* @param {!string} releaseType - The type of release
* @return {*}
*/
bump = async (releaseType) => {
bump = async(releaseType) => {
// Read the file
const fileContent = this.read()
const yamlContent = yaml.parse(fileContent) || {}
const oldVersion = objectPath.get(yamlContent, this.versionPath, null)
// Get the new version
this.newVersion =await bumpVersion(
this.newVersion = await bumpVersion(
releaseType,
oldVersion,
)

57
test-output.js 100644
View File

@ -0,0 +1,57 @@
const fs = require('fs')
const assert = require('assert')
const objectPath = require('object-path')
const yaml = require('yaml')
const toml = require('@iarna/toml')
const actionConfig = yaml.parse(fs.readFileSync('./action.yml', 'utf8'))
const {
FILES = actionConfig.inputs['version-file'].default,
EXPECTED_VERSION_PATH = actionConfig.inputs['version-path'].default,
EXPECTED_VERSION = actionConfig.inputs['fallback-version'].default,
} = process.env
assert.ok(FILES, 'Files not defined!')
/**
* Test if all the files are updated
*/
FILES.split(',').map((file, index) => {
const fileContent = fs.readFileSync(file.trim(), 'utf8')
const fileExtension = file.split('.').pop()
assert.ok(fileExtension, 'No file extension found!')
let parsedContent = null
switch (fileExtension.toLowerCase()) {
case 'json':
parsedContent = JSON.parse(fileContent)
break
case 'yaml':
case 'yml':
parsedContent = yaml.parse(fileContent)
break
case 'toml':
parsedContent = toml.parse(fileContent)
break
default:
assert.fail('File extension not supported!')
}
assert.ok(parsedContent, 'Content could not be parsed!')
const newVersionInFile = objectPath.get(parsedContent, EXPECTED_VERSION_PATH, null)
const expectedVersions = EXPECTED_VERSION.split(',')
const expectedVersion = expectedVersions.length > 0
? expectedVersions[index]
: expectedVersions
assert.strictEqual(newVersionInFile, expectedVersion.trim(), 'Version does not match what is expected')
})