Skip to content

Commit

Permalink
Merge pull request #4327 from arkamar/tmpdir
Browse files Browse the repository at this point in the history
[Test] Use TMPDIR if available
  • Loading branch information
vstakhov committed Nov 8, 2022
2 parents 18216cd + 0d67e64 commit c172190
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
18 changes: 14 additions & 4 deletions src/libutil/cxx/file_util.cxx
Expand Up @@ -368,13 +368,22 @@ TEST_CASE("check lock") {
CHECK(serrno == ENOENT);
}

auto get_tmpdir() -> const char * {
const auto *tmpdir = getenv("TMPDIR");
if (tmpdir == nullptr) {
tmpdir = G_DIR_SEPARATOR_S "tmp";
}
return tmpdir;
}

TEST_CASE("tempfile") {
std::string tmpname;
const std::string tmpdir{get_tmpdir()};
{
auto raii_locked_file = raii_locked_file::mkstemp("/tmp//doctest-XXXXXXXX",
auto raii_locked_file = raii_locked_file::mkstemp(std::string(tmpdir + G_DIR_SEPARATOR_S + "doctest-XXXXXXXX").c_str(),
O_RDONLY, 00600);
CHECK(raii_locked_file.has_value());
CHECK(raii_locked_file.value().get_dir() == "/tmp");
CHECK(raii_locked_file.value().get_dir() == tmpdir);
CHECK(access(raii_locked_file.value().get_name().data(), R_OK) == 0);
auto raii_locked_file2 = raii_locked_file::open(raii_locked_file.value().get_name().data(), O_RDONLY);
CHECK(!raii_locked_file2.has_value());
Expand All @@ -390,11 +399,12 @@ TEST_CASE("tempfile") {

TEST_CASE("mmap") {
std::string tmpname;
const std::string tmpdir{get_tmpdir()};
{
auto raii_file = raii_file::mkstemp("/tmp//doctest-XXXXXXXX",
auto raii_file = raii_file::mkstemp(std::string(tmpdir + G_DIR_SEPARATOR_S + "doctest-XXXXXXXX").c_str(),
O_RDWR|O_CREAT|O_EXCL, 00600);
CHECK(raii_file.has_value());
CHECK(raii_file->get_dir() == "/tmp");
CHECK(raii_file->get_dir() == tmpdir);
CHECK(access(raii_file->get_name().data(), R_OK) == 0);
tmpname = std::string{raii_file->get_name()};
char payload[] = {'1', '2', '3'};
Expand Down
19 changes: 10 additions & 9 deletions test/lua/unit/sqlite3.lua
@@ -1,18 +1,19 @@
context("Sqlite3 API", function()
local sqlite3 = require "rspamd_sqlite3"
local tmpdir = os.getenv("TMPDIR") or "/tmp"

test("Sqlite3 open", function()
os.remove('/tmp/rspamd_unit_test_sqlite3.sqlite')
local db = sqlite3.open('/tmp/rspamd_unit_test_sqlite3.sqlite')
os.remove(tmpdir .. '/rspamd_unit_test_sqlite3.sqlite')
local db = sqlite3.open(tmpdir .. '/rspamd_unit_test_sqlite3.sqlite')
assert_not_nil(db, "should be able to create sqlite3 db")
db = sqlite3.open('/non/existent/path/rspamd_unit_test_sqlite3.sqlite')
assert_nil(db, "should not be able to create sqlite3 db")
os.remove('/tmp/rspamd_unit_test_sqlite3.sqlite')
os.remove(tmpdir .. '/rspamd_unit_test_sqlite3.sqlite')
end)

test("Sqlite3 query", function()
os.remove('/tmp/rspamd_unit_test_sqlite3-1.sqlite')
local db = sqlite3.open('/tmp/rspamd_unit_test_sqlite3-1.sqlite')
os.remove(tmpdir .. '/rspamd_unit_test_sqlite3-1.sqlite')
local db = sqlite3.open(tmpdir .. '/rspamd_unit_test_sqlite3-1.sqlite')
assert_not_nil(db, "should be able to create sqlite3 db")

local ret = db:sql([[
Expand All @@ -23,12 +24,12 @@ context("Sqlite3 API", function()
INSERT INTO x VALUES (?1, ?2);
]], 1, 'test')
assert_true(ret, "should be able to insert row")
os.remove('/tmp/rspamd_unit_test_sqlite3-1.sqlite')
os.remove(tmpdir .. '/rspamd_unit_test_sqlite3-1.sqlite')
end)

test("Sqlite3 rows", function()
os.remove('/tmp/rspamd_unit_test_sqlite3-2.sqlite')
local db = sqlite3.open('/tmp/rspamd_unit_test_sqlite3-2.sqlite')
os.remove(tmpdir .. '/rspamd_unit_test_sqlite3-2.sqlite')
local db = sqlite3.open(tmpdir .. '/rspamd_unit_test_sqlite3-2.sqlite')
assert_not_nil(db, "should be able to create sqlite3 db")

local ret = db:sql([[
Expand All @@ -44,6 +45,6 @@ context("Sqlite3 API", function()
assert_equal(row.id, '1')
assert_equal(row.value, 'test')
end
os.remove('/tmp/rspamd_unit_test_sqlite3-2.sqlite')
os.remove(tmpdir .. '/rspamd_unit_test_sqlite3-2.sqlite')
end)
end)

0 comments on commit c172190

Please sign in to comment.