Skip to content

Commit

Permalink
Merge pull request #7713 from obsidiansystems/more-rapid-check
Browse files Browse the repository at this point in the history
Add more property tests
  • Loading branch information
roberth authored Jan 30, 2023
2 parents a31d7d4 + 560142f commit c9b9260
Show file tree
Hide file tree
Showing 23 changed files with 381 additions and 43 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ perl/Makefile.config
/src/libexpr/parser-tab.hh
/src/libexpr/parser-tab.output
/src/libexpr/nix.tbl
/src/libexpr/tests/libexpr-tests
/src/libexpr/tests/libnixexpr-tests

# /src/libstore/
*.gen.*
/src/libstore/tests/libstore-tests
/src/libstore/tests/libnixstore-tests

# /src/libutil/
/src/libutil/tests/libutil-tests
/src/libutil/tests/libnixutil-tests

/src/nix/nix

Expand Down
8 changes: 6 additions & 2 deletions mk/programs.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ programs-list :=
# Build a program with symbolic name $(1). The program is defined by
# various variables prefixed by ‘$(1)_’:
#
# - $(1)_NAME: the name of the program (e.g. ‘foo’); defaults to
# $(1).
#
# - $(1)_DIR: the directory where the (non-installed) program will be
# placed.
#
Expand All @@ -23,11 +26,12 @@ programs-list :=
# - $(1)_INSTALL_DIR: the directory where the program will be
# installed; defaults to $(bindir).
define build-program
$(1)_NAME ?= $(1)
_d := $(buildprefix)$$($(1)_DIR)
_srcs := $$(sort $$(foreach src, $$($(1)_SOURCES), $$(src)))
$(1)_OBJS := $$(addprefix $(buildprefix), $$(addsuffix .o, $$(basename $$(_srcs))))
_libs := $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_PATH))
$(1)_PATH := $$(_d)/$(1)
$(1)_PATH := $$(_d)/$$($(1)_NAME)

$$(eval $$(call create-dir, $$(_d)))

Expand All @@ -38,7 +42,7 @@ define build-program

ifdef $(1)_INSTALL_DIR

$(1)_INSTALL_PATH := $$($(1)_INSTALL_DIR)/$(1)
$(1)_INSTALL_PATH := $$($(1)_INSTALL_DIR)/$$($(1)_NAME)

$$(eval $$(call create-dir, $$($(1)_INSTALL_DIR)))

Expand Down
2 changes: 1 addition & 1 deletion src/libexpr/tests/error_traces.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include "libexprtests.hh"
#include "tests/libexpr.hh"

