Skip to content

Commit

Permalink
Test that reading from the default filesystem works
Browse files Browse the repository at this point in the history
  • Loading branch information
mmahnic committed Dec 18, 2019
1 parent 23c69f6 commit d0a7717
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ target_link_libraries( runUnitTests
${GTEST_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT}
${CMAKE_BINARY_DIR}/src/libargparse-s.a
stdc++fs
)
add_dependencies( runUnitTests ${CPPARGPARSE_TEST_LIB} )

Expand Down
37 changes: 37 additions & 0 deletions test/filesystemarguments_t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@
#include <map>
#include <sstream>

#if __has_include( <filesystem> )
#define HAVE_FILESYSTEM 1
#include <filesystem>
namespace fs = std::filesystem;
#elif __has_include( <experimental/filesystem> )
#define HAVE_FILESYSTEM 1
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#else
#define HAVE_FILESYSTEM 0
#endif

using namespace argparse;

class TestFilesystem : public Filesystem
Expand Down Expand Up @@ -107,3 +119,28 @@ TEST( FilesystemArguments, shouldFailWhenStreamsRecursedTooDeep )
EXPECT_FALSE( !!res );
EXPECT_EQ( INCLUDE_TOO_DEEP, res.errors.front().errorCode );
}

// The default Filesystem reads from the real filesystem.
TEST( FilesystemArguments, shouldUseDefaultFilesystemWhenNoneIsDefined )
{
#if HAVE_FILESYSTEM
auto tmpdir = fs::temp_directory_path() / "xdata";
if ( !fs::exists( tmpdir ) )
fs::create_directory( tmpdir );
auto tmpfile = tmpdir / "a.opt";
auto f = std::ofstream( tmpfile );
f << "--alpha\nfrom-file";
f.close();

auto parser = argument_parser{};
std::string alpha;
parser.add_argument( alpha, "--alpha" ).nargs( 1 );

auto res = parser.parse_args( { "@" + tmpfile.generic_string() } );

EXPECT_TRUE( !!res );
EXPECT_EQ( "from-file", alpha );
#else
std::cout << "No filesystem. Test skipped.\n";
#endif
}

0 comments on commit d0a7717

Please sign in to comment.