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:
Dirk-Jan C. Binnema
2025-11-25 21:25:21 +02:00
committed by Seth Ladygo
parent 9f69055ef9
commit 410ff46881
5 changed files with 48 additions and 52 deletions

View File

@ -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
** under the terms of the GNU General Public License as published by the
@ -28,6 +28,8 @@
#include <string.h>
#include "utils/mu-test-utils.hh"
#include "utils/mu-utils-file.hh"
#include <lib/mu-store.hh>
#include <utils/mu-utils.hh>
@ -38,25 +40,22 @@ static std::string test_dir;
static std::string
fill_database(void)
{
const auto cmdline = mu_format(
"/bin/sh -c '"
"{} init --muhome={} --maildir={} --quiet; "
"{} index --muhome={} --quiet'",
MU_PROGRAM,
test_dir,
MU_TESTMAILDIR2,
MU_PROGRAM,
test_dir);
{
const auto res = run_command0({MU_PROGRAM,
"--quiet", "init",
"--muhome", test_dir,
"--maildir", MU_TESTMAILDIR2});
assert_valid_result(res);
}
{
const auto res = run_command0({MU_PROGRAM, "--quiet",
"index", "--muhome", test_dir});
assert_valid_result(res);
}
if (g_test_verbose())
mu_println("{}", cmdline);
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);
}
mu_println("\nindexed {} @ {}", MU_TESTMAILDIR2, test_dir);
return test_dir;
}
@ -72,22 +71,11 @@ test_something(const char* what)
g_print("GUILE_LOAD_PATH: %s\n", GUILE_LOAD_PATH);
const auto dir = fill_database();
const auto cmdline = mu_format("{} -q -e main {}/test-mu-guile.scm "
"--muhome={} --test={}",
GUILE_BINARY, ABS_SRCDIR,
dir, what);
const auto res = run_command0({GUILE_BINARY, "-q", "-e", "main",
ABS_SRCDIR"/test-mu-guile.scm",
"--muhome", dir, "--test", what});
if (g_test_verbose())
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);
}
assert_valid_result(res);
}
static void