Skip to content

Commit

Permalink
fix(check-for-upgrades): fix version range matching for 0.x version r…
Browse files Browse the repository at this point in the history
…anges with the ~> operator (#390)

based on hashicorp/terraform-cdk#3403

---------

Signed-off-by: team-tf-cdk <github-team-tf-cdk@hashicorp.com>
Co-authored-by: team-tf-cdk <github-team-tf-cdk@hashicorp.com>
  • Loading branch information
ansgarm and team-tf-cdk authored Jan 10, 2024
1 parent 544961e commit bf89515
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 deletions.
16 changes: 13 additions & 3 deletions src/scripts/check-for-upgrades.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ async function getCurrentProviderVersion() {
}
// SEE NOTICE AT THE TOP WHY THIS IS INLINED CURRENTLY
// copied from https://github.com/hashicorp/terraform-cdk/blob/b23fc173715e90c0a5b8c8633d9ec7f71edf9ed4/packages/cdktf-cli/lib/dependencies/version-constraints.ts
// copied from https://github.com/hashicorp/terraform-cdk/blob/df858ccf4ac71a168e3636f053c6743324c98332/packages/%40cdktf/cli-core/src/lib/dependencies/version-constraints.ts
// and converted to JavaScript
// constraints can be prefixed with "~>", ">", "<", "=", ">=", "<=" or "!="
Expand Down Expand Up @@ -158,16 +158,26 @@ function versionMatchesConstraint(version, constraint) {
case "~>": {
// allows rightmost version component to increment
const parts = parsed.version.split(".");
const minorSpecified = parts.length === 2;
const majorIsZero = parts[0] === "0";
// ~>2.0 which allows 2.1 and 2.1.1 needs special handling as
// npm semver handles "~" differently for ~2.0 than for ~2 or ~2.1.0
// So we need to use "^" (e.g. ^2.0) for this case
// see: https://github.com/npm/node-semver/issues/11
const allowMinorAndPatchOnly = parsed.version.split(".").length === 2;
const allowMinorAndPatchOnly = minorSpecified;
const range = allowMinorAndPatchOnly
let range = allowMinorAndPatchOnly
? \`^\${parsed.version}\`
: \`~\${parsed.version}\`;
// versions below 1.0 are treated a bit differently in NPM than in Terraform
// meaning that NPMs ^0.4 doesn't allow 0.55 while TFs ~>0.4 allows 0.55
if (majorIsZero && minorSpecified) {
range = \`>=\${parsed.version} <1.0.0\`;
}
return semver.satisfies(version, range);
}
case ">=":
Expand Down
48 changes: 39 additions & 9 deletions test/__snapshots__/index.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bf89515

Please sign in to comment.