Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions tests/common-cleanup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ namespace testing::cleanup {
class file_t
{
public:
explicit file_t(std::string const &filename,
bool remove_on_construct = true)
: m_filename(filename)
explicit file_t(std::string filename, bool remove_on_construct = true)
: m_filename(std::move(filename))
{
if (remove_on_construct) {
delete_file(false);
Expand All @@ -38,6 +37,10 @@ class file_t
~file_t() noexcept { delete_file(true); }

private:
// This function is run from a destructor so must be noexcept. If an
// exception does occur the program is terminated and we are fine with
// that, it is test code after all.
// NOLINTNEXTLINE(bugprone-exception-escape)
void delete_file(bool warn) const noexcept
{
if (m_filename.empty()) {
Expand Down
2 changes: 2 additions & 0 deletions tests/common-options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class opt_t
m_opt.output_dbschema = "public";
}

// Implicit conversion is intended here for ease of use in lots of tests.
// NOLINTNEXTLINE(google-explicit-constructor,hicpp-explicit-conversions)
operator options_t() const { return m_opt; }

opt_t &slim()
Expand Down
37 changes: 20 additions & 17 deletions tests/common-pg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class tempdb_t
"Test database cannot be created: {}\n"
"Did you mean to run 'pg_virtualenv ctest'?\n",
e.what());
std::exit(1);
std::exit(1); // NOLINT(concurrency-mt-unsafe)
}
}

Expand All @@ -134,22 +134,25 @@ class tempdb_t

~tempdb_t() noexcept
{
if (!m_db_name.empty()) {
// Disable removal of the test database by setting the environment
// variable OSM2PGSQL_KEEP_TEST_DB to anything. This can be useful
// when debugging tests.
char const *const keep_db = std::getenv("OSM2PGSQL_KEEP_TEST_DB");
if (keep_db != nullptr) {
return;
}
try {
connection_params_t connection_params;
connection_params.set("dbname", "postgres");
conn_t const conn{connection_params};
conn.exec(R"(DROP DATABASE IF EXISTS "{}")", m_db_name);
} catch (...) {
fprintf(stderr, "DROP DATABASE failed. Ignored.\n");
}
if (m_db_name.empty()) {
return;
}

// Disable removal of the test database by setting the environment
// variable OSM2PGSQL_KEEP_TEST_DB to anything. This can be useful
// when debugging tests.
// NOLINTNEXTLINE(concurrency-mt-unsafe)
char const *const keep_db = std::getenv("OSM2PGSQL_KEEP_TEST_DB");
if (keep_db != nullptr) {
return;
}
try {
connection_params_t connection_params;
connection_params.set("dbname", "postgres");
conn_t const conn{connection_params};
conn.exec(R"(DROP DATABASE IF EXISTS "{}")", m_db_name);
} catch (...) {
fmt::print(stderr, "DROP DATABASE failed. Ignored.\n");
}
}

Expand Down