blob: a3442c3d3c412145d25ad96a020c80d45ba1fae5 [file]
#include "unit-test.h"
#include "dir.h"
#define TEST_WITHIN_DEPTH(path, depth, max_depth, expect) do { \
int actual = within_depth(path, strlen(path), \
depth, max_depth); \
if (actual != expect) \
cl_failf("path '%s' with depth '%d' and max-depth '%d': expected %d, got %d", \
path, depth, max_depth, expect, actual); \
} while (0)
void test_dir__within_depth(void)
{
/* depth = 0; max_depth = 0 */
TEST_WITHIN_DEPTH("", 0, 0, 1);
TEST_WITHIN_DEPTH("file", 0, 0, 1);
TEST_WITHIN_DEPTH("a", 0, 0, 1);
TEST_WITHIN_DEPTH("a/file", 0, 0, 0);
TEST_WITHIN_DEPTH("a/b", 0, 0, 0);
TEST_WITHIN_DEPTH("a/b/file", 0, 0, 0);
/* depth = 0; max_depth = 1 */
TEST_WITHIN_DEPTH("", 0, 1, 1);
TEST_WITHIN_DEPTH("file", 0, 1, 1);
TEST_WITHIN_DEPTH("a", 0, 1, 1);
TEST_WITHIN_DEPTH("a/file", 0, 1, 1);
TEST_WITHIN_DEPTH("a/b", 0, 1, 1);
TEST_WITHIN_DEPTH("a/b/file", 0, 1, 0);
/* depth = 1; max_depth = 1 */
TEST_WITHIN_DEPTH("", 1, 1, 1);
TEST_WITHIN_DEPTH("file", 1, 1, 1);
TEST_WITHIN_DEPTH("a", 1, 1, 1);
TEST_WITHIN_DEPTH("a/file", 1, 1, 0);
TEST_WITHIN_DEPTH("a/b", 1, 1, 0);
TEST_WITHIN_DEPTH("a/b/file", 1, 1, 0);
/* depth = 1; max_depth = 0 */
TEST_WITHIN_DEPTH("", 1, 0, 0);
TEST_WITHIN_DEPTH("file", 1, 0, 0);
TEST_WITHIN_DEPTH("a", 1, 0, 0);
TEST_WITHIN_DEPTH("a/file", 1, 0, 0);
TEST_WITHIN_DEPTH("a/b", 1, 0, 0);
TEST_WITHIN_DEPTH("a/b/file", 1, 0, 0);
}
void test_dir__common_prefix_skips_excluded_pathspec_items(void)
{
struct pathspec_item items[] = {
{
.match = "unrelated/path",
.magic = PATHSPEC_EXCLUDE,
.nowildcard_len = 14,
},
{
.match = "foo/bar",
.nowildcard_len = 7,
},
{
.match = "foo/baz",
.nowildcard_len = 7,
},
};
struct pathspec pathspec = {
.nr = ARRAY_SIZE(items),
.magic = PATHSPEC_EXCLUDE,
.items = items,
};
char *prefix = common_prefix(&pathspec);
cl_assert_equal_s(prefix, "foo/");
free(prefix);
}