|
1 | 1 | #include "app_helper.hpp"
|
2 | 2 | #include <complex>
|
3 | 3 | #include <cstdlib>
|
| 4 | +#include <sys/stat.h> |
4 | 5 |
|
5 | 6 | #include "gmock/gmock.h"
|
6 | 7 |
|
@@ -1476,6 +1477,50 @@ TEST_F(TApp, FileExists) {
|
1476 | 1477 | EXPECT_FALSE(CLI::ExistingFile(myfile).empty());
|
1477 | 1478 | }
|
1478 | 1479 |
|
| 1480 | +TEST_F(TApp, FileExistsForRead) { |
| 1481 | + std::string myfile{"TestNonFileNotUsed.txt"}; |
| 1482 | + EXPECT_FALSE(CLI::ExistingReadableFile(myfile).empty()); |
| 1483 | + |
| 1484 | + bool ok = static_cast<bool>(std::ofstream(myfile.c_str()).put('a')); // create file |
| 1485 | + EXPECT_TRUE(ok); |
| 1486 | + |
| 1487 | + std::string filename = "Failed"; |
| 1488 | + app.add_option("--file", filename)->check(CLI::ExistingReadableFile); |
| 1489 | + args = {"--file", myfile}; |
| 1490 | + |
| 1491 | + run(); |
| 1492 | + |
| 1493 | + EXPECT_EQ(myfile, filename); |
| 1494 | +#ifdef __linux__ |
| 1495 | + my_chmod(myfile.c_str(), 0); |
| 1496 | + EXPECT_THROW(run(), CLI::ValidationError); |
| 1497 | +#endif |
| 1498 | + std::remove(myfile.c_str()); |
| 1499 | + EXPECT_FALSE(CLI::ExistingFile(myfile).empty()); |
| 1500 | +} |
| 1501 | + |
| 1502 | +TEST_F(TApp, FileExistsForWrite) { |
| 1503 | + std::string myfile{"TestNonFileNotUsed.txt"}; |
| 1504 | + EXPECT_FALSE(CLI::ExistingWritableFile(myfile).empty()); |
| 1505 | + |
| 1506 | + bool ok = static_cast<bool>(std::ofstream(myfile.c_str()).put('a')); // create file |
| 1507 | + EXPECT_TRUE(ok); |
| 1508 | + |
| 1509 | + std::string filename = "Failed"; |
| 1510 | + app.add_option("--file", filename)->check(CLI::ExistingWritableFile); |
| 1511 | + args = {"--file", myfile}; |
| 1512 | + |
| 1513 | + run(); |
| 1514 | + EXPECT_EQ(myfile, filename); |
| 1515 | + |
| 1516 | + my_chmod(myfile.c_str(), S_IREAD); |
| 1517 | + EXPECT_THROW(run(), CLI::ValidationError); |
| 1518 | + |
| 1519 | + int ret = std::remove(myfile.c_str()); |
| 1520 | + EXPECT_EQ(ret, 0); |
| 1521 | + EXPECT_FALSE(CLI::ExistingFile(myfile).empty()); |
| 1522 | +} |
| 1523 | + |
1479 | 1524 | TEST_F(TApp, NotFileExists) {
|
1480 | 1525 | std::string myfile{"TestNonFileNotUsed.txt"};
|
1481 | 1526 | EXPECT_FALSE(CLI::ExistingFile(myfile).empty());
|
|
0 commit comments