mirror of
https://git.proxmox.com/git/libgit2
synced 2025-05-21 01:08:28 +00:00

Clean up a provided absolute or relative directory path. This prettification relies on basic operations such as coalescing multiple forward slashes into a single slash, removing '.' and './' current directory segments, and removing parent directory whenever '..' is encountered. If not empty, the returned path ends with a forward slash. For instance, this will turn "d1/s1///s2/..//../s3" into "d1/s3/". This only performs a string based analysis of the path. No checks are done to make sure the path actually makes sense from the file system perspective.
74 lines
2.9 KiB
C
74 lines
2.9 KiB
C
#include "test_lib.h"
|
|
#include "fileops.h"
|
|
|
|
static int ensure_normalized(const char *input_path, const char *expected_path)
|
|
{
|
|
int error = GIT_SUCCESS;
|
|
char buffer_out[GIT_PATH_MAX];
|
|
|
|
error = git_prettify_dir_path(buffer_out, input_path);
|
|
if (error < GIT_SUCCESS)
|
|
return error;
|
|
|
|
if (expected_path == NULL)
|
|
return error;
|
|
|
|
if (strcmp(buffer_out, expected_path))
|
|
error = GIT_ERROR;
|
|
|
|
return error;
|
|
}
|
|
|
|
BEGIN_TEST(path_prettifying)
|
|
must_pass(ensure_normalized("", ""));
|
|
must_pass(ensure_normalized(".", ""));
|
|
must_pass(ensure_normalized("./", ""));
|
|
must_pass(ensure_normalized("./.", ""));
|
|
must_fail(ensure_normalized("./..", NULL));
|
|
must_fail(ensure_normalized("../.", NULL));
|
|
must_fail(ensure_normalized("./.././/", NULL));
|
|
must_pass(ensure_normalized("dir/..", ""));
|
|
must_pass(ensure_normalized("dir/sub/../..", ""));
|
|
must_pass(ensure_normalized("dir/sub/..///..", ""));
|
|
must_pass(ensure_normalized("dir/sub///../..", ""));
|
|
must_pass(ensure_normalized("dir/sub///..///..", ""));
|
|
must_fail(ensure_normalized("dir/sub/../../..", NULL));
|
|
must_pass(ensure_normalized("dir", "dir/"));
|
|
must_pass(ensure_normalized("dir//", "dir/"));
|
|
must_pass(ensure_normalized("./dir", "dir/"));
|
|
must_pass(ensure_normalized("dir/.", "dir/"));
|
|
must_pass(ensure_normalized("dir///./", "dir/"));
|
|
must_pass(ensure_normalized("dir/sub/..", "dir/"));
|
|
must_pass(ensure_normalized("dir//sub/..", "dir/"));
|
|
must_pass(ensure_normalized("dir//sub/../", "dir/"));
|
|
must_pass(ensure_normalized("dir/sub/../", "dir/"));
|
|
must_pass(ensure_normalized("dir/sub/../.", "dir/"));
|
|
must_pass(ensure_normalized("dir/s1/../s2/", "dir/s2/"));
|
|
must_pass(ensure_normalized("d1/s1///s2/..//../s3/", "d1/s3/"));
|
|
must_pass(ensure_normalized("d1/s1//../s2/../../d2", "d2/"));
|
|
must_pass(ensure_normalized("dir/sub/../", "dir/"));
|
|
|
|
must_pass(ensure_normalized("/", "/"));
|
|
must_pass(ensure_normalized("//", "/"));
|
|
must_pass(ensure_normalized("///", "/"));
|
|
must_pass(ensure_normalized("/.", "/"));
|
|
must_pass(ensure_normalized("/./", "/"));
|
|
must_fail(ensure_normalized("/./..", NULL));
|
|
must_fail(ensure_normalized("/../.", NULL));
|
|
must_fail(ensure_normalized("/./.././/", NULL));
|
|
must_pass(ensure_normalized("/dir/..", "/"));
|
|
must_pass(ensure_normalized("/dir/sub/../..", "/"));
|
|
must_fail(ensure_normalized("/dir/sub/../../..", NULL));
|
|
must_pass(ensure_normalized("/dir", "/dir/"));
|
|
must_pass(ensure_normalized("/dir//", "/dir/"));
|
|
must_pass(ensure_normalized("/./dir", "/dir/"));
|
|
must_pass(ensure_normalized("/dir/.", "/dir/"));
|
|
must_pass(ensure_normalized("/dir///./", "/dir/"));
|
|
must_pass(ensure_normalized("/dir//sub/..", "/dir/"));
|
|
must_pass(ensure_normalized("/dir/sub/../", "/dir/"));
|
|
must_pass(ensure_normalized("//dir/sub/../.", "/dir/"));
|
|
must_pass(ensure_normalized("/dir/s1/../s2/", "/dir/s2/"));
|
|
must_pass(ensure_normalized("/d1/s1///s2/..//../s3/", "/d1/s3/"));
|
|
must_pass(ensure_normalized("/d1/s1//../s2/../../d2", "/d2/"));
|
|
END_TEST
|