navid/veins / b314db4a57679115094595d043fcc5b30d359768e2abd8eb72be3a14203a1f26 / xveins/tests/unit/worktree_tests.cpp
worktree_tests.cpp
← xveins/tests/unit · 8839 bytes · raw · download
#include "test_support.h"
#include "core/common.h"
#include "unit/unit_helpers.h"
#include "worktree/attributes.h"
#include "worktree/pathspec.h"
#include <string>
#include <vector>
namespace vn::unit {
namespace {
void test_pathspecs(test::Context& t) {
t.expect(pathspec_has_glob("src/*.cpp"));
t.expect(pathspec_has_glob("a?.txt"));
t.expect(!pathspec_has_glob("src/file.cpp"));
const fs::path root = "/tmp/vn-pathspec/repo";
const auto relative = pathspec_normalize(root, "sub", "./a/../b.txt");
t.expect(relative.has_value());
if (relative)
t.expect_eq(*relative, "sub/b.txt");
t.expect(!pathspec_normalize(root, "", "../outside").has_value());
t.expect(!pathspec_normalize(root, "", "").has_value());
const auto absolute = pathspec_normalize(root, "", "/tmp/vn-pathspec/repo/a/./b");
t.expect(absolute.has_value());
if (absolute)
t.expect_eq(*absolute, "a/b");
t.expect(!pathspec_normalize(root, "", "/tmp/vn-pathspec/other/a").has_value());
t.expect(!pathspec_normalize(root, "", "/tmp/vn-pathspec/repo").has_value());
const auto collapsed = pathspec_normalize(root, "", "a//b///c");
t.expect(collapsed.has_value());
if (collapsed)
t.expect_eq(*collapsed, "a/b/c");
const auto trailing = pathspec_normalize(root, "", "a/b/");
t.expect(trailing.has_value());
if (trailing)
t.expect_eq(*trailing, "a/b");
t.expect(!pathspec_normalize(root, "a", "../../b").has_value());
const auto up = pathspec_normalize(root, "a/b", "../c");
t.expect(up.has_value());
if (up)
t.expect_eq(*up, "a/c");
}
void test_ignore_rules(test::Context& t) {
t.expect_eq(glob_match("*.tmp", "file.tmp"), true);
t.expect_eq(glob_match("*.tmp", "file.txt"), false);
t.expect_eq(glob_match("a?c", "abc"), true);
t.expect_eq(glob_match("a?c", "a/c"), false);
t.expect_eq(glob_match("src/**/x", "src/a/b/x"), true);
t.expect_eq(glob_match("*", "a/b"), false);
t.expect_eq(glob_match("**/Testing/Temporary/*", "xveins/tests/Testing/Temporary/CTest.log"),
true);
t.expect_eq(glob_match("*/Testing/Temporary/*", "xveins/tests/Testing/Temporary/CTest.log"),
false);
t.expect_eq(glob_match("", ""), true);
t.expect_eq(glob_match("", "x"), false);
t.expect_eq(glob_match("abc", "abc"), true);
t.expect_eq(glob_match("abc", "abcd"), false);
const TempDir temp;
t.expect(
write_file(temp.path / ".vnexclude", bytes_of("*.tmp\nbuild/\n**/Testing/Temporary/*\n")));
t.expect(ignore_path_ignored(temp.path, "note.tmp"));
t.expect(ignore_path_ignored(temp.path, "build/out.bin"));
t.expect(ignore_path_ignored(temp.path, "xveins/tests/Testing/Temporary/CTest.log"));
t.expect(!ignore_path_ignored(temp.path, "note.txt"));
IgnoreRules rules = ignore_empty();
rules.items.push_back(IgnoreRule{"*.log", false, false, false, false, ""});
rules.items.push_back(IgnoreRule{"keep.log", true, false, false, false, ""});
t.expect(ignore_ignored(rules, "debug.log", false));
t.expect(!ignore_ignored(rules, "keep.log", false));
t.expect(valid_name("file.txt"));
t.expect(!valid_name("dir/file"));
t.expect(!valid_name(".veins"));
t.expect(!valid_name(".."));
t.expect(!valid_name(""));
t.expect(!valid_name(std::string(1, '\0')));
t.expect(valid_path("a/b/c"));
t.expect(!valid_path("/a"));
t.expect(!valid_path("a//b"));
t.expect(!valid_path("a/../b"));
t.expect(!valid_path(""));
}
void test_glob_hostile_patterns(test::Context& t) {
// Regression: the recursive matcher exhausted the stack on these inputs.
std::string pattern;
pattern.reserve(40'000);
for (int i = 0; i < 20'000; ++i)
pattern += "a*";
const std::string text(50'000, 'a');
(void)glob_match(pattern, text);
const std::string stars(100'000, '*');
const std::string letters(100'000, 'a');
t.expect_eq(glob_match(stars, letters), true);
const std::string double_stars = "**/" + std::string(50'000, '*');
(void)glob_match(double_stars, letters);
const std::string long_literal(60'000, 'z');
t.expect_eq(glob_match(long_literal, long_literal), true);
t.expect_eq(glob_match(long_literal, long_literal + "x"), false);
IgnoreRules rules = ignore_empty();
rules.items.push_back(IgnoreRule{stars, false, false, false, false, ""});
(void)ignore_ignored(rules, "some/deep/path.txt", false);
}
void test_attributes(test::Context& t) {
const TempDir temp;
const std::string rules_text = "*.c txt eol:lf\n"
"*.cmd txt eol:crlf\n"
"*.ssa gen\n"
"**/generated/* gen\n"
"*.bin gen bin\n"
"special.bin -gen\n";
t.expect(write_file(temp.path / ".vnattribs", bytes_of(rules_text)));
const AttributeRules rules = AttributeRules::load(temp.path);
const auto source = rules.lookup("src/main.c");
t.expect(source.text == AttributeText::Text);
t.expect(source.eol == AttributeEol::Lf);
t.expect(!source.generated);
const auto script = rules.lookup("scripts/build.cmd");
t.expect(script.text == AttributeText::Text);
t.expect(script.eol == AttributeEol::Crlf);
t.expect(rules.lookup("build/out.ssa").generated);
t.expect(rules.lookup("pkg/generated/table.h").generated);
const auto binary = rules.lookup("assets/blob.bin");
t.expect(binary.generated);
t.expect(binary.text == AttributeText::Binary);
const auto overridden = rules.lookup("special.bin");
t.expect(!overridden.generated);
t.expect(overridden.text == AttributeText::Binary);
const auto plain = rules.lookup("notes.txt");
t.expect(plain.text == AttributeText::Auto);
t.expect(plain.eol == AttributeEol::None);
t.expect(!plain.generated);
const auto lf = normalize_eol(bytes_of("a\r\nb\rc\nd"), AttributeEol::Lf);
t.expect_eq(to_string(bytes_of(lf)), "a\nb\nc\nd");
const auto crlf = normalize_eol(bytes_of("a\nb\rc\r\n"), AttributeEol::Crlf);
t.expect_eq(to_string(bytes_of(crlf)), "a\r\nb\r\nc\r\n");
const auto cr = normalize_eol(bytes_of("a\nb\r\nc\r"), AttributeEol::Cr);
t.expect_eq(to_string(bytes_of(cr)), "a\rb\rc\r");
const auto untouched = normalize_eol(bytes_of("a\r\nb"), AttributeEol::None);
t.expect_eq(to_string(bytes_of(untouched)), "a\r\nb");
t.expect(normalize_eol(Bytes{}, AttributeEol::Lf).empty());
}
void test_attributes_edges(test::Context& t) {
const TempDir temp;
const std::string rules_text = "# leading comment\r\n"
"\r\n"
"*.c gen\r\n"
"*.c -gen\r\n"
"*.h txt\r\n"
"*.h -bin\r\n"
"*.log eol:crlf\r\n"
"*.log -eol\r\n"
"*.mac eol:cr\r\n"
"src/*.c gen\r\n"
"*.dat unknown-token bin\r\n";
t.expect(write_file(temp.path / ".vnattribs", bytes_of(rules_text)));
const AttributeRules rules = AttributeRules::load(temp.path);
t.expect(!rules.lookup("a.c").generated);
t.expect(rules.lookup("deep/a.c").generated == false);
t.expect(rules.lookup("a.h").text == AttributeText::Auto);
t.expect(rules.lookup("a.log").eol == AttributeEol::None);
t.expect(rules.lookup("a.mac").eol == AttributeEol::Cr);
t.expect(rules.lookup("src/main.c").generated);
t.expect(!rules.lookup("nested/src/main.c").generated);
t.expect(rules.lookup("assets/blob.dat").text == AttributeText::Binary);
const TempDir empty;
const AttributeRules none = AttributeRules::load(empty.path);
const auto plain = none.lookup("anything");
t.expect(!plain.generated);
t.expect(plain.text == AttributeText::Auto);
t.expect(plain.eol == AttributeEol::None);
const TempDir malformed;
t.expect(write_file(malformed.path / ".vnattribs",
bytes_of(std::string(" \n# comment\n\n\t\n*\n"))));
const AttributeRules malformed_rules = AttributeRules::load(malformed.path);
(void)malformed_rules.lookup("file");
}
} // namespace
test::Suite worktree_suite() {
return {"unit-worktree",
{
{"pathspecs", test_pathspecs},
{"ignore_rules", test_ignore_rules},
{"glob_hostile_patterns", test_glob_hostile_patterns},
{"attributes", test_attributes},
{"attributes_edges", test_attributes_edges},
}};
}
} // namespace vn::unit