test: add scoped file helper tests and update test build targets
This commit is contained in:
@@ -5,27 +5,37 @@ CXXFLAGS := -std=c++17 \
|
||||
-Wextra \
|
||||
-Werror \
|
||||
-Icore/include \
|
||||
-Ilogger/include
|
||||
-Ilogger/include \
|
||||
-Itests/helpers
|
||||
|
||||
LDFLAGS := -lstdc++
|
||||
|
||||
BUILD_DIR := build
|
||||
|
||||
TEST_BIN := $(BUILD_DIR)/result_test
|
||||
|
||||
RESULT_TEST_BIN := $(BUILD_DIR)/result_test
|
||||
SCOPED_FILE_TEST_BIN := $(BUILD_DIR)/scoped_file_test
|
||||
|
||||
RESULT_TEST_SRC := tests/app/core/result_test.cpp
|
||||
SCOPED_FILE_TEST_SRC := tests/helpers/scoped_file_test.cpp
|
||||
|
||||
|
||||
.PHONY: all test clean
|
||||
|
||||
all: test
|
||||
|
||||
|
||||
test: $(TEST_BIN)
|
||||
./$(TEST_BIN)
|
||||
test: $(RESULT_TEST_BIN) \
|
||||
$(SCOPED_FILE_TEST_BIN)
|
||||
./$(RESULT_TEST_BIN)
|
||||
./$(SCOPED_FILE_TEST_BIN)
|
||||
|
||||
|
||||
$(TEST_BIN): $(RESULT_TEST_SRC)
|
||||
$(RESULT_TEST_BIN): $(RESULT_TEST_SRC)
|
||||
@mkdir -p $(BUILD_DIR)
|
||||
$(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
|
||||
|
||||
|
||||
$(SCOPED_FILE_TEST_BIN): $(SCOPED_FILE_TEST_SRC)
|
||||
@mkdir -p $(BUILD_DIR)
|
||||
$(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
|
||||
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
class ScopedFile {
|
||||
public:
|
||||
explicit ScopedFile(std::string fileName) : fileName_(std::move(fileName)) {}
|
||||
|
||||
void throwIfExists() const {
|
||||
if (std::filesystem::exists(fileName_)) {
|
||||
throw std::runtime_error("Test file already exists: " + fileName_);
|
||||
}
|
||||
}
|
||||
|
||||
const std::string &getFileName() const { return fileName_; }
|
||||
|
||||
~ScopedFile() noexcept(false) {
|
||||
std::error_code ec;
|
||||
|
||||
std::filesystem::remove(fileName_, ec);
|
||||
}
|
||||
|
||||
private:
|
||||
std::string fileName_;
|
||||
};
|
||||
@@ -0,0 +1,83 @@
|
||||
#include <cassert>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <stdexcept>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "scoped_file.hpp"
|
||||
|
||||
namespace {
|
||||
|
||||
void testFileRemovedAfterScope() {
|
||||
const std::string fileName = "scoped_file_test.log";
|
||||
|
||||
{
|
||||
ScopedFile file(fileName);
|
||||
|
||||
file.throwIfExists();
|
||||
|
||||
std::ofstream output(file.getFileName());
|
||||
|
||||
assert(output.is_open());
|
||||
|
||||
output << "test";
|
||||
|
||||
output.close();
|
||||
|
||||
assert(std::filesystem::exists(fileName));
|
||||
} // call ~ScopedFile()
|
||||
|
||||
assert(!std::filesystem::exists(fileName));
|
||||
}
|
||||
|
||||
void testThrowIfExists() {
|
||||
const std::string fileName = "existing_file.log";
|
||||
|
||||
{
|
||||
ScopedFile file(fileName);
|
||||
|
||||
if (std::filesystem::exists(fileName)) {
|
||||
throw std::runtime_error("Test file already exists: " + fileName);
|
||||
}
|
||||
|
||||
std::ofstream output(file.getFileName());
|
||||
|
||||
assert(output.is_open());
|
||||
|
||||
output.close();
|
||||
|
||||
bool thrown = false;
|
||||
|
||||
try {
|
||||
file.throwIfExists();
|
||||
} catch (const std::runtime_error &) {
|
||||
thrown = true;
|
||||
}
|
||||
|
||||
assert(thrown);
|
||||
}
|
||||
|
||||
assert(!std::filesystem::exists(fileName));
|
||||
}
|
||||
|
||||
void testGetFileName() {
|
||||
ScopedFile file("test.log");
|
||||
|
||||
file.throwIfExists();
|
||||
|
||||
assert(file.getFileName() == "test.log");
|
||||
}
|
||||
|
||||
void runTests() {
|
||||
testFileRemovedAfterScope();
|
||||
testThrowIfExists();
|
||||
testGetFileName();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
runTests();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user