Skip to content

Commit

Permalink
Added tests for load_overrides() in Artifacts.jl (JuliaLang#51833)
Browse files Browse the repository at this point in the history
Fixes JuliaLang#48551
Continued from JuliaLang#51815
The `load_overrides()` method in `Artifacts.jl` has a large untested
branch. This PR increases the test coverage for this method.


![image](https://github.com/JuliaLang/julia/assets/141328479/a9744e25-e79f-4664-8522-320dd29c4c50)

https://app.codecov.io/gh/JuliaLang/julia/blob/master/stdlib%2FArtifacts%2Fsrc%2FArtifacts.jl#L121
  • Loading branch information
u7581147 committed Oct 26, 2023
1 parent 342d05d commit 837522a
Showing 1 changed file with 80 additions and 1 deletion.
81 changes: 80 additions & 1 deletion stdlib/Artifacts/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,91 @@
import Base: SHA1

using Artifacts, Test, Base.BinaryPlatforms
using Artifacts: with_artifacts_directory, pack_platform!, unpack_platform
using Artifacts: with_artifacts_directory, pack_platform!, unpack_platform, load_overrides
using TOML

# prepare for the package tests by ensuring the required artifacts are downloaded now
artifacts_dir = mktempdir()
run(addenv(`$(Base.julia_cmd()) --color=no $(joinpath(@__DIR__, "refresh_artifacts.jl")) $(artifacts_dir)`, "TERM"=>"dumb"))

@testset "Load Overrides" begin
"""
create_test_overrides_toml(temp_dir::String)
Create "Overrides.toml" in the given `temp_dir`.
"""
function create_test_overrides_toml(temp_dir::String)
# Define the overrides
overrides = Dict(
"78f35e74ff113f02274ce60dab6e92b4546ef806" => "/path/to/replacement",
"c76f8cda85f83a06d17de6c57aabf9e294eb2537" => "fb886e813a4aed4147d5979fcdf27457d20aa35d",
"d57dbccd-ca19-4d82-b9b8-9d660942965b" => Dict(
"c_simple" => "/path/to/c_simple_dir",
"libfoo" => "fb886e813a4aed4147d5979fcdf27457d20aa35d"
)
)

# Get the artifacts directory
artifacts_dir = joinpath(temp_dir, "artifacts")

# Ensure the artifacts directory exists
isdir(artifacts_dir) || mkdir(artifacts_dir)

# Get the path to the Overrides.toml file
overrides_path = joinpath(artifacts_dir, "Overrides.toml")

# Create the Overrides.toml file
open(overrides_path, "w") do io
TOML.print(io, overrides)
end
end

# Specify the expected test result when depot path does not exist or no overriding happened
empty_output = Dict{Symbol, Any}(
:UUID => Dict{Base.UUID, Dict{String, Union{SHA1, String}}}(),
:hash => Dict{SHA1, Union{SHA1, String}}()
)

# Specify the expected test result when overriding happened
expected_output = Dict{Symbol, Any}(
:UUID => Dict{Base.UUID, Dict{String, Union{SHA1, String}}}(Base.UUID("d57dbccd-ca19-4d82-b9b8-9d660942965b") => Dict("c_simple" => "/path/to/c_simple_dir", "libfoo" => SHA1("fb886e813a4aed4147d5979fcdf27457d20aa35d"))),
:hash => Dict{SHA1, Union{SHA1, String}}(SHA1("78f35e74ff113f02274ce60dab6e92b4546ef806") => "/path/to/replacement", SHA1("c76f8cda85f83a06d17de6c57aabf9e294eb2537") => SHA1("fb886e813a4aed4147d5979fcdf27457d20aa35d"))
)

# Test `load_overrides()` works with *no* "Overrides.toml" file
@test load_overrides() == empty_output

# Create a temporary directory
mktempdir() do temp_dir
# Back up the old `DEPOT_PATH``
old_depot_path = copy(Base.DEPOT_PATH)

# Set `DEPOT_PATH` to that directory
empty!(Base.DEPOT_PATH)
push!(Base.DEPOT_PATH, temp_dir)

try
# Create "Overrides.toml" for the test
create_test_overrides_toml(temp_dir)

# Test `load_overrides()` works *with* "Overrides.toml" file but non-nothing ARTIFACT_OVERRIDES[]
@test load_overrides() == empty_output

# Test `load_overrides()` works *with* "Overrides.toml" file with force parameter, which overrides even when `ARTIFACT_OVERRIDES[] !== nothing``
@test load_overrides(force=true) == expected_output
finally # Make sure `DEPOT_PATH` will be restored to the status quo in the event of a bug
# Restore the old `DEPOT_PATH` to avoid messing with any other code
empty!(Base.DEPOT_PATH)
append!(Base.DEPOT_PATH, old_depot_path)
end
end
# Temporary directory and test "Overrides.toml" file will be automatically deleted when out of scope
# This means after this block, the system *should* behave like this test never happened.

# Test the "Overrides.toml" file is cleared back to the status quo
@test load_overrides(force=true) == empty_output
end

@testset "Artifact Paths" begin
mktempdir() do tempdir
with_artifacts_directory(tempdir) do
Expand Down

0 comments on commit 837522a

Please sign in to comment.