Skip to content

Commit

Permalink
simplify code loading test now that TOML files are parsed with a real…
Browse files Browse the repository at this point in the history
… TOML parser (JuliaLang#42328)
  • Loading branch information
KristofferC authored and LilithHafner committed Feb 22, 2022
1 parent 9bbac5a commit 47121ea
Showing 1 changed file with 34 additions and 45 deletions.
79 changes: 34 additions & 45 deletions test/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,6 @@ end
@test_throws ArgumentError parse(UUID, "not a UUID")
@test tryparse(UUID, "either is this") === nothing

function subset(v::Vector{T}, m::Int) where T
T[v[j] for j = 1:length(v) if ((m >>> (j - 1)) & 1) == 1]
end

function perm(p::Vector, i::Int)
for j = length(p):-1:1
i, k = divrem(i, j)
p[j], p[k+1] = p[k+1], p[j]
end
return p
end

@testset "explicit_project_deps_get" begin
mktempdir() do dir
project_file = joinpath(dir, "Project.toml")
Expand All @@ -152,39 +140,40 @@ end
proj_uuid = dummy_uuid(project_file)
root_uuid = uuid4()
this_uuid = uuid4()
# project file to subset/permute
lines = split("""
name = "Root"
uuid = "$root_uuid"
[deps]
This = "$this_uuid"
""", '\n')
N = length(lines)
# test every permutation of every subset of lines
for m = 0:2^N-1
s = subset(lines, m) # each subset of lines
for i = 1:factorial(count_ones(m))
p = perm(s, i) # each permutation of the subset
open(project_file, write=true) do io
for line in p
println(io, line)
end
end
# look at lines and their order
n = findfirst(line -> startswith(line, "name"), p)
u = findfirst(line -> startswith(line, "uuid"), p)
d = findfirst(line -> line == "[deps]", p)
t = findfirst(line -> startswith(line, "This"), p)
# look up various packages by name
root = Base.explicit_project_deps_get(project_file, "Root")
this = Base.explicit_project_deps_get(project_file, "This")
that = Base.explicit_project_deps_get(project_file, "That")
# test that the correct answers are given
@test root == (something(n, N+1) something(d, N+1) ? nothing :
something(u, N+1) < something(d, N+1) ? root_uuid : proj_uuid)
@test this == (something(d, N+1) < something(t, N+1) N ? this_uuid : nothing)
@test that == nothing
end

old_load_path = copy(LOAD_PATH)
try
copy!(LOAD_PATH, [project_file])
write(project_file, """
name = "Root"
uuid = "$root_uuid"
[deps]
This = "$this_uuid"
""")
# look up various packages by name
root = Base.identify_package("Root")
this = Base.identify_package("This")
that = Base.identify_package("That")

@test root.uuid == root_uuid
@test this.uuid == this_uuid
@test that == nothing

write(project_file, """
name = "Root"
This = "$this_uuid"
[deps]
""")
# look up various packages by name
root = Base.identify_package("Root")
this = Base.identify_package("This")
that = Base.identify_package("That")

@test root.uuid == proj_uuid
@test this == nothing
@test that == nothing
finally
copy!(LOAD_PATH, old_load_path)
end
end
end
Expand Down

0 comments on commit 47121ea

Please sign in to comment.