test: finish the temp-file cleanup (#14976)

Follow-up to #14785. Routes the tests that still hand-rolled temp paths through
the shared helpers and unifies the temp guards.

- Add ScopedTemporaryDir and a shared ScopedTemporaryPath base under it and
  ScopedTemporaryFile.
- Move test_3mf's round-trip .3mf output out of the TEST_DATA_DIR source tree
  (a fixed-name leak) and test_toolordering's fixed-name temp .gcode (a sharding
  collision) onto ScopedTemporaryFile.
- Move test_config, test_slicing_pipeline_bindings, the test_3mf backup dirs, and
  test_preset_bundle_loading onto the guards.
- Make slic3rutils ScopedDataDir compose ScopedTemporaryDir; dedupe
  test_network_versions' fixture and delete test_plugin_lifecycle's duplicate.
This commit is contained in:
Kris Austin
2026-08-07 11:33:37 -05:00
committed by GitHub
parent 8bff9aaf32
commit 74aed7a2bb
9 changed files with 106 additions and 153 deletions

View File

@@ -27,26 +27,47 @@ inline Slic3r::TriangleMesh load_model(const std::string &obj_filename)
return mesh;
}
// RAII holder for a unique temporary file path, removed when the guard goes out
// of scope so a failing assertion never leaks it. Uses the system temp dir with
// a unique name (parallel-safe, cross-platform). The file itself is created by
// whoever writes to path()/string(); this only reserves the name and cleans up.
class ScopedTemporaryFile
// ---------------------------------------------------------------------------
// Scoped temporary paths
// ---------------------------------------------------------------------------
// Owns a unique path under the system temp dir, "<prefix>-<unique>[<extension>]"
// (parallel-safe, cross-platform). Shared base for the two RAII temp guards below.
class ScopedTemporaryPath
{
public:
const boost::filesystem::path &path() const { return m_path; }
std::string string() const { return m_path.string(); }
ScopedTemporaryPath(const ScopedTemporaryPath &) = delete;
ScopedTemporaryPath &operator=(const ScopedTemporaryPath &) = delete;
protected:
ScopedTemporaryPath(const std::string &prefix, const std::string &extension)
: m_path(boost::filesystem::temp_directory_path()
/ boost::filesystem::unique_path(prefix + "-%%%%-%%%%-%%%%" + extension))
{}
~ScopedTemporaryPath() = default; // non-virtual: never deleted through a base pointer
boost::filesystem::path m_path;
};
// A temp file the caller creates by writing to path()/string(); the guard only
// reserves the name and removes the file on scope exit.
class ScopedTemporaryFile : public ScopedTemporaryPath
{
public:
explicit ScopedTemporaryFile(const std::string &extension = ".tmp")
: m_path(boost::filesystem::temp_directory_path()
/ boost::filesystem::unique_path("orca-%%%%-%%%%-%%%%" + extension))
{}
: ScopedTemporaryPath("orca", extension) {}
~ScopedTemporaryFile() { boost::system::error_code ec; boost::filesystem::remove(m_path, ec); }
ScopedTemporaryFile(const ScopedTemporaryFile &) = delete;
ScopedTemporaryFile &operator=(const ScopedTemporaryFile &) = delete;
};
const boost::filesystem::path &path() const { return m_path; }
std::string string() const { return m_path.string(); }
private:
boost::filesystem::path m_path;
// A temp directory created on construction and removed recursively on scope exit.
class ScopedTemporaryDir : public ScopedTemporaryPath
{
public:
explicit ScopedTemporaryDir(const std::string &prefix = "orca")
: ScopedTemporaryPath(prefix, "") { boost::filesystem::create_directories(m_path); }
~ScopedTemporaryDir() { boost::system::error_code ec; boost::filesystem::remove_all(m_path, ec); }
};
// ---------------------------------------------------------------------------
@@ -66,7 +87,7 @@ inline std::string debug_artifact_path(const std::string &name)
boost::filesystem::path dir = boost::filesystem::temp_directory_path()
/ boost::filesystem::unique_path("orca-test-artifacts-%%%%-%%%%");
boost::filesystem::create_directories(dir);
std::printf("Debug test artifacts will be written to %s\n", dir.string().c_str());
std::fprintf(stderr, "Debug test artifacts will be written to %s\n", dir.string().c_str());
return dir;
}();
boost::filesystem::path full = root / name;
@@ -75,57 +96,52 @@ inline std::string debug_artifact_path(const std::string &name)
}
// Dump a mesh as OBJ.
inline void write_debug_obj(const std::string &name, const Slic3r::TriangleMesh &mesh)
inline void write_debug_obj([[maybe_unused]] const std::string &name,
[[maybe_unused]] const Slic3r::TriangleMesh &mesh)
{
#ifndef NDEBUG
mesh.WriteOBJFile(debug_artifact_path(name).c_str());
#else
(void) name; (void) mesh;
#endif
}
inline void write_debug_obj(const std::string &name, const indexed_triangle_set &its)
inline void write_debug_obj([[maybe_unused]] const std::string &name,
[[maybe_unused]] const indexed_triangle_set &its)
{
#ifndef NDEBUG
its_write_obj(its, debug_artifact_path(name).c_str());
#else
(void) name; (void) its;
#endif
}
// Dump a mesh as ASCII STL.
inline void write_debug_stl(const std::string &name, const Slic3r::TriangleMesh &mesh)
inline void write_debug_stl([[maybe_unused]] const std::string &name,
[[maybe_unused]] const Slic3r::TriangleMesh &mesh)
{
#ifndef NDEBUG
mesh.write_ascii(debug_artifact_path(name).c_str());
#else
(void) name; (void) mesh;
#endif
}
// Draw an SVG artifact through a callback that receives the open SVG. Second
// overload takes a BoundingBox when the drawing needs one.
template<class Draw>
inline void write_debug_svg(const std::string &name, Draw &&draw)
inline void write_debug_svg([[maybe_unused]] const std::string &name, [[maybe_unused]] Draw &&draw)
{
#ifndef NDEBUG
Slic3r::SVG svg(debug_artifact_path(name));
draw(svg);
svg.Close();
#else
(void) name; (void) draw;
#endif
}
template<class Draw>
inline void write_debug_svg(const std::string &name, const Slic3r::BoundingBox &bbox, Draw &&draw)
inline void write_debug_svg([[maybe_unused]] const std::string &name,
[[maybe_unused]] const Slic3r::BoundingBox &bbox,
[[maybe_unused]] Draw &&draw)
{
#ifndef NDEBUG
Slic3r::SVG svg(debug_artifact_path(name), bbox);
draw(svg);
svg.Close();
#else
(void) name; (void) bbox; (void) draw;
#endif
}
@@ -133,13 +149,11 @@ inline void write_debug_svg(const std::string &name, const Slic3r::BoundingBox &
// artifact. operator<< is resolved by ADL at the call site, so this header needn't
// include the producer's headers.
template<class Produce>
inline void write_debug_stream(const std::string &name, Produce &&produce)
inline void write_debug_stream([[maybe_unused]] const std::string &name, [[maybe_unused]] Produce &&produce)
{
#ifndef NDEBUG
std::ofstream out(debug_artifact_path(name), std::ios::out | std::ios::binary);
out << produce();
#else
(void) name; (void) produce;
#endif
}