navid/veins / 83508482911f348af86cb892949a35bdce0d6b2f949b4a0faab7cba9a499fb82 / xveins/tests/integration/protocol_tests.cpp

protocol_tests.cpp

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

#include "test_support.h"

#include "integration/support.h"

namespace vn::integration {
namespace {

void protocol_clone_whoami(test::Context& t) {
    VnFixture f;
    make_source_repository(f);
    const fs::path source = f.root / "source";
    const fs::path clone = f.root / "clone";
    fs::create_directories(clone);
    f.vn({"clone", "file://" + source.string(), clone.string()}, f.root);
    t.expect_eq(f.read("a.txt", clone), "hello\n");
    t.expect_contains(f.vn({"log"}, clone).out, "initial");
    t.expect_contains(f.vn({"stat"}, clone).out, "working copy clean");
    const std::string remotes = f.vn({"remote", "list"}, clone).out;
    t.expect_contains(remotes, "o\t");
    t.expect_not_contains(remotes, "origin");
    t.expect_contains(f.vn({"cfg", "remote.o.url"}, clone).out, "file://");
    t.expect_contains(f.vn({"cfg", "remote.default"}, clone).out, "o");
    t.expect_contains(f.vn({"whoami", "o"}, clone).out, "role: read");
    t.expect_contains(f.vn({"whoami"}, clone).out, "role: read");
}

void protocol_push_fetch(test::Context& t) {
    VnFixture f;
    make_source_repository(f);
    const fs::path source = f.root / "source";
    const std::string key_info = f.vn({"key", "generate", "integration"}, source).out;
    std::string public_key;
    for (const std::string& line : split_lines(key_info)) {
        if (line.starts_with("public key: ")) {
            public_key = line.substr(std::string("public key: ").size());
            break;
        }
    }
    t.expect(!public_key.empty());
    f.vn({"access", "add", "-role", "write", "-key", public_key, "-name", "integration"}, source);
    t.expect_contains(f.vn({"key", "show", "integration"}, source).out, "fingerprint:");
    t.expect_contains(f.vn({"key", "list"}, source).out, "integration");
    f.vn({"auth", "use", "integration"}, source);
    t.expect_contains(f.vn({"auth", "list"}, source).out, "integration");

    const fs::path clone_a = f.root / "clone-a";
    f.vn({"clone", "file://" + source.string(), clone_a.string()}, f.root);
    f.vn({"cfg", "remote.o.key", "integration"}, clone_a);
    f.write("a.txt", "from clone a\n", clone_a);
    f.commit("clone a change", clone_a);
    f.write("c.txt", "from clone a again\n", clone_a);
    f.commit("clone a second change", clone_a);
    const CommandResult pushed = f.vn({"psh", "o", "master"}, clone_a);
    t.expect_contains(pushed.out, "pushed master");
    t.expect_contains(f.vn({"log"}, source).out, "clone a change");
    t.expect_contains(f.vn({"log"}, source).out, "clone a second change");
    t.expect_contains(f.vn({"whoami", "o"}, clone_a).out, "role: write");

    f.vn({"tag", "set", "v0.0.1"}, clone_a);
    const CommandResult tag_pushed = f.vn({"push"}, clone_a);
    t.expect_contains(tag_pushed.out, "pushed 1 tag(s)");
    t.expect_contains(f.vn({"tag", "ls"}, source).out, "v0.0.1");

    const fs::path clone_b = f.root / "clone-b";
    f.vn({"clone", "file://" + source.string(), clone_b.string()}, f.root);
    f.vn({"cfg", "remote.o.key", "integration"}, clone_b);
    f.write("b.txt", "from clone b\n", clone_b);
    f.commit("clone b change", clone_b);
    f.vn({"push", "o", "master"}, clone_b);
    f.vn({"fetch", "o"}, clone_a);
    t.expect_contains(f.vn({"log", "-rev", "o/master"}, clone_a).out, "clone b change");
    f.vn({"fetch"}, clone_a);
    t.expect_contains(f.vn({"log", "-rev", "o/master"}, clone_a).out, "clone b change");
}

void protocol_pull_fast_forwards_and_detects_divergence(test::Context& t) {
    VnFixture f;
    make_source_repository(f);
    const fs::path source = f.root / "source";
    const std::string key_info = f.vn({"key", "generate", "integration"}, source).out;
    std::string public_key;
    for (const std::string& line : split_lines(key_info)) {
        if (line.starts_with("public key: ")) {
            public_key = line.substr(std::string("public key: ").size());
            break;
        }
    }
    t.expect(!public_key.empty());
    f.vn({"access", "add", "-role", "write", "-key", public_key, "-name", "integration"}, source);

    const fs::path clone_a = f.root / "clone-a";
    const fs::path clone_b = f.root / "clone-b";
    f.vn({"clone", "file://" + source.string(), clone_a.string()}, f.root);
    f.vn({"clone", "file://" + source.string(), clone_b.string()}, f.root);
    f.vn({"cfg", "remote.o.key", "integration"}, clone_a);
    f.vn({"cfg", "remote.o.key", "integration"}, clone_b);

    f.write("b.txt", "from b\n", clone_b);
    f.commit("b change", clone_b);
    f.vn({"psh", "o", "master"}, clone_b);

    const CommandResult pulled = f.vn({"pl"}, clone_a);
    t.expect_contains(pulled.out, "pulled master from o");
    t.expect(f.exists("b.txt", clone_a));
    t.expect_eq(f.read("b.txt", clone_a), "from b\n");
    t.expect_contains(f.vn({"stat"}, clone_a).out, "working copy clean");
    t.expect_contains(f.vn({"pull"}, clone_a).out, "already up to date");

    f.write("a.txt", "dirty\n", clone_a);
    f.failed({"pull"}, "uncommitted changes", clone_a);
    f.commit("local change", clone_a);

    f.write("b2.txt", "from b again\n", clone_b);
    f.commit("b second change", clone_b);
    f.vn({"psh", "o", "master"}, clone_b);

    f.failed({"pull"}, "diverged", clone_a);
    t.expect(!f.exists("b2.txt", clone_a));
}

void protocol_lazy_checkout_old_commit(test::Context& t) {
    VnFixture f;
    const fs::path source = f.root / "source";
    fs::create_directories(source);
    f.init(source);
    for (int i = 1; i <= 20; ++i)
        f.write("f" + std::to_string(i) + ".txt", "old " + std::to_string(i) + "\n", source);
    f.write("a.txt", "hello\n", source);
    f.vn({"bmk", "set", "master"}, source);
    f.commit("initial", source);
    f.vn({"access", "anonymous", "on"}, source);

    for (int i = 1; i <= 20; ++i)
        f.write("f" + std::to_string(i) + ".txt", "new " + std::to_string(i) + "\n", source);
    f.write("a.txt", "updated\n", source);
    f.commit("second", source);
    const std::string first = f.commit_for_message("initial", source);

    const fs::path clone = f.root / "clone";
    f.vn({"clone", "file://" + source.string(), clone.string()}, f.root);
    f.vn({"co", first}, clone);
    t.expect_eq(f.read("a.txt", clone), "hello\n");
    t.expect_eq(f.read("f1.txt", clone), "old 1\n");
    t.expect_contains(f.vn({"stat"}, clone).out, "working copy clean");
}

void protocol_bad_inputs(test::Context& t) {
    (void)t;
    VnFixture f;
    f.init();
    f.failed({"clone", "not-a-url", "dest"}, "unsupported URL");
    f.failed({"clone", "file:///does/not/exist", "dest"}, "error:");

    const fs::path occupied = f.root / "occupied";
    fs::create_directories(occupied);
    if (!write_file(occupied / "keep.txt", bytes_of("keep\n")))
        throw test::Failure("cannot write occupied directory file");
    const std::string source = "file://" + f.repo.string();
    f.failed({"clone", source, occupied.string()}, "destination directory is not empty", f.root);
    f.failed({"gg", source, occupied.string()}, "destination directory is not empty", f.root);
    f.failed({"clone", source, (occupied / "keep.txt").string()},
             "destination already exists and is not a directory", f.root);
}

} // namespace

test::Suite protocol_suite() {
    return {"protocol",
            {
                {"clone_whoami", protocol_clone_whoami},
                {"push_fetch", protocol_push_fetch},
                {"pull_fast_forwards_and_detects_divergence",
                 protocol_pull_fast_forwards_and_detects_divergence},
                {"lazy_checkout_old_commit", protocol_lazy_checkout_old_commit},
                {"bad_inputs", protocol_bad_inputs},
            }};
}
} // namespace vn::integration