}
}
+void
+ensure_dir_exists(nonstd::string_view dir)
+{
+ if (!create_dir(dir)) {
+ throw Fatal("Failed to create directory {}: {}", dir, strerror(errno));
+ }
+}
+
std::string
get_actual_cwd()
{
return string.ends_with(suffix);
}
+// Like create_dir but throws Fatal on error.
+void ensure_dir_exists(nonstd::string_view dir);
+
// Expand all instances of $VAR or ${VAR}, where VAR is an environment variable,
// in `str`. Throws `Error` if one of the environment variables.
[[nodiscard]] std::string expand_environment_variables(const std::string& str);
CHECK_FALSE(Util::ends_with("x", "xy"));
}
+TEST_CASE("Util::ensure_dir_exists")
+{
+ TestContext test_context;
+
+ CHECK_NOTHROW(Util::ensure_dir_exists("/"));
+
+ CHECK_NOTHROW(Util::ensure_dir_exists("create/dir"));
+ CHECK(Stat::stat("create/dir").is_directory());
+
+ Util::write_file("create/dir/file", "");
+ CHECK_THROWS_WITH(
+ Util::ensure_dir_exists("create/dir/file"),
+ "Failed to create directory create/dir/file: Not a directory");
+}
+
TEST_CASE("Util::expand_environment_variables")
{
Util::setenv("FOO", "bar");