Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tests for load_overrides() in Artifacts.jl #51833

Merged
merged 5 commits into from
Oct 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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