]> git.proxmox.com Git - libgit2.git/blobdiff - tests/core/dirent.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / core / dirent.c
index d95e44196dc71167affa50403b3eb8d0e621e628..2419ec7ab92c6c497c53f06373667360265fcfad 100644 (file)
@@ -1,5 +1,5 @@
 #include "clar_libgit2.h"
-#include "fileops.h"
+#include "futils.h"
 
 typedef struct name_data {
        int count; /* return count */
@@ -9,7 +9,7 @@ typedef struct name_data {
 typedef struct walk_data {
        char *sub;              /* sub-directory name */
        name_data *names; /* name state data    */
-       git_buf path;
+       git_str path;
 } walk_data;
 
 
@@ -27,7 +27,7 @@ static void setup(walk_data *d)
        if (strcmp(d->sub, ".") != 0)
                cl_must_pass(p_mkdir(d->sub, 0777));
 
-       cl_git_pass(git_buf_sets(&d->path, d->sub));
+       cl_git_pass(git_str_sets(&d->path, d->sub));
 
        state_loc = d;
 
@@ -55,7 +55,7 @@ static void dirent_cleanup__cb(void *_d)
 
        cl_must_pass(p_rmdir(top_dir));
 
-       git_buf_free(&d->path);
+       git_str_dispose(&d->path);
 }
 
 static void check_counts(walk_data *d)
@@ -81,7 +81,7 @@ static int update_count(name_data *data, const char *name)
        return GIT_ERROR;
 }
 
-static int one_entry(void *state, git_buf *path)
+static int one_entry(void *state, git_str *path)
 {
        walk_data *d = (walk_data *) state;
 
@@ -104,7 +104,7 @@ static name_data dot_names[] = {
 static walk_data dot = {
        ".",
        dot_names,
-       GIT_BUF_INIT
+       GIT_STR_INIT
 };
 
 /* make sure that the '.' folder is not traversed */
@@ -113,7 +113,7 @@ void test_core_dirent__dont_traverse_dot(void)
        cl_set_cleanup(&dirent_cleanup__cb, &dot);
        setup(&dot);
 
-       cl_git_pass(git_path_direach(&dot.path, 0, one_entry, &dot));
+       cl_git_pass(git_fs_path_direach(&dot.path, 0, one_entry, &dot));
 
        check_counts(&dot);
 }
@@ -128,7 +128,7 @@ static name_data sub_names[] = {
 static walk_data sub = {
        "sub",
        sub_names,
-       GIT_BUF_INIT
+       GIT_STR_INIT
 };
 
 /* traverse a subfolder */
@@ -137,7 +137,7 @@ void test_core_dirent__traverse_subfolder(void)
        cl_set_cleanup(&dirent_cleanup__cb, &sub);
        setup(&sub);
 
-       cl_git_pass(git_path_direach(&sub.path, 0, one_entry, &sub));
+       cl_git_pass(git_fs_path_direach(&sub.path, 0, one_entry, &sub));
 
        check_counts(&sub);
 }
@@ -146,7 +146,7 @@ void test_core_dirent__traverse_subfolder(void)
 static walk_data sub_slash = {
        "sub/",
        sub_names,
-       GIT_BUF_INIT
+       GIT_STR_INIT
 };
 
 /* traverse a slash-terminated subfolder */
@@ -155,7 +155,7 @@ void test_core_dirent__traverse_slash_terminated_folder(void)
        cl_set_cleanup(&dirent_cleanup__cb, &sub_slash);
        setup(&sub_slash);
 
-       cl_git_pass(git_path_direach(&sub_slash.path, 0, one_entry, &sub_slash));
+       cl_git_pass(git_fs_path_direach(&sub_slash.path, 0, one_entry, &sub_slash));
 
        check_counts(&sub_slash);
 }
