From 4b9391c8b15d5f9f7fabe03d5002c3d2b9a96023 Mon Sep 17 00:00:00 2001 From: Tony Su Date: Mon, 17 Apr 2023 15:13:05 +0800 Subject: [PATCH] [ir-test]: Enhance ir-test for Linux and Windows This is a series of enhancements for ir-test and related Makefile for Linux and Windows. UpdateLog: 1) Make ir-test run from any directory 2) add --help option and print program usage 3) Enable user to specify test folders and/or files to run 4) Check unsupported option and print help usage 5) Update Makefile to use new ir-test options 6) some code refactory Signed-off-by: Tony Su Reviewed-by : Dmitry Stogov Reviewed-by : Anatol Belski --- Makefile | 4 +- ir-test.cxx | 105 ++++++++++++++++++++++++++++++++++++++----------- win32/Makefile | 8 +--- 3 files changed, 87 insertions(+), 30 deletions(-) diff --git a/Makefile b/Makefile index f1a02be..d6dfaab 100644 --- a/Makefile +++ b/Makefile @@ -91,10 +91,10 @@ test: $(BUILD_DIR)/ir $(BUILD_DIR)/ir-test $(BUILD_DIR)/ir $(SRC_DIR)/test.ir --dump --save 2>$(BUILD_DIR)/test.log $(BUILD_DIR)/ir $(SRC_DIR)/test.ir --dot $(BUILD_DIR)/ir.dot dot -Tpdf $(BUILD_DIR)/ir.dot -o $(BUILD_DIR)/ir.pdf - BUILD_DIR=$(BUILD_DIR) SRC_DIR=$(SRC_DIR) $(BUILD_DIR)/ir-test + $(BUILD_DIR)/ir-test $(SRC_DIR)/tests test-ci: $(BUILD_DIR)/ir $(BUILD_DIR)/ir-test - BUILD_DIR=$(BUILD_DIR) SRC_DIR=$(SRC_DIR) $(BUILD_DIR)/ir-test --show-diff + $(BUILD_DIR)/ir-test --show-diff $(SRC_DIR)/tests clean: rm -rf $(BUILD_DIR)/ir $(BUILD_DIR)/ir_test $(BUILD_DIR)/*.o \ diff --git a/ir-test.cxx b/ir-test.cxx index 0e4f290..4555ef2 100644 --- a/ir-test.cxx +++ b/ir-test.cxx @@ -12,8 +12,10 @@ #include #include #include +#include #include #include +#include #ifdef _WIN32 # include @@ -37,7 +39,8 @@ std::vector bad_list; std::vector> failed; std::vector> xfailed_list; bool show_diff = false, colorize = true; -std::string src_dir, build_dir, test_dir, ir_exe, ir_target; +std::string ir_exe, ir_target; +std::string this_exe, this_exe_path; namespace ir { decltype(auto) trim(std::string s) { @@ -60,12 +63,6 @@ namespace ir { return result; } - decltype(auto) get_dir_from_env(const char* name) { - const char* _tmp = std::getenv(name); - std::string ret = _tmp ? _tmp : "."; - return ret; - } - enum color { GREEN, YELLOW, RED }; decltype(auto) colorize(const std::string& s, enum color c) { @@ -218,38 +215,102 @@ namespace ir { } }; - void find_tests_in_dir(std::string& test_dir, std::vector& irt_files) { - for (const std::filesystem::directory_entry& ent : - std::filesystem::recursive_directory_iterator(test_dir)) { - std::string fl(ent.path().string()); - if (fl.length() < 4 || 0 != fl.compare(fl.length()-4, 4, ".irt")) continue; - irt_files.push_back(fl); + void find_tests_in_dir(std::set& user_files, std::vector& irt_files) { + assert(user_files.size()>0); + + for (const auto& f : user_files) { + if (std::filesystem::is_directory(f)) { + for (const std::filesystem::directory_entry& ent : + std::filesystem::recursive_directory_iterator(f)) { + std::string fl(ent.path().string()); + if (fl.length() < 4 || ent.path().extension() != ".irt") + continue; + irt_files.push_back(fl); + } + } else { + irt_files.push_back(f); + } } - std::sort(irt_files.begin(), irt_files.end(), [&](const std::string& a, const std::string& b) { - return 1 <= b.compare(a); - }); + + std::sort(irt_files.begin(), + irt_files.end(), + [&](const std::string& a, const std::string& b) { + return 1 <= b.compare(a); + }); } } +static void print_help(void) +{ + std::cout << "Run IR uint tests\n"; + std::cout << "Usage:\n "; + std::cout << ::this_exe + << " [--show-diff] [--no-color] [test folders or files...]\n"; + std::cout << " Run all tests if no test folders/files are specified\n"; +} + int main(int argc, char **argv) { + // Save this executable path to infer ir executable path + ::this_exe = argv[0]; + ::this_exe_path = std::filesystem::path{::this_exe}.parent_path().string(); + + // Store test folders/files specified by user + std::set user_files; + bool bad_opt = false; // unsupported option given + bool bad_files = false; // bad file or folder given + for (int i = 1; i < argc; i++) { // XXX use some cleaner arg parsing solution if (!std::string(argv[i]).compare("--show-diff")) { ::show_diff = true; + continue; } else if (!std::string(argv[i]).compare("--no-color")) { ::colorize = false; + continue; + } else if (!std::string(argv[i]).compare("--help")) { + print_help(); + return 0; + } else if (std::string(argv[i]).find_first_of("-") == 0) { + // Unsupported options + bad_opt = true; + std::cerr << ir::colorize("ERROR", ir::RED) + << ": Unsupported Option [" << argv[i] << "]\n"; + } else { + // User specified test folders/files + std::string file = argv[i]; + if (std::filesystem::exists(file) + && (std::filesystem::is_directory(file) + || std::filesystem::is_regular_file(file))) { + user_files.insert(argv[i]); + } else { + bad_files = true; + std::cerr << ir::colorize("ERROR", ir::RED) + << ": Bad File or Folder [" << file << "] \n"; + } } + } /* for: arguments iteration */ + + if (bad_opt || bad_files) { + if (bad_opt) + print_help(); + return 1; } + ir::init_console(); - ::build_dir = ir::get_dir_from_env("BUILD_DIR"); - ::src_dir = ir::get_dir_from_env("SRC_DIR"); - ::test_dir = ::src_dir + PATH_SEP + "tests"; - ::ir_exe = ::build_dir + PATH_SEP + "ir" + EXE_SUF; + ::ir_exe = ::this_exe_path + PATH_SEP + "ir" + EXE_SUF; ::ir_target = ir::trim(ir::exec(::ir_exe + " --target")); - std::vector irt_files; - ir::find_tests_in_dir(::test_dir, irt_files); + // Get test files, either specified by user or all tests by default + std::vector irt_files; + if (user_files.empty()) { + // Pretend user specified all test + std::string tests_dir = ::this_exe_path + PATH_SEP + "tests"; + user_files.insert(tests_dir); + } + ir::find_tests_in_dir(user_files, irt_files); + + // Run each test for (const std::string& test_fl : irt_files) { try { auto test = ir::test(test_fl); diff --git a/win32/Makefile b/win32/Makefile index 37fef36..0b8b23f 100644 --- a/win32/Makefile +++ b/win32/Makefile @@ -118,15 +118,11 @@ $(BUILD_DIR)\ir-test.exe: $(SRC_DIR)/ir-test.cxx test: $(BUILD_DIR)\ir.exe $(BUILD_DIR)\ir-test.exe set PATH=$(VCPKG_DIR)\installed\$(VCPKG_TRIPLET)\bin:%%PATH%% $(BUILD_DIR)\ir.exe $(SRC_DIR)\test.ir --dump --save $(BUILD_DIR)\test.log - set BUILD_DIR=$(BUILD_DIR) - set SRC_DIR=$(SRC_DIR) - $(BUILD_DIR)\ir-test.exe + $(BUILD_DIR)\ir-test.exe $(SRC_DIR)\tests test-ci: $(BUILD_DIR)\ir.exe $(BUILD_DIR)\ir-test.exe set PATH=$(VCPKG_DIR)\installed\$(VCPKG_TRIPLET)\bin:%%PATH%% - set BUILD_DIR=$(BUILD_DIR) - set SRC_DIR=$(SRC_DIR) - $(BUILD_DIR)\ir-test.exe --show-diff + $(BUILD_DIR)\ir-test.exe --show-diff $(SRC_DIR)\tests clean: del /f /q $(BUILD_DIR)\*.obj $(BUILD_DIR)\*.exe $(BUILD_DIR)\*.pdb $(BUILD_DIR)\*.ilk $(BUILD_DIR)\*.h