Skip to content

Commit

Permalink
Merge pull request NixOS#11331 from NixOS/make-check
Browse files Browse the repository at this point in the history
Revert "Remove unit tests from old build system" (too soon)
  • Loading branch information
edolstra authored Aug 19, 2024
2 parents 8e4149a + 9385383 commit aeabe68
Show file tree
Hide file tree
Showing 12 changed files with 337 additions and 1 deletion.
19 changes: 19 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ makefiles += \
endif
endif

ifeq ($(ENABLE_UNIT_TESTS), yes)
makefiles += \
tests/unit/libutil/local.mk \
tests/unit/libutil-support/local.mk \
tests/unit/libstore/local.mk \
tests/unit/libstore-support/local.mk \
tests/unit/libfetchers/local.mk \
tests/unit/libexpr/local.mk \
tests/unit/libexpr-support/local.mk \
tests/unit/libflake/local.mk
endif

ifeq ($(ENABLE_FUNCTIONAL_TESTS), yes)
ifdef HOST_UNIX
makefiles += \
Expand Down Expand Up @@ -92,6 +104,13 @@ include mk/lib.mk
# These must be defined after `mk/lib.mk`. Otherwise the first rule
# incorrectly becomes the default target.

ifneq ($(ENABLE_UNIT_TESTS), yes)
.PHONY: check
check:
@echo "Unit tests are disabled. Configure without '--disable-unit-tests', or avoid calling 'make check'."
@exit 1
endif

ifneq ($(ENABLE_FUNCTIONAL_TESTS), yes)
.PHONY: installcheck
installcheck:
Expand Down
1 change: 1 addition & 0 deletions Makefile.config.in
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ENABLE_BUILD = @ENABLE_BUILD@
ENABLE_DOC_GEN = @ENABLE_DOC_GEN@
ENABLE_FUNCTIONAL_TESTS = @ENABLE_FUNCTIONAL_TESTS@
ENABLE_S3 = @ENABLE_S3@
ENABLE_UNIT_TESTS = @ENABLE_UNIT_TESTS@
GTEST_LIBS = @GTEST_LIBS@
HAVE_LIBCPUID = @HAVE_LIBCPUID@
HAVE_SECCOMP = @HAVE_SECCOMP@
Expand Down
22 changes: 22 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ AC_ARG_ENABLE(build, AS_HELP_STRING([--disable-build],[Do not build nix]),
ENABLE_BUILD=$enableval, ENABLE_BUILD=yes)
AC_SUBST(ENABLE_BUILD)

# Building without unit tests is useful for bootstrapping with a smaller footprint
# or running the tests in a separate derivation. Otherwise, we do compile and
# run them.

AC_ARG_ENABLE(unit-tests, AS_HELP_STRING([--disable-unit-tests],[Do not build the tests]),
ENABLE_UNIT_TESTS=$enableval, ENABLE_UNIT_TESTS=$ENABLE_BUILD)
AC_SUBST(ENABLE_UNIT_TESTS)

AS_IF(
[test "$ENABLE_BUILD" == "no" && test "$ENABLE_UNIT_TESTS" == "yes"],
[AC_MSG_ERROR([Cannot enable unit tests when building overall is disabled. Please do not pass '--enable-unit-tests' or do not pass '--disable-build'.])])

AC_ARG_ENABLE(functional-tests, AS_HELP_STRING([--disable-functional-tests],[Do not build the tests]),
ENABLE_FUNCTIONAL_TESTS=$enableval, ENABLE_FUNCTIONAL_TESTS=yes)
AC_SUBST(ENABLE_FUNCTIONAL_TESTS)
Expand Down Expand Up @@ -346,6 +358,16 @@ if test "$gc" = yes; then
CFLAGS="$old_CFLAGS"
fi
AS_IF([test "$ENABLE_UNIT_TESTS" == "yes"],[
# Look for gtest.
PKG_CHECK_MODULES([GTEST], [gtest_main gmock_main])
# Look for rapidcheck.
PKG_CHECK_MODULES([RAPIDCHECK], [rapidcheck rapidcheck_gtest])
])
# Look for nlohmann/json.
PKG_CHECK_MODULES([NLOHMANN_JSON], [nlohmann_json >= 3.9])
Expand Down
33 changes: 32 additions & 1 deletion package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
# Whether to build Nix. Useful to skip for tasks like testing existing pre-built versions of Nix
, doBuild ? true

# Run the unit tests as part of the build. See `installUnitTests` for an
# alternative to this.
, doCheck ? __forDefaults.canRunInstalled

# Run the functional tests as part of the build.
, doInstallCheck ? test-client != null || __forDefaults.canRunInstalled

Expand Down Expand Up @@ -85,6 +89,11 @@
# - readline
, readlineFlavor ? if stdenv.hostPlatform.isWindows then "readline" else "editline"