@@ -167,7 +167,7 @@ static name_data empty_names[] = {
 static walk_data empty = {
        "empty",
        empty_names,
-       GIT_BUF_INIT
+       GIT_STR_INIT
 };
 
 /* make sure that empty folders are not traversed */
@@ -176,12 +176,12 @@ void test_core_dirent__dont_traverse_empty_folders(void)
        cl_set_cleanup(&dirent_cleanup__cb, &empty);
        setup(&empty);
 
-       cl_git_pass(git_path_direach(&empty.path, 0, one_entry, &empty));
+       cl_git_pass(git_fs_path_direach(&empty.path, 0, one_entry, &empty));
 
        check_counts(&empty);
 
        /* make sure callback not called */
-       cl_assert(git_path_is_empty_dir(empty.path.ptr));
+       cl_assert(git_fs_path_is_empty_dir(empty.path.ptr));
 }
 
 static name_data odd_names[] = {
@@ -195,7 +195,7 @@ static name_data odd_names[] = {
 static walk_data odd = {
        "odd",
        odd_names,
-       GIT_BUF_INIT
+       GIT_STR_INIT
 };
 
 /* make sure that strange looking filenames ('..c') are traversed */
@@ -204,7 +204,7 @@ void test_core_dirent__traverse_weird_filenames(void)
        cl_set_cleanup(&dirent_cleanup__cb, &odd);
        setup(&odd);
 
-       cl_git_pass(git_path_direach(&odd.path, 0, one_entry, &odd));
+       cl_git_pass(git_fs_path_direach(&odd.path, 0, one_entry, &odd));
 
        check_counts(&odd);
 }
@@ -224,30 +224,30 @@ void test_core_dirent__length_limits(void)
 void test_core_dirent__empty_dir(void)
 {
        cl_must_pass(p_mkdir("empty_dir", 0777));
-       cl_assert(git_path_is_empty_dir("empty_dir"));
+       cl_assert(git_fs_path_is_empty_dir("empty_dir"));
 
        cl_git_mkfile("empty_dir/content", "whatever\n");
-       cl_assert(!git_path_is_empty_dir("empty_dir"));
-       cl_assert(!git_path_is_empty_dir("empty_dir/content"));
+       cl_assert(!git_fs_path_is_empty_dir("empty_dir"));
+       cl_assert(!git_fs_path_is_empty_dir("empty_dir/content"));
 
        cl_must_pass(p_unlink("empty_dir/content"));
 
        cl_must_pass(p_mkdir("empty_dir/content", 0777));
-       cl_assert(!git_path_is_empty_dir("empty_dir"));
-       cl_assert(git_path_is_empty_dir("empty_dir/content"));
+       cl_assert(!git_fs_path_is_empty_dir("empty_dir"));
+       cl_assert(git_fs_path_is_empty_dir("empty_dir/content"));
 
        cl_must_pass(p_rmdir("empty_dir/content"));
 
        cl_must_pass(p_rmdir("empty_dir"));
 }
 
-static void handle_next(git_path_diriter *diriter, walk_data *walk)
+static void handle_next(git_fs_path_diriter *diriter, walk_data *walk)
 {
        const char *fullpath, *filename;
        size_t fullpath_len, filename_len;
 
-       cl_git_pass(git_path_diriter_fullpath(&fullpath, &fullpath_len, diriter));
-       cl_git_pass(git_path_diriter_filename(&filename, &filename_len, diriter));
+       cl_git_pass(git_fs_path_diriter_fullpath(&fullpath, &fullpath_len, diriter));
+       cl_git_pass(git_fs_path_diriter_filename(&filename, &filename_len, diriter));
 
        cl_assert_equal_strn(fullpath, "sub/", 4);
        cl_assert_equal_s(fullpath+4, filename);
@@ -258,20 +258,49 @@ static void handle_next(git_path_diriter *diriter, walk_data *walk)
 /* test directory iterator */
 void test_core_dirent__diriter_with_fullname(void)
 {
-       git_path_diriter diriter = GIT_PATH_DIRITER_INIT;
+       git_fs_path_diriter diriter = GIT_FS_PATH_DIRITER_INIT;
        int error;
 
        cl_set_cleanup(&dirent_cleanup__cb, &sub);
        setup(&sub);
 
-       cl_git_pass(git_path_diriter_init(&diriter, sub.path.ptr, 0));
+       cl_git_pass(git_fs_path_diriter_init(&diriter, sub.path.ptr, 0));
 
-       while ((error = git_path_diriter_next(&diriter)) == 0)
+       while ((error = git_fs_path_diriter_next(&diriter)) == 0)
                handle_next(&diriter, &sub);
 
        cl_assert_equal_i(error, GIT_ITEROVER);
 
-       git_path_diriter_free(&diriter);
+       git_fs_path_diriter_free(&diriter);
 
        check_counts(&sub);
 }
+
+void test_core_dirent__diriter_at_directory_root(void)
+{
+       git_fs_path_diriter diriter = GIT_FS_PATH_DIRITER_INIT;
+       const char *sandbox_path, *path;
+       char *root_path;
+       size_t path_len;
+       int root_offset, error;
+
+       sandbox_path = clar_sandbox_path();
+       cl_assert((root_offset = git_fs_path_root(sandbox_path)) >= 0);
+
+       cl_assert(root_path = git__calloc(1, root_offset + 2));
+       strncpy(root_path, sandbox_path, root_offset + 1);
+
+       cl_git_pass(git_fs_path_diriter_init(&diriter, root_path, 0));
+
+       while ((error = git_fs_path_diriter_next(&diriter)) == 0) {
+               cl_git_pass(git_fs_path_diriter_fullpath(&path, &path_len, &diriter));
+
+               cl_assert(path_len > (size_t)(root_offset + 1));
+               cl_assert(path[root_offset+1] != '/');
+       }
+
+       cl_assert_equal_i(error, GIT_ITEROVER);
+
+       git_fs_path_diriter_free(&diriter);
+       git__free(root_path);
+}