Skip to content

Commit a63f966

Browse files
Adding Builds Manager and unit test for it
1 parent a1dcbb3 commit a63f966

File tree

5 files changed

+1100
-2
lines changed

5 files changed

+1100
-2
lines changed

CMakeLists.txt

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,13 @@ add_executable(runcpp2
9393
"${CMAKE_CURRENT_LIST_DIR}/Src/runcpp2/PlatformUtil.cpp"
9494
"${CMAKE_CURRENT_LIST_DIR}/Src/runcpp2/runcpp2.cpp"
9595
"${CMAKE_CURRENT_LIST_DIR}/Src/runcpp2/StringUtil.cpp"
96+
"${CMAKE_CURRENT_LIST_DIR}/Src/runcpp2/BuildsManager.cpp"
97+
9698
)
9799

98-
target_include_directories(runcpp2 PRIVATE "${CMAKE_CURRENT_LIST_DIR}/Include")
100+
target_include_directories(runcpp2 PRIVATE "${CMAKE_CURRENT_LIST_DIR}/Include")
99101

100-
target_link_libraries(runcpp2 PRIVATE ssLogger ghc_filesystem System2 ryml::ryml dylib)
102+
target_link_libraries(runcpp2 PRIVATE ssLogger ghc_filesystem System2 ryml::ryml dylib CppOverride)
101103

102104
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
103105
set(STANDARD_COMPILE_FLAGS "/utf-8;/W1")
@@ -116,3 +118,24 @@ else()
116118
endif()
117119

118120
target_compile_options(runcpp2 PRIVATE ${STANDARD_COMPILE_FLAGS})
121+
122+
123+
124+
# TODO: Maybe move this to the UnitTests sub folder
125+
# Unit Tests
126+
if(RUNCPP2_BUILD_TESTS)
127+
add_executable(BuildsManagerTest "${CMAKE_CURRENT_LIST_DIR}/Src/runcpp2/BuildsManager.cpp"
128+
"${CMAKE_CURRENT_LIST_DIR}/Src/UnitTests/BuildsManagerTest.cpp")
129+
# "${CMAKE_CURRENT_LIST_DIR}/Src/UnitTests/BuildsManager/MockComponents.cpp")
130+
131+
target_include_directories(BuildsManagerTest PRIVATE "${CMAKE_CURRENT_LIST_DIR}/Include")
132+
target_compile_options(BuildsManagerTest PRIVATE "${STANDARD_COMPILE_FLAGS}")
133+
target_link_libraries(BuildsManagerTest PRIVATE ghc_filesystem CppOverride ssTest ssLogger)
134+
target_compile_definitions(BuildsManagerTest PRIVATE INTERNAL_RUNCPP2_UNIT_TESTS=1)
135+
# set_target_properties(BuildsManagerTest PROPERTIES ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests"
136+
# LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests"
137+
# RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests")
138+
endif()
139+
140+
141+
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#ifndef RUNCPP2_UNIT_TESTS_BUILDS_MANAGER_MOCK_COMPONENTS_HPP
2+
#define RUNCPP2_UNIT_TESTS_BUILDS_MANAGER_MOCK_COMPONENTS_HPP
3+
4+
#include "ghc/filesystem.hpp"
5+
6+
#include "CppOverride.hpp"
7+
8+
#include <sstream>
9+
#include <type_traits>
10+
11+
extern CO_DECLARE_INSTANCE(OverrideInstance);
12+
13+
namespace ghc
14+
{
15+
namespace filesystem
16+
{
17+
CO_OVERRIDE_METHOD( OverrideInstance,
18+
bool,
19+
Mock_exists,
20+
(const path&, std::error_code&),
21+
/* no prepend */,
22+
noexcept)
23+
24+
CO_OVERRIDE_METHOD( OverrideInstance,
25+
bool,
26+
Mock_create_directories,
27+
(const path&, std::error_code&),
28+
/* no prepend */,
29+
noexcept)
30+
31+
CO_OVERRIDE_METHOD( OverrideInstance,
32+
bool,
33+
Mock_remove_all,
34+
(const path&, std::error_code&),
35+
/* no prepend */,
36+
noexcept)
37+
}
38+
}
39+
40+
namespace std
41+
{
42+
class Mock_ifstream
43+
{
44+
public:
45+
CO_OVERRIDE_MEMBER_METHOD_CTOR(OverrideInstance, Mock_ifstream, const ghc::filesystem::path&)
46+
CO_OVERRIDE_MEMBER_METHOD(OverrideInstance, std::string, rdbuf, ())
47+
CO_OVERRIDE_MEMBER_METHOD(OverrideInstance, bool, is_open, ())
48+
CO_OVERRIDE_MEMBER_METHOD(OverrideInstance, void, close, ())
49+
};
50+
51+
class Mock_ofstream
52+
{
53+
public:
54+
std::stringstream StringStream;
55+
56+
CO_OVERRIDE_MEMBER_METHOD_CTOR(OverrideInstance, Mock_ofstream, const ghc::filesystem::path&)
57+
58+
CO_OVERRIDE_MEMBER_METHOD(OverrideInstance, bool, is_open, ())
59+
CO_OVERRIDE_MEMBER_METHOD(OverrideInstance, void, close, ())
60+
61+
template<typename T>
62+
friend Mock_ofstream& operator<<(Mock_ofstream&, T const&);
63+
};
64+
65+
template<typename T>
66+
class Mock_hash
67+
{
68+
static_assert(std::is_same<T, std::string>::value, "We are only mocking std string");
69+
public:
70+
CO_OVERRIDE_MEMBER_METHOD(OverrideInstance, std::size_t, operator(), (T))
71+
};
72+
73+
template<typename T>
74+
inline Mock_ofstream& operator<<(Mock_ofstream& mockStream, T const& value)
75+
{
76+
//CO_OVERRIDE_IMPL(FreeFunctionOverrideInstance, Mock_ofstream&, (mockStream, value));
77+
mockStream.StringStream << value;
78+
return mockStream;
79+
}
80+
81+
inline Mock_ofstream& operator<<(Mock_ofstream& mockStream, std::ostream& (*pf)(std::ostream&))
82+
{
83+
//NOTE: This is called when std::endl is passed to the << operator.
84+
// Unfortunately function pointer is not tested/implemented yet for CppOverride
85+
// void* will do for now
86+
//void* dummyPointer = nullptr;
87+
//CO_OVERRIDE_IMPL(FreeFunctionOverrideInstance, Mock_ofstream&, (mockStream, dummyPointer));
88+
89+
mockStream.StringStream << pf;
90+
return mockStream;
91+
}
92+
}
93+
94+
#define exists Mock_exists
95+
#define create_directories Mock_create_directories
96+
#define remove_all Mock_remove_all
97+
#define ifstream Mock_ifstream
98+
#define ofstream Mock_ofstream
99+
#define hash Mock_hash
100+
101+
#if INTERNAL_RUNCPP2_UNDEF_MOCKS
102+
#undef exists
103+
#undef create_directories
104+
#undef remove_all
105+
#undef ifstream
106+
#undef ofstream
107+
#undef hash
108+
#endif
109+
110+
#endif

