From 4619776fa54ae1464ffb868f880886e9896bb80a Mon Sep 17 00:00:00 2001 From: Venelin Date: Thu, 12 Dec 2024 15:16:13 +0200 Subject: [PATCH] CLI test --- run_tests.sh | 10 +++++++--- src/cppipe.cpp | 30 +++++++++++++----------------- test/c_file.c | 2 +- test/command_line_test.sh | 8 ++++++++ todo.txt | 4 +--- 5 files changed, 30 insertions(+), 24 deletions(-) create mode 100755 test/command_line_test.sh diff --git a/run_tests.sh b/run_tests.sh index 563c417..e617fa8 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -2,14 +2,18 @@ set -e # Test cppipe functions -OKs=$(cppipe test/functions_test.cppipe | grep OK | wc -l) +OKs=$(cppipe test/functions_test.cppipe 2>/dev/null | grep OK | wc -l) if ! [ $OKs = 12 ] then - echo "EXPECTED 1 OKs, got $OKs" + echo "Functions test failed: EXPECTED 12 OKs, got $OKs" exit 1 fi +echo Functions test OK! # Test on a C file -echo OK | cppipe test/c_file.c +cppipe test/c_file.c + +# Test the cppipe command +./test/command_line_test.sh echo ALL TESTS PASSED diff --git a/src/cppipe.cpp b/src/cppipe.cpp index 9066fdb..1f457c4 100644 --- a/src/cppipe.cpp +++ b/src/cppipe.cpp @@ -59,6 +59,7 @@ SrcType find_src_type(string_view path); const char DEBUG_PREFIX[] = "__DBG"; // Context +fs::path HOME; bool debug = false; fs::path src_file; SrcType src_type; @@ -72,6 +73,7 @@ int main(int argc, char* argv[]) int src_arg = parse_args_until_src(argc, argv); // Init context + HOME = getenv("HOME"); src_file = find_path_to_src( argv[src_arg] ); src_type = find_src_type( argv[src_arg] ); cache_dir = get_cache_dir_path(src_file); @@ -146,12 +148,6 @@ fs::path find_path_to_src(string_view src_file) return src_file; } - // Search in ~/cppipe - if(fs::path p = fs::path(getenv("HOME")) / "cppipe" / src_file; fs::exists(p)) - { - return p; - } - // Search files on CPPIPEPATH const char* cppipepath_var = getenv("CPPIPEPATH"); if(cppipepath_var) // is set @@ -161,16 +157,11 @@ fs::path find_path_to_src(string_view src_file) ; begin = end+1, end = cppipepath.find(':', begin) ) { - const string_view path_entry = cppipepath.substr(begin, end - begin); - if( fs::is_directory(path_entry) ) + const fs::path path_entry = cppipepath.substr(begin, end - begin); + + if( fs::path p = path_entry / src_file; fs::exists(p) ) { - for(const fs::directory_entry& e: fs::directory_iterator(path_entry)) - { - if(fs::is_regular_file(e) && e.path().filename() == src_file) - { - return e; - } - } + return p; } if(end == string::npos) @@ -178,6 +169,12 @@ fs::path find_path_to_src(string_view src_file) } } + // Search in ~/cppipe + if(fs::path p = HOME / "cppipe" / src_file; fs::exists(p)) // todo: what if ~/cppipe/../... matches + { + return p; + } + // Couln't find the src cerr << "File: " << src_file << " doesn't exist\n"; exit(1); @@ -193,8 +190,7 @@ fs::path get_cache_dir_path(const fs::path& src_file) } else { - char* home( getenv("HOME") ); - cache_dir = home; + cache_dir = HOME; cache_dir /= ".cache/cppipe"; } cache_dir += fs::canonical( src_file ).parent_path(); diff --git a/test/c_file.c b/test/c_file.c index f997a5f..b0c867a 100644 --- a/test/c_file.c +++ b/test/c_file.c @@ -8,7 +8,7 @@ int main() x = y; /* should only work on C */ if(x == 0) { - printf("C compilation OK!\n"); + printf("C compilation: OK!\n"); } return 0; diff --git a/test/command_line_test.sh b/test/command_line_test.sh new file mode 100755 index 0000000..aa211cf --- /dev/null +++ b/test/command_line_test.sh @@ -0,0 +1,8 @@ +#!/bin/sh +set -e + +export CPPIPEPATH="/:test::/usr" + +cppipe c_file.c > /dev/null + +echo Command line test: OK! diff --git a/todo.txt b/todo.txt index fb38479..d3db373 100644 --- a/todo.txt +++ b/todo.txt @@ -3,20 +3,18 @@ tips (sigaction SIGCHILD, SIG_IGN, SIG_DFL) make sure file descriptors are closed when no longer used cppipe compile options from tft respect CXXFLAGS LDFLAGS -remove append_args ? -run(cmd) string args args before src file go to compiler use exceptions (open_or_die) +throw when a command couldnt be ran precompiled header cppipe.h -Wno-unused-result in install.sh file operations, lack of uniformity (C++ vs POSIX) Check all return codes and report errors shaded obj uninstall -throw when a command couldnt be ran improve debug (the src file shown is the .ii) no optimizations option split