From 7de56f93992fdc2c2093006a70cc4a010fbb1ffb Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Sat, 9 Oct 2010 11:56:55 +0300 Subject: [PATCH] * clean-up mu_util_dir_expand, add unit test --- src/mu-util.c | 53 ++++++++++++++++++++++++++-------------- src/tests/test-mu-util.c | 19 +++++++++++++- 2 files changed, 52 insertions(+), 20 deletions(-) diff --git a/src/mu-util.c b/src/mu-util.c index fa1b9120..b4914324 100644 --- a/src/mu-util.c +++ b/src/mu-util.c @@ -45,29 +45,28 @@ #include "mu-util.h" -char* -mu_util_dir_expand (const char *path) +static char* +do_wordexp (const char *path) { wordexp_t wexp; char *dir; - char resolved[PATH_MAX + 1]; - int rv; - - g_return_val_if_fail (path, NULL); - dir = NULL; - rv = wordexp (path, &wexp, 0); - if (rv != 0) { - g_debug ("%s: expansion failed for '%s' [%d]", - __FUNCTION__, path, rv); + if (!path) { + g_debug ("%s: path is empty", __FUNCTION__); return NULL; - } else if (wexp.we_wordc != 1) { - g_debug ("%s: expansion ambiguous for '%s'", - __FUNCTION__, path); - } else - dir = g_strdup (wexp.we_wordv[0]); + } + + if (wordexp (path, &wexp, 0) != 0) { + g_debug ("%s: expansion failed for %s", __FUNCTION__, path); + return NULL; + } + + if (wexp.we_wordc != 1) /* not an *error*, we just take the first one */ + g_debug ("%s: expansion ambiguous for '%s'", __FUNCTION__, path); + + dir = g_strdup (wexp.we_wordv[0]); - /* strangely, below seems to load to a crash on MacOS (BSD); + /* strangely, below seems to lead to a crash on MacOS (BSD); so we have to allow for a tiny leak here on that platform... maybe instead of __APPLE__ it should be __BSD__?*/ @@ -75,16 +74,32 @@ mu_util_dir_expand (const char *path) wordfree (&wexp); #endif /*__APPLE__*/ + return dir; +} + + +char* +mu_util_dir_expand (const char *path) +{ + char *dir; + char resolved[PATH_MAX + 1]; + + g_return_val_if_fail (path, NULL); + + dir = do_wordexp (path); + if (!dir) + return NULL; /* error */ + /* now, resolve any symlinks, .. etc. */ if (!realpath (dir, resolved)) { - g_debug ("%s: good not get realpath for '%s': %s", + g_debug ("%s: could not get realpath for '%s': %s", __FUNCTION__, dir, strerror(errno)); g_free (dir); return NULL; } else g_free (dir); - return g_strdup(resolved); + return g_strdup (resolved); } gboolean diff --git a/src/tests/test-mu-util.c b/src/tests/test-mu-util.c index 9d4c78d7..1c81be44 100644 --- a/src/tests/test-mu-util.c +++ b/src/tests/test-mu-util.c @@ -64,6 +64,20 @@ test_mu_util_dir_expand_02 (void) } +static void +test_mu_util_dir_expand_03 (void) +{ + gchar *got; + + got = mu_util_dir_expand ("."); + g_assert_cmpstr (got,==,ABS_SRCDIR); + + g_free (got); +} + + + + static void test_mu_util_guess_maildir_01 (void) { @@ -182,6 +196,7 @@ test_mu_util_str_from_strv_03 (void) } + int main (int argc, char *argv[]) { @@ -192,7 +207,9 @@ main (int argc, char *argv[]) test_mu_util_dir_expand_01); g_test_add_func ("/mu-util/mu-util-dir-expand-02", test_mu_util_dir_expand_02); - + g_test_add_func ("/mu-util/mu-util-dir-expand-03", + test_mu_util_dir_expand_03); + /* mu_util_guess_maildir */ g_test_add_func ("/mu-util/mu-util-guess-maildir-01", test_mu_util_guess_maildir_01);