test: Add basic tests for path_split_prefix_filename()
These aren’t anything comprehensive, but provide some basic assurances that it’s working correctly. In particular, they test its behaviour when *both* the prefix and filename components are requested. Split out from the original version of this function which was part of #40236. Signed-off-by: Philip Withnall <pwithnall@gnome.org>
This commit is contained in:
committed by
Luca Boccassi
parent
e67b008fa3
commit
779ed358f2
@@ -1027,7 +1027,7 @@ TEST(last_path_component) {
|
||||
}
|
||||
|
||||
static void test_path_extract_filename_one(const char *input, const char *output, int ret) {
|
||||
_cleanup_free_ char *k = NULL;
|
||||
_cleanup_free_ char *k = NULL, *k2 = NULL;
|
||||
int r;
|
||||
|
||||
r = path_extract_filename(input, &k);
|
||||
@@ -1037,6 +1037,13 @@ static void test_path_extract_filename_one(const char *input, const char *output
|
||||
strnull(output), ret < 0 ? STRERROR(ret) : "-");
|
||||
ASSERT_STREQ(k, output);
|
||||
assert_se(r == ret);
|
||||
|
||||
/* Extra safety check: make sure that path_split_prefix_filename() behaves the same */
|
||||
r = path_split_prefix_filename(input, NULL, &k2);
|
||||
if (r >= 0) {
|
||||
ASSERT_STREQ(k2, k);
|
||||
assert_se(r == ret);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(path_extract_filename) {
|
||||
@@ -1071,7 +1078,7 @@ TEST(path_extract_filename) {
|
||||
}
|
||||
|
||||
static void test_path_extract_directory_one(const char *input, const char *output, int ret) {
|
||||
_cleanup_free_ char *k = NULL;
|
||||
_cleanup_free_ char *k = NULL, *k2 = NULL;
|
||||
int r;
|
||||
|
||||
r = path_extract_directory(input, &k);
|
||||
@@ -1082,10 +1089,18 @@ static void test_path_extract_directory_one(const char *input, const char *outpu
|
||||
ASSERT_STREQ(k, output);
|
||||
assert_se(r == ret);
|
||||
|
||||
/* Extra safety check: make sure that path_split_prefix_filename() behaves the same.
|
||||
* We can’t check the return value from it though as that differs based on the filename component.
|
||||
* We can only assert that if path_extract_directory() fails, then
|
||||
* path_split_prefix_filename() must also fail. */
|
||||
r = path_split_prefix_filename(input, &k2, NULL);
|
||||
ASSERT_STREQ(k2, k);
|
||||
assert_se(!(ret < 0) || r < 0);
|
||||
|
||||
/* Extra safety check: let's make sure that if we split out the filename too (and it works) the
|
||||
* joined parts are identical to the original again */
|
||||
if (r >= 0) {
|
||||
_cleanup_free_ char *f = NULL;
|
||||
_cleanup_free_ char *f = NULL, *k3 = NULL, *f2 = NULL;
|
||||
|
||||
r = path_extract_filename(input, &f);
|
||||
if (r >= 0) {
|
||||
@@ -1094,6 +1109,13 @@ static void test_path_extract_directory_one(const char *input, const char *outpu
|
||||
assert_se(j = path_join(k, f));
|
||||
assert_se(path_equal(input, j));
|
||||
}
|
||||
|
||||
/* And the same, but for path_split_prefix_filename() */
|
||||
r = path_split_prefix_filename(input, &k3, &f2);
|
||||
if (r >= 0) {
|
||||
ASSERT_STREQ(k3, k);
|
||||
ASSERT_STREQ(f2, f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user