namespace nix {

Expand Down
2 changes: 1 addition & 1 deletion src/libexpr/tests/json.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "libexprtests.hh"
#include "tests/libexpr.hh"
#include "value-to-json.hh"

namespace nix {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@
#include "eval-inline.hh"
#include "store-api.hh"

#include "tests/libstore.hh"

namespace nix {
class LibExprTest : public ::testing::Test {
class LibExprTest : public LibStoreTest {
public:
static void SetUpTestSuite() {
initLibStore();
LibStoreTest::SetUpTestSuite();
initGC();
}

protected:
LibExprTest()
: store(openStore("dummy://"))
: LibStoreTest()
, state({}, store)
{
}
Expand All @@ -36,7 +37,6 @@ namespace nix {
return state.symbols.create(value);
}

ref<Store> store;
EvalState state;
};

Expand Down
4 changes: 3 additions & 1 deletion src/libexpr/tests/local.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ check: libexpr-tests_RUN

programs += libexpr-tests

libexpr-tests_NAME := libnixexpr-tests

libexpr-tests_DIR := $(d)

libexpr-tests_INSTALL_DIR :=
Expand All @@ -12,6 +14,6 @@ libexpr-tests_SOURCES := \

libexpr-tests_CXXFLAGS += -I src/libexpr -I src/libutil -I src/libstore -I src/libexpr/tests

libexpr-tests_LIBS = libexpr libutil libstore libfetchers
libexpr-tests_LIBS = libstore-tests libutils-tests libexpr libutil libstore libfetchers

libexpr-tests_LDFLAGS := $(GTEST_LIBS) -lgmock
2 changes: 1 addition & 1 deletion src/libexpr/tests/primops.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include "libexprtests.hh"
#include "tests/libexpr.hh"

namespace nix {
class CaptureLogger : public Logger
Expand Down
2 changes: 1 addition & 1 deletion src/libexpr/tests/trivial.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "libexprtests.hh"
#include "tests/libexpr.hh"

namespace nix {
// Testing of trivial expressions
Expand Down
59 changes: 57 additions & 2 deletions src/libexpr/tests/value/context.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#include "value/context.hh"
#include <nlohmann/json.hpp>
#include <gtest/gtest.h>
#include <rapidcheck/gtest.h>

#include "libexprtests.hh"
#include "tests/path.hh"
#include "tests/libexpr.hh"
#include "tests/value/context.hh"

namespace nix {

Expand Down Expand Up @@ -70,3 +74,54 @@ TEST_F(NixStringContextElemTest, built) {
}

}

namespace rc {
using namespace nix;

Gen<NixStringContextElem::Opaque> Arbitrary<NixStringContextElem::Opaque>::arbitrary()
{
return gen::just(NixStringContextElem::Opaque {
.path = *gen::arbitrary<StorePath>(),
});
}

Gen<NixStringContextElem::DrvDeep> Arbitrary<NixStringContextElem::DrvDeep>::arbitrary()
{
return gen::just(NixStringContextElem::DrvDeep {
.drvPath = *gen::arbitrary<StorePath>(),
});
}

Gen<NixStringContextElem::Built> Arbitrary<NixStringContextElem::Built>::arbitrary()
{
return gen::just(NixStringContextElem::Built {
.drvPath = *gen::arbitrary<StorePath>(),
.output = (*gen::arbitrary<StorePathName>()).name,
});
}

Gen<NixStringContextElem> Arbitrary<NixStringContextElem>::arbitrary()
{
switch (*gen::inRange<uint8_t>(0, 2)) {
case 0:
return gen::just<NixStringContextElem>(*gen::arbitrary<NixStringContextElem::Opaque>());
case 1:
return gen::just<NixStringContextElem>(*gen::arbitrary<NixStringContextElem::DrvDeep>());
default:
return gen::just<NixStringContextElem>(*gen::arbitrary<NixStringContextElem::Built>());
}
}

}

namespace nix {

RC_GTEST_FIXTURE_PROP(
NixStringContextElemTest,
prop_round_rip,
(const NixStringContextElem & o))
{
RC_ASSERT(o == NixStringContextElem::parse(store(), o.to_string(store())));
}

}
30 changes: 30 additions & 0 deletions src/libexpr/tests/value/context.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include <rapidcheck/gen/Arbitrary.h>

#include <value/context.hh>

namespace rc {
using namespace nix;

template<>
struct Arbitrary<NixStringContextElem::Opaque> {
static Gen<NixStringContextElem::Opaque> arbitrary();
};

template<>
struct Arbitrary<NixStringContextElem::Built> {
static Gen<NixStringContextElem::Built> arbitrary();
};

template<>
struct Arbitrary<NixStringContextElem::DrvDeep> {
static Gen<NixStringContextElem::DrvDeep> arbitrary();
};

template<>
struct Arbitrary<NixStringContextElem> {
static Gen<NixStringContextElem> arbitrary();
};

}
17 changes: 12 additions & 5 deletions src/libexpr/value/context.hh
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#pragma once

#include "util.hh"
#include "comparator.hh"
#include "path.hh"

#include <optional>
#include <variant>

#include <nlohmann/json_fwd.hpp>

Expand Down Expand Up @@ -31,7 +32,9 @@ class Store;
Encoded as just the path: ‘<path>’.
*/
struct NixStringContextElem_Opaque {
StorePath path;
StorePath path;

GENERATE_CMP(NixStringContextElem_Opaque, me->path);
};

/* Path to a derivation and its entire build closure.
Expand All @@ -43,16 +46,20 @@ struct NixStringContextElem_Opaque {
Encoded in the form ‘=<drvPath>’.
*/
struct NixStringContextElem_DrvDeep {
StorePath drvPath;
StorePath drvPath;

GENERATE_CMP(NixStringContextElem_DrvDeep, me->drvPath);
};

/* Derivation output.
Encoded in the form ‘!<output>!<drvPath>’.
*/
struct NixStringContextElem_Built {
StorePath drvPath;
std::string output;
StorePath drvPath;
std::string output;

GENERATE_CMP(NixStringContextElem_Built, me->drvPath, me->output);
};

using _NixStringContextElem_Raw = std::variant<
Expand Down
11 changes: 6 additions & 5 deletions src/libstore/derived-path.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
#include "path.hh"
#include "realisation.hh"
#include "outputs-spec.hh"
#include "comparator.hh"

#include <optional>
#include <variant>

#include <nlohmann/json_fwd.hpp>

Expand All @@ -27,8 +28,7 @@ struct DerivedPathOpaque {
std::string to_string(const Store & store) const;
static DerivedPathOpaque parse(const Store & store, std::string_view);

bool operator < (const DerivedPathOpaque & b) const
{ return path < b.path; }
GENERATE_CMP(DerivedPathOpaque, me->path);
};

/**
Expand All @@ -51,8 +51,7 @@ struct DerivedPathBuilt {
static DerivedPathBuilt parse(const Store & store, std::string_view, std::string_view);
nlohmann::json toJSON(ref<Store> store) const;

bool operator < (const DerivedPathBuilt & b) const
{ return std::make_pair(drvPath, outputs) < std::make_pair(b.drvPath, b.outputs); }
GENERATE_CMP(DerivedPathBuilt, me->drvPath, me->outputs);
};

using _DerivedPathRaw = std::variant<
Expand Down Expand Up @@ -96,6 +95,8 @@ struct BuiltPathBuilt {

nlohmann::json toJSON(ref<Store> store) const;
static BuiltPathBuilt parse(const Store & store, std::string_view);

GENERATE_CMP(BuiltPathBuilt, me->drvPath, me->outputs);
};

using _BuiltPathRaw = std::variant<
Expand Down
62 changes: 62 additions & 0 deletions src/libstore/tests/derived-path.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#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<DerivedPath::Built> Arbitrary<DerivedPath::Built>::arbitrary()
{
return gen::just(DerivedPath::Built {
.drvPath = *gen::arbitrary<StorePath>(),
.outputs = *gen::arbitrary<OutputsSpec>(),
});
}

Gen<DerivedPath> Arbitrary<DerivedPath>::arbitrary()
{
switch (*gen::inRange<uint8_t>(0, 1)) {
case 0:
return gen::just<DerivedPath>(*gen::arbitrary<DerivedPath::Opaque>());
default:
return gen::just<DerivedPath>(*gen::arbitrary<DerivedPath::Built>());
}
}

}

namespace nix {

class DerivedPathTest : public LibStoreTest
{
};

// FIXME: `RC_GTEST_FIXTURE_PROP` isn't calling `SetUpTestSuite` because it is
// no a real fixture.
//
// See https://github.com/emil-e/rapidcheck/blob/master/doc/gtest.md#rc_gtest_fixture_propfixture-name-args
TEST_F(DerivedPathTest, force_init)
{
}

RC_GTEST_FIXTURE_PROP(
DerivedPathTest,
prop_round_rip,
(const DerivedPath & o))
{
RC_ASSERT(o == DerivedPath::parse(*store, o.to_string(*store)));
}

}
Loading

0 comments on commit c9b9260

Please sign in to comment.