generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial import of npm-package-versioner code
- Loading branch information
1 parent
76af5c0
commit f6250f3
Showing
13 changed files
with
321 additions
and
291 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
/* eslint-disable @typescript-eslint/no-floating-promises */ | ||
import assert from 'node:assert' | ||
import { contextFromEnvironment, generateVersion } from '../src/version' | ||
|
||
describe(`generateVersion`, () => { | ||
const defaultContext = { | ||
ci: true, | ||
buildNumber: 34, | ||
tag: undefined, | ||
branch: undefined | ||
} | ||
|
||
it(`should use local suffix when not in CI`, () => { | ||
const context = { | ||
...defaultContext, | ||
ci: false | ||
} | ||
assert.deepEqual(generateVersion('1.2.3-foo', context), { | ||
version: '1.2.3-local' | ||
}) | ||
}) | ||
|
||
it(`should use tag when tag specified`, () => { | ||
const context = { | ||
...defaultContext, | ||
tag: '3.2.1' | ||
} | ||
assert.deepEqual(generateVersion('1.0.0-local', context), { | ||
version: '3.2.1' | ||
}) | ||
}) | ||
|
||
it(`should strip the v prefix from tags`, () => { | ||
const context = { | ||
...defaultContext, | ||
tag: 'v3.2.1' | ||
} | ||
assert.deepEqual(generateVersion('1.0.0-local', context), { | ||
version: '3.2.1' | ||
}) | ||
}) | ||
|
||
it(`work for v1.1.1`, () => { | ||
const context = { | ||
...defaultContext, | ||
tag: 'v1.1.1' | ||
} | ||
assert.deepEqual(generateVersion('1.0.0-local', context), { | ||
version: '1.1.1' | ||
}) | ||
}) | ||
|
||
it(`should error for non-semver tag`, () => { | ||
const context = { | ||
...defaultContext, | ||
tag: 'wibble' | ||
} | ||
assert.deepEqual(generateVersion('1.0.0-local', context), { | ||
error: 'Invalid semver tag: wibble' | ||
}) | ||
}) | ||
|
||
it(`should use the build number for branches`, () => { | ||
const context = { | ||
...defaultContext, | ||
branch: 'wobble' | ||
} | ||
assert.deepEqual(generateVersion('1.0.0-local', context), { | ||
version: '1.0.0-wobble.34' | ||
}) | ||
}) | ||
|
||
// Assumption is that you only use one of these for versioned builds. | ||
for (const usesDev of ['master', 'main', 'develop']) { | ||
it(`should use dev for ${usesDev}`, () => { | ||
const context = { | ||
...defaultContext, | ||
branch: usesDev | ||
} | ||
assert.deepEqual(generateVersion('1.0.0-local', context), { | ||
version: '1.0.0-dev.34' | ||
}) | ||
}) | ||
} | ||
|
||
it(`should error if no branch or tag`, () => { | ||
assert.deepEqual(generateVersion('1.0.0-local', defaultContext), { | ||
error: 'Could not determine a version. CI environment invalid?' | ||
}) | ||
}) | ||
|
||
it(`should sanitise branch names`, () => { | ||
const context = { | ||
...defaultContext, | ||
branch: 'feature/£-foo-bar\\blort-1234.99' | ||
} | ||
assert.deepEqual(generateVersion('1.0.0-local', context), { | ||
version: '1.0.0-feature.foo.bar.blort.1234.99.34' | ||
}) | ||
}) | ||
|
||
it(`should use dot separated components as per semver`, () => { | ||
const context = { | ||
...defaultContext, | ||
branch: 'feature/my-fave-feature' | ||
} | ||
assert.deepEqual(generateVersion('1.0.0-local', context), { | ||
version: '1.0.0-feature.my.fave.feature.34' | ||
}) | ||
}) | ||
|
||
it(`treat underscores as separators`, () => { | ||
const context = { | ||
...defaultContext, | ||
branch: 'foo_bar' | ||
} | ||
assert.deepEqual(generateVersion('1.0.0-local', context), { | ||
version: '1.0.0-foo.bar.34' | ||
}) | ||
}) | ||
|
||
it(`should use a placeholder if the branch is sanitized away`, () => { | ||
const context = { | ||
...defaultContext, | ||
branch: '---' | ||
} | ||
assert.deepEqual(generateVersion('1.0.0-local', context), { | ||
version: '1.0.0-branch.34' | ||
}) | ||
}) | ||
}) | ||
|
||
describe('contextFromEnvironment', () => { | ||
it('works for GitHub actions branch case', () => { | ||
assert.deepEqual( | ||
contextFromEnvironment({ | ||
GITHUB_ACTION: 'lala', | ||
CI: 'true', | ||
GITHUB_REF: 'refs/heads/asdf', | ||
GITHUB_RUN_NUMBER: '12' | ||
}), | ||
{ | ||
branch: 'asdf', | ||
buildNumber: 12, | ||
ci: true, | ||
tag: undefined | ||
} | ||
) | ||
}) | ||
it('works for GitHub actions tag case', () => { | ||
assert.deepEqual( | ||
contextFromEnvironment({ | ||
GITHUB_ACTION: 'lala', | ||
CI: 'true', | ||
GITHUB_REF: 'refs/tags/asdf', | ||
GITHUB_RUN_NUMBER: '12' | ||
}), | ||
{ | ||
branch: undefined, | ||
buildNumber: 12, | ||
ci: true, | ||
tag: 'asdf' | ||
} | ||
) | ||
}) | ||
it('works for GitHub actions tag case numeric', () => { | ||
assert.deepEqual( | ||
contextFromEnvironment({ | ||
GITHUB_ACTION: 'lala', | ||
CI: 'true', | ||
GITHUB_REF: 'refs/tags/v1.1.1', | ||
GITHUB_RUN_NUMBER: '12' | ||
}), | ||
{ | ||
branch: undefined, | ||
buildNumber: 12, | ||
ci: true, | ||
tag: 'v1.1.1' | ||
} | ||
) | ||
}) | ||
}) |
Oops, something went wrong.