Skip to content

Commit

Permalink
test: add tests to index.js (#5)
Browse files Browse the repository at this point in the history
* test: add tests to index.js

* build dist file
  • Loading branch information
jveldboom authored Feb 11, 2023
1 parent 4f6f1e6 commit 9dd8821
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 53 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,12 @@ jobs:
```

## TODO
- [ ] Compete unit tests and strive for 100% test coverage
- [x] GitHub workflow to run unit tests
- [x] Workflow to check dist is built
- [ ] Release v1 of action
- [ ] Workflow to run regresssion tests with compiled action
- [ ] list action in marketplace
- [ ] Improve index.js file
- Should it be simplified and wrapped in a try/catch?
- How can we get 100% test coverage on it?

## Notes
- Commit Analyzer https://github.com/semantic-release/commit-analyzer#releaserules
Expand All @@ -94,4 +95,4 @@ jobs:
- https://github.com/absolute-version/commit-and-tag-version/issues

## License
This action is licensed under the MIT License. See the [LICENSE](./LICENSE) file for more information.
This action is licensed under the MIT License. See the [LICENSE](./LICENSE) file for more information.
99 changes: 54 additions & 45 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44101,7 +44101,7 @@ const getLatestTag = async (octokit, owner, repo) => {
repo
})

if (res.data.length >= 1) return res.data[0]
if (res?.data?.length >= 1) return res.data[0]
}

const compareCommits = async (octokit, owner, repo, base, head) => {
Expand Down Expand Up @@ -44136,6 +44136,52 @@ module.exports = {
}


/***/ }),

/***/ 4351:
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {

const semver = __nccwpck_require__(1383)
const core = __nccwpck_require__(2186)
const github = __nccwpck_require__(8396)
const utils = __nccwpck_require__(1608)

const run = async () => {
const octokit = github.getOctokit(core.getInput('github-token'))

const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/')
const sha = process.env.GITHUB_SHA

let latestTag
try {
latestTag = await github.getLatestTag(octokit, owner, repo)
} catch (err) {
return core.setFailed(`unable to get latest tag - error: ${err.message} ${err?.response?.status}`)
}

// return a default version if no previous github tags
if (!latestTag) {
const incrementedVersion = semver.inc('0.0.0', core.getInput('default-bump'))
return utils.setVersionOutputs(incrementedVersion, core.getInput('prefix'))
}

if (!semver.valid(latestTag.name)) {
return core.setFailed(`latest tag name is not valid semver: ${JSON.stringify(latestTag)}`)
}

// get commits from last tag and calculate version bump
const commits = await github.compareCommits(octokit, owner, repo, latestTag?.commit?.sha, sha)
const bump = await utils.getVersionBump(commits, core.getInput('default-bump'))

const incrementedVersion = semver.inc(latestTag.name, bump)
utils.setVersionOutputs(incrementedVersion, core.getInput('prefix'))
}

if (process.env.NODE_ENV !== 'test') run()

module.exports = { run }


/***/ }),

/***/ 1608:
Expand Down Expand Up @@ -44410,49 +44456,12 @@ module.exports = JSON.parse('[[[0,44],"disallowed_STD3_valid"],[[45,46],"valid"]
/******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/";
/******/
/************************************************************************/
var __webpack_exports__ = {};
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
(() => {
const semver = __nccwpck_require__(1383)
const core = __nccwpck_require__(2186)
const github = __nccwpck_require__(8396)
const utils = __nccwpck_require__(1608)

const run = async () => {
const octokit = github.getOctokit(core.getInput('github-token'))

const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/')
const sha = process.env.GITHUB_SHA

let latestTag = ''
try {
latestTag = await github.getLatestTag(octokit, owner, repo)
} catch (err) {
return core.setFailed(`unable to get latest tag - error: ${err.message} ${err?.response?.status}`)
}

// return a default version if no previous github tags
if (!latestTag) {
const incrementedVersion = semver.inc('0.0.0', core.getInput('default-bump'))
return utils.setVersionOutputs(incrementedVersion, core.getInput('prefix'))
}

if (!semver.valid(latestTag.name)) {
return core.setFailed(`latest tag name is not valid semver: ${JSON.stringify(latestTag)}`)
}

// get commits from last tag and calculate version bump
const commits = await github.compareCommits(octokit, owner, repo, latestTag.commit.sha, sha)
const bump = await utils.getVersionBump(commits, core.getInput('default-bump'))

const incrementedVersion = semver.inc(latestTag.name, bump)
utils.setVersionOutputs(incrementedVersion, core.getInput('prefix'))
}

run()

})();

module.exports = __webpack_exports__;
/******/
/******/ // startup
/******/ // Load entry module and return exports
/******/ // This entry module is referenced by other modules so it can't be inlined
/******/ var __webpack_exports__ = __nccwpck_require__(4351);
/******/ module.exports = __webpack_exports__;
/******/
/******/ })()
;
2 changes: 1 addition & 1 deletion src/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const getLatestTag = async (octokit, owner, repo) => {
repo
})

if (res.data.length >= 1) return res.data[0]
if (res?.data?.length >= 1) return res.data[0]
}

const compareCommits = async (octokit, owner, repo, base, head) => {
Expand Down
8 changes: 5 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const run = async () => {
const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/')
const sha = process.env.GITHUB_SHA

let latestTag = ''
let latestTag
try {
latestTag = await github.getLatestTag(octokit, owner, repo)
} catch (err) {
Expand All @@ -27,11 +27,13 @@ const run = async () => {
}

// get commits from last tag and calculate version bump
const commits = await github.compareCommits(octokit, owner, repo, latestTag.commit.sha, sha)
const commits = await github.compareCommits(octokit, owner, repo, latestTag?.commit?.sha, sha)
const bump = await utils.getVersionBump(commits, core.getInput('default-bump'))

const incrementedVersion = semver.inc(latestTag.name, bump)
utils.setVersionOutputs(incrementedVersion, core.getInput('prefix'))
}

run()
if (process.env.NODE_ENV !== 'test') run()

module.exports = { run }
71 changes: 71 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* eslint-env jest */
const core = require('@actions/core')
const github = require('./github')

jest.mock('./github')
jest.spyOn(core, 'getInput')
jest.spyOn(core, 'setFailed')
jest.spyOn(core, 'setOutput')

const index = require('./index')

describe('index', () => {
beforeEach(() => {
process.env.GITHUB_REPOSITORY = 'foo/bar'
process.env['INPUT_GITHUB-TOKEN'] = 'test-token'
process.env['INPUT_DEFAULT-BUMP'] = 'patch'
process.env.INPUT_PREFIX = 'v'
})

afterEach(() => {
jest.clearAllMocks()
})

it('should fail when unable to get latest tag', async () => {
github.getLatestTag.mockRejectedValueOnce(new Error('test error'))

await index.run()
expect(core.setFailed).toBeCalledTimes(1)
})

it('should output default version bump (0.0.1) if no previous tags', async () => {
github.getLatestTag.mockResolvedValueOnce()

await index.run()
expect(core.getInput).toHaveBeenNthCalledWith(2, 'default-bump')
expect(core.getInput).toHaveNthReturnedWith(2, 'patch')

expect(core.setOutput).toHaveBeenNthCalledWith(1, 'version', '0.0.1')
expect(core.setOutput).toHaveBeenNthCalledWith(2, 'version-with-prefix', 'v0.0.1')
expect(core.setOutput).toHaveBeenNthCalledWith(3, 'major', 0)
expect(core.setOutput).toHaveBeenNthCalledWith(4, 'major-with-prefix', 'v0')
expect(core.setOutput).toHaveBeenNthCalledWith(5, 'minor', 0)
expect(core.setOutput).toHaveBeenNthCalledWith(6, 'patch', 1)
})

it('should fail when latest tag is no valid semver', async () => {
github.getLatestTag.mockResolvedValueOnce('invalid-semver')

await index.run()
expect(core.setFailed).toBeCalledTimes(1)
expect(core.setFailed).toHaveBeenNthCalledWith(1, 'latest tag name is not valid semver: "invalid-semver"')
})

it('should output versions', async () => {
const latestTag = {
name: 'v1.2.3',
commit: { sha: '123456789' }
}
github.getLatestTag.mockResolvedValueOnce(latestTag)
github.compareCommits.mockResolvedValueOnce([])

await index.run()

expect(core.setOutput).toHaveBeenNthCalledWith(1, 'version', '1.2.4')
expect(core.setOutput).toHaveBeenNthCalledWith(2, 'version-with-prefix', 'v1.2.4')
expect(core.setOutput).toHaveBeenNthCalledWith(3, 'major', 1)
expect(core.setOutput).toHaveBeenNthCalledWith(4, 'major-with-prefix', 'v1')
expect(core.setOutput).toHaveBeenNthCalledWith(5, 'minor', 2)
expect(core.setOutput).toHaveBeenNthCalledWith(6, 'patch', 4)
})
})

0 comments on commit 9dd8821

Please sign in to comment.