Skip to content

Commit

Permalink
Merge branch 'main' into v1.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
kylorhall committed May 25, 2021
2 parents 5640f5f + 6489a09 commit ccfa8f0
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 16 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,26 @@ jobs:
echo resolved_version: ${{ steps.enforce.outputs.resolved_version }}
```
# Valid Inputs
# Inputs
- **`package`** _[required]_
- The name of the dependency to check.
- **`directory`**
- Directory where your `package.json` can be found.
- Defaults to to `env.GITHUB_WORKSPACE`.
- `default=env.GITHUB_WORKSPACE`
- **`range`** _[required]_
- A semver range, eg. '^1.0.0', '1.0.0', '>=1.x', etc..
- **`version_prerelease`**
- An optional prerelease target.
- `default=false`
- `false` requires there is no prerelease.
- `true` allows a prerelease—but it is not required! `1.2.3-prerelease` and `1.2.3` are both valid
- `'prerelease'` means it must match that prerelease.
- `'prerelease.#'` means it must match that prerelease and identifier.
- **`allow_multiple_versions`**
- Whether or not we allow multiple versions to be resolved, eg. you may have Typescript at `^4.2.0` in your codebase, but another package points to `3.x`.
- `false` means this should only ever resolve to a single version (and that should match our range)
- `true` allows all versions… NOTE: we only look at the first resolved version, assuming this is your version.

---

Expand Down
15 changes: 9 additions & 6 deletions __tests__/enforce.jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,17 @@ describe("enforce", () => {
}
);

test("failing: when prerelease=true (not a prerelease)", () => {
expect(() =>
enforceVersion({ ...config, version_prerelease: true })
).toThrowError("⚠️ Expected a prerelease on '7.3.5', got none.");
});
test.each([true, false])(
"passing: when version_prerelease=%p (not a prerelease)",
(prerelease) => {
expect(() =>
enforceVersion({ ...config, version_prerelease: prerelease })
).not.toThrow();
}
);

test.each(["one", "two"])(
"failing: when prerelease=%p (not a prerelease)",
"failing: when version_prerelease=%p (not a prerelease)",
(prerelease) => {
expect(() =>
enforceVersion({ ...config, version_prerelease: prerelease })
Expand Down
23 changes: 23 additions & 0 deletions __tests__/run.jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,29 @@ describe("run", () => {
expect(setOutputSpy).toHaveBeenCalledWith("resolved_version", "4.2.4"); // what is in `yarn.lock`
});

test.each([true, false])(
"passing scenario: a non-prerelease with version_prerelease=%p",
(prerelease) => {
overrideInputs({
package: "typescript",
range: "^4.2.0", // the value we're testing for…may not 100% match `package.json` or `yarn.lock`
version_prerelease: prerelease,
});

// NOTE: This runs on load, this is how you do that…
// jest.resetModules();
jest.isolateModules(() => {
require("../src/run");
});

expect(setFailedSpy).not.toHaveBeenCalled();

expect(setOutputSpy).toHaveBeenCalledTimes(2);
expect(setOutputSpy).toHaveBeenCalledWith("target_version", "^4.0.0"); // what is in `package.json`
expect(setOutputSpy).toHaveBeenCalledWith("resolved_version", "4.2.4"); // what is in `yarn.lock`
}
);

test("passing scenario: yallist (in dependencies) with allow_multiple_versions=true", () => {
overrideInputs({
package: "yallist",
Expand Down
8 changes: 5 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ inputs:
description: >-
An optional prerelease target. Defaults to false.
- `false` requires there is no prerelease.
- `true` includes prerelease in the range.
- `true` allows a prerelease—but it is not required! `1.2.3-prerelease` and `1.2.3` are both valid
- `'prerelease'` means it must match that prerelease.
- `'prerelease.#'` means it must match that prerelease and identifier.
default: false
required: false
allow_multiple_versions:
description: >-
Whether or not we allow multiple versions to be resolved. Defaults to false.
Whether or not we allow multiple versions to be resolved, eg. you may have Typescript at `^4.2.0` in your codebase, but another package points to `3.x`.
- `false` means this should only ever resolve to a single version (and that should match our range)
- `true` allows all versions…we only look at the first resolved version
- `true` allows all versions… NOTE: we only look at the first resolved version, assuming this is your version.
default: false
required: false

outputs:
Expand Down
6 changes: 1 addition & 5 deletions src/enforce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ export function enforceVersion(config: Config = getConfig()): void {
}

if (config.version_prerelease === true) {
if (parsedVersion.prerelease && !parsedVersion.prerelease.length) {
throw new Error(
`⚠️ Expected a prerelease on '${parsedVersion.version}', got none.`
);
}
// NOTE: In this case, we do not care!
} else if (config.version_prerelease === false) {
if (parsedVersion.prerelease && parsedVersion.prerelease.length) {
throw new Error(
Expand Down

0 comments on commit ccfa8f0

Please sign in to comment.