Include/runcpp2/BuildsManager.hpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#ifndef RUNCPP2_BUILDS_MANAGER_HPP
2+
#define RUNCPP2_BUILDS_MANAGER_HPP
3+
4+
#include "ghc/filesystem.hpp"
5+
6+
#include <unordered_map>
7+
8+
#if INTERNAL_RUNCPP2_UNIT_TESTS
9+
class BuildsManagerAccessor;
10+
#endif
11+
12+
namespace runcpp2
13+
{
14+
class BuildsManager
15+
{
16+
#if INTERNAL_RUNCPP2_UNIT_TESTS
17+
friend class ::BuildsManagerAccessor;
18+
#endif
19+
20+
private:
21+
const ghc::filesystem::path ConfigDirectory;
22+
23+
std::unordered_map<std::string, std::string> Mappings;
24+
std::unordered_map<std::string, std::string> ReverseMappings;
25+
26+
27+
const ghc::filesystem::path BuildDirectory;
28+
const ghc::filesystem::path MappingsFile;
29+
bool Initialized;
30+
31+
bool ParseMappings(const std::string& mappingsContent);
32+
33+
public:
34+
BuildsManager(const ghc::filesystem::path& configDirectory);
35+
BuildsManager(const BuildsManager& other);
36+
~BuildsManager();
37+
38+
bool Initialize();
39+
bool CreateBuildMapping(const ghc::filesystem::path& scriptPath);
40+
bool RemoveBuildMapping(const ghc::filesystem::path& scriptPath);
41+
bool HasBuildMapping(const ghc::filesystem::path& scriptPath);
42+
bool GetBuildMapping( const ghc::filesystem::path& scriptPath,
43+
ghc::filesystem::path& outPath);
44+
bool RemoveAllBuildsMappings();
45+
bool SaveBuildsMappings();
46+
};
47+
}
48+
49+
#endif

0 commit comments

Comments
 (0)