Skip to content

Commit 411cf1f

Browse files
committed
Implemented support for a root base directory for tests. This root directory is used by the Workspace to "groups" temporary test output files under the same directory.
1 parent a5c4808 commit 411cf1f

File tree

3 files changed

+89
-3
lines changed

3 files changed

+89
-3
lines changed

src/tests/Workspace.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@
3030

3131
namespace shellanything
3232
{
33+
std::string Workspace::sRootDirectory;
3334

3435
Workspace::Workspace()
3536
{
36-
std::string temp_dir = ra::filesystem::GetTemporaryDirectoryUtf8();
37+
std::string root_dir = (!sRootDirectory.empty() ? sRootDirectory : ra::filesystem::GetTemporaryDirectoryUtf8());
3738
std::string test_name = ra::testing::GetTestQualifiedName();
38-
std::string workspace_dir = temp_dir + "\\" + test_name;
39+
std::string workspace_dir = root_dir + "\\" + test_name;
3940

4041
Init(workspace_dir.c_str());
4142
}
@@ -243,4 +244,14 @@ namespace shellanything
243244
return source;
244245
}
245246

247+
void Workspace::SetPreferedRootDirectoryUtf8(const char* path)
248+
{
249+
sRootDirectory = path;
250+
}
251+
252+
std::string Workspace::GetPreferedRootDirectoryUtf8()
253+
{
254+
return sRootDirectory;
255+
}
256+
246257
} //namespace shellanything

src/tests/Workspace.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,24 @@ namespace shellanything
9595
/// <returns>Returns the absolute path from the given relative path.</returns>
9696
std::string GetFullPathUtf8(const char* path) const;
9797

98+
/// <summary>
99+
/// Set the prefered location for a base directory.
100+
/// </summary>
101+
/// <param name="path">An absolute path to define as the prefered location for a base directory.</param>
102+
static void SetPreferedRootDirectoryUtf8(const char* path);
103+
104+
/// <summary>
105+
/// Get the prefered location set for a base directory.
106+
/// </summary>
107+
/// <param name="path">An absolute path to define as the prefered location for a base directory.</param>
108+
/// <returns>Returns the absolute path from the given relative path.</returns>
109+
static std::string GetPreferedRootDirectoryUtf8();
110+
98111
private:
99112
void Init(const char* workspace);
100113

101114
private:
115+
static std::string sRootDirectory;
102116
std::string mWorkspace;
103117
};
104118

src/tests/main.cpp

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,69 @@
3636
#include "rapidassist/testing.h"
3737
#include "rapidassist/environment.h"
3838
#include "rapidassist/cli.h"
39-
#include "rapidassist/filesystem.h"
39+
#include "rapidassist/filesystem_utf8.h"
4040
#include "rapidassist/unicode.h"
41+
#include "rapidassist/process_utf8.h"
42+
#include "rapidassist/user_utf8.h"
4143

4244
#include "ArgumentsHandler.h"
4345
#include "GlogUtils.h"
4446
#include "SaUtils.h"
4547
#include "PropertyManager.h"
48+
#include "Workspace.h"
4649

4750
using namespace ra;
4851

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+
49102
int main(int argc, char** argv)
50103
{
51104
// Look for custom command line arguments
@@ -77,6 +130,14 @@ int main(int argc, char** argv)
77130
pmgr.SetProperty("application.directory", prop_application_directory);
78131
pmgr.SetProperty("log.directory", prop_log_directory);
79132

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+
80141
LOG(INFO) << "Starting unit tests";
81142
LOG(INFO) << __FUNCTION__ << "() - BEGIN";
82143

0 commit comments

Comments
 (0)