Skip to content

Commit 60f8aab

Browse files
committed
catch errors in setup/teardown of test database
Otherwise the tests are aborted with a runtime error which looks like a programming error.
1 parent 1142f20 commit 60f8aab

1 file changed

Lines changed: 25 additions & 15 deletions

File tree

tests/common-pg.hpp

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -150,27 +150,37 @@ class tempdb_t
150150
public:
151151
tempdb_t()
152152
{
153-
conn_t conn("dbname=postgres");
154-
155-
m_db_name =
156-
(boost::format("osm2pgsql-test-%1%-%2%") % getpid() % time(nullptr))
157-
.str();
158-
conn.exec(boost::format("DROP DATABASE IF EXISTS \"%1%\"") % m_db_name);
159-
conn.exec(
160-
boost::format("CREATE DATABASE \"%1%\" WITH ENCODING 'UTF8'") %
161-
m_db_name);
153+
try {
154+
conn_t conn("dbname=postgres");
162155

163-
conn_t local = connect();
164-
local.exec("CREATE EXTENSION postgis");
165-
local.exec("CREATE EXTENSION hstore");
156+
m_db_name =
157+
(boost::format("osm2pgsql-test-%1%-%2%") % getpid() % time(nullptr))
158+
.str();
159+
conn.exec(boost::format("DROP DATABASE IF EXISTS \"%1%\"") % m_db_name);
160+
conn.exec(
161+
boost::format("CREATE DATABASE \"%1%\" WITH ENCODING 'UTF8'") %
162+
m_db_name);
163+
164+
conn_t local = connect();
165+
local.exec("CREATE EXTENSION postgis");
166+
local.exec("CREATE EXTENSION hstore");
167+
} catch (std::runtime_error const &e) {
168+
fprintf(stderr, "Test database cannot be created: %s\n", e.what());
169+
fprintf(stderr, "Did you mean to run 'pg_virtualenv ctest'?\n");
170+
exit(1);
171+
}
166172
}
167173

168174
~tempdb_t() noexcept
169175
{
170176
if (!m_db_name.empty()) {
171-
conn_t conn("dbname=postgres");
172-
conn.query(boost::format("DROP DATABASE IF EXISTS \"%1%\"") %
173-
m_db_name);
177+
try {
178+
conn_t conn("dbname=postgres");
179+
conn.query(boost::format("DROP DATABASE IF EXISTS \"%1%\"") %
180+
m_db_name);
181+
} catch (...) {
182+
fprintf(stderr, "DROP DATABASE failed. Ignored.\n");
183+
}
174184
}
175185
}
176186

0 commit comments

Comments
 (0)