Copilot commented on code in PR #13805:
URL: https://github.com/apache/trafficserver/pull/13805#discussion_r4313641074
##########
plugins/experimental/abuse_shield/unit_tests/test_config.cc:
##########
@@ -22,14 +22,19 @@
#include <catch2/catch_test_macros.hpp>
-#include <chrono>
+#include <cerrno>
#include <cstdarg>
+#include <cstdlib>
#include <cstdio>
+#include <cstring>
#include <filesystem>
#include <fstream>
#include <sstream>
#include <string>
#include <system_error>
+#include <vector>
+
+#include <unistd.h>
Review Comment:
`mkdtemp()` and `<unistd.h>` are POSIX-specific and will not compile on
Windows/MSVC. Since the previous implementation used only standard C++
(`std::filesystem`), this change likely introduces a portability regression for
the unit tests. Consider either (a) using a project-provided cross-platform
temp-dir helper (if one exists), or (b) adding a platform abstraction/`#ifdef
_WIN32` path that uses a Windows-appropriate atomic directory creation
mechanism.
##########
plugins/experimental/abuse_shield/unit_tests/test_config.cc:
##########
@@ -49,9 +54,16 @@ class TempConfig
public:
TempConfig()
{
- auto suffix = std::chrono::steady_clock::now().time_since_epoch().count();
- dir_ = std::filesystem::temp_directory_path() /
("abuse_shield_config_test_" + std::to_string(suffix));
- std::filesystem::create_directories(dir_);
+ auto dir_template = (std::filesystem::temp_directory_path() /
"abuse_shield_config_test_XXXXXX").string();
+ std::vector<char> mutable_template{dir_template.begin(),
dir_template.end()};
+
+ mutable_template.push_back('\0');
+ auto *created_dir = mkdtemp(mutable_template.data());
Review Comment:
`mkdtemp()` and `<unistd.h>` are POSIX-specific and will not compile on
Windows/MSVC. Since the previous implementation used only standard C++
(`std::filesystem`), this change likely introduces a portability regression for
the unit tests. Consider either (a) using a project-provided cross-platform
temp-dir helper (if one exists), or (b) adding a platform abstraction/`#ifdef
_WIN32` path that uses a Windows-appropriate atomic directory creation
mechanism.
##########
plugins/experimental/abuse_shield/unit_tests/test_config.cc:
##########
@@ -49,9 +54,16 @@ class TempConfig
public:
TempConfig()
{
- auto suffix = std::chrono::steady_clock::now().time_since_epoch().count();
- dir_ = std::filesystem::temp_directory_path() /
("abuse_shield_config_test_" + std::to_string(suffix));
- std::filesystem::create_directories(dir_);
+ auto dir_template = (std::filesystem::temp_directory_path() /
"abuse_shield_config_test_XXXXXX").string();
+ std::vector<char> mutable_template{dir_template.begin(),
dir_template.end()};
+
+ mutable_template.push_back('\0');
+ auto *created_dir = mkdtemp(mutable_template.data());
Review Comment:
This copies the template string into a `std::vector<char>` just to get a
mutable, null-terminated buffer. If the codebase is using C++17 or later,
`std::string` storage is mutable and can typically be passed directly (after
ensuring null-termination), which avoids an extra allocation/copy and
simplifies the code.
##########
plugins/experimental/abuse_shield/unit_tests/test_config.cc:
##########
@@ -49,9 +54,16 @@ class TempConfig
public:
TempConfig()
{
- auto suffix = std::chrono::steady_clock::now().time_since_epoch().count();
- dir_ = std::filesystem::temp_directory_path() /
("abuse_shield_config_test_" + std::to_string(suffix));
- std::filesystem::create_directories(dir_);
+ auto dir_template = (std::filesystem::temp_directory_path() /
"abuse_shield_config_test_XXXXXX").string();
+ std::vector<char> mutable_template{dir_template.begin(),
dir_template.end()};
+
+ mutable_template.push_back('\0');
+ auto *created_dir = mkdtemp(mutable_template.data());
+ auto error = errno;
+
+ INFO(std::strerror(error));
+ REQUIRE(created_dir != nullptr);
Review Comment:
`errno` is captured and `INFO(std::strerror(error))` is emitted
unconditionally, even when `mkdtemp()` succeeds (where `errno` may be stale).
This can make diagnostics misleading if the constructor fails for a different
reason later. Prefer capturing/printing `errno` only on failure (e.g., check
`created_dir == nullptr` first, then include `std::strerror(errno)` in a
`REQUIRE_MESSAGE` or conditional `INFO`).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]