|
36 | 36 | #include "rapidassist/testing.h" |
37 | 37 | #include "rapidassist/environment.h" |
38 | 38 | #include "rapidassist/cli.h" |
39 | | -#include "rapidassist/filesystem.h" |
| 39 | +#include "rapidassist/filesystem_utf8.h" |
40 | 40 | #include "rapidassist/unicode.h" |
| 41 | +#include "rapidassist/process_utf8.h" |
| 42 | +#include "rapidassist/user_utf8.h" |
41 | 43 |
|
42 | 44 | #include "ArgumentsHandler.h" |
43 | 45 | #include "GlogUtils.h" |
44 | 46 | #include "SaUtils.h" |
45 | 47 | #include "PropertyManager.h" |
| 48 | +#include "Workspace.h" |
46 | 49 |
|
47 | 50 | using namespace ra; |
48 | 51 |
|
| 52 | +int SetTestPreferedRootDirectory() |
| 53 | +{ |
| 54 | + std::string exec_dir = ra::process::GetCurrentProcessDirUtf8(); |
| 55 | + std::string home_dir = ra::user::GetDocumentsDirectoryUtf8(); |
| 56 | + std::string temp_dir = ra::filesystem::GetTemporaryDirectoryUtf8(); |
| 57 | + |
| 58 | + // Find a suitable write directory |
| 59 | + std::string writable_dir; |
| 60 | + if (ra::filesystem::HasDirectoryWriteAccessUtf8(exec_dir.c_str())) |
| 61 | + writable_dir = exec_dir; |
| 62 | + else if (ra::filesystem::HasDirectoryWriteAccessUtf8(home_dir.c_str())) |
| 63 | + writable_dir = home_dir; |
| 64 | + else if (ra::filesystem::HasDirectoryWriteAccessUtf8(temp_dir.c_str())) |
| 65 | + writable_dir = temp_dir; |
| 66 | + else |
| 67 | + { |
| 68 | + const char* message = "Failed to find a writable directory to use for tests"; |
| 69 | + |
| 70 | + LOG(ERROR) << message; |
| 71 | + printf(message); |
| 72 | + printf("\n"); |
| 73 | + |
| 74 | + return 1; |
| 75 | + } |
| 76 | + |
| 77 | + std::string root_dir = writable_dir + ra::filesystem::GetPathSeparatorStr() + "test_workspace"; |
| 78 | + |
| 79 | + // Should we clean root directory first ? |
| 80 | + if (ra::filesystem::DirectoryExistsUtf8(root_dir.c_str())) |
| 81 | + { |
| 82 | + // Directory already exists. Clean it. |
| 83 | + bool deleted = ra::filesystem::DeleteDirectoryUtf8(root_dir.c_str()); |
| 84 | + if (!deleted) |
| 85 | + { |
| 86 | + // hope for the best... |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // Create the root directory |
| 91 | + bool created = ra::filesystem::CreateDirectoryUtf8(root_dir.c_str()); |
| 92 | + if (!created) |
| 93 | + return 2; |
| 94 | + |
| 95 | + shellanything::Workspace::SetPreferedRootDirectoryUtf8(root_dir.c_str()); |
| 96 | + printf("Using test directory: '%s'.\n", root_dir.c_str()); |
| 97 | + LOG(INFO) << "Using test directory: '" << root_dir << "'."; |
| 98 | + |
| 99 | + return 0; |
| 100 | +} |
| 101 | + |
49 | 102 | int main(int argc, char** argv) |
50 | 103 | { |
51 | 104 | // Look for custom command line arguments |
@@ -77,6 +130,14 @@ int main(int argc, char** argv) |
77 | 130 | pmgr.SetProperty("application.directory", prop_application_directory); |
78 | 131 | pmgr.SetProperty("log.directory", prop_log_directory); |
79 | 132 |
|
| 133 | + int exit_code = SetTestPreferedRootDirectory(); |
| 134 | + if (exit_code != 0) |
| 135 | + { |
| 136 | + // Shutdown Google's logging library. |
| 137 | + google::ShutdownGoogleLogging(); |
| 138 | + return exit_code; |
| 139 | + } |
| 140 | + |
80 | 141 | LOG(INFO) << "Starting unit tests"; |
81 | 142 | LOG(INFO) << __FUNCTION__ << "() - BEGIN"; |
82 | 143 |
|
|
0 commit comments