Skip to content

Commit

Permalink
Double-check the result before returning it (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
DilumAluthge authored May 8, 2021
1 parent 8079cb1 commit d48a196
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 70 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "CompatEntryUtilities"
uuid = "8be7a408-1a4a-465c-8be3-3898d13eb8a5"
authors = ["Dilum Aluthge", "contributors"]
version = "1.0.0"
version = "2.0.0-DEV"

[deps]
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Expand Down
27 changes: 19 additions & 8 deletions src/CompatEntryUtilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@ import Pkg
export semver_spec_string

@static if Base.VERSION >= v"1.7-"
const VersionRange = Pkg.Versions.VersionRange
const VersionSpec = Pkg.Versions.VersionSpec
const PKG_VERSIONS = Pkg.Versions
else
const VersionRange = Pkg.Types.VersionRange
const VersionSpec = Pkg.Types.VersionSpec
const PKG_VERSIONS = Pkg.Types
end

function semver_spec_string(r::VersionRange)
function _check_result(spec::PKG_VERSIONS.VersionSpec, str::String)
spec_from_str = PKG_VERSIONS.semver_spec(str)
result_is_correct = spec == spec_from_str
if !result_is_correct
msg = "`Pkg.Versions.semver_spec(str)` is not equal to the original `spec`"
@error(msg, spec, str, spec_from_str)
throw(ErrorException(msg))
end
return nothing
end

function _semver_spec_string(r::PKG_VERSIONS.VersionRange)
m, n = r.lower.n, r.upper.n
if (m, n) == (0, 0)
return "≥0"
Expand All @@ -30,11 +39,13 @@ end
Returns `str::AbstractString` such that `Pkg.Versions.semver_spec(str) == spec`.
"""
function semver_spec_string(spec::VersionSpec)
function semver_spec_string(spec::PKG_VERSIONS.VersionSpec)
ranges = spec.ranges
isempty(ranges) && return "1 - 0"
specs = semver_spec_string.(ranges)
return join(specs, ", ")
specs = _semver_spec_string.(ranges)
result_string = join(specs, ", ")
_check_result(spec, result_string)
return result_string
end

end # module
127 changes: 66 additions & 61 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,88 +9,93 @@ else
const PKG_VERSIONS = Pkg.Types
end

const expected_exception_1 = ErrorException("`Pkg.Versions.semver_spec(str)` is not equal to the original `spec`")
const expected_exception_2 = ArgumentError("This version range cannot be represented using SemVer notation")

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
roundtrip_succeeded = (spec_1 == spec_2) && (spec_1 == spec_3) && (spec_2 == spec_3)
if !roundtrip_succeeded
@error("Roundtrip failed", str_1, str_2, str_3, spec_1, spec_2, spec_3)
end
return result
return roundtrip_succeeded
end

@testset "CompatEntryUtilities.jl" begin
@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
@testset "_check_result" begin
@test CompatEntryUtilities._check_result(PKG_VERSIONS.semver_spec("1"), "1") isa Nothing
@test_throws expected_exception_1 CompatEntryUtilities._check_result(PKG_VERSIONS.semver_spec("2"), "1") isa Nothing
end

@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])
@test_throws expected_exception_2 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), ", "))
@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), ", "))
@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",
@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",
# multiple ranges
"1 - 2.3, 4.5.6 - 7.8.9",

# other stuff
"1 - 0",
"2 - 1",
">= 0",
]
# other stuff
"1 - 0",
"2 - 1",
">= 0",
]

for str in strs
@test roundtrip_semver_spec_string(str)
end
for str in strs
@test roundtrip_semver_spec_string(str)
end
end
end

0 comments on commit d48a196

Please sign in to comment.