diff options
author | John Ericson <John.Ericson@Obsidian.Systems> | 2023-08-25 10:20:28 -0400 |
---|---|---|
committer | John Ericson <John.Ericson@Obsidian.Systems> | 2023-12-01 13:05:03 -0500 |
commit | f7f37035c81fa825a4dfc2df1ad2589013ac6380 (patch) | |
tree | bfcfef3efeb73cb0c3c6d812280191d03bda1233 | |
parent | 30dcc19d1f30fc203be460134c4578509cce704f (diff) |
Move tests to separate directories, and document
Today, with the tests inside a `tests` intermingled with the
corresponding library's source code, we have a few problems:
- We have to be careful that wildcards don't end up with tests being
built as part of Nix proper, or test headers being installed as part
of Nix proper.
- Tests in libraries but not executables is not right:
- It means each executable runs the previous unit tests again, because
it needs the libraries.
- It doesn't work right on Windows, which doesn't want you to load a
DLL just for the side global variable . It could be made to work
with the dlopen equivalent, but that's gross!
This reorg solves these problems.
There is a remaining problem which is that sibbling headers (like
`hash.hh` the test header vs `hash.hh` the main `libnixutil` header) end
up shadowing each other. This PR doesn't solve that. That is left as
future work for a future PR.
Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io>
(cherry picked from commit 91b6833686a6a6d9eac7f3f66393ec89ef1d3b57)
(cherry picked from commit a61e42adb528b3d40ce43e07c79368d779a8b624)
-rw-r--r-- | .gitignore | 6 | ||||
-rw-r--r-- | Makefile | 9 | ||||
-rw-r--r-- | doc/internal-api/doxygen.cfg.in | 12 | ||||
-rw-r--r-- | doc/manual/src/contributing/testing.md | 14 | ||||
-rw-r--r-- | flake.nix | 1 | ||||
-rw-r--r-- | src/libexpr/tests/local.mk | 19 | ||||
-rw-r--r-- | src/libstore/tests/local.mk | 29 | ||||
-rw-r--r-- | src/libutil/tests/local.mk | 29 | ||||
-rw-r--r-- | tests/unit/libexpr-support/local.mk | 19 | ||||
-rw-r--r-- | tests/unit/libexpr-support/tests/libexpr.hh (renamed from src/libexpr/tests/libexpr.hh) | 0 | ||||
-rw-r--r-- | tests/unit/libexpr-support/tests/value/context.cc | 30 | ||||
-rw-r--r-- | tests/unit/libexpr-support/tests/value/context.hh (renamed from src/libexpr/tests/value/context.hh) | 2 | ||||
-rw-r--r-- | tests/unit/libexpr/derived-path.cc (renamed from src/libexpr/tests/derived-path.cc) | 0 | ||||
-rw-r--r-- | tests/unit/libexpr/error_traces.cc (renamed from src/libexpr/tests/error_traces.cc) | 0 | ||||
-rw-r--r-- | tests/unit/libexpr/flakeref.cc (renamed from src/libexpr/tests/flakeref.cc) | 0 | ||||
-rw-r--r-- | tests/unit/libexpr/json.cc (renamed from src/libexpr/tests/json.cc) | 0 | ||||
-rw-r--r-- | tests/unit/libexpr/local.mk | 32 | ||||
-rw-r--r-- | tests/unit/libexpr/primops.cc (renamed from src/libexpr/tests/primops.cc) | 0 | ||||
-rw-r--r-- | tests/unit/libexpr/search-path.cc (renamed from src/libexpr/tests/search-path.cc) | 0 | ||||
-rw-r--r-- | tests/unit/libexpr/trivial.cc (renamed from src/libexpr/tests/trivial.cc) | 0 | ||||
-rw-r--r-- | tests/unit/libexpr/value/context.cc (renamed from src/libexpr/tests/value/context.cc) | 30 | ||||
-rw-r--r-- | tests/unit/libexpr/value/print.cc (renamed from src/libexpr/tests/value/print.cc) | 0 | ||||
-rw-r--r-- | tests/unit/libstore-support/local.mk | 17 | ||||
-rw-r--r-- | tests/unit/libstore-support/tests/derived-path.cc | 57 | ||||
-rw-r--r-- | tests/unit/libstore-support/tests/derived-path.hh (renamed from src/libstore/tests/derived-path.hh) | 0 | ||||
-rw-r--r-- | tests/unit/libstore-support/tests/libstore.hh (renamed from src/libstore/tests/libstore.hh) | 0 | ||||
-rw-r--r-- | tests/unit/libstore-support/tests/outputs-spec.cc | 24 | ||||
-rw-r--r-- | tests/unit/libstore-support/tests/outputs-spec.hh (renamed from src/libstore/tests/outputs-spec.hh) | 2 | ||||
-rw-r--r-- | tests/unit/libstore-support/tests/path.cc | 82 | ||||
-rw-r--r-- | tests/unit/libstore-support/tests/path.hh (renamed from src/libstore/tests/path.hh) | 3 | ||||
-rw-r--r-- | tests/unit/libstore/derivation.cc (renamed from src/libstore/tests/derivation.cc) | 0 | ||||
-rw-r--r-- | tests/unit/libstore/derived-path.cc (renamed from src/libstore/tests/derived-path.cc) | 53 | ||||
-rw-r--r-- | tests/unit/libstore/downstream-placeholder.cc (renamed from src/libstore/tests/downstream-placeholder.cc) | 0 | ||||
-rw-r--r-- | tests/unit/libstore/local.mk | 27 | ||||
-rw-r--r-- | tests/unit/libstore/machines.cc (renamed from src/libstore/tests/machines.cc) | 4 | ||||
-rw-r--r-- | tests/unit/libstore/nar-info-disk-cache.cc (renamed from src/libstore/tests/nar-info-disk-cache.cc) | 0 | ||||
-rw-r--r-- | tests/unit/libstore/outputs-spec.cc (renamed from src/libstore/tests/outputs-spec.cc) | 27 | ||||
-rw-r--r-- | tests/unit/libstore/path.cc (renamed from src/libstore/tests/path.cc) | 73 | ||||
-rw-r--r-- | tests/unit/libstore/references.cc (renamed from src/libstore/tests/references.cc) | 0 | ||||
-rw-r--r-- | tests/unit/libstore/test-data/machines.bad_format (renamed from src/libstore/tests/test-data/machines.bad_format) | 0 | ||||
-rw-r--r-- | tests/unit/libstore/test-data/machines.valid (renamed from src/libstore/tests/test-data/machines.valid) | 0 | ||||
-rw-r--r-- | tests/unit/libutil-support/local.mk | 15 | ||||
-rw-r--r-- | tests/unit/libutil-support/tests/hash.cc | 20 | ||||
-rw-r--r-- | tests/unit/libutil-support/tests/hash.hh (renamed from src/libutil/tests/hash.hh) | 0 | ||||
-rw-r--r-- | tests/unit/libutil/canon-path.cc (renamed from src/libutil/tests/canon-path.cc) | 0 | ||||
-rw-r--r-- | tests/unit/libutil/chunked-vector.cc (renamed from src/libutil/tests/chunked-vector.cc) | 0 | ||||
-rw-r--r-- | tests/unit/libutil/closure.cc (renamed from src/libutil/tests/closure.cc) | 0 | ||||
-rw-r--r-- | tests/unit/libutil/compression.cc (renamed from src/libutil/tests/compression.cc) | 0 | ||||
-rw-r--r-- | tests/unit/libutil/config.cc (renamed from src/libutil/tests/config.cc) | 0 | ||||
-rw-r--r-- | tests/unit/libutil/git.cc (renamed from src/libutil/tests/git.cc) | 0 | ||||
-rw-r--r-- | tests/unit/libutil/hash.cc (renamed from src/libutil/tests/hash.cc) | 20 | ||||
-rw-r--r-- | tests/unit/libutil/hilite.cc (renamed from src/libutil/tests/hilite.cc) | 0 | ||||
-rw-r--r-- | tests/unit/libutil/local.mk | 23 | ||||
-rw-r--r-- | tests/unit/libutil/logging.cc (renamed from src/libutil/tests/logging.cc) | 0 | ||||
-rw-r--r-- | tests/unit/libutil/lru-cache.cc (renamed from src/libutil/tests/lru-cache.cc) | 0 | ||||
-rw-r--r-- | tests/unit/libutil/pool.cc (renamed from src/libutil/tests/pool.cc) | 0 | ||||
-rw-r--r-- | tests/unit/libutil/references.cc (renamed from src/libutil/tests/references.cc) | 0 | ||||
-rw-r--r-- | tests/unit/libutil/suggestions.cc (renamed from src/libutil/tests/suggestions.cc) | 0 | ||||
-rw-r--r-- | tests/unit/libutil/tests.cc (renamed from src/libutil/tests/tests.cc) | 0 | ||||
-rw-r--r-- | tests/unit/libutil/url.cc (renamed from src/libutil/tests/url.cc) | 0 | ||||
-rw-r--r-- | tests/unit/libutil/xml-writer.cc (renamed from src/libutil/tests/xml-writer.cc) | 0 |
61 files changed, 386 insertions, 293 deletions
diff --git a/.gitignore b/.gitignore index 2d3314015..1e1361f5a 100644 --- a/.gitignore +++ b/.gitignore @@ -41,14 +41,14 @@ perl/Makefile.config /src/libexpr/parser-tab.hh /src/libexpr/parser-tab.output /src/libexpr/nix.tbl -/src/libexpr/tests/libnixexpr-tests +/tests/unit/libexpr/libnixexpr-tests # /src/libstore/ *.gen.* -/src/libstore/tests/libnixstore-tests +/tests/unit/libstore/libnixstore-tests # /src/libutil/ -/src/libutil/tests/libnixutil-tests +/tests/unit/libutil/libnixutil-tests /src/nix/nix @@ -23,9 +23,12 @@ makefiles = \ ifeq ($(tests), yes) makefiles += \ - src/libutil/tests/local.mk \ - src/libstore/tests/local.mk \ - src/libexpr/tests/local.mk \ + tests/unit/libutil/local.mk \ + tests/unit/libutil-support/local.mk \ + tests/unit/libstore/local.mk \ + tests/unit/libstore-support/local.mk \ + tests/unit/libexpr/local.mk \ + tests/unit/libexpr-support/local.mk \ tests/functional/local.mk \ tests/functional/ca/local.mk \ tests/functional/dyn-drv/local.mk \ diff --git a/doc/internal-api/doxygen.cfg.in b/doc/internal-api/doxygen.cfg.in index 8f526536d..a339662e3 100644 --- a/doc/internal-api/doxygen.cfg.in +++ b/doc/internal-api/doxygen.cfg.in @@ -39,17 +39,21 @@ INPUT = \ src/libcmd \ src/libexpr \ src/libexpr/flake \ - src/libexpr/tests \ - src/libexpr/tests/value \ + tests/unit/libexpr \ + tests/unit/libexpr/value \ + tests/unit/libexpr/test \ + tests/unit/libexpr/test/value \ src/libexpr/value \ src/libfetchers \ src/libmain \ src/libstore \ src/libstore/build \ src/libstore/builtins \ - src/libstore/tests \ + tests/unit/libstore \ + tests/unit/libstore/test \ src/libutil \ - src/libutil/tests \ + tests/unit/libutil \ + tests/unit/libutil/test \ src/nix \ src/nix-env \ src/nix-store diff --git a/doc/manual/src/contributing/testing.md b/doc/manual/src/contributing/testing.md index ccd90088e..1b15d12b1 100644 --- a/doc/manual/src/contributing/testing.md +++ b/doc/manual/src/contributing/testing.md @@ -3,13 +3,25 @@ ## Unit-tests The unit-tests for each Nix library (`libexpr`, `libstore`, etc..) are defined -under `src/{library_name}/tests` using the +under `tests/unit/{library_name}/tests` using the [googletest](https://google.github.io/googletest/) and [rapidcheck](https://github.com/emil-e/rapidcheck) frameworks. You can run the whole testsuite with `make check`, or the tests for a specific component with `make libfoo-tests_RUN`. Finer-grained filtering is also possible using the [--gtest_filter](https://google.github.io/googletest/advanced.html#running-a-subset-of-the-tests) command-line option, or the `GTEST_FILTER` environment variable. +### Unit test support libraries + +There are headers and code which are not just used to test the library in question, but also downstream libraries. +For example, we do [property testing] with the [rapidcheck] library. +This requires writing `Arbitrary` "instances", which are used to describe how to generate values of a given type for the sake of running property tests. +Because types contain other types, `Arbitrary` "instances" for some type are not just useful for testing that type, but also any other type that contains it. +Downstream types frequently contain upstream types, so it is very important that we share arbitrary instances so that downstream libraries' property tests can also use them. + +It is important that these testing libraries don't contain any actual tests themselves. +On some platforms they would be run as part of every test executable that uses them, which is redundant. +On other platforms they wouldn't be run at all. + ## Functional tests The functional tests reside under the `tests/functional` directory and are listed in `tests/functional/local.mk`. @@ -73,6 +73,7 @@ ./precompiled-headers.h ./src ./tests/functional + ./tests/unit ./COPYING ./scripts/local.mk (fileset.fileFilter (f: lib.strings.hasPrefix "nix-profile" f.name) ./scripts) diff --git a/src/libexpr/tests/local.mk b/src/libexpr/tests/local.mk deleted file mode 100644 index c56848a14..000000000 --- a/src/libexpr/tests/local.mk +++ /dev/null @@ -1,19 +0,0 @@ -check: libexpr-tests_RUN - -programs += libexpr-tests - -libexpr-tests_NAME := libnixexpr-tests - -libexpr-tests_DIR := $(d) - -libexpr-tests_INSTALL_DIR := - -libexpr-tests_SOURCES := \ - $(wildcard $(d)/*.cc) \ - $(wildcard $(d)/value/*.cc) - -libexpr-tests_CXXFLAGS += -I src/libexpr -I src/libutil -I src/libstore -I src/libexpr/tests -I src/libfetchers - -libexpr-tests_LIBS = libstore-tests libutils-tests libexpr libutil libstore libfetchers - -libexpr-tests_LDFLAGS := -lrapidcheck $(GTEST_LIBS) -lgmock diff --git a/src/libstore/tests/local.mk b/src/libstore/tests/local.mk deleted file mode 100644 index 03becc7d1..000000000 --- a/src/libstore/tests/local.mk +++ /dev/null @@ -1,29 +0,0 @@ -check: libstore-tests-exe_RUN - -programs += libstore-tests-exe - -libstore-tests-exe_NAME = libnixstore-tests - -libstore-tests-exe_DIR := $(d) - -libstore-tests-exe_INSTALL_DIR := - -libstore-tests-exe_LIBS = libstore-tests - -libstore-tests-exe_LDFLAGS := $(GTEST_LIBS) - -libraries += libstore-tests - -libstore-tests_NAME = libnixstore-tests - -libstore-tests_DIR := $(d) - -libstore-tests_INSTALL_DIR := - -libstore-tests_SOURCES := $(wildcard $(d)/*.cc) - -libstore-tests_CXXFLAGS += -I src/libstore -I src/libutil - -libstore-tests_LIBS = libutil-tests libstore libutil - -libstore-tests_LDFLAGS := -lrapidcheck $(GTEST_LIBS) diff --git a/src/libutil/tests/local.mk b/src/libutil/tests/local.mk deleted file mode 100644 index 167915439..000000000 --- a/src/libutil/tests/local.mk +++ /dev/null @@ -1,29 +0,0 @@ -check: libutil-tests_RUN - -programs += libutil-tests - -libutil-tests-exe_NAME = libnixutil-tests - -libutil-tests-exe_DIR := $(d) - -libutil-tests-exe_INSTALL_DIR := - -libutil-tests-exe_LIBS = libutil-tests - -libutil-tests-exe_LDFLAGS := $(GTEST_LIBS) - -libraries += libutil-tests - -libutil-tests_NAME = libnixutil-tests - -libutil-tests_DIR := $(d) - -libutil-tests_INSTALL_DIR := - -libutil-tests_SOURCES := $(wildcard $(d)/*.cc) - -libutil-tests_CXXFLAGS += -I src/libutil - -libutil-tests_LIBS = libutil - -libutil-tests_LDFLAGS := -lrapidcheck $(GTEST_LIBS) diff --git a/tests/unit/libexpr-support/local.mk b/tests/unit/libexpr-support/local.mk new file mode 100644 index 000000000..6b3ad64ee --- /dev/null +++ b/tests/unit/libexpr-support/local.mk @@ -0,0 +1,19 @@ +libraries += libexpr-test-support + +libexpr-test-support_NAME = libnixexpr-test-support + +libexpr-test-support_DIR := $(d) + +libexpr-test-support_INSTALL_DIR := + +libexpr-test-support_SOURCES := \ + $(wildcard $(d)/tests/*.cc) \ + $(wildcard $(d)/tests/value/*.cc) + +libexpr-test-support_CXXFLAGS += $(libexpr-tests_EXTRA_INCLUDES) + +libexpr-test-support_LIBS = \ + libstore-test-support libutil-test-support \ + libexpr libstore libutil + +libexpr-test-support_LDFLAGS := -lrapidcheck diff --git a/src/libexpr/tests/libexpr.hh b/tests/unit/libexpr-support/tests/libexpr.hh index b8e65aafe..b8e65aafe 100644 --- a/src/libexpr/tests/libexpr.hh +++ b/tests/unit/libexpr-support/tests/libexpr.hh diff --git a/tests/unit/libexpr-support/tests/value/context.cc b/tests/unit/libexpr-support/tests/value/context.cc new file mode 100644 index 000000000..8658bdaef --- /dev/null +++ b/tests/unit/libexpr-support/tests/value/context.cc @@ -0,0 +1,30 @@ +#include <rapidcheck.h> + +#include "tests/path.hh" +#include "tests/value/context.hh" + +namespace rc { +using namespace nix; + +Gen<NixStringContextElem::DrvDeep> Arbitrary<NixStringContextElem::DrvDeep>::arbitrary() +{ + return gen::just(NixStringContextElem::DrvDeep { + .drvPath = *gen::arbitrary<StorePath>(), + }); +} + +Gen<NixStringContextElem> Arbitrary<NixStringContextElem>::arbitrary() +{ + switch (*gen::inRange<uint8_t>(0, std::variant_size_v<NixStringContextElem::Raw>)) { + case 0: + return gen::just<NixStringContextElem>(*gen::arbitrary<NixStringContextElem::Opaque>()); + case 1: + return gen::just<NixStringContextElem>(*gen::arbitrary<NixStringContextElem::DrvDeep>()); + case 2: + return gen::just<NixStringContextElem>(*gen::arbitrary<NixStringContextElem::Built>()); + default: + assert(false); + } +} + +} diff --git a/src/libexpr/tests/value/context.hh b/tests/unit/libexpr-support/tests/value/context.hh index c0bc97ba3..8c68c78bb 100644 --- a/src/libexpr/tests/value/context.hh +++ b/tests/unit/libexpr-support/tests/value/context.hh @@ -3,7 +3,7 @@ #include <rapidcheck/gen/Arbitrary.h> -#include <value/context.hh> +#include "value/context.hh" namespace rc { using namespace nix; diff --git a/src/libexpr/tests/derived-path.cc b/tests/unit/libexpr/derived-path.cc index d5fc6f201..d5fc6f201 100644 --- a/src/libexpr/tests/derived-path.cc +++ b/tests/unit/libexpr/derived-path.cc diff --git a/src/libexpr/tests/error_traces.cc b/tests/unit/libexpr/error_traces.cc index 285651256..285651256 100644 --- a/src/libexpr/tests/error_traces.cc +++ b/tests/unit/libexpr/error_traces.cc diff --git a/src/libexpr/tests/flakeref.cc b/tests/unit/libexpr/flakeref.cc index 2b7809b93..2b7809b93 100644 --- a/src/libexpr/tests/flakeref.cc +++ b/tests/unit/libexpr/flakeref.cc diff --git a/src/libexpr/tests/json.cc b/tests/unit/libexpr/json.cc index 7586bdd9b..7586bdd9b 100644 --- a/src/libexpr/tests/json.cc +++ b/tests/unit/libexpr/json.cc diff --git a/tests/unit/libexpr/local.mk b/tests/unit/libexpr/local.mk new file mode 100644 index 000000000..068dd7a17 --- /dev/null +++ b/tests/unit/libexpr/local.mk @@ -0,0 +1,32 @@ +check: libexpr-tests_RUN + +programs += libexpr-tests + +libexpr-tests_NAME := libnixexpr-tests + +libexpr-tests_ENV := _NIX_TEST_UNIT_DATA=$(d)/data + +libexpr-tests_DIR := $(d) + +libexpr-tests_INSTALL_DIR := + +libexpr-tests_SOURCES := \ + $(wildcard $(d)/*.cc) \ + $(wildcard $(d)/value/*.cc) + +libexpr-tests_EXTRA_INCLUDES = \ + -I tests/unit/libexpr-support \ + -I tests/unit/libstore-support \ + -I tests/unit/libutil-support \ + -I src/libexpr \ + -I src/libfetchers \ + -I src/libstore \ + -I src/libutil + +libexpr-tests_CXXFLAGS += $(libexpr-tests_EXTRA_INCLUDES) + +libexpr-tests_LIBS = \ + libexpr-test-support libstore-test-support libutils-test-support \ + libexpr libfetchers libstore libutil + +libexpr-tests_LDFLAGS := -lrapidcheck $(GTEST_LIBS) -lgmock diff --git a/src/libexpr/tests/primops.cc b/tests/unit/libexpr/primops.cc index ce3b5d11f..ce3b5d11f 100644 --- a/src/libexpr/tests/primops.cc +++ b/tests/unit/libexpr/primops.cc diff --git a/src/libexpr/tests/search-path.cc b/tests/unit/libexpr/search-path.cc index dbe7ab95f..dbe7ab95f 100644 --- a/src/libexpr/tests/search-path.cc +++ b/tests/unit/libexpr/search-path.cc diff --git a/src/libexpr/tests/trivial.cc b/tests/unit/libexpr/trivial.cc index 171727ac7..171727ac7 100644 --- a/src/libexpr/tests/trivial.cc +++ b/tests/unit/libexpr/trivial.cc diff --git a/src/libexpr/tests/value/context.cc b/tests/unit/libexpr/value/context.cc index 92d4889ab..761286dbd 100644 --- a/src/libexpr/tests/value/context.cc +++ b/tests/unit/libexpr/value/context.cc @@ -117,36 +117,6 @@ TEST(NixStringContextElemTest, built_built_xp) { NixStringContextElem::parse("!foo!bar!g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-x.drv"), MissingExperimentalFeature); } -} - -namespace rc { -using namespace nix; - -Gen<NixStringContextElem::DrvDeep> Arbitrary<NixStringContextElem::DrvDeep>::arbitrary() -{ - return gen::just(NixStringContextElem::DrvDeep { - .drvPath = *gen::arbitrary<StorePath>(), - }); -} - -Gen<NixStringContextElem> Arbitrary<NixStringContextElem>::arbitrary() -{ - switch (*gen::inRange<uint8_t>(0, std::variant_size_v<NixStringContextElem::Raw>)) { - case 0: - return gen::just<NixStringContextElem>(*gen::arbitrary<NixStringContextElem::Opaque>()); - case 1: - return gen::just<NixStringContextElem>(*gen::arbitrary<NixStringContextElem::DrvDeep>()); - case 2: - return gen::just<NixStringContextElem>(*gen::arbitrary<NixStringContextElem::Built>()); - default: - assert(false); - } -} - -} - -namespace nix { - #ifndef COVERAGE RC_GTEST_PROP( diff --git a/src/libexpr/tests/value/print.cc b/tests/unit/libexpr/value/print.cc index 5e96e12ec..5e96e12ec 100644 --- a/src/libexpr/tests/value/print.cc +++ b/tests/unit/libexpr/value/print.cc diff --git a/tests/unit/libstore-support/local.mk b/tests/unit/libstore-support/local.mk new file mode 100644 index 000000000..2738278d8 --- /dev/null +++ b/tests/unit/libstore-support/local.mk @@ -0,0 +1,17 @@ +libraries += libstore-test-support + +libstore-test-support_NAME = libnixstore-test-support + +libstore-test-support_DIR := $(d) + +libstore-test-support_INSTALL_DIR := + +libstore-test-support_SOURCES := $(wildcard $(d)/tests/*.cc) + +libstore-test-support_CXXFLAGS += $(libstore-tests_EXTRA_INCLUDES) + +libstore-test-support_LIBS = \ + libutil-test-support \ + libstore libutil + +libstore-test-support_LDFLAGS := -lrapidcheck diff --git a/tests/unit/libstore-support/tests/derived-path.cc b/tests/unit/libstore-support/tests/derived-path.cc new file mode 100644 index 000000000..091706dba --- /dev/null +++ b/tests/unit/libstore-support/tests/derived-path.cc @@ -0,0 +1,57 @@ +#include <regex> + +#include <rapidcheck.h> + +#include "tests/derived-path.hh" + +namespace rc { +using namespace nix; + +Gen<DerivedPath::Opaque> Arbitrary<DerivedPath::Opaque>::arbitrary() +{ + return gen::just(DerivedPath::Opaque { + .path = *gen::arbitrary<StorePath>(), + }); +} + +Gen<SingleDerivedPath::Built> Arbitrary<SingleDerivedPath::Built>::arbitrary() +{ + return gen::just(SingleDerivedPath::Built { + .drvPath = make_ref<SingleDerivedPath>(*gen::arbitrary<SingleDerivedPath>()), + .output = (*gen::arbitrary<StorePathName>()).name, + }); +} + +Gen<DerivedPath::Built> Arbitrary<DerivedPath::Built>::arbitrary() +{ + return gen::just(DerivedPath::Built { + .drvPath = make_ref<SingleDerivedPath>(*gen::arbitrary<SingleDerivedPath>()), + .outputs = *gen::arbitrary<OutputsSpec>(), + }); +} + +Gen<SingleDerivedPath> Arbitrary<SingleDerivedPath>::arbitrary() +{ + switch (*gen::inRange<uint8_t>(0, std::variant_size_v<SingleDerivedPath::Raw>)) { + case 0: + return gen::just<SingleDerivedPath>(*gen::arbitrary<SingleDerivedPath::Opaque>()); + case 1: + return gen::just<SingleDerivedPath>(*gen::arbitrary<SingleDerivedPath::Built>()); + default: + assert(false); + } +} + +Gen<DerivedPath> Arbitrary<DerivedPath>::arbitrary() +{ + switch (*gen::inRange<uint8_t>(0, std::variant_size_v<DerivedPath::Raw>)) { + case 0: + return gen::just<DerivedPath>(*gen::arbitrary<DerivedPath::Opaque>()); + case 1: + return gen::just<DerivedPath>(*gen::arbitrary<DerivedPath::Built>()); + default: + assert(false); + } +} + +} diff --git a/src/libstore/tests/derived-path.hh b/tests/unit/libstore-support/tests/derived-path.hh index 98d61f228..98d61f228 100644 --- a/src/libstore/tests/derived-path.hh +++ b/tests/unit/libstore-support/tests/derived-path.hh diff --git a/src/libstore/tests/libstore.hh b/tests/unit/libstore-support/tests/libstore.hh index ef93457b5..ef93457b5 100644 --- a/src/libstore/tests/libstore.hh +++ b/tests/unit/libstore-support/tests/libstore.hh diff --git a/tests/unit/libstore-support/tests/outputs-spec.cc b/tests/unit/libstore-support/tests/outputs-spec.cc new file mode 100644 index 000000000..e9d602203 --- /dev/null +++ b/tests/unit/libstore-support/tests/outputs-spec.cc @@ -0,0 +1,24 @@ +#include "tests/outputs-spec.hh" + +#include <rapidcheck.h> + +namespace rc { +using namespace nix; + +Gen<OutputsSpec> Arbitrary<OutputsSpec>::arbitrary() +{ + switch (*gen::inRange<uint8_t>(0, std::variant_size_v<OutputsSpec::Raw>)) { + case 0: + return gen::just((OutputsSpec) OutputsSpec::All { }); + case 1: + return gen::just((OutputsSpec) OutputsSpec::Names { + *gen::nonEmpty(gen::container<StringSet>(gen::map( + gen::arbitrary<StorePathName>(), + [](StorePathName n) { return n.name; }))), + }); + default: + assert(false); + } +} + +} diff --git a/src/libstore/tests/outputs-spec.hh b/tests/unit/libstore-support/tests/outputs-spec.hh index ded331b33..f5bf9042d 100644 --- a/src/libstore/tests/outputs-spec.hh +++ b/tests/unit/libstore-support/tests/outputs-spec.hh @@ -5,7 +5,7 @@ #include <outputs-spec.hh> -#include <tests/path.hh> +#include "tests/path.hh" namespace rc { using namespace nix; diff --git a/tests/unit/libstore-support/tests/path.cc b/tests/unit/libstore-support/tests/path.cc new file mode 100644 index 000000000..e5f169e94 --- /dev/null +++ b/tests/unit/libstore-support/tests/path.cc @@ -0,0 +1,82 @@ +#include <regex> + +#include <rapidcheck.h> + +#include "path-regex.hh" +#include "store-api.hh" + +#include "tests/hash.hh" +#include "tests/path.hh" + +namespace nix { + +void showValue(const StorePath & p, std::ostream & os) +{ + os << p.to_string(); +} + +} + +namespace rc { +using namespace nix; + +Gen<StorePathName> Arbitrary<StorePathName>::arbitrary() +{ + auto len = *gen::inRange<size_t>( + 1, + StorePath::MaxPathLen - StorePath::HashLen); + + std::string pre; + pre.reserve(len); + + for (size_t c = 0; c < len; ++c) { + switch (auto i = *gen::inRange<uint8_t>(0, 10 + 2 * 26 + 6)) { + case 0 ... 9: + pre += '0' + i; + case 10 ... 35: + pre += 'A' + (i - 10); + break; + case 36 ... 61: + pre += 'a' + (i - 36); + break; + case 62: + pre += '+'; + break; + case 63: + pre += '-'; + break; + case 64: + // names aren't permitted to start with a period, + // so just fall through to the next case here + if (c != 0) { + pre += '.'; + break; + } + case 65: + pre += '_'; + break; + case 66: + pre += '?'; + break; + case 67: + pre += '='; + break; + default: + assert(false); + } + } + + return gen::just(StorePathName { + .name = std::move(pre), + }); +} + +Gen<StorePath> Arbitrary<StorePath>::arbitrary() +{ + return gen::just(StorePath { + *gen::arbitrary<Hash>(), + (*gen::arbitrary<StorePathName>()).name, + }); +} + +} // namespace rc diff --git a/src/libstore/tests/path.hh b/tests/unit/libstore-support/tests/path.hh index 21cb62310..4751b3373 100644 --- a/src/libstore/tests/path.hh +++ b/tests/unit/libstore-support/tests/path.hh @@ -11,6 +11,9 @@ struct StorePathName { std::string name; }; +// For rapidcheck +void showValue(const StorePath & p, std::ostream & os); + } namespace rc { diff --git a/src/libstore/tests/derivation.cc b/tests/unit/libstore/derivation.cc index c360c9707..c360c9707 100644 --- a/src/libstore/tests/derivation.cc +++ b/tests/unit/libstore/derivation.cc diff --git a/src/libstore/tests/derived-path.cc b/tests/unit/libstore/derived-path.cc index 3fa3c0801..c62d79a78 100644 --- a/src/libstore/tests/derived-path.cc +++ b/tests/unit/libstore/derived-path.cc @@ -1,64 +1,11 @@ #include <regex> -#include <nlohmann/json.hpp> #include <gtest/gtest.h> #include <rapidcheck/gtest.h> #include "tests/derived-path.hh" #include "tests/libstore.hh" -namespace rc { -using namespace nix; - -Gen<DerivedPath::Opaque> Arbitrary<DerivedPath::Opaque>::arbitrary() -{ - return gen::just(DerivedPath::Opaque { - .path = *gen::arbitrary<StorePath>(), - }); -} - -Gen<SingleDerivedPath::Built> Arbitrary<SingleDerivedPath::Built>::arbitrary() -{ - return gen::just(SingleDerivedPath::Built { - .drvPath = make_ref<SingleDerivedPath>(*gen::arbitrary<SingleDerivedPath>()), - .output = (*gen::arbitrary<StorePathName>()).name, - }); -} - -Gen<DerivedPath::Built> Arbitrary<DerivedPath::Built>::arbitrary() -{ - return gen::just(DerivedPath::Built { - .drvPath = make_ref<SingleDerivedPath>(*gen::arbitrary<SingleDerivedPath>()), - .outputs = *gen::arbitrary<OutputsSpec>(), - }); -} - -Gen<SingleDerivedPath> Arbitrary<SingleDerivedPath>::arbitrary() -{ - switch (*gen::inRange<uint8_t>(0, std::variant_size_v<SingleDerivedPath::Raw>)) { - case 0: - return gen::just<SingleDerivedPath>(*gen::arbitrary<SingleDerivedPath::Opaque>()); - case 1: - return gen::just<SingleDerivedPath>(*gen::arbitrary<SingleDerivedPath::Built>()); - default: - assert(false); - } -} - -Gen<DerivedPath> Arbitrary<DerivedPath>::arbitrary() -{ - switch (*gen::inRange<uint8_t>(0, std::variant_size_v<DerivedPath::Raw>)) { - case 0: - return gen::just<DerivedPath>(*gen::arbitrary<DerivedPath::Opaque>()); - case 1: - return gen::just<DerivedPath>(*gen::arbitrary<DerivedPath::Built>()); - default: - assert(false); - } -} - -} - namespace nix { class DerivedPathTest : public LibStoreTest diff --git a/src/libstore/tests/downstream-placeholder.cc b/tests/unit/libstore/downstream-placeholder.cc index fd29530ac..fd29530ac 100644 --- a/src/libstore/tests/downstream-placeholder.cc +++ b/tests/unit/libstore/downstream-placeholder.cc diff --git a/tests/unit/libstore/local.mk b/tests/unit/libstore/local.mk new file mode 100644 index 000000000..fb7385ec1 --- /dev/null +++ b/tests/unit/libstore/local.mk @@ -0,0 +1,27 @@ +check: libstore-tests_RUN + +programs += libstore-tests + +libstore-tests_NAME = libnixstore-tests + +libstore-tests_ENV := _NIX_TEST_UNIT_DATA=$(d)/data + +libstore-tests_DIR := $(d) + +libstore-tests_INSTALL_DIR := + +libstore-tests_SOURCES := $(wildcard $(d)/*.cc) + +libstore-tests_EXTRA_INCLUDES = \ + -I tests/unit/libstore-support \ + -I tests/unit/libutil-support \ + -I src/libstore \ + -I src/libutil + +libstore-tests_CXXFLAGS += $(libstore-tests_EXTRA_INCLUDES) + +libstore-tests_LIBS = \ + libstore-test-support libutil-test-support \ + libstore libutil + +libstore-tests_LDFLAGS := -lrapidcheck $(GTEST_LIBS) diff --git a/src/libstore/tests/machines.cc b/tests/unit/libstore/machines.cc index f51052b14..72b2c3ac6 100644 --- a/src/libstore/tests/machines.cc +++ b/tests/unit/libstore/machines.cc @@ -137,7 +137,7 @@ TEST(machines, getMachinesWithIncorrectFormat) { } TEST(machines, getMachinesWithCorrectFileReference) { - auto path = absPath("src/libstore/tests/test-data/machines.valid"); + auto path = absPath("tests/unit/libstore/test-data/machines.valid"); ASSERT_TRUE(pathExists(path)); settings.builders = std::string("@") + path; @@ -164,6 +164,6 @@ TEST(machines, getMachinesWithIncorrectFileReference) { } TEST(machines, getMachinesWithCorrectFileReferenceToIncorrectFile) { - settings.builders = std::string("@") + absPath("src/libstore/tests/test-data/machines.bad_format"); + settings.builders = std::string("@") + absPath("tests/unit/libstore/test-data/machines.bad_format"); EXPECT_THROW(getMachines(), FormatError); } diff --git a/src/libstore/tests/nar-info-disk-cache.cc b/tests/unit/libstore/nar-info-disk-cache.cc index b4bdb8329..b4bdb8329 100644 --- a/src/libstore/tests/nar-info-disk-cache.cc +++ b/tests/unit/libstore/nar-info-disk-cache.cc diff --git a/src/libstore/tests/outputs-spec.cc b/tests/unit/libstore/outputs-spec.cc index 952945185..456196be1 100644 --- a/src/libstore/tests/outputs-spec.cc +++ b/tests/unit/libstore/outputs-spec.cc @@ -1,4 +1,4 @@ -#include "outputs-spec.hh" +#include "tests/outputs-spec.hh" #include <nlohmann/json.hpp> #include <gtest/gtest.h> @@ -199,31 +199,6 @@ TEST_JSON(ExtendedOutputsSpec, names, R"(["a","b"])", (ExtendedOutputsSpec::Expl #undef TEST_JSON -} - -namespace rc { -using namespace nix; - -Gen<OutputsSpec> Arbitrary<OutputsSpec>::arbitrary() -{ - switch (*gen::inRange<uint8_t>(0, std::variant_size_v<OutputsSpec::Raw>)) { - case 0: - return gen::just((OutputsSpec) OutputsSpec::All { }); - case 1: - return gen::just((OutputsSpec) OutputsSpec::Names { - *gen::nonEmpty(gen::container<StringSet>(gen::map( - gen::arbitrary<StorePathName>(), - [](StorePathName n) { return n.name; }))), - }); - default: - assert(false); - } -} - -} - -namespace nix { - #ifndef COVERAGE RC_GTEST_PROP( diff --git a/src/libstore/tests/path.cc b/tests/unit/libstore/path.cc index 5a84d646c..30631b5fd 100644 --- a/src/libstore/tests/path.cc +++ b/tests/unit/libstore/path.cc @@ -66,79 +66,6 @@ TEST_DO_PARSE(equals_sign, "foo=foo") #undef TEST_DO_PARSE -// For rapidcheck -void showValue(const StorePath & p, std::ostream & os) { - os << p.to_string(); -} - -} - -namespace rc { -using namespace nix; - -Gen<StorePathName> Arbitrary<StorePathName>::arbitrary() -{ - auto len = *gen::inRange<size_t>( - 1, - StorePath::MaxPathLen - std::string_view { HASH_PART }.size()); - - std::string pre; - pre.reserve(len); - - for (size_t c = 0; c < len; ++c) { - switch (auto i = *gen::inRange<uint8_t>(0, 10 + 2 * 26 + 6)) { - case 0 ... 9: - pre += '0' + i; - case 10 ... 35: - pre += 'A' + (i - 10); - break; - case 36 ... 61: - pre += 'a' + (i - 36); - break; - case 62: - pre += '+'; - break; - case 63: - pre += '-'; - break; - case 64: - // names aren't permitted to start with a period, - // so just fall through to the next case here - if (c != 0) { - pre += '.'; - break; - } - case 65: - pre += '_'; - break; - case 66: - pre += '?'; - break; - case 67: - pre += '='; - break; - default: - assert(false); - } - } - - return gen::just(StorePathName { - .name = std::move(pre), - }); -} - -Gen<StorePath> Arbitrary<StorePath>::arbitrary() -{ - return gen::just(StorePath { - *gen::arbitrary<Hash>(), - (*gen::arbitrary<StorePathName>()).name, - }); -} - -} // namespace rc - -namespace nix { - #ifndef COVERAGE RC_GTEST_FIXTURE_PROP( diff --git a/src/libstore/tests/references.cc b/tests/unit/libstore/references.cc index d91d1cedd..d91d1cedd 100644 --- a/src/libstore/tests/references.cc +++ b/tests/unit/libstore/references.cc diff --git a/src/libstore/tests/test-data/machines.bad_format b/tests/unit/libstore/test-data/machines.bad_format index 7255a1216..7255a1216 100644 --- a/src/libstore/tests/test-data/machines.bad_format +++ b/tests/unit/libstore/test-data/machines.bad_format diff --git a/src/libstore/tests/test-data/machines.valid b/tests/unit/libstore/test-data/machines.valid index 1a6c8017c..1a6c8017c 100644 --- a/src/libstore/tests/test-data/machines.valid +++ b/tests/unit/libstore/test-data/machines.valid diff --git a/tests/unit/libutil-support/local.mk b/tests/unit/libutil-support/local.mk new file mode 100644 index 000000000..d085eda4e --- /dev/null +++ b/tests/unit/libutil-support/local.mk @@ -0,0 +1,15 @@ +libraries += libutil-test-support + +libutil-test-support_NAME = libnixutil-test-support + +libutil-test-support_DIR := $(d) + +libutil-test-support_INSTALL_DIR := + +libutil-test-support_SOURCES := $(wildcard $(d)/tests/*.cc) + +libutil-test-support_CXXFLAGS += $(libutil-tests_EXTRA_INCLUDES) + +libutil-test-support_LIBS = libutil + +libutil-test-support_LDFLAGS := -lrapidcheck diff --git a/tests/unit/libutil-support/tests/hash.cc b/tests/unit/libutil-support/tests/hash.cc new file mode 100644 index 000000000..577e9890e --- /dev/null +++ b/tests/unit/libutil-support/tests/hash.cc @@ -0,0 +1,20 @@ +#include <regex> + +#include <rapidcheck.h> + +#include "hash.hh" + +#include "tests/hash.hh" + +namespace rc { +using namespace nix; + +Gen<Hash> Arbitrary<Hash>::arbitrary() +{ + Hash hash(htSHA1); + for (size_t i = 0; i < hash.hashSize; ++i) + hash.hash[i] = *gen::arbitrary<uint8_t>(); + return gen::just(hash); +} + +} diff --git a/src/libutil/tests/hash.hh b/tests/unit/libutil-support/tests/hash.hh index 1f9fa59ae..1f9fa59ae 100644 --- a/src/libutil/tests/hash.hh +++ b/tests/unit/libutil-support/tests/hash.hh diff --git a/src/libutil/tests/canon-path.cc b/tests/unit/libutil/canon-path.cc index fc94ccc3d..fc94ccc3d 100644 --- a/src/libutil/tests/canon-path.cc +++ b/tests/unit/libutil/canon-path.cc diff --git a/src/libutil/tests/chunked-vector.cc b/tests/unit/libutil/chunked-vector.cc index 868d11f6f..868d11f6f 100644 --- a/src/libutil/tests/chunked-vector.cc +++ b/tests/unit/libutil/chunked-vector.cc diff --git a/src/libutil/tests/closure.cc b/tests/unit/libutil/closure.cc index 7597e7807..7597e7807 100644 --- a/src/libutil/tests/closure.cc +++ b/tests/unit/libutil/closure.cc diff --git a/src/libutil/tests/compression.cc b/tests/unit/libutil/compression.cc index bbbf3500f..bbbf3500f 100644 --- a/src/libutil/tests/compression.cc +++ b/tests/unit/libutil/compression.cc diff --git a/src/libutil/tests/config.cc b/tests/unit/libutil/config.cc index 886e70da5..886e70da5 100644 --- a/src/libutil/tests/config.cc +++ b/tests/unit/libutil/config.cc diff --git a/src/libutil/tests/git.cc b/tests/unit/libutil/git.cc index 5b5715fc2..5b5715fc2 100644 --- a/src/libutil/tests/git.cc +++ b/tests/unit/libutil/git.cc diff --git a/src/libutil/tests/hash.cc b/tests/unit/libutil/hash.cc index e4e928b3b..1f40edc95 100644 --- a/src/libutil/tests/hash.cc +++ b/tests/unit/libutil/hash.cc @@ -1,12 +1,8 @@ #include <regex> -#include <nlohmann/json.hpp> #include <gtest/gtest.h> -#include <rapidcheck/gtest.h> -#include <hash.hh> - -#include "tests/hash.hh" +#include "hash.hh" namespace nix { @@ -68,7 +64,6 @@ namespace nix { "7ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd" "454d4423643ce80e2a9ac94fa54ca49f"); } - TEST(hashString, testKnownSHA512Hashes2) { // values taken from: https://tools.ietf.org/html/rfc4634 auto s = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"; @@ -80,16 +75,3 @@ namespace nix { "c7d329eeb6dd26545e96e55b874be909"); } } - -namespace rc { -using namespace nix; - -Gen<Hash> Arbitrary<Hash>::arbitrary() -{ - Hash hash(htSHA1); - for (size_t i = 0; i < hash.hashSize; ++i) - hash.hash[i] = *gen::arbitrary<uint8_t>(); - return gen::just(hash); -} - -} diff --git a/src/libutil/tests/hilite.cc b/tests/unit/libutil/hilite.cc index 1ff5980d5..1ff5980d5 100644 --- a/src/libutil/tests/hilite.cc +++ b/tests/unit/libutil/hilite.cc diff --git a/tests/unit/libutil/local.mk b/tests/unit/libutil/local.mk new file mode 100644 index 000000000..6de96c0dc --- /dev/null +++ b/tests/unit/libutil/local.mk @@ -0,0 +1,23 @@ +check: libutil-tests_RUN + +programs += libutil-tests + +libutil-tests_NAME = libnixutil-tests + +libutil-tests_ENV := _NIX_TEST_UNIT_DATA=$(d)/data + +libutil-tests_DIR := $(d) + +libutil-tests_INSTALL_DIR := + +libutil-tests_SOURCES := $(wildcard $(d)/*.cc) + +libutil-tests_EXTRA_INCLUDES = \ + -I tests/unit/libutil-support \ + -I src/libutil + +libutil-tests_CXXFLAGS += $(libutil-tests_EXTRA_INCLUDES) + +libutil-tests_LIBS = libutil-test-support libutil + +libutil-tests_LDFLAGS := -lrapidcheck $(GTEST_LIBS) diff --git a/src/libutil/tests/logging.cc b/tests/unit/libutil/logging.cc index 2ffdc2e9b..2ffdc2e9b 100644 --- a/src/libutil/tests/logging.cc +++ b/tests/unit/libutil/logging.cc diff --git a/src/libutil/tests/lru-cache.cc b/tests/unit/libutil/lru-cache.cc index 091d3d5ed..091d3d5ed 100644 --- a/src/libutil/tests/lru-cache.cc +++ b/tests/unit/libutil/lru-cache.cc diff --git a/src/libutil/tests/pool.cc b/tests/unit/libutil/pool.cc index 127e42dda..127e42dda 100644 --- a/src/libutil/tests/pool.cc +++ b/tests/unit/libutil/pool.cc diff --git a/src/libutil/tests/references.cc b/tests/unit/libutil/references.cc index a517d9aa1..a517d9aa1 100644 --- a/src/libutil/tests/references.cc +++ b/tests/unit/libutil/references.cc diff --git a/src/libutil/tests/suggestions.cc b/tests/unit/libutil/suggestions.cc index 279994abc..279994abc 100644 --- a/src/libutil/tests/suggestions.cc +++ b/tests/unit/libutil/suggestions.cc diff --git a/src/libutil/tests/tests.cc b/tests/unit/libutil/tests.cc index f3c1e8248..f3c1e8248 100644 --- a/src/libutil/tests/tests.cc +++ b/tests/unit/libutil/tests.cc diff --git a/src/libutil/tests/url.cc b/tests/unit/libutil/url.cc index a908631e6..a908631e6 100644 --- a/src/libutil/tests/url.cc +++ b/tests/unit/libutil/url.cc diff --git a/src/libutil/tests/xml-writer.cc b/tests/unit/libutil/xml-writer.cc index adcde25c9..adcde25c9 100644 --- a/src/libutil/tests/xml-writer.cc +++ b/tests/unit/libutil/xml-writer.cc |