From 48bcabbd372f3ee27dee3045332c28ae223afeac Mon Sep 17 00:00:00 2001 From: Raffi Enficiaud Date: Thu, 11 Aug 2016 11:50:51 +0200 Subject: [PATCH] JUnit default output stream - string replacement function refactoring - junit now determining the output stream based on available file - junit does not replace existing file --- .../boost/test/impl/junit_log_formatter.ipp | 30 +++++++++++++++++++ .../boost/test/output/junit_log_formatter.hpp | 5 ++++ 2 files changed, 35 insertions(+) diff --git a/include/boost/test/impl/junit_log_formatter.ipp b/include/boost/test/impl/junit_log_formatter.ipp index 565837fe..2a5b2637 100644 --- a/include/boost/test/impl/junit_log_formatter.ipp +++ b/include/boost/test/impl/junit_log_formatter.ipp @@ -28,6 +28,7 @@ #include #include +#include //#include @@ -37,6 +38,7 @@ // STL #include +#include #include #include @@ -604,6 +606,34 @@ junit_log_formatter::log_entry_context( std::ostream& ostr, const_string context //____________________________________________________________________________// + +std::string +junit_log_formatter::get_default_stream_description() const { + std::string name = framework::master_test_suite().p_name.value; + + static const std::string to_replace[] = { " ", "\"", "/", "\\", ":"}; + static const std::string replacement[] = { "_", "_" , "_", "_" , "_"}; + + name = unit_test::utils::replace_all_occurrences_of( + name, + to_replace, to_replace + sizeof(to_replace)/sizeof(to_replace[0]), + replacement, replacement + sizeof(replacement)/sizeof(replacement[0])); + + std::ifstream check_init((name + ".xml").c_str()); + if(!check_init) + return name + ".xml"; + + int index = 0; + for(; index < 100; index++) { + std::string candidate = name + "_" + utils::string_cast(index) + ".xml"; + std::ifstream file(candidate.c_str()); + if(!file) + return candidate; + } + + return name + ".xml"; +} + } // namespace output } // namespace unit_test } // namespace boost diff --git a/include/boost/test/output/junit_log_formatter.hpp b/include/boost/test/output/junit_log_formatter.hpp index fdd4f251..e2c09ebc 100644 --- a/include/boost/test/output/junit_log_formatter.hpp +++ b/include/boost/test/output/junit_log_formatter.hpp @@ -114,6 +114,11 @@ public: { } + //! Instead of a regular stream, returns a file name corresponding to + //! the current master test suite. If the file already exists, adds an index + //! to it. + virtual std::string get_default_stream_description() const; + private: typedef std::map map_trace_t;