navid/veins / 4d6f82fdba18f6ff7edfc7aae8c1f3e78bb0b7fe7cb7224d40c4dd7bcdc7c069 / xveins/tests/integration/worktree_tests.cpp
worktree_tests.cpp
← xveins/tests/integration · 11488 bytes · raw · download
#include "test_support.h"
#include "integration/support.h"
namespace vn::integration {
namespace {
void worktree_rename_and_delete(test::Context& t) {
VnFixture f;
f.init();
f.write("a.txt", "alpha\n");
f.write("b.txt", "beta\n");
f.commit("base");
f.vn({"mv", "a.txt", "renamed.txt"});
t.expect(!f.exists("a.txt"));
t.expect_eq(f.read("renamed.txt"), "alpha\n");
const std::string status = f.vn({"stat"}).out;
t.expect_contains(status, "D a.txt");
t.expect_contains(status, "A renamed.txt");
f.commit("rename");
t.expect_contains(f.vn({"ls", "-plain"}).out, "renamed.txt");
fs::remove(f.repo / "b.txt");
t.expect_contains(f.vn({"stat"}).out, "D b.txt");
f.commit("delete");
t.expect_not_contains(f.vn({"ls", "-plain"}).out, "b.txt");
}
void worktree_exclude_and_remove(test::Context& t) {
VnFixture f;
f.init();
f.write("src/a.c", "a\n");
f.write("src/b.c", "b\n");
f.write("docs/readme.md", "docs\n");
f.write("top.txt", "top\n");
t.expect_contains(f.vn({"rm", "src/*.c"}).out, "will be re-added");
f.vn({"ex", "src/*.c"});
t.expect_contains(f.read(".vnexclude"), "src/*.c");
const std::string tracked = f.vn({"ls", "-plain"}).out;
t.expect_not_contains(tracked, "src/a.c");
t.expect_not_contains(tracked, "src/b.c");
t.expect(f.exists("src/a.c"));
t.expect(f.exists("src/b.c"));
f.vn({"rm", "-delete", "docs/readme.md"});
t.expect(!f.exists("docs/readme.md"));
t.expect_not_contains(f.vn({"ls", "-plain"}).out, "docs/readme.md");
t.expect_contains(f.vn({"ex", "src/*.c"}).out, "no new exclude rules");
f.failed({"rm", "missing.txt"}, "not tracked");
}
void worktree_ignore_rules(test::Context& t) {
VnFixture f;
f.init();
f.write(".gitignore", "*.log\nbuild/\n");
f.write("debug.log", "ignored\n");
f.write("build/out.bin", "ignored\n");
f.write("src/.gitignore", "*.tmp\n");
f.write("src/notes.tmp", "ignored\n");
f.write("src/main.c", "kept\n");
std::vector<std::string> tracked = split_lines(f.vn({"ls", "-plain"}).out);
tracked.erase(std::remove(tracked.begin(), tracked.end(), ""), tracked.end());
std::sort(tracked.begin(), tracked.end());
t.expect_eq(tracked.size(), 3u);
if (tracked.size() == 3) {
t.expect_eq(tracked[0], ".gitignore");
t.expect_eq(tracked[1], "src/.gitignore");
t.expect_eq(tracked[2], "src/main.c");
}
f.write("root.log", "ignored\n");
t.expect_not_contains(f.vn({"stat"}).out, "root.log");
}
void worktree_checkout_removes_later_files(test::Context& t) {
VnFixture f;
f.init();
f.write("keep.txt", "keep\n");
f.commit("first");
const std::string first = f.commit_for_message("first");
f.write("later.txt", "later\n");
f.write("keep.txt", "changed\n");
f.commit("second");
f.vn({"co", first});
t.expect(f.exists("keep.txt"));
t.expect(!f.exists("later.txt"));
t.expect_eq(f.read("keep.txt"), "keep\n");
t.expect_contains(f.vn({"stat"}).out, "working copy clean");
}
void worktree_editor(test::Context& t) {
#if defined(_WIN32)
t.expect(true);
#else
VnFixture f;
f.init();
const fs::path editor = f.root / "editor.sh";
f.write("editor.sh", "#!/bin/sh\nprintf \"edited:%s\\n\" \"$1\" > \"$1\"\n", f.root);
fs::permissions(editor, fs::perms::owner_exec | fs::perms::owner_read | fs::perms::owner_write,
fs::perm_options::add);
f.vn({"cfg", "editor", editor.string()});
f.write("target.txt", "before\n");
f.vn({"edit", "target.txt"});
t.expect_eq(f.read("target.txt"), "edited:target.txt\n");
f.vn({"cfg", "-unset", "editor"});
#endif
}
void worktree_attributes_eol(test::Context& t) {
VnFixture f;
f.init();
f.write(".vnattribs", "*.txt txt eol:lf\n*.cmd txt eol:crlf\n");
f.write("a.txt", "a\r\nb\r\n");
f.write("b.cmd", "x\ny\n");
f.commit("base");
const std::string base = f.commit_for_message("base");
fs::remove(f.repo / "a.txt");
fs::remove(f.repo / "b.cmd");
f.vn({"co", base});
t.expect_eq(f.read("a.txt"), "a\nb\n");
t.expect_eq(f.read("b.cmd"), "x\r\ny\r\n");
}
void worktree_inf_generated(test::Context& t) {
VnFixture f;
f.init();
f.write(".vnattribs", "*.ssa gen\n*.c txt eol:lf\n");
f.write("main.c", "one\ntwo\n");
f.write("out.ssa", "one\ntwo\nthree\n");
f.commit("base");
const std::string default_output = f.vn({"inf"}).out;
t.expect_contains(default_output, "Generated");
t.expect_contains(default_output, "1 files");
t.expect_contains(default_output, "excluded; use -inc-gen");
t.expect_not_contains(default_output, ".ssa");
const std::string included_output = f.vn({"inf", "-inc-gen"}).out;
t.expect_contains(included_output, ".ssa");
t.expect_not_contains(included_output, "excluded; use -inc-gen");
}
void worktree_attributes_command(test::Context& t) {
VnFixture f;
f.init();
t.expect_contains(f.vn({"attr", "gen", "*.ssa", "*.bin"}).out, "updated 2");
t.expect_contains(f.read(".vnattribs"), "*.ssa gen\n*.bin gen\n");
f.vn({"attr", "bin", "*.exe"});
f.vn({"attr", "lf", "*.c"});
f.vn({"attr", "crlf", "*.cmd"});
const std::string rules = f.read(".vnattribs");
t.expect_contains(rules, "*.exe bin\n");
t.expect_contains(rules, "*.c eol:lf\n");
t.expect_contains(rules, "*.cmd eol:crlf\n");
f.vn({"attr", "txt", "*.c"});
t.expect_contains(f.read(".vnattribs"), "*.c eol:lf txt\n");
f.vn({"attr", "crlf", "*.c"});
t.expect_contains(f.read(".vnattribs"), "*.c txt eol:crlf\n");
t.expect_not_contains(f.read(".vnattribs"), "eol:lf");
t.expect_contains(f.vn({"attr", "crlf", "*.c"}).out, "no new attribute rules");
f.vn({"attr", "gen", "*.c"});
t.expect_contains(f.read(".vnattribs"), "*.c txt eol:crlf gen\n");
f.vn({"attr", "rm", "gen", "*.c"});
t.expect_not_contains(f.read(".vnattribs"), "*.c txt eol:crlf gen");
f.vn({"attr", "rm", "crlf", "*.c"});
t.expect_contains(f.read(".vnattribs"), "*.c txt\n");
f.vn({"attr", "rm", "txt", "*.c"});
t.expect_not_contains(f.read(".vnattribs"), "*.c txt");
t.expect_not_contains(f.read(".vnattribs"), "*.c eol");
f.vn({"attr", "rm", "gen", "*.ssa"});
t.expect_not_contains(f.read(".vnattribs"), "*.ssa");
t.expect_contains(f.vn({"attr", "rm", "lf", "*.missing"}).out, "no matching attribute rules");
fs::create_directories(f.repo / "src");
f.vn({"attr", "gen", "*.ssa"}, f.repo / "src");
t.expect_contains(f.read(".vnattribs"), "src/*.ssa gen\n");
f.write(".vnattribs", "# keep this comment\n*.c gen\n\n*.c eol:lf\n*.tmp -gen\n");
const std::string listed = f.vn({"attr", "ls"}).out;
t.expect_contains(listed, "*.c gen\n");
t.expect_contains(listed, "*.c eol:lf\n");
t.expect_not_contains(listed, "# keep this comment");
t.expect_contains(listed, "*.tmp -gen\n");
f.vn({"attr", "rm", "gen", "*.c"});
const std::string after_remove = f.read(".vnattribs");
t.expect_contains(after_remove, "# keep this comment\n");
t.expect_contains(after_remove, "*.c eol:lf\n");
t.expect_not_contains(after_remove, "*.c gen");
f.vn({"attr", "rm", "gen", "*.tmp"});
t.expect_not_contains(f.read(".vnattribs"), "-gen");
VnFixture outside;
outside.failed({"attr", "gen", "*.c"}, "not a veins repository");
}
void worktree_attributes_command_edges(test::Context& t) {
VnFixture f;
f.init();
t.expect_eq(f.vn({"attr", "ls"}).out, "");
t.expect_contains(f.vn({"attr", "rm", "gen", "*.c"}).out, "no matching attribute rules");
t.expect(!f.exists(".vnattribs"));
f.write(".vnattribs", "# leading comment\r\n\r\n*.c gen\r\n*.c txt\r\n*.cmd txt eol:crlf\r\n");
t.expect_eq(f.vn({"attr", "ls"}).out, "*.c gen\n*.c txt\n*.cmd txt eol:crlf\n");
f.vn({"attr", "bin", "*.c"});
t.expect_eq(f.read(".vnattribs"),
"# leading comment\r\n\r\n*.c gen\r\n*.c bin\r\n*.cmd txt eol:crlf\r\n");
f.vn({"attr", "rm", "gen", "*.c"});
t.expect_eq(f.read(".vnattribs"), "# leading comment\r\n\r\n*.c bin\r\n*.cmd txt eol:crlf\r\n");
f.vn({"attr", "rm", "bin", "*.c"});
t.expect_eq(f.read(".vnattribs"), "# leading comment\r\n\r\n*.cmd txt eol:crlf\r\n");
f.write(".vnattribs", "*.c future eol:cr -gen\n*.d -bin\n*.e -txt\n*.f txt -eol\n");
f.vn({"attr", "txt", "*.c"});
t.expect_eq(f.read(".vnattribs"),
"*.c future eol:cr -gen txt\n*.d -bin\n*.e -txt\n*.f txt -eol\n");
f.vn({"attr", "gen", "*.c"});
t.expect_eq(f.read(".vnattribs"),
"*.c future eol:cr txt gen\n*.d -bin\n*.e -txt\n*.f txt -eol\n");
f.vn({"attr", "crlf", "*.c"});
t.expect_eq(f.read(".vnattribs"),
"*.c future txt gen eol:crlf\n*.d -bin\n*.e -txt\n*.f txt -eol\n");
f.vn({"attr", "rm", "bin", "*.d"});
f.vn({"attr", "rm", "txt", "*.e"});
f.vn({"attr", "lf", "*.f"});
t.expect_eq(f.read(".vnattribs"), "*.c future txt gen eol:crlf\n*.f txt eol:lf\n");
f.write(".vnattribs", "*.c txt");
f.vn({"attr", "gen", "*.c"});
t.expect_eq(f.read(".vnattribs"), "*.c txt gen\n");
f.write(".vnattribs", "*.tmp gen");
f.vn({"attr", "gen", "*.new"});
t.expect_eq(f.read(".vnattribs"), "*.tmp gen\n*.new gen\n");
f.write(".vnattribs", "*.c txt\n");
const std::string unchanged = f.read(".vnattribs");
t.expect_contains(f.vn({"attr", "rm", "gen", "*.c"}).out, "no matching attribute rules");
t.expect_eq(f.read(".vnattribs"), unchanged);
t.expect_eq(f.vn({"attr", "ls", "--"}).out, "*.c txt\n");
f.failed({"attr", "gen", "bad pattern"}, "not a valid attribute pattern");
f.failed({"attr", "gen", "#comment"}, "not a valid attribute pattern");
f.failed({"attr", "gen", ""}, "not a valid attribute pattern");
t.expect_eq(f.read(".vnattribs"), unchanged);
f.failed({"attr"}, "requires a subcommand");
f.failed({"attr", "gen"}, "requires at least one glob");
f.failed({"attr", "rm"}, "usage: vn attr rm");
f.failed({"attr", "rm", "gen"}, "usage: vn attr rm");
f.failed({"attr", "wat", "*.c"}, "unknown attr subcommand");
f.failed({"attr", "rm", "eol", "*.c"}, "unknown attribute");
f.failed({"attr", "gen", "-x"}, "unknown flag");
f.failed({"attr", "ls", "extra"}, "usage: vn attr ls");
f.write(".vnattribs", "");
f.vn({"attr", "gen", "--", "*.c"});
t.expect_eq(f.read(".vnattribs"), "*.c gen\n");
VnFixture outside;
outside.failed({"attr", "ls"}, "not a veins repository");
outside.failed({"attr", "gen", "*.c"}, "not a veins repository");
outside.failed({"attr", "rm", "gen", "*.c"}, "not a veins repository");
}
} // namespace
test::Suite worktree_suite() {
return {"worktree",
{
{"rename_and_delete", worktree_rename_and_delete},
{"exclude_and_remove", worktree_exclude_and_remove},
{"ignore_rules", worktree_ignore_rules},
{"checkout_removes_later_files", worktree_checkout_removes_later_files},
{"attributes_eol", worktree_attributes_eol},
{"inf_generated", worktree_inf_generated},
{"attributes_command", worktree_attributes_command},
{"attributes_command_edges", worktree_attributes_command_edges},
{"editor", worktree_editor},
}};
}
} // namespace vn::integration