navid/veins / ac26a51bd7b5f4a22beb0ca203a433857c68b337b79aa5ce14683290ab9ae3a7 / xveins/tests/integration/config_tests.cpp

config_tests.cpp

← xveins/tests/integration · 4266 bytes · raw · download

#include "test_support.h"

#include "integration/support.h"

namespace vn::integration {
namespace {

void config_local(test::Context& t) {
    VnFixture f;
    f.init();
    f.vn({"cfg", "test.key", "value"});
    t.expect_eq(trim_copy(f.vn({"cfg", "test.key"}).out), "value");
    t.expect_contains(f.vn({"cfg"}).out, "test.key = value");
    f.vn({"cfg", "set", "test.other", "two"});
    t.expect_eq(trim_copy(f.vn({"cfg", "get", "test.other"}).out), "two");
    f.vn({"cfg", "-unset", "test.key"});
    f.failed({"cfg", "test.key"}, "unknown config key");
    f.vn({"cfg", "unset", "test.other"});
    f.failed({"cfg", "test.other"}, "unknown config key");
}

void config_global_identity(test::Context& t) {
    VnFixture f;
    f.init();
    f.vn({"cfg", "-global", "user.name", "Global Tester"});
    f.vn({"cfg", "-global", "user.email", "global@example.com"});
    t.expect_eq(trim_copy(f.vn({"cfg", "-global", "user.name"}).out), "Global Tester");
    f.write("f.txt", "content\n");
    f.commit("global identity");
    t.expect_contains(f.vn({"show", f.commit_for_message("global identity")}).out, "Global Tester");
    t.expect(fs::exists(f.home / ".local" / "config" / "veins" / "veins.toml"));
}

void config_errors(test::Context& t) {
    (void)t;
    VnFixture f;
    f.init();
    f.failed({"cfg", "key", "value", "extra"}, "accepts a key and one value");
    f.failed({"cfg", "-unset"}, "requires one key");
    f.failed({"cfg", "unknown"}, "unknown config key");
}

void config_defaults(test::Context& t) {
    VnFixture f;
    const fs::path outside = f.root / "outside";
    fs::create_directories(outside);
#if defined(_WIN32)
    const std::string editor_default = "notepad";
    const std::string pager_default = "more";
#else
    const std::string editor_default = "vi";
    const std::string pager_default = "less -R";
#endif
    const std::string defaults = f.vn({"cfg", "-d"}, outside).out;
    t.expect_contains(defaults, "log.default_amount = 20");
    t.expect_contains(defaults, "rename.autodetect = similar");
    t.expect_contains(defaults, "merge.adjacent = strict");
    t.expect_contains(defaults, "user.name = (unset)");
    t.expect_contains(defaults, "editor = " + editor_default);
    t.expect_contains(defaults, "pager = " + pager_default);
    t.expect_not_contains(defaults, "bookmark.current");

    f.vn({"cfg", "-global", "log.default_amount", "50"}, outside);
    f.vn({"cfg", "-global", "editor", "code --wait"}, outside);
    const std::string with_current = f.vn({"cfg", "-d", "-global"}, outside).out;
    t.expect_contains(with_current, "log.default_amount = 20 (current: 50)");
    t.expect_contains(with_current, "editor = " + editor_default + " (current: code --wait)");

    f.init();
    f.vn({"cfg", "log.default_amount", "1"});
    const std::string local_ignored = f.vn({"cfg", "-d"}).out;
    t.expect_contains(local_ignored, "log.default_amount = 20 (current: 50)");
    t.expect_not_contains(local_ignored, "current: 1");

    const std::string internal = f.vn({"cfg", "-i", "-d"}).out;
    t.expect_contains(internal, "bookmark.current = (unset)");
    t.expect_contains(internal, "remote.<name>.url = (unset)");
    t.expect_contains(internal, "promisor = (unset)");
    const std::string long_internal = f.vn({"cfg", "-internal", "-defaults"}).out;
    t.expect_contains(long_internal, "bookmark.current = (unset)");

    f.failed({"cfg", "-d", "extra"}, "takes no operands");
    f.failed({"cfg", "-d", "-list"}, "takes no operands");
    f.failed({"cfg", "-i"}, "requires -d");
    f.failed({"cfg", "--defaults"}, "unknown flag");
    f.failed({"cfg", "--global"}, "unknown flag");
}

void config_pager(test::Context& t) {
    VnFixture f;
    f.init();
    f.vn({"cfg", "pager", "cat"});
    t.expect_eq(pager_command(f.repo), "cat");
    f.vn({"cfg", "-global", "pager", "more"});
    t.expect_eq(pager_command(f.repo), "more");
}

} // namespace

test::Suite config_suite() {
    return {"config",
            {
                {"local", config_local},
                {"global_identity", config_global_identity},
                {"errors", config_errors},
                {"defaults", config_defaults},
                {"pager", config_pager},
            }};
}
} // namespace vn::integration