* tests: add tests for mu_util_dir_expand and mu_util_guess_maildir

This commit is contained in:
Dirk-Jan C. Binnema 2010-01-25 13:27:02 +02:00
parent afcd800a2e
commit 3b8f908fa6
4 changed files with 99 additions and 22 deletions

View File

@ -52,10 +52,9 @@ mu_util_dir_expand (const char *path)
return dir; return dir;
} }
gboolean gboolean
mu_util_check_dir (const gchar* path, gboolean readable, gboolean writeable) mu_util_check_dir (const gchar* path, gboolean readable,
gboolean writeable)
{ {
mode_t mode; mode_t mode;
struct stat statbuf; struct stat statbuf;
@ -66,34 +65,38 @@ mu_util_check_dir (const gchar* path, gboolean readable, gboolean writeable)
mode = F_OK | (readable ? R_OK : 0) | (writeable ? W_OK : 0); mode = F_OK | (readable ? R_OK : 0) | (writeable ? W_OK : 0);
if (access (path, mode) != 0) { if (access (path, mode) != 0) {
MU_WRITE_LOG ("Cannot access %s: %s", path, strerror (errno)); MU_WRITE_LOG ("Cannot access %s: %s", path,
strerror (errno));
return FALSE; return FALSE;
} }
if (stat (path, &statbuf) != 0) { if (stat (path, &statbuf) != 0) {
MU_WRITE_LOG ("Cannot stat %s: %s", path, strerror (errno)); MU_WRITE_LOG ("Cannot stat %s: %s", path,
strerror (errno));
return FALSE; return FALSE;
} }
return S_ISDIR(statbuf.st_mode); return S_ISDIR(statbuf.st_mode) ? TRUE: FALSE;
} }
gchar* gchar*
mu_util_guess_maildir (void) mu_util_guess_maildir (void)
{ {
char *dir; const gchar *mdir1;
gchar *mdir2;
/* first, try MAILDIR */ /* first, try MAILDIR */
dir = getenv ("MAILDIR"); mdir1 = g_getenv ("MAILDIR");
if (mu_util_check_dir (dir, TRUE, FALSE))
return g_strdup (dir);
if (mdir1 && mu_util_check_dir (mdir1, TRUE, FALSE))
return g_strdup (mdir1);
/* then, try ~/Maildir */ /* then, try ~/Maildir */
dir = mu_util_dir_expand ("~/Maildir"); mdir2 = mu_util_dir_expand ("~/Maildir");
if (mu_util_check_dir (dir, TRUE, FALSE)) if (mu_util_check_dir (mdir2, TRUE, FALSE))
return dir; return mdir2;
/* nope; nothing found */ /* nope; nothing found */
return NULL; return NULL;
} }

View File

@ -35,8 +35,8 @@ G_BEGIN_DECLS
char* mu_util_dir_expand (const char* path); char* mu_util_dir_expand (const char* path);
/** /**
* guess the maildir; first try MAILDIR, then try ~/Maildir * guess the maildir; first try $MAILDIR; if it is unset or
* if both fail, return NULL * non-existant, try ~/Maildir if both fail, return NULL
* *
* @return full path of the guessed Maildir, or NULL; must be freed (gfree) * @return full path of the guessed Maildir, or NULL; must be freed (gfree)
*/ */

Binary file not shown.

View File

@ -23,24 +23,88 @@
#include <glib.h> #include <glib.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
#include "src/mu-util.h" #include "src/mu-util.h"
static void static void
test_mu_util_dir_expand (void) test_mu_util_dir_expand_01 (void)
{ {
gchar *got, *expected; gchar *got, *expected;
got = mu_util_dir_expand ("~/Desktop"); got = mu_util_dir_expand ("~/Desktop");
expected = g_strdup_printf ("%s%cDesktop", expected = g_strdup_printf ("%s%cDesktop",
getenv("HOME"), G_DIR_SEPARATOR); getenv("HOME"), G_DIR_SEPARATOR);
g_assert_cmpstr (got,==,expected); g_assert_cmpstr (got,==,expected);
g_free (got);
g_free (expected);
}
static void
test_mu_util_dir_expand_02 (void)
{
gchar *got, *expected, *tmp;
tmp = g_strdup_printf ("~%s/Desktop", getenv("LOGNAME"));
got = mu_util_dir_expand (tmp);
expected = g_strdup_printf ("%s%cDesktop",
getenv("HOME"), G_DIR_SEPARATOR);
g_assert_cmpstr (got,==,expected);
g_free (tmp);
g_free (got); g_free (got);
g_free (expected); g_free (expected);
} }
static void
test_mu_util_guess_maildir_01 (void)
{
char *got;
const char *expected;
/* skip the test if there's no /tmp */
if (access ("/tmp", F_OK))
return;
g_setenv ("MAILDIR", "/tmp", TRUE);
got = mu_util_guess_maildir ();
expected = "/tmp";
g_assert_cmpstr (got,==,expected);
g_free (got);
}
static void
test_mu_util_guess_maildir_02 (void)
{
char *got, *mdir;
g_unsetenv ("MAILDIR");
mdir = g_strdup_printf ("%s%cMaildir",
getenv("HOME"), G_DIR_SEPARATOR);
got = mu_util_guess_maildir ();
if (access (mdir, F_OK) == 0)
g_assert_cmpstr (got, ==, mdir);
else
g_assert_cmpstr (got, == , NULL);
g_free (got);
g_free (mdir);
}
static void static void
shutup (void) {} shutup (void) {}
@ -49,12 +113,22 @@ main (int argc, char *argv[])
{ {
g_test_init (&argc, &argv, NULL); g_test_init (&argc, &argv, NULL);
/* mu_util_dir_expand */
g_test_add_func ("/mu-util/mu-util-dir-expand-01", g_test_add_func ("/mu-util/mu-util-dir-expand-01",
test_mu_util_dir_expand); test_mu_util_dir_expand_01);
g_test_add_func ("/mu-util/mu-util-dir-expand-02",
test_mu_util_dir_expand_02);
/* mu_util_guess_maildir */
g_test_add_func ("/mu-util/mu-util-guess-maildir-01",
test_mu_util_guess_maildir_01);
g_test_add_func ("/mu-util/mu-util-guess-maildir-02",
test_mu_util_guess_maildir_02);
g_log_set_handler (NULL, g_log_set_handler (NULL,
G_LOG_LEVEL_DEBUG|G_LOG_LEVEL_MESSAGE|G_LOG_LEVEL_INFO, G_LOG_LEVEL_DEBUG|
(GLogFunc)shutup, NULL); G_LOG_LEVEL_MESSAGE|
G_LOG_LEVEL_INFO, (GLogFunc)shutup, NULL);
return g_test_run (); return g_test_run ();
} }