Skip to content

Commit 4e16f43

Browse files
authored
Merge pull request #4 from aymaneelmaini/fix-tests-and-ci
Fix tests and ci
2 parents ad015a7 + 532c441 commit 4e16f43

3 files changed

Lines changed: 49 additions & 29 deletions

File tree

.github/workflows/ci.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ jobs:
2323
sudo apt-get update
2424
sudo apt-get install -y \
2525
build-essential \
26-
cmake
26+
cmake \
27+
libcriterion-dev
2728
2829
- name: Configure CMake
2930
run: |
@@ -32,7 +33,7 @@ jobs:
3233
cmake ..
3334
3435
- name: Build
35-
run: cmake --build build
36+
run: cmake --build build --target xpscan_tests
3637

3738
- name: Run tests
38-
run: cd build && make test
39+
run: cd build && ./xpscan_tests

CMakeLists.txt

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,9 @@ set(CMAKE_CXX_STANDARD 17)
55
set(SOURCES src/Scanner.cpp src/Exporter.cpp src/Printer.cpp)
66
set(TEST_SOURCES tests/ExporterTest.cpp tests/ScannerTest.cpp tests/PrinterTest.cpp)
77

8-
# Fetch Criterion (Testing library)
9-
include(FetchContent)
10-
FetchContent_Declare(criterion
11-
GIT_REPOSITORY https://github.com/Snaipe/Criterion.git
12-
GIT_TAG v2.4.2
13-
GIT_SHALLOW TRUE
14-
GIT_PROGRESS TRUE
15-
)
16-
set(FETCHCONTENT_QUIET FALSE)
17-
FetchContent_MakeAvailable(criterion)
8+
# Find Criterion (Testing library)
9+
find_package(PkgConfig REQUIRED)
10+
pkg_check_modules(CRITERION REQUIRED criterion)
1811

1912
# Look for threads
2013
find_package(Threads REQUIRED)
@@ -28,7 +21,8 @@ target_link_libraries(xpscan PRIVATE Threads::Threads)
2821

2922
# Tests runner
3023
add_executable(xpscan_tests EXCLUDE_FROM_ALL ${TEST_SOURCES} ${SOURCES})
31-
target_link_libraries(xpscan_tests PRIVATE Threads::Threads criterion)
24+
target_include_directories(xpscan_tests PRIVATE ${CRITERION_INCLUDE_DIRS})
25+
target_link_libraries(xpscan_tests PRIVATE Threads::Threads ${CRITERION_LIBRARIES})
3226

3327
# Test target
3428
add_custom_target(test

tests/ExporterTest.cpp

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
#include <iostream>
77
#include <sstream>
88
#include <vector>
9+
#include <random>
10+
#include <chrono>
911

1012
// Helper functions signatures
11-
std::string createTestDirectory();
13+
std::string createTestDirectory(const std::string &test_name);
1214
std::string createTestConfig(const std::string &export_path,
1315
const std::string &suffix = "");
14-
void cleanupTestFiles(const std::string &pattern);
16+
void cleanupTestFiles(const std::string &directory);
1517

1618
Test(Exporter, empty_results_should_skip_json_export) {
1719
Exporter exporter;
@@ -39,7 +41,7 @@ Test(Exporter, empty_results_should_skip_text_export) {
3941
}
4042

4143
Test(Exporter, should_export_valid_json_with_results) {
42-
std::string test_dir = createTestDirectory();
44+
std::string test_dir = createTestDirectory("json_test");
4345
std::string config_path = createTestConfig(test_dir, "_json");
4446

4547
std::vector<PortResult> results = {{80, "http"}, {443, "https"}, {22, "ssh"}};
@@ -79,13 +81,12 @@ Test(Exporter, should_export_valid_json_with_results) {
7981
cr_assert(json_content.find("\"service\": \"http\"") != std::string::npos,
8082
"JSON should contain http service");
8183

82-
cleanupTestFiles(test_dir);
8384
std::filesystem::remove(config_path);
85+
std::filesystem::remove_all(test_dir);
8486
}
8587

8688
Test(Exporter, should_export_valid_text_with_results) {
87-
std::string test_dir = createTestDirectory();
88-
cleanupTestFiles(test_dir);
89+
std::string test_dir = createTestDirectory("txt_test");
8990
std::string config_path = createTestConfig(test_dir, "_txt");
9091

9192
std::vector<PortResult> results = {{80, "http"}, {443, "https"}};
@@ -126,12 +127,12 @@ Test(Exporter, should_export_valid_text_with_results) {
126127
cr_assert(txt_content.find("Service: http") != std::string::npos,
127128
"TXT should contain http service");
128129

129-
cleanupTestFiles(test_dir);
130130
std::filesystem::remove(config_path);
131+
std::filesystem::remove_all(test_dir);
131132
}
132133

133134
Test(Exporter, getExportPath_should_read_from_config) {
134-
std::string test_dir = "/tmp/custom_export_path/";
135+
std::string test_dir = createTestDirectory("getpath_test");
135136
std::string config_path = createTestConfig(test_dir, "_getpath");
136137

137138
Exporter exporter(config_path);
@@ -143,26 +144,50 @@ Test(Exporter, getExportPath_should_read_from_config) {
143144
result.c_str(), test_dir.c_str());
144145

145146
std::filesystem::remove(config_path);
147+
std::filesystem::remove_all(test_dir);
146148
}
147149

148-
std::string createTestDirectory() {
149-
std::string test_dir = "/tmp/xpscan_test/";
150+
std::string createTestDirectory(const std::string &test_name) {
151+
// Create unique directory using timestamp and random number to avoid collisions
152+
auto now = std::chrono::high_resolution_clock::now();
153+
auto timestamp = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()).count();
154+
155+
std::random_device rd;
156+
std::mt19937 gen(rd());
157+
std::uniform_int_distribution<> dis(1000, 9999);
158+
int random_id = dis(gen);
159+
160+
std::string test_dir = "/tmp/xpscan_test_" + test_name + "_" +
161+
std::to_string(timestamp) + "_" +
162+
std::to_string(random_id) + "/";
150163
std::filesystem::create_directories(test_dir);
151164
return test_dir;
152165
}
153166

154167
std::string createTestConfig(const std::string &export_path,
155168
const std::string &suffix) {
156-
std::string config_path = "/tmp/test_config" + suffix + ".conf";
169+
// Create unique config file using timestamp and random number
170+
auto now = std::chrono::high_resolution_clock::now();
171+
auto timestamp = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()).count();
172+
173+
std::random_device rd;
174+
std::mt19937 gen(rd());
175+
std::uniform_int_distribution<> dis(1000, 9999);
176+
int random_id = dis(gen);
177+
178+
std::string config_path = "/tmp/test_config" + suffix + "_" +
179+
std::to_string(timestamp) + "_" +
180+
std::to_string(random_id) + ".conf";
157181
std::ofstream config_file(config_path);
158182
config_file << export_path;
159183
config_file.close();
160184
return config_path;
161185
}
162186

163-
void cleanupTestFiles(const std::string &pattern) {
164-
for (const auto &entry :
165-
std::filesystem::directory_iterator("/tmp/xpscan_test/")) {
166-
std::filesystem::remove(entry.path());
187+
void cleanupTestFiles(const std::string &directory) {
188+
if (std::filesystem::exists(directory)) {
189+
for (const auto &entry : std::filesystem::directory_iterator(directory)) {
190+
std::filesystem::remove(entry.path());
191+
}
167192
}
168193
}

0 commit comments

Comments
 (0)