diff options
author | Maksim Denisov <denisov.maksim@huawei.com> | 2024-09-12 14:28:13 +0200 |
---|---|---|
committer | Maksim Denisov <denisov.maksim@huawei.com> | 2024-09-13 14:41:00 +0200 |
commit | 5fb67de531fead44123ff30ee5cf37340fc63a0b (patch) | |
tree | 0e1c7c8810e8ee311467e5e07ebb8ff2902adf28 /test/buildtool/common | |
parent | dd15af0ba4b22348e38d535aadc9fa5e929f3824 (diff) | |
download | justbuild-5fb67de531fead44123ff30ee5cf37340fc63a0b.tar.gz |
Use TestHashType in tests
...instead of calling ProtocolTraits::IsCompatible
Diffstat (limited to 'test/buildtool/common')
-rw-r--r-- | test/buildtool/common/TARGETS | 4 | ||||
-rw-r--r-- | test/buildtool/common/action_description.test.cpp | 11 | ||||
-rw-r--r-- | test/buildtool/common/artifact_description.test.cpp | 62 |
3 files changed, 25 insertions, 52 deletions
diff --git a/test/buildtool/common/TARGETS b/test/buildtool/common/TARGETS index 1355af3f..020d0f28 100644 --- a/test/buildtool/common/TARGETS +++ b/test/buildtool/common/TARGETS @@ -9,9 +9,9 @@ , ["@", "src", "src/buildtool/common", "artifact_description"] , ["@", "src", "src/buildtool/common", "common"] , ["@", "src", "src/buildtool/common", "artifact_digest_factory"] - , ["@", "src", "src/buildtool/common", "protocol_traits"] , ["@", "src", "src/buildtool/crypto", "hash_function"] , ["@", "src", "src/buildtool/file_system", "object_type"] + , ["utils", "test_hash_function_type"] ] , "stage": ["test", "buildtool", "common"] } @@ -25,8 +25,8 @@ , ["@", "json", "", "json"] , ["@", "src", "src/buildtool/common", "action_description"] , ["@", "src", "src/buildtool/common", "common"] - , ["@", "src", "src/buildtool/common", "protocol_traits"] , ["@", "src", "src/buildtool/crypto", "hash_function"] + , ["utils", "test_hash_function_type"] ] , "stage": ["test", "buildtool", "common"] } diff --git a/test/buildtool/common/action_description.test.cpp b/test/buildtool/common/action_description.test.cpp index 504f6aa3..65f5d92e 100644 --- a/test/buildtool/common/action_description.test.cpp +++ b/test/buildtool/common/action_description.test.cpp @@ -20,14 +20,8 @@ #include "nlohmann/json.hpp" #include "src/buildtool/common/action.hpp" #include "src/buildtool/common/artifact_description.hpp" -#include "src/buildtool/common/protocol_traits.hpp" #include "src/buildtool/crypto/hash_function.hpp" - -[[nodiscard]] static inline auto GetHashType(bool compatible) noexcept - -> HashFunction::Type { - return compatible ? HashFunction::Type::PlainSHA256 - : HashFunction::Type::GitSHA1; -} +#include "test/utils/hermeticity/test_hash_function_type.hpp" TEST_CASE("From JSON", "[action_description]") { using path = std::filesystem::path; @@ -45,8 +39,7 @@ TEST_CASE("From JSON", "[action_description]") { desc.Inputs()} .ToJson(); - auto const hash_type = - GetHashType(ProtocolTraits::Instance().IsCompatible()); + auto const hash_type = TestHashType::ReadFromEnvironment(); SECTION("Parse full action") { auto description = ActionDescription::FromJson(hash_type, "id", json); REQUIRE(description); diff --git a/test/buildtool/common/artifact_description.test.cpp b/test/buildtool/common/artifact_description.test.cpp index 99a24964..44acaae6 100644 --- a/test/buildtool/common/artifact_description.test.cpp +++ b/test/buildtool/common/artifact_description.test.cpp @@ -22,14 +22,12 @@ #include "src/buildtool/common/artifact.hpp" #include "src/buildtool/common/artifact_digest.hpp" #include "src/buildtool/common/artifact_digest_factory.hpp" -#include "src/buildtool/common/protocol_traits.hpp" #include "src/buildtool/crypto/hash_function.hpp" #include "src/buildtool/file_system/object_type.hpp" +#include "test/utils/hermeticity/test_hash_function_type.hpp" static auto NamedDigest(std::string const& str) -> ArtifactDigest { - HashFunction const hash_function{ProtocolTraits::Instance().IsCompatible() - ? HashFunction::Type::PlainSHA256 - : HashFunction::Type::GitSHA1}; + HashFunction const hash_function{TestHashType::ReadFromEnvironment()}; return ArtifactDigestFactory::HashDataAs<ObjectType::File>(hash_function, str); } @@ -40,18 +38,11 @@ static auto NamedDigest(std::string const& str) -> ArtifactDigest { lhs.Info() == rhs.Info(); } -[[nodiscard]] static inline auto GetHashType(bool compatible) noexcept - -> HashFunction::Type { - return compatible ? HashFunction::Type::PlainSHA256 - : HashFunction::Type::GitSHA1; -} - TEST_CASE("Local artifact", "[artifact_description]") { auto local_desc = ArtifactDescription::CreateLocal( std::filesystem::path{"local_path"}, "repo"); auto from_json = ArtifactDescription::FromJson( - GetHashType(ProtocolTraits::Instance().IsCompatible()), - local_desc.ToJson()); + TestHashType::ReadFromEnvironment(), local_desc.ToJson()); CHECK(local_desc == *from_json); } @@ -60,24 +51,21 @@ TEST_CASE("Known artifact", "[artifact_description]") { auto known_desc = ArtifactDescription::CreateKnown( NamedDigest("f_fake_hash"), ObjectType::File); auto from_json = ArtifactDescription::FromJson( - GetHashType(ProtocolTraits::Instance().IsCompatible()), - known_desc.ToJson()); + TestHashType::ReadFromEnvironment(), known_desc.ToJson()); CHECK(known_desc == *from_json); } SECTION("Executable object") { auto known_desc = ArtifactDescription::CreateKnown( NamedDigest("x_fake_hash"), ObjectType::Executable); auto from_json = ArtifactDescription::FromJson( - GetHashType(ProtocolTraits::Instance().IsCompatible()), - known_desc.ToJson()); + TestHashType::ReadFromEnvironment(), known_desc.ToJson()); CHECK(known_desc == *from_json); } SECTION("Symlink object") { auto known_desc = ArtifactDescription::CreateKnown( NamedDigest("l_fake_hash"), ObjectType::Symlink); auto from_json = ArtifactDescription::FromJson( - GetHashType(ProtocolTraits::Instance().IsCompatible()), - known_desc.ToJson()); + TestHashType::ReadFromEnvironment(), known_desc.ToJson()); CHECK(known_desc == *from_json); } } @@ -86,8 +74,7 @@ TEST_CASE("Action artifact", "[artifact_description]") { auto action_desc = ArtifactDescription::CreateAction( "action_id", std::filesystem::path{"out_path"}); auto from_json = ArtifactDescription::FromJson( - GetHashType(ProtocolTraits::Instance().IsCompatible()), - action_desc.ToJson()); + TestHashType::ReadFromEnvironment(), action_desc.ToJson()); CHECK(action_desc == *from_json); } @@ -98,8 +85,7 @@ TEST_CASE("From JSON", "[artifact_description]") { .ToJson(); auto action = ArtifactDescription::CreateAction("id", "output").ToJson(); - auto const hash_type = - GetHashType(ProtocolTraits::Instance().IsCompatible()); + auto const hash_type = TestHashType::ReadFromEnvironment(); SECTION("Parse artifacts") { CHECK(ArtifactDescription::FromJson(hash_type, local)); CHECK(ArtifactDescription::FromJson(hash_type, known)); @@ -177,20 +163,19 @@ TEST_CASE("From JSON", "[artifact_description]") { TEST_CASE("Description missing mandatory key/value pair", "[artifact_description]") { nlohmann::json const missing_type = {{"data", {{"path", "some/path"}}}}; - CHECK(not ArtifactDescription::FromJson( - GetHashType(ProtocolTraits::Instance().IsCompatible()), missing_type)); + CHECK(not ArtifactDescription::FromJson(TestHashType::ReadFromEnvironment(), + missing_type)); nlohmann::json const missing_data = {{"type", "LOCAL"}}; - CHECK(not ArtifactDescription::FromJson( - GetHashType(ProtocolTraits::Instance().IsCompatible()), missing_data)); + CHECK(not ArtifactDescription::FromJson(TestHashType::ReadFromEnvironment(), + missing_data)); } TEST_CASE("Local artifact description contains incorrect value for \"data\"", "[artifact_description]") { nlohmann::json const local_art_missing_path = { {"type", "LOCAL"}, {"data", nlohmann::json::object()}}; - CHECK(not ArtifactDescription::FromJson( - GetHashType(ProtocolTraits::Instance().IsCompatible()), - local_art_missing_path)); + CHECK(not ArtifactDescription::FromJson(TestHashType::ReadFromEnvironment(), + local_art_missing_path)); } TEST_CASE("Known artifact description contains incorrect value for \"data\"", @@ -202,24 +187,21 @@ TEST_CASE("Known artifact description contains incorrect value for \"data\"", {"type", "KNOWN"}, {"data", {{"size", 15}, {"file_type", file_type}}}}; CHECK(not ArtifactDescription::FromJson( - GetHashType(ProtocolTraits::Instance().IsCompatible()), - known_art_missing_id)); + TestHashType::ReadFromEnvironment(), known_art_missing_id)); } SECTION("missing \"size\"") { nlohmann::json const known_art_missing_size = { {"type", "KNOWN"}, {"data", {{"id", "known_input"}, {"file_type", file_type}}}}; CHECK(not ArtifactDescription::FromJson( - GetHashType(ProtocolTraits::Instance().IsCompatible()), - known_art_missing_size)); + TestHashType::ReadFromEnvironment(), known_art_missing_size)); } SECTION("missing \"file_type\"") { nlohmann::json const known_art_missing_file_type = { {"type", "KNOWN"}, {"data", {{"id", "known_input"}, {"size", 15}}}}; CHECK(not ArtifactDescription::FromJson( - GetHashType(ProtocolTraits::Instance().IsCompatible()), - known_art_missing_file_type)); + TestHashType::ReadFromEnvironment(), known_art_missing_file_type)); } } @@ -227,13 +209,11 @@ TEST_CASE("Action artifact description contains incorrect value for \"data\"", "[artifact_description]") { nlohmann::json const action_art_missing_id = { {"type", "ACTION"}, {"data", {{"path", "output/path"}}}}; - CHECK(not ArtifactDescription::FromJson( - GetHashType(ProtocolTraits::Instance().IsCompatible()), - action_art_missing_id)); + CHECK(not ArtifactDescription::FromJson(TestHashType::ReadFromEnvironment(), + action_art_missing_id)); nlohmann::json const action_art_missing_path = { {"type", "ACTION"}, {"data", {{"id", "action_id"}}}}; - CHECK(not ArtifactDescription::FromJson( - GetHashType(ProtocolTraits::Instance().IsCompatible()), - action_art_missing_path)); + CHECK(not ArtifactDescription::FromJson(TestHashType::ReadFromEnvironment(), + action_art_missing_path)); } |