utils: add some more helpers for test code

Creating and removing (temp) dirs, running mu commands.
This commit is contained in:
Dirk-Jan C. Binnema
2023-07-09 12:44:47 +03:00
parent 904f64aa03
commit cc65b8b401
4 changed files with 87 additions and 11 deletions

View File

@ -147,6 +147,33 @@ Mu::canonicalize_filename(const std::string& path, const std::string& relative_t
return str;
}
Result<std::string>
Mu::make_temp_dir()
{
GError *err{};
if (auto tmpdir{g_dir_make_tmp("mu-tmp-XXXXXX", &err)}; !tmpdir)
return Err(Error::Code::File, &err,
"failed to create temporary directory");
else
return Ok(to_string_gchar(std::move(tmpdir)));
}
Result<void>
Mu::remove_directory(const std::string& path)
{
/* ugly */
GError *err{};
const auto cmd{mu_format("/bin/rm -rf '{}'", path)};
if (!g_spawn_command_line_sync(cmd.c_str(), NULL,
NULL, NULL, &err))
return Err(Error::Code::File, &err, "failed to remove {}", path);
else
return Ok();
}
std::string
Mu::runtime_path(Mu::RuntimePath path, const std::string& muhome)