Skip to content

Commit

Permalink
Add the semver_spec_string function
Browse files Browse the repository at this point in the history
  • Loading branch information
DilumAluthge committed Apr 3, 2021
1 parent 9f43976 commit 7adcd3e
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 5 deletions.
10 changes: 7 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ uuid = "8be7a408-1a4a-465c-8be3-3898d13eb8a5"
authors = ["Dilum Aluthge", "contributors"]
version = "1.0.0-DEV"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
[deps]
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"

[compat]
julia = "1.6"

[extras]
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
test = ["Pkg", "Test"]
24 changes: 23 additions & 1 deletion src/CompatEntryUtilities.jl
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
86 changes: 85 additions & 1 deletion test/runtests.jl
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

0 comments on commit 7adcd3e

Please sign in to comment.