diff options
author | Shea Levy <shea@shealevy.com> | 2024-01-11 07:21:16 -0500 |
---|---|---|
committer | Shea Levy <shea@shealevy.com> | 2024-01-11 07:21:16 -0500 |
commit | ba48ab4b954dd1c8af388d1c5a33bbd62373c6f5 (patch) | |
tree | f17f8492bed32f78c04d9d4e9ffd2b672af85943 /src/libutil/tests | |
parent | e7c2b35827e9f4ddbec4248c5cf1ad793a2988ad (diff) | |
parent | 4dd5171652018e29bf9e496522df3be51d615a2c (diff) |
Merge branch '2.18-maintenance' into ifd-buildStore-2.18
Diffstat (limited to 'src/libutil/tests')
-rw-r--r-- | src/libutil/tests/canon-path.cc | 162 | ||||
-rw-r--r-- | src/libutil/tests/chunked-vector.cc | 54 | ||||
-rw-r--r-- | src/libutil/tests/closure.cc | 70 | ||||
-rw-r--r-- | src/libutil/tests/compression.cc | 96 | ||||
-rw-r--r-- | src/libutil/tests/config.cc | 295 | ||||
-rw-r--r-- | src/libutil/tests/git.cc | 33 | ||||
-rw-r--r-- | src/libutil/tests/hash.cc | 95 | ||||
-rw-r--r-- | src/libutil/tests/hash.hh | 16 | ||||
-rw-r--r-- | src/libutil/tests/hilite.cc | 66 | ||||
-rw-r--r-- | src/libutil/tests/local.mk | 29 | ||||
-rw-r--r-- | src/libutil/tests/logging.cc | 370 | ||||
-rw-r--r-- | src/libutil/tests/lru-cache.cc | 130 | ||||
-rw-r--r-- | src/libutil/tests/pool.cc | 127 | ||||
-rw-r--r-- | src/libutil/tests/references.cc | 46 | ||||
-rw-r--r-- | src/libutil/tests/suggestions.cc | 43 | ||||
-rw-r--r-- | src/libutil/tests/tests.cc | 659 | ||||
-rw-r--r-- | src/libutil/tests/url.cc | 338 | ||||
-rw-r--r-- | src/libutil/tests/xml-writer.cc | 105 |
18 files changed, 0 insertions, 2734 deletions
diff --git a/src/libutil/tests/canon-path.cc b/src/libutil/tests/canon-path.cc deleted file mode 100644 index fc94ccc3d..000000000 --- a/src/libutil/tests/canon-path.cc +++ /dev/null @@ -1,162 +0,0 @@ -#include "canon-path.hh" - -#include <gtest/gtest.h> - -namespace nix { - - TEST(CanonPath, basic) { - { - CanonPath p("/"); - ASSERT_EQ(p.abs(), "/"); - ASSERT_EQ(p.rel(), ""); - ASSERT_EQ(p.baseName(), std::nullopt); - ASSERT_EQ(p.dirOf(), std::nullopt); - ASSERT_FALSE(p.parent()); - } - - { - CanonPath p("/foo//"); - ASSERT_EQ(p.abs(), "/foo"); - ASSERT_EQ(p.rel(), "foo"); - ASSERT_EQ(*p.baseName(), "foo"); - ASSERT_EQ(*p.dirOf(), ""); // FIXME: do we want this? - ASSERT_EQ(p.parent()->abs(), "/"); - } - - { - CanonPath p("foo/bar"); - ASSERT_EQ(p.abs(), "/foo/bar"); - ASSERT_EQ(p.rel(), "foo/bar"); - ASSERT_EQ(*p.baseName(), "bar"); - ASSERT_EQ(*p.dirOf(), "/foo"); - ASSERT_EQ(p.parent()->abs(), "/foo"); - } - - { - CanonPath p("foo//bar/"); - ASSERT_EQ(p.abs(), "/foo/bar"); - ASSERT_EQ(p.rel(), "foo/bar"); - ASSERT_EQ(*p.baseName(), "bar"); - ASSERT_EQ(*p.dirOf(), "/foo"); - } - } - - TEST(CanonPath, pop) { - CanonPath p("foo/bar/x"); - ASSERT_EQ(p.abs(), "/foo/bar/x"); - p.pop(); - ASSERT_EQ(p.abs(), "/foo/bar"); - p.pop(); - ASSERT_EQ(p.abs(), "/foo"); - p.pop(); - ASSERT_EQ(p.abs(), "/"); - } - - TEST(CanonPath, removePrefix) { - CanonPath p1("foo/bar"); - CanonPath p2("foo/bar/a/b/c"); - ASSERT_EQ(p2.removePrefix(p1).abs(), "/a/b/c"); - ASSERT_EQ(p1.removePrefix(p1).abs(), "/"); - ASSERT_EQ(p1.removePrefix(CanonPath("/")).abs(), "/foo/bar"); - } - - TEST(CanonPath, iter) { - { - CanonPath p("a//foo/bar//"); - std::vector<std::string_view> ss; - for (auto & c : p) ss.push_back(c); - ASSERT_EQ(ss, std::vector<std::string_view>({"a", "foo", "bar"})); - } - - { - CanonPath p("/"); - std::vector<std::string_view> ss; - for (auto & c : p) ss.push_back(c); - ASSERT_EQ(ss, std::vector<std::string_view>()); - } - } - - TEST(CanonPath, concat) { - { - CanonPath p1("a//foo/bar//"); - CanonPath p2("xyzzy/bla"); - ASSERT_EQ((p1 + p2).abs(), "/a/foo/bar/xyzzy/bla"); - } - - { - CanonPath p1("/"); - CanonPath p2("/a/b"); - ASSERT_EQ((p1 + p2).abs(), "/a/b"); - } - - { - CanonPath p1("/a/b"); - CanonPath p2("/"); - ASSERT_EQ((p1 + p2).abs(), "/a/b"); - } - - { - CanonPath p("/foo/bar"); - ASSERT_EQ((p + "x").abs(), "/foo/bar/x"); - } - - { - CanonPath p("/"); - ASSERT_EQ((p + "foo" + "bar").abs(), "/foo/bar"); - } - } - - TEST(CanonPath, within) { - ASSERT_TRUE(CanonPath("foo").isWithin(CanonPath("foo"))); - ASSERT_FALSE(CanonPath("foo").isWithin(CanonPath("bar"))); - ASSERT_FALSE(CanonPath("foo").isWithin(CanonPath("fo"))); - ASSERT_TRUE(CanonPath("foo/bar").isWithin(CanonPath("foo"))); - ASSERT_FALSE(CanonPath("foo").isWithin(CanonPath("foo/bar"))); - ASSERT_TRUE(CanonPath("/foo/bar/default.nix").isWithin(CanonPath("/"))); - ASSERT_TRUE(CanonPath("/").isWithin(CanonPath("/"))); - } - - TEST(CanonPath, sort) { - ASSERT_FALSE(CanonPath("foo") < CanonPath("foo")); - ASSERT_TRUE (CanonPath("foo") < CanonPath("foo/bar")); - ASSERT_TRUE (CanonPath("foo/bar") < CanonPath("foo!")); - ASSERT_FALSE(CanonPath("foo!") < CanonPath("foo")); - ASSERT_TRUE (CanonPath("foo") < CanonPath("foo!")); - } - - TEST(CanonPath, allowed) { - std::set<CanonPath> allowed { - CanonPath("foo/bar"), - CanonPath("foo!"), - CanonPath("xyzzy"), - CanonPath("a/b/c"), - }; - - ASSERT_TRUE (CanonPath("foo/bar").isAllowed(allowed)); - ASSERT_TRUE (CanonPath("foo/bar/bla").isAllowed(allowed)); - ASSERT_TRUE (CanonPath("foo").isAllowed(allowed)); - ASSERT_FALSE(CanonPath("bar").isAllowed(allowed)); - ASSERT_FALSE(CanonPath("bar/a").isAllowed(allowed)); - ASSERT_TRUE (CanonPath("a").isAllowed(allowed)); - ASSERT_TRUE (CanonPath("a/b").isAllowed(allowed)); - ASSERT_TRUE (CanonPath("a/b/c").isAllowed(allowed)); - ASSERT_TRUE (CanonPath("a/b/c/d").isAllowed(allowed)); - ASSERT_TRUE (CanonPath("a/b/c/d/e").isAllowed(allowed)); - ASSERT_FALSE(CanonPath("a/b/a").isAllowed(allowed)); - ASSERT_FALSE(CanonPath("a/b/d").isAllowed(allowed)); - ASSERT_FALSE(CanonPath("aaa").isAllowed(allowed)); - ASSERT_FALSE(CanonPath("zzz").isAllowed(allowed)); - ASSERT_TRUE (CanonPath("/").isAllowed(allowed)); - } - - TEST(CanonPath, makeRelative) { - CanonPath d("/foo/bar"); - ASSERT_EQ(d.makeRelative(CanonPath("/foo/bar")), "."); - ASSERT_EQ(d.makeRelative(CanonPath("/foo")), ".."); - ASSERT_EQ(d.makeRelative(CanonPath("/")), "../.."); - ASSERT_EQ(d.makeRelative(CanonPath("/foo/bar/xyzzy")), "xyzzy"); - ASSERT_EQ(d.makeRelative(CanonPath("/foo/bar/xyzzy/bla")), "xyzzy/bla"); - ASSERT_EQ(d.makeRelative(CanonPath("/foo/xyzzy/bla")), "../xyzzy/bla"); - ASSERT_EQ(d.makeRelative(CanonPath("/xyzzy/bla")), "../../xyzzy/bla"); - } -} diff --git a/src/libutil/tests/chunked-vector.cc b/src/libutil/tests/chunked-vector.cc deleted file mode 100644 index 868d11f6f..000000000 --- a/src/libutil/tests/chunked-vector.cc +++ /dev/null @@ -1,54 +0,0 @@ -#include "chunked-vector.hh" - -#include <gtest/gtest.h> - -namespace nix { - TEST(ChunkedVector, InitEmpty) { - auto v = ChunkedVector<int, 2>(100); - ASSERT_EQ(v.size(), 0); - } - - TEST(ChunkedVector, GrowsCorrectly) { - auto v = ChunkedVector<int, 2>(100); - for (auto i = 1; i < 20; i++) { - v.add(i); - ASSERT_EQ(v.size(), i); - } - } - - TEST(ChunkedVector, AddAndGet) { - auto v = ChunkedVector<int, 2>(100); - for (auto i = 1; i < 20; i++) { - auto [i2, idx] = v.add(i); - auto & i3 = v[idx]; - ASSERT_EQ(i, i2); - ASSERT_EQ(&i2, &i3); - } - } - - TEST(ChunkedVector, ForEach) { - auto v = ChunkedVector<int, 2>(100); - for (auto i = 1; i < 20; i++) { - v.add(i); - } - int count = 0; - v.forEach([&count](int elt) { - count++; - }); - ASSERT_EQ(count, v.size()); - } - - TEST(ChunkedVector, OverflowOK) { - // Similar to the AddAndGet, but intentionnally use a small - // initial ChunkedVector to force it to overflow - auto v = ChunkedVector<int, 2>(2); - for (auto i = 1; i < 20; i++) { - auto [i2, idx] = v.add(i); - auto & i3 = v[idx]; - ASSERT_EQ(i, i2); - ASSERT_EQ(&i2, &i3); - } - } - -} - diff --git a/src/libutil/tests/closure.cc b/src/libutil/tests/closure.cc deleted file mode 100644 index 7597e7807..000000000 --- a/src/libutil/tests/closure.cc +++ /dev/null @@ -1,70 +0,0 @@ -#include "closure.hh" -#include <gtest/gtest.h> - -namespace nix { - -using namespace std; - -map<string, set<string>> testGraph = { - { "A", { "B", "C", "G" } }, - { "B", { "A" } }, // Loops back to A - { "C", { "F" } }, // Indirect reference - { "D", { "A" } }, // Not reachable, but has backreferences - { "E", {} }, // Just not reachable - { "F", {} }, - { "G", { "G" } }, // Self reference -}; - -TEST(closure, correctClosure) { - set<string> aClosure; - set<string> expectedClosure = {"A", "B", "C", "F", "G"}; - computeClosure<string>( - {"A"}, - aClosure, - [&](const string currentNode, function<void(promise<set<string>> &)> processEdges) { - promise<set<string>> promisedNodes; - promisedNodes.set_value(testGraph[currentNode]); - processEdges(promisedNodes); - } - ); - - ASSERT_EQ(aClosure, expectedClosure); -} - -TEST(closure, properlyHandlesDirectExceptions) { - struct TestExn {}; - set<string> aClosure; - EXPECT_THROW( - computeClosure<string>( - {"A"}, - aClosure, - [&](const string currentNode, function<void(promise<set<string>> &)> processEdges) { - throw TestExn(); - } - ), - TestExn - ); -} - -TEST(closure, properlyHandlesExceptionsInPromise) { - struct TestExn {}; - set<string> aClosure; - EXPECT_THROW( - computeClosure<string>( - {"A"}, - aClosure, - [&](const string currentNode, function<void(promise<set<string>> &)> processEdges) { - promise<set<string>> promise; - try { - throw TestExn(); - } catch (...) { - promise.set_exception(std::current_exception()); - } - processEdges(promise); - } - ), - TestExn - ); -} - -} diff --git a/src/libutil/tests/compression.cc b/src/libutil/tests/compression.cc deleted file mode 100644 index bbbf3500f..000000000 --- a/src/libutil/tests/compression.cc +++ /dev/null @@ -1,96 +0,0 @@ -#include "compression.hh" -#include <gtest/gtest.h> - -namespace nix { - - /* ---------------------------------------------------------------------------- - * compress / decompress - * --------------------------------------------------------------------------*/ - - TEST(compress, compressWithUnknownMethod) { - ASSERT_THROW(compress("invalid-method", "something-to-compress"), UnknownCompressionMethod); - } - - TEST(compress, noneMethodDoesNothingToTheInput) { - auto o = compress("none", "this-is-a-test"); - - ASSERT_EQ(o, "this-is-a-test"); - } - - TEST(decompress, decompressNoneCompressed) { - auto method = "none"; - auto str = "slfja;sljfklsa;jfklsjfkl;sdjfkl;sadjfkl;sdjf;lsdfjsadlf"; - auto o = decompress(method, str); - - ASSERT_EQ(o, str); - } - - TEST(decompress, decompressEmptyCompressed) { - // Empty-method decompression used e.g. by S3 store - // (Content-Encoding == ""). - auto method = ""; - auto str = "slfja;sljfklsa;jfklsjfkl;sdjfkl;sadjfkl;sdjf;lsdfjsadlf"; - auto o = decompress(method, str); - - ASSERT_EQ(o, str); - } - - TEST(decompress, decompressXzCompressed) { - auto method = "xz"; - auto str = "slfja;sljfklsa;jfklsjfkl;sdjfkl;sadjfkl;sdjf;lsdfjsadlf"; - auto o = decompress(method, compress(method, str)); - - ASSERT_EQ(o, str); - } - - TEST(decompress, decompressBzip2Compressed) { - auto method = "bzip2"; - auto str = "slfja;sljfklsa;jfklsjfkl;sdjfkl;sadjfkl;sdjf;lsdfjsadlf"; - auto o = decompress(method, compress(method, str)); - - ASSERT_EQ(o, str); - } - - TEST(decompress, decompressBrCompressed) { - auto method = "br"; - auto str = "slfja;sljfklsa;jfklsjfkl;sdjfkl;sadjfkl;sdjf;lsdfjsadlf"; - auto o = decompress(method, compress(method, str)); - - ASSERT_EQ(o, str); - } - - TEST(decompress, decompressInvalidInputThrowsCompressionError) { - auto method = "bzip2"; - auto str = "this is a string that does not qualify as valid bzip2 data"; - - ASSERT_THROW(decompress(method, str), CompressionError); - } - - /* ---------------------------------------------------------------------------- - * compression sinks - * --------------------------------------------------------------------------*/ - - TEST(makeCompressionSink, noneSinkDoesNothingToInput) { - StringSink strSink; - auto inputString = "slfja;sljfklsa;jfklsjfkl;sdjfkl;sadjfkl;sdjf;lsdfjsadlf"; - auto sink = makeCompressionSink("none", strSink); - (*sink)(inputString); - sink->finish(); - - ASSERT_STREQ(strSink.s.c_str(), inputString); - } - - TEST(makeCompressionSink, compressAndDecompress) { - StringSink strSink; - auto inputString = "slfja;sljfklsa;jfklsjfkl;sdjfkl;sadjfkl;sdjf;lsdfjsadlf"; - auto decompressionSink = makeDecompressionSink("bzip2", strSink); - auto sink = makeCompressionSink("bzip2", *decompressionSink); - - (*sink)(inputString); - sink->finish(); - decompressionSink->finish(); - - ASSERT_STREQ(strSink.s.c_str(), inputString); - } - -} diff --git a/src/libutil/tests/config.cc b/src/libutil/tests/config.cc deleted file mode 100644 index 886e70da5..000000000 --- a/src/libutil/tests/config.cc +++ /dev/null @@ -1,295 +0,0 @@ -#include "config.hh" -#include "args.hh" - -#include <sstream> -#include <gtest/gtest.h> -#include <nlohmann/json.hpp> - -namespace nix { - - /* ---------------------------------------------------------------------------- - * Config - * --------------------------------------------------------------------------*/ - - TEST(Config, setUndefinedSetting) { - Config config; - ASSERT_EQ(config.set("undefined-key", "value"), false); - } - - TEST(Config, setDefinedSetting) { - Config config; - std::string value; - Setting<std::string> foo{&config, value, "name-of-the-setting", "description"}; - ASSERT_EQ(config.set("name-of-the-setting", "value"), true); - } - - TEST(Config, getDefinedSetting) { - Config config; - std::string value; - std::map<std::string, Config::SettingInfo> settings; - Setting<std::string> foo{&config, value, "name-of-the-setting", "description"}; - - config.getSettings(settings, /* overriddenOnly = */ false); - const auto iter = settings.find("name-of-the-setting"); - ASSERT_NE(iter, settings.end()); - ASSERT_EQ(iter->second.value, ""); - ASSERT_EQ(iter->second.description, "description\n"); - } - - TEST(Config, getDefinedOverriddenSettingNotSet) { - Config config; - std::string value; - std::map<std::string, Config::SettingInfo> settings; - Setting<std::string> foo{&config, value, "name-of-the-setting", "description"}; - - config.getSettings(settings, /* overriddenOnly = */ true); - const auto e = settings.find("name-of-the-setting"); - ASSERT_EQ(e, settings.end()); - } - - TEST(Config, getDefinedSettingSet1) { - Config config; - std::string value; - std::map<std::string, Config::SettingInfo> settings; - Setting<std::string> setting{&config, value, "name-of-the-setting", "description"}; - - setting.assign("value"); - - config.getSettings(settings, /* overriddenOnly = */ false); - const auto iter = settings.find("name-of-the-setting"); - ASSERT_NE(iter, settings.end()); - ASSERT_EQ(iter->second.value, "value"); - ASSERT_EQ(iter->second.description, "description\n"); - } - - TEST(Config, getDefinedSettingSet2) { - Config config; - std::map<std::string, Config::SettingInfo> settings; - Setting<std::string> setting{&config, "", "name-of-the-setting", "description"}; - - ASSERT_TRUE(config.set("name-of-the-setting", "value")); - - config.getSettings(settings, /* overriddenOnly = */ false); - const auto e = settings.find("name-of-the-setting"); - ASSERT_NE(e, settings.end()); - ASSERT_EQ(e->second.value, "value"); - ASSERT_EQ(e->second.description, "description\n"); - } - - TEST(Config, addSetting) { - class TestSetting : public AbstractSetting { - public: - TestSetting() : AbstractSetting("test", "test", {}) {} - void set(const std::string & value, bool append) override {} - std::string to_string() const override { return {}; } - bool isAppendable() override { return false; } - }; - - Config config; - TestSetting setting; - - ASSERT_FALSE(config.set("test", "value")); - config.addSetting(&setting); - ASSERT_TRUE(config.set("test", "value")); - ASSERT_FALSE(config.set("extra-test", "value")); - } - - TEST(Config, withInitialValue) { - const StringMap initials = { - { "key", "value" }, - }; - Config config(initials); - - { - std::map<std::string, Config::SettingInfo> settings; - config.getSettings(settings, /* overriddenOnly = */ false); - ASSERT_EQ(settings.find("key"), settings.end()); - } - - Setting<std::string> setting{&config, "default-value", "key", "description"}; - - { - std::map<std::string, Config::SettingInfo> settings; - config.getSettings(settings, /* overriddenOnly = */ false); - ASSERT_EQ(settings["key"].value, "value"); - } - } - - TEST(Config, resetOverridden) { - Config config; - config.resetOverridden(); - } - - TEST(Config, resetOverriddenWithSetting) { - Config config; - Setting<std::string> setting{&config, "", "name-of-the-setting", "description"}; - - { - std::map<std::string, Config::SettingInfo> settings; - - setting.set("foo"); - ASSERT_EQ(setting.get(), "foo"); - config.getSettings(settings, /* overriddenOnly = */ true); - ASSERT_TRUE(settings.empty()); - } - - { - std::map<std::string, Config::SettingInfo> settings; - - setting.override("bar"); - ASSERT_TRUE(setting.overridden); - ASSERT_EQ(setting.get(), "bar"); - config.getSettings(settings, /* overriddenOnly = */ true); - ASSERT_FALSE(settings.empty()); - } - - { - std::map<std::string, Config::SettingInfo> settings; - - config.resetOverridden(); - ASSERT_FALSE(setting.overridden); - config.getSettings(settings, /* overriddenOnly = */ true); - ASSERT_TRUE(settings.empty()); - } - } - - TEST(Config, toJSONOnEmptyConfig) { - ASSERT_EQ(Config().toJSON().dump(), "{}"); - } - - TEST(Config, toJSONOnNonEmptyConfig) { - using nlohmann::literals::operator "" _json; - Config config; - Setting<std::string> setting{ - &config, - "", - "name-of-the-setting", - "description", - }; - setting.assign("value"); - - ASSERT_EQ(config.toJSON(), - R"#({ - "name-of-the-setting": { - "aliases": [], - "defaultValue": "", - "description": "description\n", - "documentDefault": true, - "value": "value", - "experimentalFeature": null - } - })#"_json); - } - - TEST(Config, toJSONOnNonEmptyConfigWithExperimentalSetting) { - using nlohmann::literals::operator "" _json; - Config config; - Setting<std::string> setting{ - &config, - "", - "name-of-the-setting", - "description", - {}, - true, - Xp::Flakes, - }; - setting.assign("value"); - - ASSERT_EQ(config.toJSON(), - R"#({ - "name-of-the-setting": { - "aliases": [], - "defaultValue": "", - "description": "description\n", - "documentDefault": true, - "value": "value", - "experimentalFeature": "flakes" - } - })#"_json); - } - - TEST(Config, setSettingAlias) { - Config config; - Setting<std::string> setting{&config, "", "some-int", "best number", { "another-int" }}; - ASSERT_TRUE(config.set("some-int", "1")); - ASSERT_EQ(setting.get(), "1"); - ASSERT_TRUE(config.set("another-int", "2")); - ASSERT_EQ(setting.get(), "2"); - ASSERT_TRUE(config.set("some-int", "3")); - ASSERT_EQ(setting.get(), "3"); - } - - /* FIXME: The reapplyUnknownSettings method doesn't seem to do anything - * useful (these days). Whenever we add a new setting to Config the - * unknown settings are always considered. In which case is this function - * actually useful? Is there some way to register a Setting without calling - * addSetting? */ - TEST(Config, DISABLED_reapplyUnknownSettings) { - Config config; - ASSERT_FALSE(config.set("name-of-the-setting", "unknownvalue")); - Setting<std::string> setting{&config, "default", "name-of-the-setting", "description"}; - ASSERT_EQ(setting.get(), "default"); - config.reapplyUnknownSettings(); - ASSERT_EQ(setting.get(), "unknownvalue"); - } - - TEST(Config, applyConfigEmpty) { - Config config; - std::map<std::string, Config::SettingInfo> settings; - config.applyConfig(""); - config.getSettings(settings); - ASSERT_TRUE(settings.empty()); - } - - TEST(Config, applyConfigEmptyWithComment) { - Config config; - std::map<std::string, Config::SettingInfo> settings; - config.applyConfig("# just a comment"); - config.getSettings(settings); - ASSERT_TRUE(settings.empty()); - } - - TEST(Config, applyConfigAssignment) { - Config config; - std::map<std::string, Config::SettingInfo> settings; - Setting<std::string> setting{&config, "", "name-of-the-setting", "description"}; - config.applyConfig( - "name-of-the-setting = value-from-file #useful comment\n" - "# name-of-the-setting = foo\n" - ); - config.getSettings(settings); - ASSERT_FALSE(settings.empty()); - ASSERT_EQ(settings["name-of-the-setting"].value, "value-from-file"); - } - - TEST(Config, applyConfigWithReassignedSetting) { - Config config; - std::map<std::string, Config::SettingInfo> settings; - Setting<std::string> setting{&config, "", "name-of-the-setting", "description"}; - config.applyConfig( - "name-of-the-setting = first-value\n" - "name-of-the-setting = second-value\n" - ); - config.getSettings(settings); - ASSERT_FALSE(settings.empty()); - ASSERT_EQ(settings["name-of-the-setting"].value, "second-value"); - } - - TEST(Config, applyConfigFailsOnMissingIncludes) { - Config config; - std::map<std::string, Config::SettingInfo> settings; - Setting<std::string> setting{&config, "", "name-of-the-setting", "description"}; - - ASSERT_THROW(config.applyConfig( - "name-of-the-setting = value-from-file\n" - "# name-of-the-setting = foo\n" - "include /nix/store/does/not/exist.nix" - ), Error); - } - - TEST(Config, applyConfigInvalidThrows) { - Config config; - ASSERT_THROW(config.applyConfig("value == key"), UsageError); - ASSERT_THROW(config.applyConfig("value "), UsageError); - } -} diff --git a/src/libutil/tests/git.cc b/src/libutil/tests/git.cc deleted file mode 100644 index 5b5715fc2..000000000 --- a/src/libutil/tests/git.cc +++ /dev/null @@ -1,33 +0,0 @@ -#include "git.hh" -#include <gtest/gtest.h> - -namespace nix { - - TEST(GitLsRemote, parseSymrefLineWithReference) { - auto line = "ref: refs/head/main HEAD"; - auto res = git::parseLsRemoteLine(line); - ASSERT_TRUE(res.has_value()); - ASSERT_EQ(res->kind, git::LsRemoteRefLine::Kind::Symbolic); - ASSERT_EQ(res->target, "refs/head/main"); - ASSERT_EQ(res->reference, "HEAD"); - } - - TEST(GitLsRemote, parseSymrefLineWithNoReference) { - auto line = "ref: refs/head/main"; - auto res = git::parseLsRemoteLine(line); - ASSERT_TRUE(res.has_value()); - ASSERT_EQ(res->kind, git::LsRemoteRefLine::Kind::Symbolic); - ASSERT_EQ(res->target, "refs/head/main"); - ASSERT_EQ(res->reference, std::nullopt); - } - - TEST(GitLsRemote, parseObjectRefLine) { - auto line = "abc123 refs/head/main"; - auto res = git::parseLsRemoteLine(line); - ASSERT_TRUE(res.has_value()); - ASSERT_EQ(res->kind, git::LsRemoteRefLine::Kind::Object); - ASSERT_EQ(res->target, "abc123"); - ASSERT_EQ(res->reference, "refs/head/main"); - } -} - diff --git a/src/libutil/tests/hash.cc b/src/libutil/tests/hash.cc deleted file mode 100644 index e4e928b3b..000000000 --- a/src/libutil/tests/hash.cc +++ /dev/null @@ -1,95 +0,0 @@ -#include <regex> - -#include <nlohmann/json.hpp> -#include <gtest/gtest.h> -#include <rapidcheck/gtest.h> - -#include <hash.hh> - -#include "tests/hash.hh" - -namespace nix { - - /* ---------------------------------------------------------------------------- - * hashString - * --------------------------------------------------------------------------*/ - - TEST(hashString, testKnownMD5Hashes1) { - // values taken from: https://tools.ietf.org/html/rfc1321 - auto s1 = ""; - auto hash = hashString(HashType::htMD5, s1); - ASSERT_EQ(hash.to_string(Base::Base16, true), "md5:d41d8cd98f00b204e9800998ecf8427e"); - } - - TEST(hashString, testKnownMD5Hashes2) { - // values taken from: https://tools.ietf.org/html/rfc1321 - auto s2 = "abc"; - auto hash = hashString(HashType::htMD5, s2); - ASSERT_EQ(hash.to_string(Base::Base16, true), "md5:900150983cd24fb0d6963f7d28e17f72"); - } - - TEST(hashString, testKnownSHA1Hashes1) { - // values taken from: https://tools.ietf.org/html/rfc3174 - auto s = "abc"; - auto hash = hashString(HashType::htSHA1, s); - ASSERT_EQ(hash.to_string(Base::Base16, true),"sha1:a9993e364706816aba3e25717850c26c9cd0d89d"); - } - - TEST(hashString, testKnownSHA1Hashes2) { - // values taken from: https://tools.ietf.org/html/rfc3174 - auto s = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; - auto hash = hashString(HashType::htSHA1, s); - ASSERT_EQ(hash.to_string(Base::Base16, true),"sha1:84983e441c3bd26ebaae4aa1f95129e5e54670f1"); - } - - TEST(hashString, testKnownSHA256Hashes1) { - // values taken from: https://tools.ietf.org/html/rfc4634 - auto s = "abc"; - - auto hash = hashString(HashType::htSHA256, s); - ASSERT_EQ(hash.to_string(Base::Base16, true), - "sha256:ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"); - } - - TEST(hashString, testKnownSHA256Hashes2) { - // values taken from: https://tools.ietf.org/html/rfc4634 - auto s = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; - auto hash = hashString(HashType::htSHA256, s); - ASSERT_EQ(hash.to_string(Base::Base16, true), - "sha256:248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"); - } - - TEST(hashString, testKnownSHA512Hashes1) { - // values taken from: https://tools.ietf.org/html/rfc4634 - auto s = "abc"; - auto hash = hashString(HashType::htSHA512, s); - ASSERT_EQ(hash.to_string(Base::Base16, true), - "sha512:ddaf35a193617abacc417349ae20413112e6fa4e89a9" - "7ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd" - "454d4423643ce80e2a9ac94fa54ca49f"); - } - - TEST(hashString, testKnownSHA512Hashes2) { - // values taken from: https://tools.ietf.org/html/rfc4634 - auto s = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"; - - auto hash = hashString(HashType::htSHA512, s); - ASSERT_EQ(hash.to_string(Base::Base16, true), - "sha512:8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa1" - "7299aeadb6889018501d289e4900f7e4331b99dec4b5433a" - "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/hash.hh b/src/libutil/tests/hash.hh deleted file mode 100644 index 1f9fa59ae..000000000 --- a/src/libutil/tests/hash.hh +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once -///@file - -#include <rapidcheck/gen/Arbitrary.h> - -#include <hash.hh> - -namespace rc { -using namespace nix; - -template<> -struct Arbitrary<Hash> { - static Gen<Hash> arbitrary(); -}; - -} diff --git a/src/libutil/tests/hilite.cc b/src/libutil/tests/hilite.cc deleted file mode 100644 index 1ff5980d5..000000000 --- a/src/libutil/tests/hilite.cc +++ /dev/null @@ -1,66 +0,0 @@ -#include "hilite.hh" - -#include <gtest/gtest.h> - -namespace nix { -/* ----------- tests for fmt.hh -------------------------------------------------*/ - - TEST(hiliteMatches, noHighlight) { - ASSERT_STREQ(hiliteMatches("Hello, world!", std::vector<std::smatch>(), "(", ")").c_str(), "Hello, world!"); - } - - TEST(hiliteMatches, simpleHighlight) { - std::string str = "Hello, world!"; - std::regex re = std::regex("world"); - auto matches = std::vector(std::sregex_iterator(str.begin(), str.end(), re), std::sregex_iterator()); - ASSERT_STREQ( - hiliteMatches(str, matches, "(", ")").c_str(), - "Hello, (world)!" - ); - } - - TEST(hiliteMatches, multipleMatches) { - std::string str = "Hello, world, world, world, world, world, world, Hello!"; - std::regex re = std::regex("world"); - auto matches = std::vector(std::sregex_iterator(str.begin(), str.end(), re), std::sregex_iterator()); - ASSERT_STREQ( - hiliteMatches(str, matches, "(", ")").c_str(), - "Hello, (world), (world), (world), (world), (world), (world), Hello!" - ); - } - - TEST(hiliteMatches, overlappingMatches) { - std::string str = "world, Hello, world, Hello, world, Hello, world, Hello, world!"; - std::regex re = std::regex("Hello, world"); - std::regex re2 = std::regex("world, Hello"); - auto v = std::vector(std::sregex_iterator(str.begin(), str.end(), re), std::sregex_iterator()); - for(auto it = std::sregex_iterator(str.begin(), str.end(), re2); it != std::sregex_iterator(); ++it) { - v.push_back(*it); - } - ASSERT_STREQ( - hiliteMatches(str, v, "(", ")").c_str(), - "(world, Hello, world, Hello, world, Hello, world, Hello, world)!" - ); - } - - TEST(hiliteMatches, complexOverlappingMatches) { - std::string str = "legacyPackages.x86_64-linux.git-crypt"; - std::vector regexes = { - std::regex("t-cry"), - std::regex("ux\\.git-cry"), - std::regex("git-c"), - std::regex("pt"), - }; - std::vector<std::smatch> matches; - for(auto regex : regexes) - { - for(auto it = std::sregex_iterator(str.begin(), str.end(), regex); it != std::sregex_iterator(); ++it) { - matches.push_back(*it); - } - } - ASSERT_STREQ( - hiliteMatches(str, matches, "(", ")").c_str(), - "legacyPackages.x86_64-lin(ux.git-crypt)" - ); - } -} 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/src/libutil/tests/logging.cc b/src/libutil/tests/logging.cc deleted file mode 100644 index 2ffdc2e9b..000000000 --- a/src/libutil/tests/logging.cc +++ /dev/null @@ -1,370 +0,0 @@ -#if 0 - -#include "logging.hh" -#include "nixexpr.hh" -#include "util.hh" -#include <fstream> - -#include <gtest/gtest.h> - -namespace nix { - - /* ---------------------------------------------------------------------------- - * logEI - * --------------------------------------------------------------------------*/ - - const char *test_file = - "previous line of code\n" - "this is the problem line of code\n" - "next line of code\n"; - const char *one_liner = - "this is the other problem line of code"; - - TEST(logEI, catpuresBasicProperties) { - - MakeError(TestError, Error); - ErrorInfo::programName = std::optional("error-unit-test"); - - try { - throw TestError("an error for testing purposes"); - } catch (Error &e) { - testing::internal::CaptureStderr(); - logger->logEI(e.info()); - auto str = testing::internal::GetCapturedStderr(); - - ASSERT_STREQ(str.c_str(),"\x1B[31;1merror:\x1B[0m\x1B[34;1m --- TestError --- error-unit-test\x1B[0m\nan error for testing purposes\n"); - } - } - - TEST(logEI, jsonOutput) { - SymbolTable testTable; - auto problem_file = testTable.create("random.nix"); - testing::internal::CaptureStderr(); - - makeJSONLogger(*logger)->logEI({ - .name = "error name", - .msg = hintfmt("this hint has %1% templated %2%!!", - "yellow", - "values"), - .errPos = Pos(foFile, problem_file, 02, 13) - }); - - auto str = testing::internal::GetCapturedStderr(); - ASSERT_STREQ(str.c_str(), "@nix {\"action\":\"msg\",\"column\":13,\"file\":\"random.nix\",\"level\":0,\"line\":2,\"msg\":\"\\u001b[31;1merror:\\u001b[0m\\u001b[34;1m --- error name --- error-unit-test\\u001b[0m\\n\\u001b[34;1mat: \\u001b[33;1m(2:13)\\u001b[34;1m in file: \\u001b[0mrandom.nix\\n\\nerror without any code lines.\\n\\nthis hint has \\u001b[33;1myellow\\u001b[0m templated \\u001b[33;1mvalues\\u001b[0m!!\",\"raw_msg\":\"this hint has \\u001b[33;1myellow\\u001b[0m templated \\u001b[33;1mvalues\\u001b[0m!!\"}\n"); - } - - TEST(logEI, appendingHintsToPreviousError) { - - MakeError(TestError, Error); - ErrorInfo::programName = std::optional("error-unit-test"); - - try { - auto e = Error("initial error"); - throw TestError(e.info()); - } catch (Error &e) { - ErrorInfo ei = e.info(); - ei.msg = hintfmt("%s; subsequent error message.", normaltxt(e.info().msg.str())); - - testing::internal::CaptureStderr(); - logger->logEI(ei); - auto str = testing::internal::GetCapturedStderr(); - - ASSERT_STREQ(str.c_str(), "\x1B[31;1merror:\x1B[0m\x1B[34;1m --- TestError --- error-unit-test\x1B[0m\ninitial error; subsequent error message.\n"); - } - - } - - TEST(logEI, picksUpSysErrorExitCode) { - - MakeError(TestError, Error); - ErrorInfo::programName = std::optional("error-unit-test"); - - try { - auto x = readFile(-1); - } - catch (SysError &e) { - testing::internal::CaptureStderr(); - logError(e.info()); - auto str = testing::internal::GetCapturedStderr(); - - ASSERT_STREQ(str.c_str(), "\x1B[31;1merror:\x1B[0m\x1B[34;1m --- SysError --- error-unit-test\x1B[0m\nstatting file: \x1B[33;1mBad file descriptor\x1B[0m\n"); - } - } - - TEST(logEI, loggingErrorOnInfoLevel) { - testing::internal::CaptureStderr(); - - logger->logEI({ .level = lvlInfo, - .name = "Info name", - }); - - auto str = testing::internal::GetCapturedStderr(); - ASSERT_STREQ(str.c_str(), "\x1B[32;1minfo:\x1B[0m\x1B[34;1m --- Info name --- error-unit-test\x1B[0m\nInfo description\n"); - } - - TEST(logEI, loggingErrorOnTalkativeLevel) { - verbosity = lvlTalkative; - - testing::internal::CaptureStderr(); - - logger->logEI({ .level = lvlTalkative, - .name = "Talkative name", - }); - - auto str = testing::internal::GetCapturedStderr(); - ASSERT_STREQ(str.c_str(), "\x1B[32;1mtalk:\x1B[0m\x1B[34;1m --- Talkative name --- error-unit-test\x1B[0m\nTalkative description\n"); - } - - TEST(logEI, loggingErrorOnChattyLevel) { - verbosity = lvlChatty; - - testing::internal::CaptureStderr(); - - logger->logEI({ .level = lvlChatty, - .name = "Chatty name", - }); - - auto str = testing::internal::GetCapturedStderr(); - ASSERT_STREQ(str.c_str(), "\x1B[32;1mchat:\x1B[0m\x1B[34;1m --- Chatty name --- error-unit-test\x1B[0m\nTalkative description\n"); - } - - TEST(logEI, loggingErrorOnDebugLevel) { - verbosity = lvlDebug; - - testing::internal::CaptureStderr(); - - logger->logEI({ .level = lvlDebug, - .name = "Debug name", - }); - - auto str = testing::internal::GetCapturedStderr(); - ASSERT_STREQ(str.c_str(), "\x1B[33;1mdebug:\x1B[0m\x1B[34;1m --- Debug name --- error-unit-test\x1B[0m\nDebug description\n"); - } - - TEST(logEI, loggingErrorOnVomitLevel) { - verbosity = lvlVomit; - - testing::internal::CaptureStderr(); - - logger->logEI({ .level = lvlVomit, - .name = "Vomit name", - }); - - auto str = testing::internal::GetCapturedStderr(); - ASSERT_STREQ(str.c_str(), "\x1B[32;1mvomit:\x1B[0m\x1B[34;1m --- Vomit name --- error-unit-test\x1B[0m\nVomit description\n"); - } - - /* ---------------------------------------------------------------------------- - * logError - * --------------------------------------------------------------------------*/ - - TEST(logError, logErrorWithoutHintOrCode) { - testing::internal::CaptureStderr(); - - logError({ - .name = "name", - }); - - auto str = testing::internal::GetCapturedStderr(); - ASSERT_STREQ(str.c_str(), "\x1B[31;1merror:\x1B[0m\x1B[34;1m --- name --- error-unit-test\x1B[0m\nerror description\n"); - } - - TEST(logError, logErrorWithPreviousAndNextLinesOfCode) { - SymbolTable testTable; - auto problem_file = testTable.create(test_file); - - testing::internal::CaptureStderr(); - - logError({ - .name = "error name", - .msg = hintfmt("this hint has %1% templated %2%!!", - "yellow", - "values"), - .errPos = Pos(foString, problem_file, 02, 13), - }); - - auto str = testing::internal::GetCapturedStderr(); - ASSERT_STREQ(str.c_str(), "\x1B[31;1merror:\x1B[0m\x1B[34;1m --- error name --- error-unit-test\x1B[0m\n\x1B[34;1mat: \x1B[33;1m(2:13)\x1B[34;1m from string\x1B[0m\n\nerror with code lines\n\n 1| previous line of code\n 2| this is the problem line of code\n | \x1B[31;1m^\x1B[0m\n 3| next line of code\n\nthis hint has \x1B[33;1myellow\x1B[0m templated \x1B[33;1mvalues\x1B[0m!!\n"); - } - - TEST(logError, logErrorWithInvalidFile) { - SymbolTable testTable; - auto problem_file = testTable.create("invalid filename"); - testing::internal::CaptureStderr(); - - logError({ - .name = "error name", - .msg = hintfmt("this hint has %1% templated %2%!!", - "yellow", - "values"), - .errPos = Pos(foFile, problem_file, 02, 13) - }); - - auto str = testing::internal::GetCapturedStderr(); - ASSERT_STREQ(str.c_str(), "\x1B[31;1merror:\x1B[0m\x1B[34;1m --- error name --- error-unit-test\x1B[0m\n\x1B[34;1mat: \x1B[33;1m(2:13)\x1B[34;1m in file: \x1B[0minvalid filename\n\nerror without any code lines.\n\nthis hint has \x1B[33;1myellow\x1B[0m templated \x1B[33;1mvalues\x1B[0m!!\n"); - } - - TEST(logError, logErrorWithOnlyHintAndName) { - testing::internal::CaptureStderr(); - - logError({ - .name = "error name", - .msg = hintfmt("hint %1%", "only"), - }); - - auto str = testing::internal::GetCapturedStderr(); - ASSERT_STREQ(str.c_str(), "\x1B[31;1merror:\x1B[0m\x1B[34;1m --- error name --- error-unit-test\x1B[0m\nhint \x1B[33;1monly\x1B[0m\n"); - - } - - /* ---------------------------------------------------------------------------- - * logWarning - * --------------------------------------------------------------------------*/ - - TEST(logWarning, logWarningWithNameDescriptionAndHint) { - testing::internal::CaptureStderr(); - - logWarning({ - .name = "name", - .msg = hintfmt("there was a %1%", "warning"), - }); - - auto str = testing::internal::GetCapturedStderr(); - ASSERT_STREQ(str.c_str(), "\x1B[33;1mwarning:\x1B[0m\x1B[34;1m --- name --- error-unit-test\x1B[0m\nwarning description\n\nthere was a \x1B[33;1mwarning\x1B[0m\n"); - } - - TEST(logWarning, logWarningWithFileLineNumAndCode) { - - SymbolTable testTable; - auto problem_file = testTable.create(test_file); - - testing::internal::CaptureStderr(); - - logWarning({ - .name = "warning name", - .msg = hintfmt("this hint has %1% templated %2%!!", - "yellow", - "values"), - .errPos = Pos(foStdin, problem_file, 2, 13), - }); - - - auto str = testing::internal::GetCapturedStderr(); - ASSERT_STREQ(str.c_str(), "\x1B[33;1mwarning:\x1B[0m\x1B[34;1m --- warning name --- error-unit-test\x1B[0m\n\x1B[34;1mat: \x1B[33;1m(2:13)\x1B[34;1m from stdin\x1B[0m\n\nwarning description\n\n 1| previous line of code\n 2| this is the problem line of code\n | \x1B[31;1m^\x1B[0m\n 3| next line of code\n\nthis hint has \x1B[33;1myellow\x1B[0m templated \x1B[33;1mvalues\x1B[0m!!\n"); - } - - /* ---------------------------------------------------------------------------- - * traces - * --------------------------------------------------------------------------*/ - - TEST(addTrace, showTracesWithShowTrace) { - SymbolTable testTable; - auto problem_file = testTable.create(test_file); - auto oneliner_file = testTable.create(one_liner); - auto invalidfilename = testTable.create("invalid filename"); - - auto e = AssertionError(ErrorInfo { - .name = "wat", - .msg = hintfmt("it has been %1% days since our last error", "zero"), - .errPos = Pos(foString, problem_file, 2, 13), - }); - - e.addTrace(Pos(foStdin, oneliner_file, 1, 19), "while trying to compute %1%", 42); - e.addTrace(std::nullopt, "while doing something without a %1%", "pos"); - e.addTrace(Pos(foFile, invalidfilename, 100, 1), "missing %s", "nix file"); - - testing::internal::CaptureStderr(); - - loggerSettings.showTrace.assign(true); - - logError(e.info()); - - auto str = testing::internal::GetCapturedStderr(); - ASSERT_STREQ(str.c_str(), "\x1B[31;1merror:\x1B[0m\x1B[34;1m --- AssertionError --- error-unit-test\x1B[0m\n\x1B[34;1mat: \x1B[33;1m(2:13)\x1B[34;1m from string\x1B[0m\n\nshow-traces\n\n 1| previous line of code\n 2| this is the problem line of code\n | \x1B[31;1m^\x1B[0m\n 3| next line of code\n\nit has been \x1B[33;1mzero\x1B[0m days since our last error\n\x1B[34;1m---- show-trace ----\x1B[0m\n\x1B[34;1mtrace: \x1B[0mwhile trying to compute \x1B[33;1m42\x1B[0m\n\x1B[34;1mat: \x1B[33;1m(1:19)\x1B[34;1m from stdin\x1B[0m\n\n 1| this is the other problem line of code\n | \x1B[31;1m^\x1B[0m\n\n\x1B[34;1mtrace: \x1B[0mwhile doing something without a \x1B[33;1mpos\x1B[0m\n\x1B[34;1mtrace: \x1B[0mmissing \x1B[33;1mnix file\x1B[0m\n\x1B[34;1mat: \x1B[33;1m(100:1)\x1B[34;1m in file: \x1B[0minvalid filename\n"); - } - - TEST(addTrace, hideTracesWithoutShowTrace) { - SymbolTable testTable; - auto problem_file = testTable.create(test_file); - auto oneliner_file = testTable.create(one_liner); - auto invalidfilename = testTable.create("invalid filename"); - - auto e = AssertionError(ErrorInfo { - .name = "wat", - .msg = hintfmt("it has been %1% days since our last error", "zero"), - .errPos = Pos(foString, problem_file, 2, 13), - }); - - e.addTrace(Pos(foStdin, oneliner_file, 1, 19), "while trying to compute %1%", 42); - e.addTrace(std::nullopt, "while doing something without a %1%", "pos"); - e.addTrace(Pos(foFile, invalidfilename, 100, 1), "missing %s", "nix file"); - - testing::internal::CaptureStderr(); - - loggerSettings.showTrace.assign(false); - - logError(e.info()); - - auto str = testing::internal::GetCapturedStderr(); - ASSERT_STREQ(str.c_str(), "\x1B[31;1merror:\x1B[0m\x1B[34;1m --- AssertionError --- error-unit-test\x1B[0m\n\x1B[34;1mat: \x1B[33;1m(2:13)\x1B[34;1m from string\x1B[0m\n\nhide traces\n\n 1| previous line of code\n 2| this is the problem line of code\n | \x1B[31;1m^\x1B[0m\n 3| next line of code\n\nit has been \x1B[33;1mzero\x1B[0m days since our last error\n"); - } - - - /* ---------------------------------------------------------------------------- - * hintfmt - * --------------------------------------------------------------------------*/ - - TEST(hintfmt, percentStringWithoutArgs) { - - const char *teststr = "this is 100%s correct!"; - - ASSERT_STREQ( - hintfmt(teststr).str().c_str(), - teststr); - - } - - TEST(hintfmt, fmtToHintfmt) { - - ASSERT_STREQ( - hintfmt(fmt("the color of this this text is %1%", "not yellow")).str().c_str(), - "the color of this this text is not yellow"); - - } - - TEST(hintfmt, tooFewArguments) { - - ASSERT_STREQ( - hintfmt("only one arg %1% %2%", "fulfilled").str().c_str(), - "only one arg " ANSI_WARNING "fulfilled" ANSI_NORMAL " "); - - } - - TEST(hintfmt, tooManyArguments) { - - ASSERT_STREQ( - hintfmt("what about this %1% %2%", "%3%", "one", "two").str().c_str(), - "what about this " ANSI_WARNING "%3%" ANSI_NORMAL " " ANSI_YELLOW "one" ANSI_NORMAL); - - } - - /* ---------------------------------------------------------------------------- - * ErrPos - * --------------------------------------------------------------------------*/ - - TEST(errpos, invalidPos) { - - // contains an invalid symbol, which we should not dereference! - Pos invalid; - - // constructing without access violation. - ErrPos ep(invalid); - - // assignment without access violation. - ep = invalid; - - } - -} - -#endif diff --git a/src/libutil/tests/lru-cache.cc b/src/libutil/tests/lru-cache.cc deleted file mode 100644 index 091d3d5ed..000000000 --- a/src/libutil/tests/lru-cache.cc +++ /dev/null @@ -1,130 +0,0 @@ -#include "lru-cache.hh" -#include <gtest/gtest.h> - -namespace nix { - - /* ---------------------------------------------------------------------------- - * size - * --------------------------------------------------------------------------*/ - - TEST(LRUCache, sizeOfEmptyCacheIsZero) { - LRUCache<std::string, std::string> c(10); - ASSERT_EQ(c.size(), 0); - } - - TEST(LRUCache, sizeOfSingleElementCacheIsOne) { - LRUCache<std::string, std::string> c(10); - c.upsert("foo", "bar"); - ASSERT_EQ(c.size(), 1); - } - - /* ---------------------------------------------------------------------------- - * upsert / get - * --------------------------------------------------------------------------*/ - - TEST(LRUCache, getFromEmptyCache) { - LRUCache<std::string, std::string> c(10); - auto val = c.get("x"); - ASSERT_EQ(val.has_value(), false); - } - - TEST(LRUCache, getExistingValue) { - LRUCache<std::string, std::string> c(10); - c.upsert("foo", "bar"); - auto val = c.get("foo"); - ASSERT_EQ(val, "bar"); - } - - TEST(LRUCache, getNonExistingValueFromNonEmptyCache) { - LRUCache<std::string, std::string> c(10); - c.upsert("foo", "bar"); - auto val = c.get("another"); - ASSERT_EQ(val.has_value(), false); - } - - TEST(LRUCache, upsertOnZeroCapacityCache) { - LRUCache<std::string, std::string> c(0); - c.upsert("foo", "bar"); - auto val = c.get("foo"); - ASSERT_EQ(val.has_value(), false); - } - - TEST(LRUCache, updateExistingValue) { - LRUCache<std::string, std::string> c(1); - c.upsert("foo", "bar"); - - auto val = c.get("foo"); - ASSERT_EQ(val.value_or("error"), "bar"); - ASSERT_EQ(c.size(), 1); - - c.upsert("foo", "changed"); - val = c.get("foo"); - ASSERT_EQ(val.value_or("error"), "changed"); - ASSERT_EQ(c.size(), 1); - } - - TEST(LRUCache, overwriteOldestWhenCapacityIsReached) { - LRUCache<std::string, std::string> c(3); - c.upsert("one", "eins"); - c.upsert("two", "zwei"); - c.upsert("three", "drei"); - - ASSERT_EQ(c.size(), 3); - ASSERT_EQ(c.get("one").value_or("error"), "eins"); - - // exceed capacity - c.upsert("another", "whatever"); - - ASSERT_EQ(c.size(), 3); - // Retrieving "one" makes it the most recent element thus - // two will be the oldest one and thus replaced. - ASSERT_EQ(c.get("two").has_value(), false); - ASSERT_EQ(c.get("another").value(), "whatever"); - } - - /* ---------------------------------------------------------------------------- - * clear - * --------------------------------------------------------------------------*/ - - TEST(LRUCache, clearEmptyCache) { - LRUCache<std::string, std::string> c(10); - c.clear(); - ASSERT_EQ(c.size(), 0); - } - - TEST(LRUCache, clearNonEmptyCache) { - LRUCache<std::string, std::string> c(10); - c.upsert("one", "eins"); - c.upsert("two", "zwei"); - c.upsert("three", "drei"); - ASSERT_EQ(c.size(), 3); - c.clear(); - ASSERT_EQ(c.size(), 0); - } - - /* ---------------------------------------------------------------------------- - * erase - * --------------------------------------------------------------------------*/ - - TEST(LRUCache, eraseFromEmptyCache) { - LRUCache<std::string, std::string> c(10); - ASSERT_EQ(c.erase("foo"), false); - ASSERT_EQ(c.size(), 0); - } - - TEST(LRUCache, eraseMissingFromNonEmptyCache) { - LRUCache<std::string, std::string> c(10); - c.upsert("one", "eins"); - ASSERT_EQ(c.erase("foo"), false); - ASSERT_EQ(c.size(), 1); - ASSERT_EQ(c.get("one").value_or("error"), "eins"); - } - - TEST(LRUCache, eraseFromNonEmptyCache) { - LRUCache<std::string, std::string> c(10); - c.upsert("one", "eins"); - ASSERT_EQ(c.erase("one"), true); - ASSERT_EQ(c.size(), 0); - ASSERT_EQ(c.get("one").value_or("empty"), "empty"); - } -} diff --git a/src/libutil/tests/pool.cc b/src/libutil/tests/pool.cc deleted file mode 100644 index 127e42dda..000000000 --- a/src/libutil/tests/pool.cc +++ /dev/null @@ -1,127 +0,0 @@ -#include "pool.hh" -#include <gtest/gtest.h> - -namespace nix { - - struct TestResource - { - - TestResource() { - static int counter = 0; - num = counter++; - } - - int dummyValue = 1; - bool good = true; - int num; - }; - - /* ---------------------------------------------------------------------------- - * Pool - * --------------------------------------------------------------------------*/ - - TEST(Pool, freshPoolHasZeroCountAndSpecifiedCapacity) { - auto isGood = [](const ref<TestResource> & r) { return r->good; }; - auto createResource = []() { return make_ref<TestResource>(); }; - - Pool<TestResource> pool = Pool<TestResource>((size_t)1, createResource, isGood); - - ASSERT_EQ(pool.count(), 0); - ASSERT_EQ(pool.capacity(), 1); - } - - TEST(Pool, freshPoolCanGetAResource) { - auto isGood = [](const ref<TestResource> & r) { return r->good; }; - auto createResource = []() { return make_ref<TestResource>(); }; - - Pool<TestResource> pool = Pool<TestResource>((size_t)1, createResource, isGood); - ASSERT_EQ(pool.count(), 0); - - TestResource r = *(pool.get()); - - ASSERT_EQ(pool.count(), 1); - ASSERT_EQ(pool.capacity(), 1); - ASSERT_EQ(r.dummyValue, 1); - ASSERT_EQ(r.good, true); - } - - TEST(Pool, capacityCanBeIncremented) { - auto isGood = [](const ref<TestResource> & r) { return r->good; }; - auto createResource = []() { return make_ref<TestResource>(); }; - - Pool<TestResource> pool = Pool<TestResource>((size_t)1, createResource, isGood); - ASSERT_EQ(pool.capacity(), 1); - pool.incCapacity(); - ASSERT_EQ(pool.capacity(), 2); - } - - TEST(Pool, capacityCanBeDecremented) { - auto isGood = [](const ref<TestResource> & r) { return r->good; }; - auto createResource = []() { return make_ref<TestResource>(); }; - - Pool<TestResource> pool = Pool<TestResource>((size_t)1, createResource, isGood); - ASSERT_EQ(pool.capacity(), 1); - pool.decCapacity(); - ASSERT_EQ(pool.capacity(), 0); - } - - TEST(Pool, flushBadDropsOutOfScopeResources) { - auto isGood = [](const ref<TestResource> & r) { return false; }; - auto createResource = []() { return make_ref<TestResource>(); }; - - Pool<TestResource> pool = Pool<TestResource>((size_t)1, createResource, isGood); - - { - auto _r = pool.get(); - ASSERT_EQ(pool.count(), 1); - } - - pool.flushBad(); - ASSERT_EQ(pool.count(), 0); - } - - // Test that the resources we allocate are being reused when they are still good. - TEST(Pool, reuseResource) { - auto isGood = [](const ref<TestResource> & r) { return true; }; - auto createResource = []() { return make_ref<TestResource>(); }; - - Pool<TestResource> pool = Pool<TestResource>((size_t)1, createResource, isGood); - - // Compare the instance counter between the two handles. We expect them to be equal - // as the pool should hand out the same (still) good one again. - int counter = -1; - { - Pool<TestResource>::Handle h = pool.get(); - counter = h->num; - } // the first handle goes out of scope - - { // the second handle should contain the same resource (with the same counter value) - Pool<TestResource>::Handle h = pool.get(); - ASSERT_EQ(h->num, counter); - } - } - - // Test that the resources we allocate are being thrown away when they are no longer good. - TEST(Pool, badResourceIsNotReused) { - auto isGood = [](const ref<TestResource> & r) { return false; }; - auto createResource = []() { return make_ref<TestResource>(); }; - - Pool<TestResource> pool = Pool<TestResource>((size_t)1, createResource, isGood); - - // Compare the instance counter between the two handles. We expect them - // to *not* be equal as the pool should hand out a new instance after - // the first one was returned. - int counter = -1; - { - Pool<TestResource>::Handle h = pool.get(); - counter = h->num; - } // the first handle goes out of scope - - { - // the second handle should contain a different resource (with a - //different counter value) - Pool<TestResource>::Handle h = pool.get(); - ASSERT_NE(h->num, counter); - } - } -} diff --git a/src/libutil/tests/references.cc b/src/libutil/tests/references.cc deleted file mode 100644 index a517d9aa1..000000000 --- a/src/libutil/tests/references.cc +++ /dev/null @@ -1,46 +0,0 @@ -#include "references.hh" -#include <gtest/gtest.h> - -namespace nix { - -using std::string; - -struct RewriteParams { - string originalString, finalString; - StringMap rewrites; - - friend std::ostream& operator<<(std::ostream& os, const RewriteParams& bar) { - StringSet strRewrites; - for (auto & [from, to] : bar.rewrites) - strRewrites.insert(from + "->" + to); - return os << - "OriginalString: " << bar.originalString << std::endl << - "Rewrites: " << concatStringsSep(",", strRewrites) << std::endl << - "Expected result: " << bar.finalString; - } -}; - -class RewriteTest : public ::testing::TestWithParam<RewriteParams> { -}; - -TEST_P(RewriteTest, IdentityRewriteIsIdentity) { - RewriteParams param = GetParam(); - StringSink rewritten; - auto rewriter = RewritingSink(param.rewrites, rewritten); - rewriter(param.originalString); - rewriter.flush(); - ASSERT_EQ(rewritten.s, param.finalString); -} - -INSTANTIATE_TEST_CASE_P( - references, - RewriteTest, - ::testing::Values( - RewriteParams{ "foooo", "baroo", {{"foo", "bar"}, {"bar", "baz"}}}, - RewriteParams{ "foooo", "bazoo", {{"fou", "bar"}, {"foo", "baz"}}}, - RewriteParams{ "foooo", "foooo", {}} - ) -); - -} - diff --git a/src/libutil/tests/suggestions.cc b/src/libutil/tests/suggestions.cc deleted file mode 100644 index 279994abc..000000000 --- a/src/libutil/tests/suggestions.cc +++ /dev/null @@ -1,43 +0,0 @@ -#include "suggestions.hh" -#include <gtest/gtest.h> - -namespace nix { - - struct LevenshteinDistanceParam { - std::string s1, s2; - int distance; - }; - - class LevenshteinDistanceTest : - public testing::TestWithParam<LevenshteinDistanceParam> { - }; - - TEST_P(LevenshteinDistanceTest, CorrectlyComputed) { - auto params = GetParam(); - - ASSERT_EQ(levenshteinDistance(params.s1, params.s2), params.distance); - ASSERT_EQ(levenshteinDistance(params.s2, params.s1), params.distance); - } - - INSTANTIATE_TEST_SUITE_P(LevenshteinDistance, LevenshteinDistanceTest, - testing::Values( - LevenshteinDistanceParam{"foo", "foo", 0}, - LevenshteinDistanceParam{"foo", "", 3}, - LevenshteinDistanceParam{"", "", 0}, - LevenshteinDistanceParam{"foo", "fo", 1}, - LevenshteinDistanceParam{"foo", "oo", 1}, - LevenshteinDistanceParam{"foo", "fao", 1}, - LevenshteinDistanceParam{"foo", "abc", 3} - ) - ); - - TEST(Suggestions, Trim) { - auto suggestions = Suggestions::bestMatches({"foooo", "bar", "fo", "gao"}, "foo"); - auto onlyOne = suggestions.trim(1); - ASSERT_EQ(onlyOne.suggestions.size(), 1); - ASSERT_TRUE(onlyOne.suggestions.begin()->suggestion == "fo"); - - auto closest = suggestions.trim(999, 2); - ASSERT_EQ(closest.suggestions.size(), 3); - } -} diff --git a/src/libutil/tests/tests.cc b/src/libutil/tests/tests.cc deleted file mode 100644 index f3c1e8248..000000000 --- a/src/libutil/tests/tests.cc +++ /dev/null @@ -1,659 +0,0 @@ -#include "util.hh" -#include "types.hh" - -#include <limits.h> -#include <gtest/gtest.h> - -#include <numeric> - -namespace nix { - -/* ----------- tests for util.hh ------------------------------------------------*/ - - /* ---------------------------------------------------------------------------- - * absPath - * --------------------------------------------------------------------------*/ - - TEST(absPath, doesntChangeRoot) { - auto p = absPath("/"); - - ASSERT_EQ(p, "/"); - } - - - - - TEST(absPath, turnsEmptyPathIntoCWD) { - char cwd[PATH_MAX+1]; - auto p = absPath(""); - - ASSERT_EQ(p, getcwd((char*)&cwd, PATH_MAX)); - } - - TEST(absPath, usesOptionalBasePathWhenGiven) { - char _cwd[PATH_MAX+1]; - char* cwd = getcwd((char*)&_cwd, PATH_MAX); - - auto p = absPath("", cwd); - - ASSERT_EQ(p, cwd); - } - - TEST(absPath, isIdempotent) { - char _cwd[PATH_MAX+1]; - char* cwd = getcwd((char*)&_cwd, PATH_MAX); - auto p1 = absPath(cwd); - auto p2 = absPath(p1); - - ASSERT_EQ(p1, p2); - } - - - TEST(absPath, pathIsCanonicalised) { - auto path = "/some/path/with/trailing/dot/."; - auto p1 = absPath(path); - auto p2 = absPath(p1); - - ASSERT_EQ(p1, "/some/path/with/trailing/dot"); - ASSERT_EQ(p1, p2); - } - - /* ---------------------------------------------------------------------------- - * canonPath - * --------------------------------------------------------------------------*/ - - TEST(canonPath, removesTrailingSlashes) { - auto path = "/this/is/a/path//"; - auto p = canonPath(path); - - ASSERT_EQ(p, "/this/is/a/path"); - } - - TEST(canonPath, removesDots) { - auto path = "/this/./is/a/path/./"; - auto p = canonPath(path); - - ASSERT_EQ(p, "/this/is/a/path"); - } - - TEST(canonPath, removesDots2) { - auto path = "/this/a/../is/a////path/foo/.."; - auto p = canonPath(path); - - ASSERT_EQ(p, "/this/is/a/path"); - } - - TEST(canonPath, requiresAbsolutePath) { - ASSERT_ANY_THROW(canonPath(".")); - ASSERT_ANY_THROW(canonPath("..")); - ASSERT_ANY_THROW(canonPath("../")); - ASSERT_DEATH({ canonPath(""); }, "path != \"\""); - } - - /* ---------------------------------------------------------------------------- - * dirOf - * --------------------------------------------------------------------------*/ - - TEST(dirOf, returnsEmptyStringForRoot) { - auto p = dirOf("/"); - - ASSERT_EQ(p, "/"); - } - - TEST(dirOf, returnsFirstPathComponent) { - auto p1 = dirOf("/dir/"); - ASSERT_EQ(p1, "/dir"); - auto p2 = dirOf("/dir"); - ASSERT_EQ(p2, "/"); - auto p3 = dirOf("/dir/.."); - ASSERT_EQ(p3, "/dir"); - auto p4 = dirOf("/dir/../"); - ASSERT_EQ(p4, "/dir/.."); - } - - /* ---------------------------------------------------------------------------- - * baseNameOf - * --------------------------------------------------------------------------*/ - - TEST(baseNameOf, emptyPath) { - auto p1 = baseNameOf(""); - ASSERT_EQ(p1, ""); - } - - TEST(baseNameOf, pathOnRoot) { - auto p1 = baseNameOf("/dir"); - ASSERT_EQ(p1, "dir"); - } - - TEST(baseNameOf, relativePath) { - auto p1 = baseNameOf("dir/foo"); - ASSERT_EQ(p1, "foo"); - } - - TEST(baseNameOf, pathWithTrailingSlashRoot) { - auto p1 = baseNameOf("/"); - ASSERT_EQ(p1, ""); - } - - TEST(baseNameOf, trailingSlash) { - auto p1 = baseNameOf("/dir/"); - ASSERT_EQ(p1, "dir"); - } - - /* ---------------------------------------------------------------------------- - * isInDir - * --------------------------------------------------------------------------*/ - - TEST(isInDir, trivialCase) { - auto p1 = isInDir("/foo/bar", "/foo"); - ASSERT_EQ(p1, true); - } - - TEST(isInDir, notInDir) { - auto p1 = isInDir("/zes/foo/bar", "/foo"); - ASSERT_EQ(p1, false); - } - - // XXX: hm, bug or feature? :) Looking at the implementation - // this might be problematic. - TEST(isInDir, emptyDir) { - auto p1 = isInDir("/zes/foo/bar", ""); - ASSERT_EQ(p1, true); - } - - /* ---------------------------------------------------------------------------- - * isDirOrInDir - * --------------------------------------------------------------------------*/ - - TEST(isDirOrInDir, trueForSameDirectory) { - ASSERT_EQ(isDirOrInDir("/nix", "/nix"), true); - ASSERT_EQ(isDirOrInDir("/", "/"), true); - } - - TEST(isDirOrInDir, trueForEmptyPaths) { - ASSERT_EQ(isDirOrInDir("", ""), true); - } - - TEST(isDirOrInDir, falseForDisjunctPaths) { - ASSERT_EQ(isDirOrInDir("/foo", "/bar"), false); - } - - TEST(isDirOrInDir, relativePaths) { - ASSERT_EQ(isDirOrInDir("/foo/..", "/foo"), true); - } - - // XXX: while it is possible to use "." or ".." in the - // first argument this doesn't seem to work in the second. - TEST(isDirOrInDir, DISABLED_shouldWork) { - ASSERT_EQ(isDirOrInDir("/foo/..", "/foo/."), true); - - } - - /* ---------------------------------------------------------------------------- - * pathExists - * --------------------------------------------------------------------------*/ - - TEST(pathExists, rootExists) { - ASSERT_TRUE(pathExists("/")); - } - - TEST(pathExists, cwdExists) { - ASSERT_TRUE(pathExists(".")); - } - - TEST(pathExists, bogusPathDoesNotExist) { - ASSERT_FALSE(pathExists("/schnitzel/darmstadt/pommes")); - } - - /* ---------------------------------------------------------------------------- - * concatStringsSep - * --------------------------------------------------------------------------*/ - - TEST(concatStringsSep, buildCommaSeparatedString) { - Strings strings; - strings.push_back("this"); - strings.push_back("is"); - strings.push_back("great"); - - ASSERT_EQ(concatStringsSep(",", strings), "this,is,great"); - } - - TEST(concatStringsSep, buildStringWithEmptySeparator) { - Strings strings; - strings.push_back("this"); - strings.push_back("is"); - strings.push_back("great"); - - ASSERT_EQ(concatStringsSep("", strings), "thisisgreat"); - } - - TEST(concatStringsSep, buildSingleString) { - Strings strings; - strings.push_back("this"); - - ASSERT_EQ(concatStringsSep(",", strings), "this"); - } - - /* ---------------------------------------------------------------------------- - * hasPrefix - * --------------------------------------------------------------------------*/ - - TEST(hasPrefix, emptyStringHasNoPrefix) { - ASSERT_FALSE(hasPrefix("", "foo")); - } - - TEST(hasPrefix, emptyStringIsAlwaysPrefix) { - ASSERT_TRUE(hasPrefix("foo", "")); - ASSERT_TRUE(hasPrefix("jshjkfhsadf", "")); - } - - TEST(hasPrefix, trivialCase) { - ASSERT_TRUE(hasPrefix("foobar", "foo")); - } - - /* ---------------------------------------------------------------------------- - * hasSuffix - * --------------------------------------------------------------------------*/ - - TEST(hasSuffix, emptyStringHasNoSuffix) { - ASSERT_FALSE(hasSuffix("", "foo")); - } - - TEST(hasSuffix, trivialCase) { - ASSERT_TRUE(hasSuffix("foo", "foo")); - ASSERT_TRUE(hasSuffix("foobar", "bar")); - } - - /* ---------------------------------------------------------------------------- - * base64Encode - * --------------------------------------------------------------------------*/ - - TEST(base64Encode, emptyString) { - ASSERT_EQ(base64Encode(""), ""); - } - - TEST(base64Encode, encodesAString) { - ASSERT_EQ(base64Encode("quod erat demonstrandum"), "cXVvZCBlcmF0IGRlbW9uc3RyYW5kdW0="); - } - - TEST(base64Encode, encodeAndDecode) { - auto s = "quod erat demonstrandum"; - auto encoded = base64Encode(s); - auto decoded = base64Decode(encoded); - - ASSERT_EQ(decoded, s); - } - - TEST(base64Encode, encodeAndDecodeNonPrintable) { - char s[256]; - std::iota(std::rbegin(s), std::rend(s), 0); - - auto encoded = base64Encode(s); - auto decoded = base64Decode(encoded); - - EXPECT_EQ(decoded.length(), 255); - ASSERT_EQ(decoded, s); - } - - /* ---------------------------------------------------------------------------- - * base64Decode - * --------------------------------------------------------------------------*/ - - TEST(base64Decode, emptyString) { - ASSERT_EQ(base64Decode(""), ""); - } - - TEST(base64Decode, decodeAString) { - ASSERT_EQ(base64Decode("cXVvZCBlcmF0IGRlbW9uc3RyYW5kdW0="), "quod erat demonstrandum"); - } - - TEST(base64Decode, decodeThrowsOnInvalidChar) { - ASSERT_THROW(base64Decode("cXVvZCBlcm_0IGRlbW9uc3RyYW5kdW0="), Error); - } - - /* ---------------------------------------------------------------------------- - * getLine - * --------------------------------------------------------------------------*/ - - TEST(getLine, all) { - { - auto [line, rest] = getLine("foo\nbar\nxyzzy"); - ASSERT_EQ(line, "foo"); - ASSERT_EQ(rest, "bar\nxyzzy"); - } - - { - auto [line, rest] = getLine("foo\r\nbar\r\nxyzzy"); - ASSERT_EQ(line, "foo"); - ASSERT_EQ(rest, "bar\r\nxyzzy"); - } - - { - auto [line, rest] = getLine("foo\n"); - ASSERT_EQ(line, "foo"); - ASSERT_EQ(rest, ""); - } - - { - auto [line, rest] = getLine("foo"); - ASSERT_EQ(line, "foo"); - ASSERT_EQ(rest, ""); - } - - { - auto [line, rest] = getLine(""); - ASSERT_EQ(line, ""); - ASSERT_EQ(rest, ""); - } - } - - /* ---------------------------------------------------------------------------- - * toLower - * --------------------------------------------------------------------------*/ - - TEST(toLower, emptyString) { - ASSERT_EQ(toLower(""), ""); - } - - TEST(toLower, nonLetters) { - auto s = "!@(*$#)(@#=\\234_"; - ASSERT_EQ(toLower(s), s); - } - - // std::tolower() doesn't handle unicode characters. In the context of - // store paths this isn't relevant but doesn't hurt to record this behavior - // here. - TEST(toLower, umlauts) { - auto s = "ÄÖÜ"; - ASSERT_EQ(toLower(s), "ÄÖÜ"); - } - - /* ---------------------------------------------------------------------------- - * string2Float - * --------------------------------------------------------------------------*/ - - TEST(string2Float, emptyString) { - ASSERT_EQ(string2Float<double>(""), std::nullopt); - } - - TEST(string2Float, trivialConversions) { - ASSERT_EQ(string2Float<double>("1.0"), 1.0); - - ASSERT_EQ(string2Float<double>("0.0"), 0.0); - - ASSERT_EQ(string2Float<double>("-100.25"), -100.25); - } - - /* ---------------------------------------------------------------------------- - * string2Int - * --------------------------------------------------------------------------*/ - - TEST(string2Int, emptyString) { - ASSERT_EQ(string2Int<int>(""), std::nullopt); - } - - TEST(string2Int, trivialConversions) { - ASSERT_EQ(string2Int<int>("1"), 1); - - ASSERT_EQ(string2Int<int>("0"), 0); - - ASSERT_EQ(string2Int<int>("-100"), -100); - } - - /* ---------------------------------------------------------------------------- - * statusOk - * --------------------------------------------------------------------------*/ - - TEST(statusOk, zeroIsOk) { - ASSERT_EQ(statusOk(0), true); - ASSERT_EQ(statusOk(1), false); - } - - - /* ---------------------------------------------------------------------------- - * rewriteStrings - * --------------------------------------------------------------------------*/ - - TEST(rewriteStrings, emptyString) { - StringMap rewrites; - rewrites["this"] = "that"; - - ASSERT_EQ(rewriteStrings("", rewrites), ""); - } - - TEST(rewriteStrings, emptyRewrites) { - StringMap rewrites; - - ASSERT_EQ(rewriteStrings("this and that", rewrites), "this and that"); - } - - TEST(rewriteStrings, successfulRewrite) { - StringMap rewrites; - rewrites["this"] = "that"; - - ASSERT_EQ(rewriteStrings("this and that", rewrites), "that and that"); - } - - TEST(rewriteStrings, doesntOccur) { - StringMap rewrites; - rewrites["foo"] = "bar"; - - ASSERT_EQ(rewriteStrings("this and that", rewrites), "this and that"); - } - - /* ---------------------------------------------------------------------------- - * replaceStrings - * --------------------------------------------------------------------------*/ - - TEST(replaceStrings, emptyString) { - ASSERT_EQ(replaceStrings("", "this", "that"), ""); - ASSERT_EQ(replaceStrings("this and that", "", ""), "this and that"); - } - - TEST(replaceStrings, successfulReplace) { - ASSERT_EQ(replaceStrings("this and that", "this", "that"), "that and that"); - } - - TEST(replaceStrings, doesntOccur) { - ASSERT_EQ(replaceStrings("this and that", "foo", "bar"), "this and that"); - } - - /* ---------------------------------------------------------------------------- - * trim - * --------------------------------------------------------------------------*/ - - TEST(trim, emptyString) { - ASSERT_EQ(trim(""), ""); - } - - TEST(trim, removesWhitespace) { - ASSERT_EQ(trim("foo"), "foo"); - ASSERT_EQ(trim(" foo "), "foo"); - ASSERT_EQ(trim(" foo bar baz"), "foo bar baz"); - ASSERT_EQ(trim(" \t foo bar baz\n"), "foo bar baz"); - } - - /* ---------------------------------------------------------------------------- - * chomp - * --------------------------------------------------------------------------*/ - - TEST(chomp, emptyString) { - ASSERT_EQ(chomp(""), ""); - } - - TEST(chomp, removesWhitespace) { - ASSERT_EQ(chomp("foo"), "foo"); - ASSERT_EQ(chomp("foo "), "foo"); - ASSERT_EQ(chomp(" foo "), " foo"); - ASSERT_EQ(chomp(" foo bar baz "), " foo bar baz"); - ASSERT_EQ(chomp("\t foo bar baz\n"), "\t foo bar baz"); - } - - /* ---------------------------------------------------------------------------- - * quoteStrings - * --------------------------------------------------------------------------*/ - - TEST(quoteStrings, empty) { - Strings s = { }; - Strings expected = { }; - - ASSERT_EQ(quoteStrings(s), expected); - } - - TEST(quoteStrings, emptyStrings) { - Strings s = { "", "", "" }; - Strings expected = { "''", "''", "''" }; - ASSERT_EQ(quoteStrings(s), expected); - - } - - TEST(quoteStrings, trivialQuote) { - Strings s = { "foo", "bar", "baz" }; - Strings expected = { "'foo'", "'bar'", "'baz'" }; - - ASSERT_EQ(quoteStrings(s), expected); - } - - TEST(quoteStrings, quotedStrings) { - Strings s = { "'foo'", "'bar'", "'baz'" }; - Strings expected = { "''foo''", "''bar''", "''baz''" }; - - ASSERT_EQ(quoteStrings(s), expected); - } - - /* ---------------------------------------------------------------------------- - * tokenizeString - * --------------------------------------------------------------------------*/ - - TEST(tokenizeString, empty) { - Strings expected = { }; - - ASSERT_EQ(tokenizeString<Strings>(""), expected); - } - - TEST(tokenizeString, tokenizeSpacesWithDefaults) { - auto s = "foo bar baz"; - Strings expected = { "foo", "bar", "baz" }; - - ASSERT_EQ(tokenizeString<Strings>(s), expected); - } - - TEST(tokenizeString, tokenizeTabsWithDefaults) { - auto s = "foo\tbar\tbaz"; - Strings expected = { "foo", "bar", "baz" }; - - ASSERT_EQ(tokenizeString<Strings>(s), expected); - } - - TEST(tokenizeString, tokenizeTabsSpacesWithDefaults) { - auto s = "foo\t bar\t baz"; - Strings expected = { "foo", "bar", "baz" }; - - ASSERT_EQ(tokenizeString<Strings>(s), expected); - } - - TEST(tokenizeString, tokenizeTabsSpacesNewlineWithDefaults) { - auto s = "foo\t\n bar\t\n baz"; - Strings expected = { "foo", "bar", "baz" }; - - ASSERT_EQ(tokenizeString<Strings>(s), expected); - } - - TEST(tokenizeString, tokenizeTabsSpacesNewlineRetWithDefaults) { - auto s = "foo\t\n\r bar\t\n\r baz"; - Strings expected = { "foo", "bar", "baz" }; - - ASSERT_EQ(tokenizeString<Strings>(s), expected); - - auto s2 = "foo \t\n\r bar \t\n\r baz"; - Strings expected2 = { "foo", "bar", "baz" }; - - ASSERT_EQ(tokenizeString<Strings>(s2), expected2); - } - - TEST(tokenizeString, tokenizeWithCustomSep) { - auto s = "foo\n,bar\n,baz\n"; - Strings expected = { "foo\n", "bar\n", "baz\n" }; - - ASSERT_EQ(tokenizeString<Strings>(s, ","), expected); - } - - /* ---------------------------------------------------------------------------- - * get - * --------------------------------------------------------------------------*/ - - TEST(get, emptyContainer) { - StringMap s = { }; - auto expected = nullptr; - - ASSERT_EQ(get(s, "one"), expected); - } - - TEST(get, getFromContainer) { - StringMap s; - s["one"] = "yi"; - s["two"] = "er"; - auto expected = "yi"; - - ASSERT_EQ(*get(s, "one"), expected); - } - - TEST(getOr, emptyContainer) { - StringMap s = { }; - auto expected = "yi"; - - ASSERT_EQ(getOr(s, "one", "yi"), expected); - } - - TEST(getOr, getFromContainer) { - StringMap s; - s["one"] = "yi"; - s["two"] = "er"; - auto expected = "yi"; - - ASSERT_EQ(getOr(s, "one", "nope"), expected); - } - - /* ---------------------------------------------------------------------------- - * filterANSIEscapes - * --------------------------------------------------------------------------*/ - - TEST(filterANSIEscapes, emptyString) { - auto s = ""; - auto expected = ""; - - ASSERT_EQ(filterANSIEscapes(s), expected); - } - - TEST(filterANSIEscapes, doesntChangePrintableChars) { - auto s = "09 2q304ruyhr slk2-19024 kjsadh sar f"; - - ASSERT_EQ(filterANSIEscapes(s), s); - } - - TEST(filterANSIEscapes, filtersColorCodes) { - auto s = "\u001b[30m A \u001b[31m B \u001b[32m C \u001b[33m D \u001b[0m"; - - ASSERT_EQ(filterANSIEscapes(s, true, 2), " A" ); - ASSERT_EQ(filterANSIEscapes(s, true, 3), " A " ); - ASSERT_EQ(filterANSIEscapes(s, true, 4), " A " ); - ASSERT_EQ(filterANSIEscapes(s, true, 5), " A B" ); - ASSERT_EQ(filterANSIEscapes(s, true, 8), " A B C" ); - } - - TEST(filterANSIEscapes, expandsTabs) { - auto s = "foo\tbar\tbaz"; - - ASSERT_EQ(filterANSIEscapes(s, true), "foo bar baz" ); - } - - TEST(filterANSIEscapes, utf8) { - ASSERT_EQ(filterANSIEscapes("foobar", true, 5), "fooba"); - ASSERT_EQ(filterANSIEscapes("fóóbär", true, 6), "fóóbär"); - ASSERT_EQ(filterANSIEscapes("fóóbär", true, 5), "fóóbä"); - ASSERT_EQ(filterANSIEscapes("fóóbär", true, 3), "fóó"); - ASSERT_EQ(filterANSIEscapes("f€€bär", true, 4), "f€€b"); - ASSERT_EQ(filterANSIEscapes("f𐍈𐍈bär", true, 4), "f𐍈𐍈b"); - } - -} diff --git a/src/libutil/tests/url.cc b/src/libutil/tests/url.cc deleted file mode 100644 index a908631e6..000000000 --- a/src/libutil/tests/url.cc +++ /dev/null @@ -1,338 +0,0 @@ -#include "url.hh" -#include <gtest/gtest.h> - -namespace nix { - -/* ----------- tests for url.hh --------------------------------------------------*/ - - std::string print_map(std::map<std::string, std::string> m) { - std::map<std::string, std::string>::iterator it; - std::string s = "{ "; - for (it = m.begin(); it != m.end(); ++it) { - s += "{ "; - s += it->first; - s += " = "; - s += it->second; - s += " } "; - } - s += "}"; - return s; - } - - - std::ostream& operator<<(std::ostream& os, const ParsedURL& p) { - return os << "\n" - << "url: " << p.url << "\n" - << "base: " << p.base << "\n" - << "scheme: " << p.scheme << "\n" - << "authority: " << p.authority.value() << "\n" - << "path: " << p.path << "\n" - << "query: " << print_map(p.query) << "\n" - << "fragment: " << p.fragment << "\n"; - } - - TEST(parseURL, parsesSimpleHttpUrl) { - auto s = "http://www.example.org/file.tar.gz"; - auto parsed = parseURL(s); - - ParsedURL expected { - .url = "http://www.example.org/file.tar.gz", - .base = "http://www.example.org/file.tar.gz", - .scheme = "http", - .authority = "www.example.org", - .path = "/file.tar.gz", - .query = (StringMap) { }, - .fragment = "", - }; - - ASSERT_EQ(parsed, expected); - } - - TEST(parseURL, parsesSimpleHttpsUrl) { - auto s = "https://www.example.org/file.tar.gz"; - auto parsed = parseURL(s); - - ParsedURL expected { - .url = "https://www.example.org/file.tar.gz", - .base = "https://www.example.org/file.tar.gz", - .scheme = "https", - .authority = "www.example.org", - .path = "/file.tar.gz", - .query = (StringMap) { }, - .fragment = "", - }; - - ASSERT_EQ(parsed, expected); - } - - TEST(parseURL, parsesSimpleHttpUrlWithQueryAndFragment) { - auto s = "https://www.example.org/file.tar.gz?download=fast&when=now#hello"; - auto parsed = parseURL(s); - - ParsedURL expected { - .url = "https://www.example.org/file.tar.gz", - .base = "https://www.example.org/file.tar.gz", - .scheme = "https", - .authority = "www.example.org", - .path = "/file.tar.gz", - .query = (StringMap) { { "download", "fast" }, { "when", "now" } }, - .fragment = "hello", - }; - - ASSERT_EQ(parsed, expected); - } - - TEST(parseURL, parsesSimpleHttpUrlWithComplexFragment) { - auto s = "http://www.example.org/file.tar.gz?field=value#?foo=bar%23"; - auto parsed = parseURL(s); - - ParsedURL expected { - .url = "http://www.example.org/file.tar.gz", - .base = "http://www.example.org/file.tar.gz", - .scheme = "http", - .authority = "www.example.org", - .path = "/file.tar.gz", - .query = (StringMap) { { "field", "value" } }, - .fragment = "?foo=bar#", - }; - - ASSERT_EQ(parsed, expected); - } - - TEST(parseURL, parsesFilePlusHttpsUrl) { - auto s = "file+https://www.example.org/video.mp4"; - auto parsed = parseURL(s); - - ParsedURL expected { - .url = "file+https://www.example.org/video.mp4", - .base = "https://www.example.org/video.mp4", - .scheme = "file+https", - .authority = "www.example.org", - .path = "/video.mp4", - .query = (StringMap) { }, - .fragment = "", - }; - - ASSERT_EQ(parsed, expected); - } - - TEST(parseURL, rejectsAuthorityInUrlsWithFileTransportation) { - auto s = "file://www.example.org/video.mp4"; - ASSERT_THROW(parseURL(s), Error); - } - - TEST(parseURL, parseIPv4Address) { - auto s = "http://127.0.0.1:8080/file.tar.gz?download=fast&when=now#hello"; - auto parsed = parseURL(s); - - ParsedURL expected { - .url = "http://127.0.0.1:8080/file.tar.gz", - .base = "https://127.0.0.1:8080/file.tar.gz", - .scheme = "http", - .authority = "127.0.0.1:8080", - .path = "/file.tar.gz", - .query = (StringMap) { { "download", "fast" }, { "when", "now" } }, - .fragment = "hello", - }; - - ASSERT_EQ(parsed, expected); - } - - TEST(parseURL, parseScopedRFC4007IPv6Address) { - auto s = "http://[fe80::818c:da4d:8975:415c\%enp0s25]:8080"; - auto parsed = parseURL(s); - - ParsedURL expected { - .url = "http://[fe80::818c:da4d:8975:415c\%enp0s25]:8080", - .base = "http://[fe80::818c:da4d:8975:415c\%enp0s25]:8080", - .scheme = "http", - .authority = "[fe80::818c:da4d:8975:415c\%enp0s25]:8080", - .path = "", - .query = (StringMap) { }, - .fragment = "", - }; - - ASSERT_EQ(parsed, expected); - - } - - TEST(parseURL, parseIPv6Address) { - auto s = "http://[2a02:8071:8192:c100:311d:192d:81ac:11ea]:8080"; - auto parsed = parseURL(s); - - ParsedURL expected { - .url = "http://[2a02:8071:8192:c100:311d:192d:81ac:11ea]:8080", - .base = "http://[2a02:8071:8192:c100:311d:192d:81ac:11ea]:8080", - .scheme = "http", - .authority = "[2a02:8071:8192:c100:311d:192d:81ac:11ea]:8080", - .path = "", - .query = (StringMap) { }, - .fragment = "", - }; - - ASSERT_EQ(parsed, expected); - - } - - TEST(parseURL, parseEmptyQueryParams) { - auto s = "http://127.0.0.1:8080/file.tar.gz?&&&&&"; - auto parsed = parseURL(s); - ASSERT_EQ(parsed.query, (StringMap) { }); - } - - TEST(parseURL, parseUserPassword) { - auto s = "http://user:pass@www.example.org:8080/file.tar.gz"; - auto parsed = parseURL(s); - - ParsedURL expected { - .url = "http://user:pass@www.example.org/file.tar.gz", - .base = "http://user:pass@www.example.org/file.tar.gz", - .scheme = "http", - .authority = "user:pass@www.example.org:8080", - .path = "/file.tar.gz", - .query = (StringMap) { }, - .fragment = "", - }; - - - ASSERT_EQ(parsed, expected); - } - - TEST(parseURL, parseFileURLWithQueryAndFragment) { - auto s = "file:///none/of//your/business"; - auto parsed = parseURL(s); - - ParsedURL expected { - .url = "", - .base = "", - .scheme = "file", - .authority = "", - .path = "/none/of//your/business", - .query = (StringMap) { }, - .fragment = "", - }; - - ASSERT_EQ(parsed, expected); - - } - - TEST(parseURL, parsedUrlsIsEqualToItself) { - auto s = "http://www.example.org/file.tar.gz"; - auto url = parseURL(s); - - ASSERT_TRUE(url == url); - } - - TEST(parseURL, parseFTPUrl) { - auto s = "ftp://ftp.nixos.org/downloads/nixos.iso"; - auto parsed = parseURL(s); - - ParsedURL expected { - .url = "ftp://ftp.nixos.org/downloads/nixos.iso", - .base = "ftp://ftp.nixos.org/downloads/nixos.iso", - .scheme = "ftp", - .authority = "ftp.nixos.org", - .path = "/downloads/nixos.iso", - .query = (StringMap) { }, - .fragment = "", - }; - - ASSERT_EQ(parsed, expected); - } - - TEST(parseURL, parsesAnythingInUriFormat) { - auto s = "whatever://github.com/NixOS/nixpkgs.git"; - auto parsed = parseURL(s); - } - - TEST(parseURL, parsesAnythingInUriFormatWithoutDoubleSlash) { - auto s = "whatever:github.com/NixOS/nixpkgs.git"; - auto parsed = parseURL(s); - } - - TEST(parseURL, emptyStringIsInvalidURL) { - ASSERT_THROW(parseURL(""), Error); - } - - /* ---------------------------------------------------------------------------- - * decodeQuery - * --------------------------------------------------------------------------*/ - - TEST(decodeQuery, emptyStringYieldsEmptyMap) { - auto d = decodeQuery(""); - ASSERT_EQ(d, (StringMap) { }); - } - - TEST(decodeQuery, simpleDecode) { - auto d = decodeQuery("yi=one&er=two"); - ASSERT_EQ(d, ((StringMap) { { "yi", "one" }, { "er", "two" } })); - } - - TEST(decodeQuery, decodeUrlEncodedArgs) { - auto d = decodeQuery("arg=%3D%3D%40%3D%3D"); - ASSERT_EQ(d, ((StringMap) { { "arg", "==@==" } })); - } - - TEST(decodeQuery, decodeArgWithEmptyValue) { - auto d = decodeQuery("arg="); - ASSERT_EQ(d, ((StringMap) { { "arg", ""} })); - } - - /* ---------------------------------------------------------------------------- - * percentDecode - * --------------------------------------------------------------------------*/ - - TEST(percentDecode, decodesUrlEncodedString) { - std::string s = "==@=="; - std::string d = percentDecode("%3D%3D%40%3D%3D"); - ASSERT_EQ(d, s); - } - - TEST(percentDecode, multipleDecodesAreIdempotent) { - std::string once = percentDecode("%3D%3D%40%3D%3D"); - std::string twice = percentDecode(once); - - ASSERT_EQ(once, twice); - } - - TEST(percentDecode, trailingPercent) { - std::string s = "==@==%"; - std::string d = percentDecode("%3D%3D%40%3D%3D%25"); - - ASSERT_EQ(d, s); - } - - - /* ---------------------------------------------------------------------------- - * percentEncode - * --------------------------------------------------------------------------*/ - - TEST(percentEncode, encodesUrlEncodedString) { - std::string s = percentEncode("==@=="); - std::string d = "%3D%3D%40%3D%3D"; - ASSERT_EQ(d, s); - } - - TEST(percentEncode, keepArgument) { - std::string a = percentEncode("abd / def"); - std::string b = percentEncode("abd / def", "/"); - ASSERT_EQ(a, "abd%20%2F%20def"); - ASSERT_EQ(b, "abd%20/%20def"); - } - - TEST(percentEncode, inverseOfDecode) { - std::string original = "%3D%3D%40%3D%3D"; - std::string once = percentEncode(original); - std::string back = percentDecode(once); - - ASSERT_EQ(back, original); - } - - TEST(percentEncode, trailingPercent) { - std::string s = percentEncode("==@==%"); - std::string d = "%3D%3D%40%3D%3D%25"; - - ASSERT_EQ(d, s); - } - -} diff --git a/src/libutil/tests/xml-writer.cc b/src/libutil/tests/xml-writer.cc deleted file mode 100644 index adcde25c9..000000000 --- a/src/libutil/tests/xml-writer.cc +++ /dev/null @@ -1,105 +0,0 @@ -#include "xml-writer.hh" -#include <gtest/gtest.h> -#include <sstream> - -namespace nix { - - /* ---------------------------------------------------------------------------- - * XMLWriter - * --------------------------------------------------------------------------*/ - - TEST(XMLWriter, emptyObject) { - std::stringstream out; - { - XMLWriter t(false, out); - } - - ASSERT_EQ(out.str(), "<?xml version='1.0' encoding='utf-8'?>\n"); - } - - TEST(XMLWriter, objectWithEmptyElement) { - std::stringstream out; - { - XMLWriter t(false, out); - t.openElement("foobar"); - } - - ASSERT_EQ(out.str(), "<?xml version='1.0' encoding='utf-8'?>\n<foobar></foobar>"); - } - - TEST(XMLWriter, objectWithElementWithAttrs) { - std::stringstream out; - { - XMLWriter t(false, out); - XMLAttrs attrs = { - { "foo", "bar" } - }; - t.openElement("foobar", attrs); - } - - ASSERT_EQ(out.str(), "<?xml version='1.0' encoding='utf-8'?>\n<foobar foo=\"bar\"></foobar>"); - } - - TEST(XMLWriter, objectWithElementWithEmptyAttrs) { - std::stringstream out; - { - XMLWriter t(false, out); - XMLAttrs attrs = {}; - t.openElement("foobar", attrs); - } - - ASSERT_EQ(out.str(), "<?xml version='1.0' encoding='utf-8'?>\n<foobar></foobar>"); - } - - TEST(XMLWriter, objectWithElementWithAttrsEscaping) { - std::stringstream out; - { - XMLWriter t(false, out); - XMLAttrs attrs = { - { "<key>", "<value>" } - }; - t.openElement("foobar", attrs); - } - - // XXX: While "<value>" is escaped, "<key>" isn't which I think is a bug. - ASSERT_EQ(out.str(), "<?xml version='1.0' encoding='utf-8'?>\n<foobar <key>=\"<value>\"></foobar>"); - } - - TEST(XMLWriter, objectWithElementWithAttrsIndented) { - std::stringstream out; - { - XMLWriter t(true, out); - XMLAttrs attrs = { - { "foo", "bar" } - }; - t.openElement("foobar", attrs); - } - - ASSERT_EQ(out.str(), "<?xml version='1.0' encoding='utf-8'?>\n<foobar foo=\"bar\">\n</foobar>\n"); - } - - TEST(XMLWriter, writeEmptyElement) { - std::stringstream out; - { - XMLWriter t(false, out); - t.writeEmptyElement("foobar"); - } - - ASSERT_EQ(out.str(), "<?xml version='1.0' encoding='utf-8'?>\n<foobar />"); - } - - TEST(XMLWriter, writeEmptyElementWithAttributes) { - std::stringstream out; - { - XMLWriter t(false, out); - XMLAttrs attrs = { - { "foo", "bar" } - }; - t.writeEmptyElement("foobar", attrs); - - } - - ASSERT_EQ(out.str(), "<?xml version='1.0' encoding='utf-8'?>\n<foobar foo=\"bar\" />"); - } - -} |