improve invoking external commands
- don't make assumptions on where programs live (i.e., /bin/sh, /bin/rm, /bin/mv) are not universal - dont invoke shell when unnecessary - improve error-handling
This commit is contained in:
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** Copyright (C) 2012-2023 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
** Copyright (C) 2012-2025 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
||||||
**
|
**
|
||||||
** This program is free software; you can redistribute it and/or modify it
|
** This program is free software; you can redistribute it and/or modify it
|
||||||
** under the terms of the GNU General Public License as published by the
|
** under the terms of the GNU General Public License as published by the
|
||||||
@ -28,6 +28,8 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "utils/mu-test-utils.hh"
|
#include "utils/mu-test-utils.hh"
|
||||||
|
#include "utils/mu-utils-file.hh"
|
||||||
|
|
||||||
#include <lib/mu-store.hh>
|
#include <lib/mu-store.hh>
|
||||||
#include <utils/mu-utils.hh>
|
#include <utils/mu-utils.hh>
|
||||||
|
|
||||||
@ -38,25 +40,22 @@ static std::string test_dir;
|
|||||||
static std::string
|
static std::string
|
||||||
fill_database(void)
|
fill_database(void)
|
||||||
{
|
{
|
||||||
const auto cmdline = mu_format(
|
{
|
||||||
"/bin/sh -c '"
|
const auto res = run_command0({MU_PROGRAM,
|
||||||
"{} init --muhome={} --maildir={} --quiet; "
|
"--quiet", "init",
|
||||||
"{} index --muhome={} --quiet'",
|
"--muhome", test_dir,
|
||||||
MU_PROGRAM,
|
"--maildir", MU_TESTMAILDIR2});
|
||||||
test_dir,
|
assert_valid_result(res);
|
||||||
MU_TESTMAILDIR2,
|
}
|
||||||
MU_PROGRAM,
|
|
||||||
test_dir);
|
{
|
||||||
|
const auto res = run_command0({MU_PROGRAM, "--quiet",
|
||||||
|
"index", "--muhome", test_dir});
|
||||||
|
assert_valid_result(res);
|
||||||
|
}
|
||||||
|
|
||||||
if (g_test_verbose())
|
if (g_test_verbose())
|
||||||
mu_println("{}", cmdline);
|
mu_println("\nindexed {} @ {}", MU_TESTMAILDIR2, test_dir);
|
||||||
|
|
||||||
GError *err{};
|
|
||||||
if (!g_spawn_command_line_sync(cmdline.c_str(), NULL, NULL, NULL, &err)) {
|
|
||||||
mu_printerrln("Error: {}", err ? err->message : "?");
|
|
||||||
g_clear_error(&err);
|
|
||||||
g_assert(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
return test_dir;
|
return test_dir;
|
||||||
}
|
}
|
||||||
@ -72,22 +71,11 @@ test_something(const char* what)
|
|||||||
g_print("GUILE_LOAD_PATH: %s\n", GUILE_LOAD_PATH);
|
g_print("GUILE_LOAD_PATH: %s\n", GUILE_LOAD_PATH);
|
||||||
|
|
||||||
const auto dir = fill_database();
|
const auto dir = fill_database();
|
||||||
const auto cmdline = mu_format("{} -q -e main {}/test-mu-guile.scm "
|
const auto res = run_command0({GUILE_BINARY, "-q", "-e", "main",
|
||||||
"--muhome={} --test={}",
|
ABS_SRCDIR"/test-mu-guile.scm",
|
||||||
GUILE_BINARY, ABS_SRCDIR,
|
"--muhome", dir, "--test", what});
|
||||||
dir, what);
|
|
||||||
|
|
||||||
if (g_test_verbose())
|
assert_valid_result(res);
|
||||||
mu_println("cmdline: {}", cmdline);
|
|
||||||
|
|
||||||
GError *err{};
|
|
||||||
int status{};
|
|
||||||
if (!g_spawn_command_line_sync(cmdline.c_str(), NULL, NULL, &status, &err) ||
|
|
||||||
status != 0) {
|
|
||||||
mu_printerrln("Error: {}", err ? err->message : "something went wrong");
|
|
||||||
g_clear_error(&err);
|
|
||||||
g_assert(0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
|||||||
@ -297,7 +297,11 @@ msg_move_g_file(const std::string& src, const std::string& dst)
|
|||||||
G_GNUC_UNUSED static Mu::Result<void>
|
G_GNUC_UNUSED static Mu::Result<void>
|
||||||
msg_move_mv_file(const std::string& src, const std::string& dst)
|
msg_move_mv_file(const std::string& src, const std::string& dst)
|
||||||
{
|
{
|
||||||
if (auto res{run_command0({"/bin/mv", src, dst})}; !res)
|
static const auto mv_path{program_in_path("mv")};
|
||||||
|
if (!mv_path)
|
||||||
|
return Err(Error::Code::File, "failed to find 'mv'");
|
||||||
|
|
||||||
|
if (auto res{run_command0({*mv_path, src, dst})}; !res)
|
||||||
return Err(Error::Code::File, "error moving {}->{}; err={}", src, dst, res.error());
|
return Err(Error::Code::File, "error moving {}->{}; err={}", src, dst, res.error());
|
||||||
else
|
else
|
||||||
return Ok();
|
return Ok();
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** Copyright (C) 2022-2023 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
** Copyright (C) 2022-2025 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
||||||
**
|
**
|
||||||
** This program is free software; you can redistribute it and/or modify it
|
** This program is free software; you can redistribute it and/or modify it
|
||||||
** under the terms of the GNU General Public License as published by the
|
** under the terms of the GNU General Public License as published by the
|
||||||
@ -24,6 +24,8 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
#include <utils/mu-utils.hh>
|
#include <utils/mu-utils.hh>
|
||||||
|
#include <utils/mu-utils-file.hh>
|
||||||
|
|
||||||
#include <utils/mu-regex.hh>
|
#include <utils/mu-regex.hh>
|
||||||
#include <mu-store.hh>
|
#include <mu-store.hh>
|
||||||
#include "mu-maildir.hh"
|
#include "mu-maildir.hh"
|
||||||
@ -438,12 +440,9 @@ setup(const TestData& tdata)
|
|||||||
static void
|
static void
|
||||||
tear_down()
|
tear_down()
|
||||||
{
|
{
|
||||||
/* ugly */
|
for (auto&& dir : { BENCH_MAILDIRS, BENCH_STORE } ) {
|
||||||
GError *err{};
|
if (const auto res = remove_directory(dir); !res)
|
||||||
const auto cmd{mu_format("/bin/rm -rf '{}' '{}'", BENCH_MAILDIRS, BENCH_STORE)};
|
mu_warning("failed to remove {}: {}", dir, res.error().what());
|
||||||
if (!g_spawn_command_line_sync(cmd.c_str(), NULL, NULL, NULL, &err)) {
|
|
||||||
mu_warning("error: {}", err ? err->message : "?");
|
|
||||||
g_clear_error(&err);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -829,13 +829,14 @@ add_scripts(CLI::App& app, Options& opts)
|
|||||||
static Result<Options>
|
static Result<Options>
|
||||||
show_manpage(Options& opts, const std::string& name)
|
show_manpage(Options& opts, const std::string& name)
|
||||||
{
|
{
|
||||||
char *path = g_find_program_in_path("man");
|
const auto manprog{program_in_path("man")};
|
||||||
if (!path)
|
if (!manprog)
|
||||||
return Err(Error::Code::Command,
|
return Err(Error::Code::Command,
|
||||||
"cannot find 'man' program");
|
"cannot find 'man' program");
|
||||||
|
|
||||||
GError* err{};
|
GError* err{};
|
||||||
auto cmd{to_string_gchar(std::move(path)) + " " + name};
|
const auto cmd{mu_format("{} {}", *manprog, name)};
|
||||||
|
// run_command0 doesn't work here.
|
||||||
auto res = g_spawn_command_line_sync(cmd.c_str(), {}, {}, {}, &err);
|
auto res = g_spawn_command_line_sync(cmd.c_str(), {}, {}, {}, &err);
|
||||||
if (!res)
|
if (!res)
|
||||||
return Err(Error::Code::Command, &err,
|
return Err(Error::Code::Command, &err,
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** Copyright (C) 2008-2022 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
** Copyright (C) 2008-2025 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
||||||
**
|
**
|
||||||
** This program is free software; you can redistribute it and/or modify it
|
** This program is free software; you can redistribute it and/or modify it
|
||||||
** under the terms of the GNU General Public License as published by the
|
** under the terms of the GNU General Public License as published by the
|
||||||
@ -47,16 +47,20 @@ make_database(const std::string& dbdir, const std::string& testdir)
|
|||||||
{
|
{
|
||||||
/* use the env var rather than `--muhome` */
|
/* use the env var rather than `--muhome` */
|
||||||
g_setenv("MUHOME", dbdir.c_str(), 1);
|
g_setenv("MUHOME", dbdir.c_str(), 1);
|
||||||
const auto cmdline{mu_format(
|
|
||||||
"/bin/sh -c '"
|
{
|
||||||
"{} --quiet init --maildir={} ; "
|
const auto res = run_command0({MU_PROGRAM, "--quiet", "init", "--maildir", testdir});
|
||||||
"{} --quiet index'",
|
assert_valid_result(res);
|
||||||
MU_PROGRAM, testdir, MU_PROGRAM)};
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const auto res = run_command0({MU_PROGRAM, "--quiet", "index"});
|
||||||
|
assert_valid_result(res);
|
||||||
|
}
|
||||||
|
|
||||||
if (g_test_verbose())
|
if (g_test_verbose())
|
||||||
mu_printerrln("\n{}", cmdline);
|
mu_info("\nindexed {} @ {}", testdir, dbdir);
|
||||||
|
|
||||||
g_assert(g_spawn_command_line_sync(cmdline.c_str(), NULL, NULL, NULL, NULL));
|
|
||||||
auto xpath = join_paths(dbdir, "xapian");
|
auto xpath = join_paths(dbdir, "xapian");
|
||||||
/* ensure MUHOME worked */
|
/* ensure MUHOME worked */
|
||||||
g_assert_cmpuint(::access(xpath.c_str(), F_OK), ==, 0);
|
g_assert_cmpuint(::access(xpath.c_str(), F_OK), ==, 0);
|
||||||
|
|||||||
Reference in New Issue
Block a user