From 975c9de32f697214494e181369fb700dd2ea91e8 Mon Sep 17 00:00:00 2001
From: gcontini <contini.mailing@gmail.com>
Date: 周日, 13 10月 2019 19:44:07 +0800
Subject: [PATCH] fix test windows

---
 test/library/LicenseLocator_test.cpp |   85 +++++++++++++++++++++++++-----------------
 test/functional/CMakeLists.txt       |   16 ++++----
 test/library/LicenseReader_test.cpp  |    2 
 test/functional/volid_test.cpp       |    5 +-
 4 files changed, 62 insertions(+), 46 deletions(-)

diff --git a/test/functional/CMakeLists.txt b/test/functional/CMakeLists.txt
index 1214157..3d122ea 100644
--- a/test/functional/CMakeLists.txt
+++ b/test/functional/CMakeLists.txt
@@ -20,24 +20,24 @@
 )
 
 add_executable(
- date_test
+ test_date
  date_test.cpp
 )
 
 target_link_libraries(
- date_test
+ test_date
  licensecc_static
  license_generator_snippet
  ${Boost_LIBRARIES}
 )
 
 add_executable(
- volid_test
+ test_volid
  volid_test.cpp
 )
 
 target_link_libraries(
- volid_test
+ test_volid
  licensecc_static
  license_generator_snippet
  ${Boost_LIBRARIES}
@@ -47,12 +47,12 @@
 IF( ( CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux") AND CMAKE_CROSSCOMPILING)
 #binfmt_misc doesn't work in my system :(
 	ADD_TEST(NAME standard_license_test COMMAND wine ${CMAKE_CURRENT_BINARY_DIR}/standard_license_test WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
-	ADD_TEST(NAME date_test COMMAND wine ${CMAKE_CURRENT_BINARY_DIR}/date_test WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
-	ADD_TEST(NAME volid_test COMMAND wine ${CMAKE_CURRENT_BINARY_DIR}/volid_test WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
+	ADD_TEST(NAME test_date COMMAND wine ${CMAKE_CURRENT_BINARY_DIR}/test_date WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
+	ADD_TEST(NAME test_volid COMMAND wine ${CMAKE_CURRENT_BINARY_DIR}/test_volid WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
 ELSE()
 	ADD_TEST(NAME standard_license_test COMMAND standard_license_test WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
-	ADD_TEST(NAME date_test COMMAND date_test WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
-	ADD_TEST(NAME volid_test COMMAND volid_test WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
+	ADD_TEST(NAME test_date COMMAND test_date WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
+	ADD_TEST(NAME test_volid COMMAND test_volid WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
 ENDIF()
 
 
diff --git a/test/functional/volid_test.cpp b/test/functional/volid_test.cpp
index a91d0bc..d02b36d 100644
--- a/test/functional/volid_test.cpp
+++ b/test/functional/volid_test.cpp
@@ -1,6 +1,5 @@
-#define BOOST_TEST_MODULE standard_license_test
-//#define BOOST_TEST_MAIN
-//#undef BOOST_TEST_DYN_LINK
+#define BOOST_TEST_MODULE test_volid
+
 #include <boost/test/unit_test.hpp>
 #include <fstream>
 #include <stdio.h>
diff --git a/test/library/LicenseLocator_test.cpp b/test/library/LicenseLocator_test.cpp
index 512ffb1..ce966fe 100644
--- a/test/library/LicenseLocator_test.cpp
+++ b/test/library/LicenseLocator_test.cpp
@@ -7,6 +7,8 @@
 #include <string>
 #include <vector>
 
+#include <boost/filesystem.hpp>
+#include <boost/optional.hpp>
 #include <boost/test/unit_test.hpp>
 #include <stdlib.h>
 #include <cstdio>
@@ -22,51 +24,66 @@
 namespace test {
 using namespace license::locate;
 using namespace std;
+using namespace boost::filesystem;
+
+static boost::optional<path> find_file(const path& dir_path, const path& file_name) {
+	const recursive_directory_iterator end;
+	const auto it = find_if(recursive_directory_iterator(dir_path), end,
+		[&file_name](const directory_entry& e) {
+			return e.path().filename() == file_name;
+		});
+	return it == end ? boost::optional<path>() : it->path();
+}
+
 
 /*****************************************************************************
  * Application Folder tests
  *****************************************************************************/
 BOOST_AUTO_TEST_CASE( read_license_near_module ) {
-#ifdef _WIN32
-#ifdef _DEBUG
-	const string testExeFolder = PROJECT_BINARY_DIR "/test/library/Debug";
-#else
-	const string testExeFolder = PROJECT_BINARY_DIR "/test/library/Release";
-#endif
-	const string testExe = testExeFolder + "/" + BOOST_TEST_MODULE ".exe";
-#else
 	const string testExeFolder = PROJECT_BINARY_DIR "/test/library";
-	const string testExe = testExeFolder + "/" + BOOST_TEST_MODULE;
+	bool exeFileFound = false;
+	string referenceExeFileName;
+	string referenceLicenseFileName;
+	//Verify we're pointing the correct executable, in windows isn't clear where it's built
+#ifdef _WIN32
+	boost::optional<path> exeLocation(find_file(path(testExeFolder), path(BOOST_TEST_MODULE ".exe")));
+	exeFileFound = exeLocation.has_value();
+	if (exeFileFound) {
+		referenceExeFileName = exeLocation.get().string();
+		referenceLicenseFileName = referenceExeFileName.replace(referenceExeFileName.find(BOOST_TEST_MODULE ".exe"), 
+			string(BOOST_TEST_MODULE ".exe").size(), BOOST_TEST_MODULE ".lic");
+	}
+#else
+	const string referenceExeFileName = testExeFolder + "/" + BOOST_TEST_MODULE;
+	ifstream f(referenceExeFileName.c_str());
+	exeFileFound = f.good();
+	referenceLicenseFileName = testExeFolder + "/" + BOOST_TEST_MODULE ".lic";
 #endif	
-	const string referenceLicenseFileName = testExeFolder + "/"
-			+ BOOST_TEST_MODULE ".lic";
+	BOOST_WARN_MESSAGE(!exeFileFound, "File [" + referenceExeFileName + "] NOT found");
+	if (exeFileFound) {
+		//copy test license near module
+		std::ifstream src(MOCK_LICENSE, std::ios::binary);
+		std::ofstream dst(referenceLicenseFileName, std::ios::binary);
+		dst << src.rdbuf();
+		dst.close();
 
-	//Verify we're pointing the correct executable
-	ifstream f(testExe.c_str());
-	BOOST_REQUIRE_MESSAGE(f.good(), "File [" + testExe + "] NOT found");
-
-	//copy test license near module
-	std::ifstream src(MOCK_LICENSE, std::ios::binary);
-	std::ofstream dst(referenceLicenseFileName, std::ios::binary);
-	dst << src.rdbuf();
-	dst.close();
-
-	license::EventRegistry registry;
-	ApplicationFolder applicationFolder;
-	vector<string> licenseInfos = applicationFolder.licenseLocations(registry);
-	BOOST_CHECK(registry.isGood());
-	BOOST_REQUIRE_EQUAL(1, licenseInfos.size());
-	string currentLocation = licenseInfos[0];
-	BOOST_CHECK_MESSAGE(referenceLicenseFileName.compare(currentLocation) == 0,
-			"file found at expected location");
-	string licenseRealContent = applicationFolder.retrieveLicense(
+		license::EventRegistry registry;
+		ApplicationFolder applicationFolder;
+		vector<string> licenseInfos = applicationFolder.licenseLocations(registry);
+		BOOST_CHECK(registry.isGood());
+		BOOST_REQUIRE_EQUAL(1, licenseInfos.size());
+		string currentLocation = licenseInfos[0];
+		BOOST_CHECK_MESSAGE(equivalent(path(referenceLicenseFileName),path(currentLocation)),
+			"file " +currentLocation + "found at expected location");
+		string licenseRealContent = applicationFolder.retrieveLicense(
 			currentLocation);
-	src.seekg(0, ios::beg);
-	std::string referenceContent((std::istreambuf_iterator<char>(src)),
+		src.seekg(0, ios::beg);
+		std::string referenceContent((std::istreambuf_iterator<char>(src)),
 			std::istreambuf_iterator<char>());
-	BOOST_CHECK_MESSAGE(referenceContent.compare(licenseRealContent) == 0,
+		BOOST_CHECK_MESSAGE(referenceContent.compare(licenseRealContent) == 0,
 			"File content is same");
-	remove(referenceLicenseFileName.c_str());
+		remove(referenceLicenseFileName.c_str());
+	}
 }
 
 /*****************************************************************************
diff --git a/test/library/LicenseReader_test.cpp b/test/library/LicenseReader_test.cpp
index b413fcf..df86061 100644
--- a/test/library/LicenseReader_test.cpp
+++ b/test/library/LicenseReader_test.cpp
@@ -1,4 +1,4 @@
-#define BOOST_TEST_MODULE "license_reader_test"
+#define BOOST_TEST_MODULE "test_license_reader"
 
 #include <boost/test/unit_test.hpp>
 #include <stdlib.h>

--
Gitblit v1.9.1