File tree Expand file tree Collapse file tree 4 files changed +64
-2
lines changed
Expand file tree Collapse file tree 4 files changed +64
-2
lines changed Original file line number Diff line number Diff line change @@ -140,6 +140,7 @@ if(BUILD_TESTING)
140140 add_executable (serial_tests
141141 test /test_serial_simple.cpp
142142 test /test_serial_pty.cpp
143+ test /test_ports.cpp
143144 )
144145
145146 target_include_directories (serial_tests PRIVATE
Original file line number Diff line number Diff line change @@ -40,6 +40,14 @@ class Ports {
4040 */
4141Ports () = default ;
4242
43+ /* *
44+ * @brief Testable constructor allowing a custom system path
45+ *
46+ * Primarily intended for testing to inject a non-existent or
47+ * non-readable directory to validate error handling paths.
48+ */
49+ explicit Ports (const char * sys_path) : sys_path_(sys_path) {}
50+
4351/* *
4452 * @brief Destroyer of the Ports class
4553 *
@@ -96,6 +104,11 @@ std::optional<std::string> findName(uint16_t id) const;
96104 */
97105static constexpr const char * kSysSerialByIdPath = " /dev/serial/by-id/" ;
98106
107+ /* *
108+ * @brief Configurable path used by scanPorts; defaults to kSysSerialByIdPath
109+ */
110+ const char * sys_path_ { kSysSerialByIdPath };
111+
99112/* *
100113 * @brief Internal list of detected serial devices
101114 */
Original file line number Diff line number Diff line change @@ -23,10 +23,10 @@ uint16_t Ports::scanPorts() {
2323
2424 // Directory where udev creates symlinks for serial devices by ID
2525 // this directory may not exist if no serial devices are connected
26- const char * by_id_dir = kSysSerialByIdPath ;
26+ const char * by_id_dir = sys_path_ ;
2727 DIR* dir = opendir (by_id_dir);
2828 if (!dir) {
29- throw SerialException (" Error while reading " + std::string (by_id_dir) + " : " + strerror (errno));
29+ throw PortNotFoundException (" Error while reading " + std::string (by_id_dir) + " : " + strerror (errno));
3030 }
3131
3232 // The POSIX directory-entry structure used by readdir() to describe files
Original file line number Diff line number Diff line change 1+ // Copyright 2020-2025 Nestor Neto
2+
3+ #include < gtest/gtest.h>
4+ #include < memory>
5+ #include < string>
6+
7+ // Include libserial headers
8+ #include " libserial/ports.hpp"
9+ #include " libserial/serial_exception.hpp"
10+
11+ // Simple unit tests that don't require actual hardware
12+ class PortsTest : public ::testing::Test {
13+ protected:
14+ void SetUp () override {
15+ // Test setup if needed
16+ }
17+
18+ void TearDown () override {
19+ // Test cleanup if needed
20+ }
21+ };
22+
23+ TEST_F (PortsTest, DefaultConstructor) {
24+ EXPECT_NO_THROW ({
25+ libserial::Ports ports;
26+ });
27+ }
28+
29+ TEST_F (PortsTest, ScanPortsThrowsWhenPathMissing) {
30+ // Use an unlikely path to exist to trigger the exception path
31+ const char * missing_path = " /this/path/should/not/exist/serial/by-id" ;
32+ libserial::Ports ports (missing_path);
33+
34+
35+ try {
36+ (void )ports.scanPorts ();
37+ } catch (const libserial::SerialException& e) {
38+ std::cout << " Caught SerialException: " << e.what () << std::endl;
39+ // Optionally assert something about the message:
40+ // EXPECT_NE(std::string(e.what()).find("Error while reading"), std::string::npos);
41+ }
42+
43+ EXPECT_THROW ({
44+ (void )ports.scanPorts ();
45+ }, libserial::PortNotFoundException);
46+ }
47+
48+
You can’t perform that action at this time.
0 commit comments