mirror of https://github.com/djcb/mu.git
maildir: add tests for maildir_link / maildir_clear_links
This commit is contained in:
parent
eb790727ed
commit
23ac71e6a7
|
@ -147,7 +147,8 @@ check_subdir(const std::string& src, bool& in_cur)
|
||||||
}
|
}
|
||||||
|
|
||||||
static Mu::Result<std::string>
|
static Mu::Result<std::string>
|
||||||
get_target_fullpath(const std::string& src, const std::string& targetpath)
|
get_target_fullpath(const std::string& src, const std::string& targetpath,
|
||||||
|
bool unique_names)
|
||||||
{
|
{
|
||||||
bool in_cur{};
|
bool in_cur{};
|
||||||
if (auto&& res = check_subdir(src, in_cur); !res)
|
if (auto&& res = check_subdir(src, in_cur); !res)
|
||||||
|
@ -159,21 +160,30 @@ get_target_fullpath(const std::string& src, const std::string& targetpath)
|
||||||
* including a hash of the srcname in the targetname. This helps if
|
* including a hash of the srcname in the targetname. This helps if
|
||||||
* there are copies of a message (which all have the same basename)
|
* there are copies of a message (which all have the same basename)
|
||||||
*/
|
*/
|
||||||
auto targetfullpath = format("%s%c%s%c%u_%s",
|
std::string fulltargetpath;
|
||||||
|
if (unique_names)
|
||||||
|
fulltargetpath = format("%s%c%s%c%u_%s",
|
||||||
targetpath.c_str(),
|
targetpath.c_str(),
|
||||||
G_DIR_SEPARATOR, in_cur ? "cur" : "new",
|
G_DIR_SEPARATOR, in_cur ? "cur" : "new",
|
||||||
G_DIR_SEPARATOR,
|
G_DIR_SEPARATOR,
|
||||||
g_str_hash(src.c_str()),
|
g_str_hash(src.c_str()),
|
||||||
srcfile);
|
srcfile);
|
||||||
|
else
|
||||||
|
fulltargetpath = format("%s%c%s%c%s",
|
||||||
|
targetpath.c_str(),
|
||||||
|
G_DIR_SEPARATOR, in_cur ? "cur" : "new",
|
||||||
|
G_DIR_SEPARATOR,
|
||||||
|
srcfile);
|
||||||
g_free(srcfile);
|
g_free(srcfile);
|
||||||
|
|
||||||
return targetfullpath;
|
return fulltargetpath;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<void>
|
Result<void>
|
||||||
Mu::maildir_link(const std::string& src, const std::string& targetpath)
|
Mu::maildir_link(const std::string& src, const std::string& targetpath,
|
||||||
|
bool unique_names)
|
||||||
{
|
{
|
||||||
auto path_res{get_target_fullpath(src, targetpath)};
|
auto path_res{get_target_fullpath(src, targetpath, unique_names)};
|
||||||
if (!path_res)
|
if (!path_res)
|
||||||
return Err(std::move(path_res.error()));
|
return Err(std::move(path_res.error()));
|
||||||
|
|
||||||
|
|
|
@ -57,10 +57,13 @@ Result<void> maildir_mkdir(const std::string& path, mode_t mode=0700,
|
||||||
* @param targetpath the path to the target maildir; ie., *not*
|
* @param targetpath the path to the target maildir; ie., *not*
|
||||||
* MyMaildir/cur, but just MyMaildir/. The function will figure out
|
* MyMaildir/cur, but just MyMaildir/. The function will figure out
|
||||||
* the correct subdir then.
|
* the correct subdir then.
|
||||||
|
* @param unique_names whether to create unique names; should be true unless
|
||||||
|
* for tests.
|
||||||
*
|
*
|
||||||
* @return a valid result (!!result) or an Error
|
* @return a valid result (!!result) or an Error
|
||||||
*/
|
*/
|
||||||
Result<void> maildir_link(const std::string& src, const std::string& targetpath);
|
Result<void> maildir_link(const std::string& src, const std::string& targetpath,
|
||||||
|
bool unique_names=true);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Recursively delete all the symbolic links in a directory tree
|
* Recursively delete all the symbolic links in a directory tree
|
||||||
|
|
|
@ -24,6 +24,7 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
#include "test-mu-common.hh"
|
#include "test-mu-common.hh"
|
||||||
#include "mu-maildir.hh"
|
#include "mu-maildir.hh"
|
||||||
|
@ -454,6 +455,47 @@ test_maildir_from_path(void)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_maildir_link()
|
||||||
|
{
|
||||||
|
TempDir tmpdir{false};
|
||||||
|
|
||||||
|
g_message("%s", tmpdir.path().c_str());
|
||||||
|
|
||||||
|
g_assert_true(!!maildir_mkdir(tmpdir.path() + "/foo"));
|
||||||
|
g_assert_true(!!maildir_mkdir(tmpdir.path() + "/bar"));
|
||||||
|
|
||||||
|
const auto srcpath1 = tmpdir.path() + "/foo/cur/msg1";
|
||||||
|
const auto srcpath2 = tmpdir.path() + "/foo/new/msg2";
|
||||||
|
|
||||||
|
{
|
||||||
|
std::ofstream stream(srcpath1);
|
||||||
|
stream.write("cur", 3);
|
||||||
|
g_assert_true(stream.good());
|
||||||
|
stream.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
std::ofstream stream(srcpath2);
|
||||||
|
stream.write("new", 3);
|
||||||
|
g_assert_true(stream.good());
|
||||||
|
stream.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
g_assert_true(!!maildir_link(srcpath1, tmpdir.path() + "/bar", false));
|
||||||
|
g_assert_true(!!maildir_link(srcpath2, tmpdir.path() + "/bar", false));
|
||||||
|
|
||||||
|
const auto dstpath1 = tmpdir.path() + "/bar/cur/msg1";
|
||||||
|
const auto dstpath2 = tmpdir.path() + "/bar/new/msg2";
|
||||||
|
|
||||||
|
g_assert_true(g_access(dstpath1.c_str(), F_OK) == 0);
|
||||||
|
g_assert_true(g_access(dstpath2.c_str(), F_OK) == 0);
|
||||||
|
|
||||||
|
g_assert_true(!!maildir_clear_links(tmpdir.path() + "/bar"));
|
||||||
|
g_assert_false(g_access(dstpath1.c_str(), F_OK) == 0);
|
||||||
|
g_assert_false(g_access(dstpath2.c_str(), F_OK) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char* argv[])
|
main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
|
@ -479,11 +521,7 @@ main(int argc, char* argv[])
|
||||||
g_test_add_func("/mu-maildir/mu-maildir-from-path",
|
g_test_add_func("/mu-maildir/mu-maildir-from-path",
|
||||||
test_maildir_from_path);
|
test_maildir_from_path);
|
||||||
|
|
||||||
g_log_set_handler(
|
g_test_add_func("/mu-maildir/mu-maildir-link", test_maildir_link);
|
||||||
NULL,
|
|
||||||
(GLogLevelFlags)(G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION),
|
|
||||||
(GLogFunc)black_hole,
|
|
||||||
NULL);
|
|
||||||
|
|
||||||
return g_test_run();
|
return g_test_run();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue