From 363dbeae95ce46be2b716506de90717998391a57 Mon Sep 17 00:00:00 2001 From: Griffin Smith Date: Fri, 27 Nov 2020 19:38:07 -0500 Subject: refactor(tvix): Centralize arbitrary definitions To make it easier for future tests to use the arbitrary specialisations we've defined for some nix types, centralize them all in a single arbitrary.hh header file. Change-Id: I767f27949cfe7ec55c79901f7d7aa538d2f98c6b Reviewed-on: https://cl.tvl.fyi/c/depot/+/2182 Tested-by: BuildkiteCI Reviewed-by: kanepyork --- third_party/nix/src/tests/arbitrary.hh | 176 ++++++++++++++++++++++++++ third_party/nix/src/tests/attr-set.cc | 88 +------------ third_party/nix/src/tests/derivations_test.cc | 68 +--------- third_party/nix/src/tests/store-api-test.cc | 23 +--- 4 files changed, 180 insertions(+), 175 deletions(-) create mode 100644 third_party/nix/src/tests/arbitrary.hh (limited to 'third_party/nix/src') diff --git a/third_party/nix/src/tests/arbitrary.hh b/third_party/nix/src/tests/arbitrary.hh new file mode 100644 index 000000000000..026f8522cf71 --- /dev/null +++ b/third_party/nix/src/tests/arbitrary.hh @@ -0,0 +1,176 @@ +#pragma once + +#include +#include +#include + +#include "libexpr/attr-set.hh" +#include "libexpr/nixexpr.hh" +#include "libstore/derivations.hh" +#include "libutil/hash.hh" + +namespace nix::tests { +static nix::SymbolTable* symbol_table; +} + +namespace rc { + +using nix::Derivation; +using nix::DerivationOutput; +using nix::Pos; +using nix::Value; + +template <> +struct Arbitrary { + static Gen arbitrary() { + return gen::map(gen::arbitrary(), [](std::string s) { + return nix::tests::symbol_table->Create(s); + }); + } +}; + +template <> +struct Arbitrary { + static Gen arbitrary() { + return gen::build(gen::construct(), + // TODO(grfn) generalize to more types + gen::set(&Value::type, gen::just(nix::ValueType::tInt)), + gen::set(&Value::integer, gen::arbitrary())); + } +}; + +template <> +struct Arbitrary { + static Gen arbitrary() { + return gen::apply( + [](nix::ValueType typ, int i) { + auto ret = new Value(); + ret->type = typ; + ret->integer = i; + return ret; + }, + gen::just(nix::ValueType::tInt), gen::arbitrary()); + } +}; + +template <> +struct Arbitrary { + static Gen arbitrary() { + return gen::construct(gen::arbitrary(), + gen::arbitrary(), + gen::arbitrary()); + } +}; + +template <> +struct Arbitrary { + static Gen arbitrary() { + return gen::apply( + [](unsigned int line, unsigned int column) { + return new Pos({}, line, column); + }, + gen::arbitrary(), gen::arbitrary()); + } +}; + +template <> +struct Arbitrary { + static Gen arbitrary() { + return gen::construct(gen::arbitrary(), + gen::arbitrary(), + gen::arbitrary()); + } +}; + +template <> +struct Arbitrary { + static Gen arbitrary() { + return gen::map(gen::arbitrary>(), [](auto attrs) { + nix::Bindings res; + for (const auto& attr : attrs) { + res.push_back(attr); + } + return res; + }); + } +}; + +template +struct Arbitrary> { + static Gen> arbitrary() { + return gen::map(gen::arbitrary>(), [](std::map map) { + absl::btree_map out_map; + out_map.insert(map.begin(), map.end()); + return out_map; + }); + } +}; + +template <> +struct Arbitrary { + static Gen arbitrary() { + return gen::element(nix::Base16, nix::Base32, nix::Base64); + } +}; + +template <> +struct Arbitrary { + static Gen arbitrary() { + return gen::apply( + [](std::string content, std::string path, std::string hash_algo, + bool recursive, bool include_algo_in_hash, nix::Base base) { + auto hash_type = nix::parseHashType(hash_algo); + auto hash = nix::hashString(hash_type, content); + return DerivationOutput( + path, recursive ? absl::StrCat("r:", hash_algo) : hash_algo, + hash.to_string(base, include_algo_in_hash)); + }, + gen::arbitrary(), + gen::map(gen::arbitrary(), + [](std::string s) { return absl::StrCat("/", s); }), + gen::element("md5", "sha1", "sha256", "sha512"), + gen::arbitrary(), gen::arbitrary(), + gen::arbitrary()); + } +}; + +template <> +struct Arbitrary { + static Gen arbitrary() { + auto gen_path = gen::map(gen::arbitrary(), [](std::string s) { + return absl::StrCat("/", s); + }); + + return gen::build( + gen::set(&nix::BasicDerivation::outputs), + gen::set(&nix::BasicDerivation::inputSrcs, + gen::container(gen_path)), + gen::set(&nix::BasicDerivation::platform), + gen::set(&nix::BasicDerivation::builder, gen_path), + gen::set(&nix::BasicDerivation::args), + gen::set(&nix::BasicDerivation::env), + gen::set(&Derivation::inputDrvs, + gen::container( + gen_path, gen::arbitrary()))); + } +}; + +template <> +struct Arbitrary { + static Gen arbitrary() { + return gen::element(nix::BuildResult::Status::Built, + nix::BuildResult::Status::Substituted, + nix::BuildResult::Status::AlreadyValid, + nix::BuildResult::Status::PermanentFailure, + nix::BuildResult::Status::InputRejected, + nix::BuildResult::Status::OutputRejected, + nix::BuildResult::Status::TransientFailure, + nix::BuildResult::Status::CachedFailure, + nix::BuildResult::Status::TimedOut, + nix::BuildResult::Status::MiscFailure, + nix::BuildResult::Status::DependencyFailed, + nix::BuildResult::Status::LogLimitExceeded, + nix::BuildResult::Status::NotDeterministic); + } +}; +} // namespace rc diff --git a/third_party/nix/src/tests/attr-set.cc b/third_party/nix/src/tests/attr-set.cc index 84756c60940e..35932bbeff65 100644 --- a/third_party/nix/src/tests/attr-set.cc +++ b/third_party/nix/src/tests/attr-set.cc @@ -20,93 +20,9 @@ #include "libexpr/nixexpr.hh" #include "libexpr/symbol-table.hh" #include "libexpr/value.hh" +#include "tests/arbitrary.hh" #include "tests/dummy-store.hh" -static nix::SymbolTable* symbol_table; - -namespace rc { -using nix::Pos; -using nix::Value; - -// TODO(grfn): These arbitrary implementations should be pulled out to a util -// file sooner rather than later - -template <> -struct Arbitrary { - static Gen arbitrary() { - return gen::map(gen::arbitrary(), - [](std::string s) { return symbol_table->Create(s); }); - } -}; - -template <> -struct Arbitrary { - static Gen arbitrary() { - return gen::build(gen::construct(), - // TODO(grfn) generalize to more types - gen::set(&Value::type, gen::just(nix::ValueType::tInt)), - gen::set(&Value::integer, gen::arbitrary())); - } -}; - -template <> -struct Arbitrary { - static Gen arbitrary() { - return gen::apply( - [](nix::ValueType typ, int i) { - auto ret = new Value(); - ret->type = typ; - ret->integer = i; - return ret; - }, - gen::just(nix::ValueType::tInt), gen::arbitrary()); - } -}; - -template <> -struct Arbitrary { - static Gen arbitrary() { - return gen::construct(gen::arbitrary(), - gen::arbitrary(), - gen::arbitrary()); - } -}; - -template <> -struct Arbitrary { - static Gen arbitrary() { - return gen::apply( - [](unsigned int line, unsigned int column) { - return new Pos({}, line, column); - }, - gen::arbitrary(), gen::arbitrary()); - } -}; - -template <> -struct Arbitrary { - static Gen arbitrary() { - return gen::construct(gen::arbitrary(), - gen::arbitrary(), - gen::arbitrary()); - } -}; - -template <> -struct Arbitrary { - static Gen arbitrary() { - return gen::map(gen::arbitrary>(), [](auto attrs) { - nix::Bindings res; - for (const auto& attr : attrs) { - res.push_back(attr); - } - return res; - }); - } -}; - -} // namespace rc - namespace nix { using nix::tests::DummyStore; @@ -118,7 +34,7 @@ class AttrSetTest : public ::testing::Test { nix::expr::InitGC(); auto store = std::make_shared(); eval_state_ = new EvalState({"."}, ref(store)); - symbol_table = &eval_state_->symbols; + tests::symbol_table = &eval_state_->symbols; } void assert_bindings_equal(nix::Bindings* lhs, nix::Bindings* rhs) { diff --git a/third_party/nix/src/tests/derivations_test.cc b/third_party/nix/src/tests/derivations_test.cc index 50a540e84aac..6ebe17824ce1 100644 --- a/third_party/nix/src/tests/derivations_test.cc +++ b/third_party/nix/src/tests/derivations_test.cc @@ -16,73 +16,7 @@ #include "libexpr/eval.hh" #include "libutil/hash.hh" #include "libutil/types.hh" - -namespace rc { - -using nix::Derivation; -using nix::DerivationOutput; - -template -struct Arbitrary> { - static Gen> arbitrary() { - return gen::map(gen::arbitrary>(), [](std::map map) { - absl::btree_map out_map; - out_map.insert(map.begin(), map.end()); - return out_map; - }); - } -}; - -template <> -struct Arbitrary { - static Gen arbitrary() { - return gen::element(nix::Base16, nix::Base32, nix::Base64); - } -}; - -template <> -struct Arbitrary { - static Gen arbitrary() { - return gen::apply( - [](std::string content, std::string path, std::string hash_algo, - bool recursive, bool include_algo_in_hash, nix::Base base) { - auto hash_type = nix::parseHashType(hash_algo); - auto hash = nix::hashString(hash_type, content); - return DerivationOutput( - path, recursive ? absl::StrCat("r:", hash_algo) : hash_algo, - hash.to_string(base, include_algo_in_hash)); - }, - gen::arbitrary(), - gen::map(gen::arbitrary(), - [](std::string s) { return absl::StrCat("/", s); }), - gen::element("md5", "sha1", "sha256", "sha512"), - gen::arbitrary(), gen::arbitrary(), - gen::arbitrary()); - } -}; - -template <> -struct Arbitrary { - static Gen arbitrary() { - auto gen_path = gen::map(gen::arbitrary(), [](std::string s) { - return absl::StrCat("/", s); - }); - - return gen::build( - gen::set(&nix::BasicDerivation::outputs), - gen::set(&nix::BasicDerivation::inputSrcs, - gen::container(gen_path)), - gen::set(&nix::BasicDerivation::platform), - gen::set(&nix::BasicDerivation::builder, gen_path), - gen::set(&nix::BasicDerivation::args), - gen::set(&nix::BasicDerivation::env), - gen::set(&Derivation::inputDrvs, - gen::container( - gen_path, gen::arbitrary()))); - } -}; - -} // namespace rc +#include "tests/arbitrary.hh" namespace nix { diff --git a/third_party/nix/src/tests/store-api-test.cc b/third_party/nix/src/tests/store-api-test.cc index 515613b799ee..259e4b991b57 100644 --- a/third_party/nix/src/tests/store-api-test.cc +++ b/third_party/nix/src/tests/store-api-test.cc @@ -2,31 +2,10 @@ #include #include -#include #include #include "libproto/worker.pb.h" - -namespace rc { -template <> -struct Arbitrary { - static Gen arbitrary() { - return gen::element(nix::BuildResult::Status::Built, - nix::BuildResult::Status::Substituted, - nix::BuildResult::Status::AlreadyValid, - nix::BuildResult::Status::PermanentFailure, - nix::BuildResult::Status::InputRejected, - nix::BuildResult::Status::OutputRejected, - nix::BuildResult::Status::TransientFailure, - nix::BuildResult::Status::CachedFailure, - nix::BuildResult::Status::TimedOut, - nix::BuildResult::Status::MiscFailure, - nix::BuildResult::Status::DependencyFailed, - nix::BuildResult::Status::LogLimitExceeded, - nix::BuildResult::Status::NotDeterministic); - } -}; -} // namespace rc +#include "tests/arbitrary.hh" namespace nix { -- cgit 1.4.1