diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a40338c..4cda755 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 "$( { - 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, + ) + } + } + })() diff --git a/src/index.js b/src/index.js index ce228dc..f3ceb30 100644 --- a/src/index.js +++ b/src/index.js @@ -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) diff --git a/src/version/git.js b/src/version/git.js index 0171882..38de4cb 100644 --- a/src/version/git.js +++ b/src/version/git.js @@ -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 diff --git a/src/version/json.js b/src/version/json.js index 33ce3ee..4f75ba0 100644 --- a/src/version/json.js +++ b/src/version/json.js @@ -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() diff --git a/src/version/toml.js b/src/version/toml.js index 50aec9a..33ce04d 100644 --- a/src/version/toml.js +++ b/src/version/toml.js @@ -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 diff --git a/src/version/yaml.js b/src/version/yaml.js index 1f674cf..80975e2 100644 --- a/src/version/yaml.js +++ b/src/version/yaml.js @@ -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, ) diff --git a/test-output.js b/test-output.js new file mode 100644 index 0000000..8ae7821 --- /dev/null +++ b/test-output.js @@ -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') +}) +