>From eb9b88be07e10853dbdcd341dd79bc87235b03c7 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 14 Dec 2014 17:21:48 +0100 Subject: [PATCH 2/4] Add --gui_test_path command line option to the GUI test. This option is used to construct full paths for the input files used by the test and all the output files deliberately created during the test. --- main_wx_test.cpp | 86 +++++++++++++++++++++++++++++++++++++++-- wx_test_case.hpp | 15 +++++++ wx_test_create_open.cpp | 10 +--- wx_test_input_sequences.cpp | 8 +--- wx_test_input_validation.cpp | 5 +-- wx_test_paste_census.cpp | 3 +- wx_test_validate_output.cpp | 11 +---- 7 files changed, 104 insertions(+), 34 deletions(-) diff --git a/main_wx_test.cpp b/main_wx_test.cpp index 26df872..b1fa798 100644 --- a/main_wx_test.cpp +++ b/main_wx_test.cpp @@ -48,6 +48,8 @@ #include #include +#include +#include #include #include // std::sort() @@ -177,6 +179,10 @@ class application_test // Used by tests to retrieve their configuration parameters. wxConfigBase const& get_config_for(char const* name); + // Return the configured directory (current one by default) to use for the + // test files. + fs::path const& get_test_files_path() const { return test_files_path_; } + // Used to check if distribution tests should be enabled. bool is_distribution_test() const { return is_distribution_test_; } @@ -231,6 +237,8 @@ class application_test boost::scoped_ptr config_; + fs::path test_files_path_; + bool run_all_; bool is_distribution_test_; @@ -302,11 +310,20 @@ void remove_arg(int n, int& argc, char* argv[]) bool application_test::process_command_line(int& argc, char* argv[]) { + // THIRD_PARTY !! We have this long and error-prone code to parse the + // command line manually here only because getopt_long() is not composable + // and so we can't use it with our own options here while still leaving the + // standard options for the base class to handle. It would be better to use + // a standard command line parsing mechanism if it ever becomes possible. + // This variable is used both as a flag indicating that the last option was // the one selecting the test to run and so must be followed by the test // name, but also for the diagnostic message at the end of this function. char const* last_test_option = 0; + char const* opt_gui_test_path = "--gui_test_path"; + int const opt_gui_test_path_length = strlen(opt_gui_test_path); + for(int n = 1; n < argc; ) { char const* const arg = argv[n]; @@ -339,6 +356,32 @@ bool application_test::process_command_line(int& argc, char* argv[]) is_distribution_test_ = true; remove_arg(n, argc, argv); } + else if(0 == std::strncmp(arg, opt_gui_test_path, opt_gui_test_path_length)) + { + if (arg[opt_gui_test_path_length]=='=') + { + test_files_path_ = arg + opt_gui_test_path_length + 1; + } + else + { + if (n == argc - 1) + { + warning() + << "Option '" + << opt_gui_test_path + << "' must be followed by the path to use." + << std::flush + ; + } + else + { + remove_arg(n, argc, argv); + test_files_path_ = argv[n]; + } + } + + remove_arg(n, argc, argv); + } else if ( 0 == std::strcmp(arg, "-h") @@ -352,11 +395,12 @@ bool application_test::process_command_line(int& argc, char* argv[]) "Usage: " << argv[0] << "\n" - " -h,\t--help \tdisplay this help and exit\n" - " -l,\t--list \tlist all available tests and exit\n" - " -t or \trun only the specified test (may occur\n" - " --test \tmultiple times); default: run all tests\n" - " --distribution \tenable distribution-specific tests\n" + " -h,\t--help \tdisplay this help and exit\n" + " -l,\t--list \tlist all available tests and exit\n" + " -t or \trun only the specified test (may occur\n" + " --test \tmultiple times); default: run all tests\n" + " --gui_test_path \tpath to use for test files\n" + " --distribution \tenable distribution-specific tests\n" "\n" "Additionally, all command line options supported by the\n" "main lmi executable are also supported." @@ -380,6 +424,27 @@ bool application_test::process_command_line(int& argc, char* argv[]) ; } + // Ensure that the path used for the test files is always valid and + // absolute, so that it doesn't change even if the program current + // directory changes for whatever reason. + if(test_files_path_.empty() || !fs::exists(test_files_path_)) + { + if(!test_files_path_.empty()) + { + warning() + << "Test files path '" + << test_files_path_.native_file_string() + << "' doesn't exist." + << std::flush + ; + } + test_files_path_ = fs::current_path(); + } + else + { + test_files_path_ = fs::system_complete(test_files_path_); + } + return true; } @@ -508,6 +573,17 @@ void wx_base_test_case::skip_if_not_supported(char const* file) } } +fs::path wx_base_test_case::get_test_files_path() const +{ + return application_test::instance().get_test_files_path(); +} + +std::string +wx_base_test_case::get_test_file_path_for(std::string const& basename) const +{ + return (get_test_files_path() / basename).native_file_string(); +} + bool wx_base_test_case::is_distribution_test() const { return application_test::instance().is_distribution_test(); diff --git a/wx_test_case.hpp b/wx_test_case.hpp index 07b2533..ab517c1 100644 --- a/wx_test_case.hpp +++ b/wx_test_case.hpp @@ -28,6 +28,8 @@ #include "uncopyable_lmi.hpp" +#include + class wxConfigBase; /// Base class for the test case objects. @@ -65,6 +67,19 @@ class wx_base_test_case /// Throws test_skipped_exception if the file is not supported. void skip_if_not_supported(char const* file); + /// Return the base directory containing the test files. + /// + /// This is the same directory as is used by get_test_file_path_for(), + /// prefer to use that function if possible. + fs::path get_test_files_path() const; + + /// Return the full path for the file with the given base name (which + /// should include the extension, but no path components). + /// + /// The directory of the returned path can be changed by using the command + /// line --gui_test_path option when running the test. + std::string get_test_file_path_for(std::string const& basename) const; + /// Return true if running in distribution testing mode. /// /// This method is used to restrict execution of the tests that are diff --git a/wx_test_create_open.cpp b/wx_test_create_open.cpp index 967bcd2..096d988 100644 --- a/wx_test_create_open.cpp +++ b/wx_test_create_open.cpp @@ -49,11 +49,12 @@ void do_test_create_open (wx_base_test_case& test ,int key - ,wxString const& file + ,char const* basename ,bool uses_dialog) { - test.skip_if_not_supported(file.c_str()); + test.skip_if_not_supported(basename); + wxString const file = test.get_test_file_path_for(basename); LMI_ASSERT(!wxFileExists(file)); wxUIActionSimulator z; @@ -104,11 +105,6 @@ wxYield(); } -// ERASE THIS BLOCK COMMENT WHEN IMPLEMENTATION COMPLETE. The block -// comment below changes the original specification, and does not -// yet describe the present code. Desired changes: -// - Put all files in 'gui_test_path'. - /// Create, save, and reopen a file of each available type. /// /// Validate each tested operation, then erase the file. diff --git a/wx_test_input_sequences.cpp b/wx_test_input_sequences.cpp index 584eebd..e31b336 100644 --- a/wx_test_input_sequences.cpp +++ b/wx_test_input_sequences.cpp @@ -30,7 +30,6 @@ #include "configurable_settings.hpp" #include "wx_test_case.hpp" -#include #include #include @@ -61,17 +60,12 @@ LMI_WX_TEST_CASE(input_sequences) { - // Construct the path of the file to open, it's supposed to be in the same - // directory as the default input filename. - wxFileName fn(configurable_settings::instance().default_input_filename()); // Instead use '--gui_test_path'. - fn.SetFullName("InputSequences.cns"); - wxUIActionSimulator ui; ui.Char('o', wxMOD_CONTROL); // "File|Open" wxTEST_DIALOG (wxYield() - ,wxExpectModal(fn.GetFullPath()) + ,wxExpectModal(get_test_file_path_for("InputSequences.cns")) ); ui.Char('r', wxMOD_CONTROL | wxMOD_SHIFT); // "Census|Run case" diff --git a/wx_test_input_validation.cpp b/wx_test_input_validation.cpp index 19eda9b..caa0ae3 100644 --- a/wx_test_input_validation.cpp +++ b/wx_test_input_validation.cpp @@ -30,7 +30,6 @@ #include "configurable_settings.hpp" #include "wx_test_case.hpp" -#include #include #include @@ -63,11 +62,9 @@ wxUIActionSimulator ui; ui.Char('o', wxMOD_CONTROL); // "File|Open" - wxFileName fn(configurable_settings::instance().default_input_filename()); // Instead use '--gui_test_path'. - fn.SetFullName("CoiMultiplier.cns"); wxTEST_DIALOG (wxYield() - ,wxExpectModal(fn.GetFullPath()) + ,wxExpectModal(get_test_file_path_for("CoiMultiplier.cns")) ); ui.Char('r', wxMOD_CONTROL | wxMOD_SHIFT); // "Census|Run case" diff --git a/wx_test_paste_census.cpp b/wx_test_paste_census.cpp index a0e75e5..849908a 100644 --- a/wx_test_paste_census.cpp +++ b/wx_test_paste_census.cpp @@ -141,7 +141,6 @@ bool does_list_have_column(wxDataViewCtrl* dvc, wxString const& name) // - Save pastable data inline; don't extract from user manual. // - Validate all columns after each step (after initial pasting). // - Test change in class defaults (in addition to case defaults). -// - Place the saved file in 'gui_test_path'. /// Test pasting spreadsheet data into a census. /// @@ -268,7 +267,7 @@ bool does_list_have_column(wxDataViewCtrl* dvc, wxString const& name) LMI_ASSERT(!does_list_have_column(list_window, column_title)); // Finally save the census with the pasted data for later inspection. - static char const* census_file_name = "PasteCensus.cns"; + wxString const census_file_name = get_test_file_path_for("PasteCensus.cns"); ui.Char('a', wxMOD_CONTROL); // "File|Save as" wxTEST_DIALOG diff --git a/wx_test_validate_output.cpp b/wx_test_validate_output.cpp index f288849..6ba33de 100644 --- a/wx_test_validate_output.cpp +++ b/wx_test_validate_output.cpp @@ -33,7 +33,6 @@ #include "wx_test_case.hpp" #include "wx_test_new.hpp" -#include #include #include @@ -167,13 +166,10 @@ class output_file_existence_checker output_file_existence_checker existing_trace("MonthlyTrace.monthly_trace" + ext); - wxFileName fn(configurable_settings::instance().default_input_filename()); // Instead use '--gui_test_path'. - fn.SetFullName("MonthlyTrace.ill"); - ui.Char('o', wxMOD_CONTROL); // "File|Open" wxTEST_DIALOG (wxYield() - ,wxExpectModal(fn.GetFullPath()) + ,wxExpectModal(get_test_file_path_for("MonthlyTrace.ill")) ,wxExpectModal(wxID_OK) // Ignore warning. ,wxExpectDismissableModal(wxID_OK) // Accept defaults. ); @@ -211,13 +207,10 @@ class output_file_existence_checker // And when opening an existing one. output_file_existence_checker existing_output("MecTesting.mec" + ext); - wxFileName fn(configurable_settings::instance().default_input_filename()); // Instead use '--gui_test_path'. - fn.SetFullName("MecTesting.mec"); - ui.Char('o', wxMOD_CONTROL); // "File|Open" wxTEST_DIALOG (wxYield() - ,wxExpectModal(fn.GetFullPath()) + ,wxExpectModal(get_test_file_path_for("MecTesting.mec")) ,wxExpectDismissableModal(wxID_OK) // Accept defaults. ); -- 1.7.9