-
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.
- Loading branch information
1 parent
9f43976
commit 7adcd3e
Showing
3 changed files
with
115 additions
and
5 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 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 |
---|---|---|
@@ -1,5 +1,27 @@ | ||
module CompatEntryUtilities | ||
|
||
# Write your package code here. | ||
import Pkg | ||
|
||
export semver_spec_string | ||
|
||
function semver_spec_string(spec::Pkg.Versions.VersionSpec) | ||
ranges = spec.ranges | ||
isempty(ranges) && return "1 - 0" | ||
specs = semver_spec_string.(ranges) | ||
return join(specs, ", ") | ||
end | ||
|
||
function semver_spec_string(r::Pkg.Versions.VersionRange) | ||
m, n = r.lower.n, r.upper.n | ||
if (m, n) == (0, 0) | ||
return "≥0" | ||
elseif m == 0 | ||
throw(ArgumentError("This version range cannot be represented using SemVer notation")) | ||
elseif n == 0 | ||
return string("≥", join(r.lower.t, "."),) | ||
else | ||
return string(join(r.lower.t[1:m], "."), " - ", join(r.upper.t[1:n], ".")) | ||
end | ||
end | ||
|
||
end # module |
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 |
---|---|---|
@@ -1,6 +1,90 @@ | ||
using CompatEntryUtilities | ||
using Test | ||
|
||
import Pkg | ||
|
||
function roundtrip_semver_spec_string(str_1::AbstractString) | ||
spec_1 = Pkg.Versions.semver_spec(str_1) | ||
str_2 = semver_spec_string(spec_1) | ||
spec_2 = Pkg.Versions.semver_spec(str_2) | ||
str_3 = semver_spec_string(spec_2) | ||
spec_3 = Pkg.Versions.semver_spec(str_3) | ||
result = (spec_1 == spec_2) && (spec_1 == spec_3) && (spec_2 == spec_3) | ||
if !result | ||
@error("Roundtrip failed", str_1, str_2, str_3, spec_1, spec_2, spec_3) | ||
end | ||
return result | ||
end | ||
|
||
@testset "CompatEntryUtilities.jl" begin | ||
# Write your tests here. | ||
@testset "semver_spec_string" begin | ||
@testset begin | ||
let | ||
lower = Pkg.Versions.VersionBound() | ||
upper = Pkg.Versions.VersionBound(1) | ||
r = Pkg.Versions.VersionRange(lower, upper) | ||
spec = Pkg.Versions.VersionSpec([r]) | ||
msg = "This version range cannot be represented using SemVer notation" | ||
@test_throws ArgumentError(msg) semver_spec_string(spec) | ||
end | ||
end | ||
|
||
@testset begin | ||
bases = ["0.0.3", "0.2.3", "1.2.3", "0.0", "0.2", "1.2", "0", "1"] | ||
specifiers = ["", "^", "~", "= ", ">= ", "≥ "] | ||
for specifier in specifiers | ||
for base in bases | ||
@test roundtrip_semver_spec_string("$(specifier)$(base)") | ||
end | ||
@test roundtrip_semver_spec_string(join(string.(Ref(specifier), bases), ", ")) | ||
end | ||
end | ||
|
||
@testset begin | ||
bases = ["0.0.3", "0.2.3", "1.2.3", "0.2", "1.2", "1"] | ||
specifiers = ["< "] | ||
for specifier in specifiers | ||
for base in bases | ||
@test roundtrip_semver_spec_string("$(specifier)$(base)") | ||
end | ||
@test roundtrip_semver_spec_string(join(string.(Ref(specifier), bases), ", ")) | ||
end | ||
end | ||
|
||
@testset begin | ||
strs = [ | ||
# ranges | ||
"1.2.3 - 4.5.6", | ||
"0.2.3 - 4.5.6", | ||
"1.2 - 4.5.6", | ||
"1 - 4.5.6", | ||
"0.2 - 4.5.6", | ||
"0.2 - 0.5.6", | ||
"1.2.3 - 4.5", | ||
"1.2.3 - 4", | ||
"1.2 - 4.5", | ||
"1.2 - 4", | ||
"1 - 4.5", | ||
"1 - 4", | ||
"0.2.3 - 4.5", | ||
"0.2.3 - 4", | ||
"0.2 - 4.5", | ||
"0.2 - 4", | ||
"0.2 - 0.5", | ||
"0.2 - 0", | ||
|
||
# multiple ranges | ||
"1 - 2.3, 4.5.6 - 7.8.9", | ||
|
||
# other stuff | ||
"1 - 0", | ||
"2 - 1", | ||
">= 0", | ||
] | ||
|
||
for str in strs | ||
@test roundtrip_semver_spec_string(str) | ||
end | ||
end | ||
end | ||
end |