# Whether to install unit tests. This is useful when cross compiling
# since we cannot run them natively during the build, but can do so
# later.
, installUnitTests ? doBuild && !__forDefaults.canExecuteHost

# For running the functional tests against a pre-built Nix. Probably
# want to use in conjunction with `doBuild = false;`.
, test-daemon ? null
Expand All @@ -108,7 +117,7 @@ let
# things which should instead be gotten via `finalAttrs` in order to
# work with overriding.
attrs = {
inherit doBuild doInstallCheck;
inherit doBuild doCheck doInstallCheck;
};

mkDerivation =
Expand All @@ -124,11 +133,16 @@ in
mkDerivation (finalAttrs: let

inherit (finalAttrs)
doCheck
doInstallCheck
;

doBuild = !finalAttrs.dontBuild;

# Either running the unit tests during the build, or installing them
# to be run later, requiresthe unit tests to be built.
buildUnitTests = doCheck || installUnitTests;

in {
inherit pname version;

Expand Down Expand Up @@ -162,6 +176,8 @@ in {
./scripts/local.mk
] ++ lib.optionals enableManual [
./doc/manual
] ++ lib.optionals buildUnitTests [
./tests/unit
] ++ lib.optionals doInstallCheck [
./tests/functional
]));
Expand All @@ -174,6 +190,8 @@ in {
# If we are doing just build or just docs, the one thing will use
# "out". We only need additional outputs if we are doing both.
++ lib.optional (doBuild && enableManual) "doc"
++ lib.optional installUnitTests "check"
++ lib.optional doCheck "testresults"
;

nativeBuildInputs = [
Expand Down Expand Up @@ -212,6 +230,9 @@ in {
({ inherit readline editline; }.${readlineFlavor})
] ++ lib.optionals enableMarkdown [
lowdown
] ++ lib.optionals buildUnitTests [
gtest
rapidcheck
] ++ lib.optional stdenv.isLinux libseccomp
++ lib.optional stdenv.hostPlatform.isx86_64 libcpuid
# There have been issues building these dependencies
Expand All @@ -226,16 +247,22 @@ in {
);

dontBuild = !attrs.doBuild;
doCheck = attrs.doCheck;

configureFlags = [
(lib.enableFeature doBuild "build")
(lib.enableFeature buildUnitTests "unit-tests")
(lib.enableFeature doInstallCheck "functional-tests")
(lib.enableFeature enableManual "doc-gen")
(lib.enableFeature enableGC "gc")
(lib.enableFeature enableMarkdown "markdown")
(lib.enableFeature installUnitTests "install-unit-tests")
(lib.withFeatureAs true "readline-flavor" readlineFlavor)
] ++ lib.optionals (!forDevShell) [
"--sysconfdir=/etc"
] ++ lib.optionals installUnitTests [
"--with-check-bin-dir=${builtins.placeholder "check"}/bin"
"--with-check-lib-dir=${builtins.placeholder "check"}/lib"
] ++ lib.optionals (doBuild) [
"--with-boost=${boost}/lib"
] ++ lib.optionals (doBuild && stdenv.isLinux) [
Expand Down Expand Up @@ -316,6 +343,10 @@ in {
platforms = lib.platforms.unix ++ lib.platforms.windows;
mainProgram = "nix";
broken = !(lib.all (a: a) [
# We cannot run or install unit tests if we don't build them or
# Nix proper (which they depend on).
(installUnitTests -> doBuild)
(doCheck -> doBuild)
# The build process for the manual currently requires extracting
# data from the Nix executable we are trying to document.
(enableManual -> doBuild)
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/libexpr-support/local.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
libraries += libexpr-test-support

libexpr-test-support_NAME = libnixexpr-test-support

libexpr-test-support_DIR := $(d)

ifeq ($(INSTALL_UNIT_TESTS), yes)
libexpr-test-support_INSTALL_DIR := $(checklibdir)
else
libexpr-test-support_INSTALL_DIR :=
endif

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 := $(THREAD_LDFLAGS) -lrapidcheck
45 changes: 45 additions & 0 deletions tests/unit/libexpr/local.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
check: libexpr-tests_RUN

programs += libexpr-tests

libexpr-tests_NAME := libnixexpr-tests

libexpr-tests_ENV := _NIX_TEST_UNIT_DATA=$(d)/data GTEST_OUTPUT=xml:$$testresults/libexpr-tests.xml

libexpr-tests_DIR := $(d)

ifeq ($(INSTALL_UNIT_TESTS), yes)
libexpr-tests_INSTALL_DIR := $(checkbindir)
else
libexpr-tests_INSTALL_DIR :=
endif

libexpr-tests_SOURCES := \
$(wildcard $(d)/*.cc) \
$(wildcard $(d)/value/*.cc) \
$(wildcard $(d)/flake/*.cc)

libexpr-tests_EXTRA_INCLUDES = \
-I tests/unit/libexpr-support \
-I tests/unit/libstore-support \
-I tests/unit/libutil-support \
$(INCLUDE_libexpr) \
$(INCLUDE_libexprc) \
$(INCLUDE_libfetchers) \
$(INCLUDE_libstore) \
$(INCLUDE_libstorec) \
$(INCLUDE_libutil) \
$(INCLUDE_libutilc)

libexpr-tests_CXXFLAGS += $(libexpr-tests_EXTRA_INCLUDES)

libexpr-tests_LIBS = \
libexpr-test-support libstore-test-support libutil-test-support \
libexpr libexprc libfetchers libstore libstorec libutil libutilc

libexpr-tests_LDFLAGS := -lrapidcheck $(GTEST_LIBS) -lgmock

ifdef HOST_WINDOWS
# Increase the default reserved stack size to 65 MB so Nix doesn't run out of space
libexpr-tests_LDFLAGS += -Wl,--stack,$(shell echo $$((65 * 1024 * 1024)))
endif
37 changes: 37 additions & 0 deletions tests/unit/libfetchers/local.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
check: libfetchers-tests_RUN

programs += libfetchers-tests

libfetchers-tests_NAME = libnixfetchers-tests

libfetchers-tests_ENV := _NIX_TEST_UNIT_DATA=$(d)/data GTEST_OUTPUT=xml:$$testresults/libfetchers-tests.xml

libfetchers-tests_DIR := $(d)

ifeq ($(INSTALL_UNIT_TESTS), yes)
libfetchers-tests_INSTALL_DIR := $(checkbindir)
else
libfetchers-tests_INSTALL_DIR :=
endif

libfetchers-tests_SOURCES := $(wildcard $(d)/*.cc)

libfetchers-tests_EXTRA_INCLUDES = \
-I tests/unit/libstore-support \
-I tests/unit/libutil-support \
$(INCLUDE_libfetchers) \
$(INCLUDE_libstore) \
$(INCLUDE_libutil)

libfetchers-tests_CXXFLAGS += $(libfetchers-tests_EXTRA_INCLUDES)

libfetchers-tests_LIBS = \
libstore-test-support libutil-test-support \
libfetchers libstore libutil

libfetchers-tests_LDFLAGS := -lrapidcheck $(GTEST_LIBS) $(LIBGIT2_LIBS)

ifdef HOST_WINDOWS
# Increase the default reserved stack size to 65 MB so Nix doesn't run out of space
libfetchers-tests_LDFLAGS += -Wl,--stack,$(shell echo $$((65 * 1024 * 1024)))
endif
43 changes: 43 additions & 0 deletions tests/unit/libflake/local.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
check: libflake-tests_RUN

programs += libflake-tests

libflake-tests_NAME := libnixflake-tests

libflake-tests_ENV := _NIX_TEST_UNIT_DATA=$(d)/data GTEST_OUTPUT=xml:$$testresults/libflake-tests.xml

libflake-tests_DIR := $(d)

ifeq ($(INSTALL_UNIT_TESTS), yes)
libflake-tests_INSTALL_DIR := $(checkbindir)
else
libflake-tests_INSTALL_DIR :=
endif

libflake-tests_SOURCES := \
$(wildcard $(d)/*.cc) \
$(wildcard $(d)/value/*.cc) \
$(wildcard $(d)/flake/*.cc)

libflake-tests_EXTRA_INCLUDES = \
-I tests/unit/libflake-support \
-I tests/unit/libstore-support \
-I tests/unit/libutil-support \
$(INCLUDE_libflake) \
$(INCLUDE_libexpr) \
$(INCLUDE_libfetchers) \
$(INCLUDE_libstore) \
$(INCLUDE_libutil) \

libflake-tests_CXXFLAGS += $(libflake-tests_EXTRA_INCLUDES)

libflake-tests_LIBS = \
libexpr-test-support libstore-test-support libutil-test-support \
libflake libexpr libfetchers libstore libutil

libflake-tests_LDFLAGS := -lrapidcheck $(GTEST_LIBS) -lgmock

ifdef HOST_WINDOWS
# Increase the default reserved stack size to 65 MB so Nix doesn't run out of space
libflake-tests_LDFLAGS += -Wl,--stack,$(shell echo $$((65 * 1024 * 1024)))
endif
21 changes: 21 additions & 0 deletions tests/unit/libstore-support/local.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
libraries += libstore-test-support

libstore-test-support_NAME = libnixstore-test-support

libstore-test-support_DIR := $(d)

ifeq ($(INSTALL_UNIT_TESTS), yes)
libstore-test-support_INSTALL_DIR := $(checklibdir)
else
libstore-test-support_INSTALL_DIR :=
endif

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 := $(THREAD_LDFLAGS) -lrapidcheck
Loading

0 comments on commit aeabe68

Please sign in to comment.