diff --git a/tests/end-to-end/config.yaml b/tests/end-to-end/config.yaml index 323cb0f..ce2d5f1 100644 --- a/tests/end-to-end/config.yaml +++ b/tests/end-to-end/config.yaml @@ -131,6 +131,31 @@ suites: expected: version: 4.1.2 tag: v4.1.2 + - name: target-branch + yaml: + name: Bump Version (Target Branch) + on: + push: + branches: + - main + jobs: + bump-version: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: git branch other-branch + - id: version-bump + uses: ./action + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + target-branch: other-branch + tests: + - message: no keywords + expected: + version: 4.1.3 + branch: other-branch + actionFiles: - index.js - Dockerfile diff --git a/tests/end-to-end/git.js b/tests/end-to-end/git.js index e1480e7..5388454 100644 --- a/tests/end-to-end/git.js +++ b/tests/end-to-end/git.js @@ -4,17 +4,4 @@ function git(options, ...params) { return exec('git', options, ...params); } -let firstCommit = true; -function push() { - if (firstCommit) { - firstCommit = false; - return git('push', '--force', '--set-upstream', 'origin', 'main'); - } else { - return git('push'); - } -} - -module.exports = { - push, - default: git, -}; +module.exports = git; diff --git a/tests/end-to-end/index.test.js b/tests/end-to-end/index.test.js index 3253d2d..d03df21 100644 --- a/tests/end-to-end/index.test.js +++ b/tests/end-to-end/index.test.js @@ -5,7 +5,7 @@ const { readFileSync } = require('fs'); const { writeFile, readFile, mkdir } = require('fs/promises'); const { resolve, join } = require('path'); const { cwd } = require('process'); -const { default: git, push: gitPush } = require('./git'); +const git = require('./git'); const { getMostRecentWorkflowRun, getWorkflowRun } = require('./actionsApi'); dotenv.config(); @@ -29,12 +29,11 @@ config.suites.forEach((suite) => { await git('commit', '--message', commit.message); const mostRecentDate = await getMostRecentWorkflowRunDate(); - await gitPush(); + await git('push'); const completedRun = await getCompletedRunAfter(mostRecentDate); expect(completedRun.conclusion).toBe('success'); - await git('pull'); await assertExpectation(commit.expected); }); }); @@ -60,7 +59,7 @@ async function generateReadMe(commit, suiteYaml) { '## Message', commit.message, '## Expectation', - `**Version:** ${commit.expected.version}`, + generateExpectationText(commit.expected), ].join('\n'); await writeFile(join(cwd(), readmePath), readMeContents); await git('add', readmePath); @@ -100,13 +99,32 @@ async function getMostRecentWorkflowRunDate() { return date; } -async function assertExpectation({ version: expectedVersion, tag: expectedTag }) { +function generateExpectationText({ version: expectedVersion, tag: expectedTag, branch: expectedBranch }) { + const results = [`- **Version:** ${expectedVersion}`]; + if (expectedTag) { + results.push(`- **Tag:** ${expectedTag}`); + } + if (expectedBranch) { + results.push(`- **Branch:** ${expectedBranch}`); + } + return results.join('\n'); +} + +async function assertExpectation({ version: expectedVersion, tag: expectedTag, branch: expectedBranch }) { if (expectedTag === undefined) { expectedTag = expectedVersion; } + if (expectedBranch) { + await git('fetch', 'origin', expectedBranch); + await git('checkout', expectedBranch); + } + await git('pull'); const [packageVersion, latestTag] = await Promise.all([getPackageJsonVersion(), getLatestTag()]); expect(packageVersion).toBe(expectedVersion); expect(latestTag).toBe(expectedTag); + if (expectedBranch) { + await git('checkout', 'main'); + } } async function getPackageJsonVersion() { diff --git a/tests/end-to-end/setupTestRepo.js b/tests/end-to-end/setupTestRepo.js index b4e949b..bf2fff6 100644 --- a/tests/end-to-end/setupTestRepo.js +++ b/tests/end-to-end/setupTestRepo.js @@ -3,7 +3,7 @@ const { rm, mkdir, copyFile, stat } = require('fs/promises'); const { chdir, cwd } = require('process'); const { resolve, join, dirname } = require('path'); const exec = require('./exec'); -const { default: git } = require('./git'); +const git = require('./git'); const glob = require('tiny-glob'); const { clearWorkflowRuns } = require('./actionsApi'); @@ -21,7 +21,8 @@ module.exports = async function setupTestRepo(actionFileGlobPaths) { await git('config', 'user.email', 'gh-action-bump-version-test@users.noreply.github.com'); await git('add', '.'); await git('commit', '--message', 'initial commit (version 1.0.0)'); - await deleteTags(); + await git('push', '--force', '--set-upstream', 'origin', 'main'); + await deleteTagsAndBranches(); }; function createNpmPackage() { @@ -57,13 +58,13 @@ async function copyActionFiles(globPaths) { ); } -async function deleteTags() { - const listTagsResult = await git({ suppressOutput: true }, 'ls-remote', '--tags', 'origin'); - if (listTagsResult.stdout) { - const lines = listTagsResult.stdout.split('\n'); - const tags = lines.map((line) => line.split('\t')[1]); - for (const tag of tags) { - await git('push', 'origin', '--delete', tag); +async function deleteTagsAndBranches() { + const listResult = await git({ suppressOutput: true }, 'ls-remote', '--tags', '--heads', 'origin'); + if (listResult.stdout) { + const lines = listResult.stdout.split('\n'); + const refs = lines.map((line) => line.split('\t')[1]).filter((ref) => ref !== 'refs/heads/main'); + if (refs.length > 0) { + await git('push', 'origin', '--delete', ...refs); } } }