Skip to content

Commit 4f5b154

Browse files
authored
[Feature] Verify toolchain (#169)
1 parent 6d17fe3 commit 4f5b154

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+767
-51
lines changed

buildcc/lib/args/test/test_register.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include "expect_command.h"
44

5+
#include "mock_command_copier.h"
6+
57
// NOTE, Make sure all these includes are AFTER the system and header includes
68
#include "CppUTest/CommandLineTestRunner.h"
79
#include "CppUTest/MemoryLeakDetectorNewMacros.h"
@@ -531,7 +533,9 @@ TEST(RegisterTestGroup, Register_Test) {
531533
target);
532534
reg.Test(stateSuccess, "{executable}", target);
533535

534-
buildcc::m::CommandExpect_Execute(1, true);
536+
std::vector<std::string> stdout_data;
537+
std::vector<std::string> stderr_data;
538+
buildcc::m::CommandExpect_Execute(1, true, &stdout_data, &stderr_data);
535539
reg.RunTest();
536540
}
537541

@@ -601,7 +605,8 @@ TEST(RegisterTestGroup, Register_TestWithOutput) {
601605
{}, {},
602606
buildcc::TestOutput(buildcc::TestOutput::Type::TestPrintOnStderr)));
603607

604-
buildcc::m::CommandExpect_Execute(1, true);
608+
std::vector<std::string> stderr_data;
609+
buildcc::m::CommandExpect_Execute(1, true, nullptr, &stderr_data);
605610
reg.RunTest();
606611
}
607612

@@ -618,7 +623,8 @@ TEST(RegisterTestGroup, Register_TestWithOutput) {
618623
{}, {},
619624
buildcc::TestOutput(buildcc::TestOutput::Type::TestPrintOnStdout)));
620625

621-
buildcc::m::CommandExpect_Execute(1, true);
626+
std::vector<std::string> stdout_data;
627+
buildcc::m::CommandExpect_Execute(1, true, &stdout_data, nullptr);
622628
reg.RunTest();
623629
}
624630

@@ -635,7 +641,9 @@ TEST(RegisterTestGroup, Register_TestWithOutput) {
635641
buildcc::TestOutput(
636642
buildcc::TestOutput::Type::TestPrintOnStderrAndStdout)));
637643

638-
buildcc::m::CommandExpect_Execute(1, true);
644+
std::vector<std::string> stdout_data;
645+
std::vector<std::string> stderr_data;
646+
buildcc::m::CommandExpect_Execute(1, true, &stdout_data, &stderr_data);
639647
reg.RunTest();
640648
}
641649

@@ -676,5 +684,7 @@ TEST(RegisterTestGroup, Register_TestWithOutput) {
676684

677685
int main(int ac, char **av) {
678686
MemoryLeakWarningPlugin::turnOffNewDeleteOverloads();
687+
buildcc::m::VectorStringCopier copier;
688+
mock().installCopier(TEST_VECTOR_STRING_TYPE, copier);
679689
return CommandLineTestRunner::RunAllTests(ac, av);
680690
}

buildcc/lib/command/CMakeLists.txt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,12 @@ if(${BUILDCC_BUILD_AS_INTERFACE})
5757
)
5858
target_compile_options(command PRIVATE ${BUILD_COMPILE_FLAGS})
5959
target_link_options(command PRIVATE ${BUILD_LINK_FLAGS})
60+
endif()
6061

61-
62-
# Command install
63-
if (${BUILDCC_INSTALL})
62+
if (${BUILDCC_INSTALL})
63+
if(${BUILDCC_BUILD_AS_INTERFACE})
6464
install(TARGETS command DESTINATION lib EXPORT commandConfig)
6565
install(EXPORT commandConfig DESTINATION "${BUILDCC_INSTALL_LIB_PREFIX}/command")
6666
endif()
67-
endif()
68-
69-
if (${BUILDCC_INSTALL})
7067
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/ DESTINATION "${BUILDCC_INSTALL_HEADER_PREFIX}")
7168
endif()

buildcc/lib/command/mock/execute.cpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
#include "command/command.h"
22

3+
#include "expect_command.h"
4+
35
#include "CppUTestExt/MockSupport.h"
46

57
namespace buildcc {
68

79
static constexpr const char *const EXECUTE_FUNCTION = "execute";
10+
static constexpr const char *const STDOUT_DATA_STRING = "stdout_data";
11+
static constexpr const char *const STDERR_DATA_STRING = "stderr_data";
812

913
// command
1014
bool Command::Execute(const std::string &command,
@@ -13,15 +17,33 @@ bool Command::Execute(const std::string &command,
1317
std::vector<std::string> *stderr_data) {
1418
(void)command;
1519
(void)working_directory;
16-
(void)stdout_data;
17-
(void)stderr_data;
18-
return mock().actualCall(EXECUTE_FUNCTION).returnBoolValue();
20+
auto &actualcall = mock().actualCall(EXECUTE_FUNCTION);
21+
if (stdout_data != nullptr) {
22+
actualcall.withOutputParameterOfType(
23+
TEST_VECTOR_STRING_TYPE, STDOUT_DATA_STRING, (void *)stdout_data);
24+
}
25+
if (stderr_data != nullptr) {
26+
actualcall.withOutputParameterOfType(
27+
TEST_VECTOR_STRING_TYPE, STDERR_DATA_STRING, (void *)stderr_data);
28+
}
29+
return actualcall.returnBoolValue();
1930
}
2031

2132
namespace m {
2233

23-
void CommandExpect_Execute(unsigned int calls, bool expectation) {
24-
mock().expectNCalls(calls, EXECUTE_FUNCTION).andReturnValue(expectation);
34+
void CommandExpect_Execute(unsigned int calls, bool expectation,
35+
std::vector<std::string> *stdout_data,
36+
std::vector<std::string> *stderr_data) {
37+
auto &expectedcall = mock().expectNCalls(calls, EXECUTE_FUNCTION);
38+
if (stdout_data != nullptr) {
39+
expectedcall.withOutputParameterOfTypeReturning(
40+
TEST_VECTOR_STRING_TYPE, STDOUT_DATA_STRING, (void *)stdout_data);
41+
}
42+
if (stderr_data != nullptr) {
43+
expectedcall.withOutputParameterOfTypeReturning(
44+
TEST_VECTOR_STRING_TYPE, STDERR_DATA_STRING, (void *)stderr_data);
45+
}
46+
expectedcall.andReturnValue(expectation);
2547
}
2648

2749
} // namespace m

buildcc/lib/command/mock/expect_command.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
#ifndef COMMAND_MOCK_EXPECT_COMMAND_H_
22
#define COMMAND_MOCK_EXPECT_COMMAND_H_
33

4+
#include <string>
5+
#include <vector>
6+
7+
constexpr const char *const TEST_VECTOR_STRING_TYPE = "vector_string";
8+
49
namespace buildcc::m {
510

6-
void CommandExpect_Execute(unsigned int calls, bool expectation);
11+
void CommandExpect_Execute(unsigned int calls, bool expectation,
12+
std::vector<std::string> *stdout_data = nullptr,
13+
std::vector<std::string> *stderr_data = nullptr);
714

815
} // namespace buildcc::m
916

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef COMMAND_MOCK_MOCK_COMMAND_COPIER_H_
2+
#define COMMAND_MOCK_MOCK_COMMAND_COPIER_H_
3+
4+
#include <string>
5+
#include <vector>
6+
7+
#include "CppUTestExt/MockSupport.h"
8+
9+
namespace buildcc::m {
10+
11+
class VectorStringCopier : public MockNamedValueCopier {
12+
public:
13+
void copy(void *out, const void *in) override {
14+
const std::vector<std::string> *vin = (const std::vector<std::string> *)in;
15+
std::vector<std::string> *vout = (std::vector<std::string> *)out;
16+
17+
if (vout == nullptr || vin == nullptr) {
18+
return;
19+
}
20+
21+
vout->clear();
22+
vout->insert(vout->end(), vin->begin(), vin->end());
23+
}
24+
};
25+
26+
} // namespace buildcc::m
27+
28+
#endif

buildcc/lib/env/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ set(ENV_SRCS
4040
include/env/assert_throw.h
4141
include/env/env.h
4242
include/env/logging.h
43+
include/env/util.h
44+
4345
include/env/host_os.h
4446
include/env/host_compiler.h
45-
include/env/util.h
47+
include/env/host_os_util.h
4648

4749
src/task_state.cpp
4850
include/env/task_state.h
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2021 Niket Naidu. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef ENV_HOST_OS_UTIL_H_
18+
#define ENV_HOST_OS_UTIL_H_
19+
20+
#include <stdlib.h>
21+
22+
#include "host_os.h"
23+
#include "logging.h"
24+
25+
// https://askubuntu.com/questions/156392/what-is-the-equivalent-of-an-exe-file
26+
namespace buildcc::env {
27+
28+
// Common
29+
constexpr const char *const kRaiseIssueStr =
30+
"Unknown operating system, returning nullptr. Raise an "
31+
"issue at http://github.com/coder137/build_in_cpp";
32+
33+
// OS Path delimiters
34+
constexpr const char *const kWinEnvDelim = ";";
35+
constexpr const char *const kUnixEnvDelim = ":";
36+
37+
/**
38+
* @brief Get the OS environment variable delimiter
39+
* ; for windows
40+
* : for linux, unix and mac
41+
*
42+
* @return constexpr char const* for supported operating systems
43+
* @return nullptr otherwise with a critical message to raise an issue
44+
*/
45+
inline constexpr char const *get_os_envvar_delim() {
46+
if constexpr (is_win()) {
47+
return kWinEnvDelim;
48+
} else if constexpr (is_linux() || is_unix() || is_mac()) {
49+
return kUnixEnvDelim;
50+
}
51+
log_critical(__FUNCTION__, kRaiseIssueStr);
52+
return nullptr;
53+
}
54+
55+
// OS executable extensions
56+
constexpr const char *const kWinExecutableExt = ".exe";
57+
constexpr const char *const kUnixExecutableExt = "";
58+
59+
/**
60+
* @brief Get the OS executable extension
61+
* ".exe" for windows
62+
* "" for linux, unix and mac
63+
*
64+
* @return constexpr const char* for supported operating systems
65+
* @return nullptr otherwise with a critical message to raise an issue
66+
*/
67+
inline constexpr const char *get_os_executable_extension() {
68+
if constexpr (is_win()) {
69+
return kWinExecutableExt;
70+
} else if constexpr (is_linux() || is_unix() || is_mac()) {
71+
return kUnixExecutableExt;
72+
}
73+
74+
log_critical(__FUNCTION__, kRaiseIssueStr);
75+
return nullptr;
76+
}
77+
78+
} // namespace buildcc::env
79+
80+
#endif

buildcc/lib/env/include/env/util.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <filesystem>
3737
#include <fstream>
3838
#include <sstream>
39+
#include <vector>
3940

4041
namespace fs = std::filesystem;
4142

@@ -106,6 +107,19 @@ inline bool load_file(const char *name, bool binary, std::string *buf) {
106107
return !ifs.bad();
107108
}
108109

110+
// TODO, Shift this to a common apis folder or something
111+
// https://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c
112+
inline std::vector<std::string> split(const std::string &s, char delim) {
113+
std::vector<std::string> result;
114+
std::stringstream ss(s);
115+
std::string item;
116+
while (getline(ss, item, delim)) {
117+
result.push_back(item);
118+
}
119+
120+
return result;
121+
}
122+
109123
} // namespace buildcc::env
110124

111125
#endif

buildcc/lib/env/test/test_env_util.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,28 @@ TEST(EnvUtilTestGroup, Util_LoadFile_CannotOpen) {
159159
CHECK_FALSE(load);
160160
}
161161

162+
TEST(EnvUtilTestGroup, Util_Split) {
163+
{
164+
std::vector<std::string> paths = buildcc::env::split("", ':');
165+
CHECK_EQUAL(paths.size(), 0);
166+
}
167+
168+
{
169+
std::vector<std::string> paths = buildcc::env::split("path1", ':');
170+
CHECK_EQUAL(paths.size(), 1);
171+
STRCMP_EQUAL(paths[0].c_str(), "path1");
172+
}
173+
174+
{
175+
std::vector<std::string> paths =
176+
buildcc::env::split("path1:path2:path3", ':');
177+
CHECK_EQUAL(paths.size(), 3);
178+
STRCMP_EQUAL(paths[0].c_str(), "path1");
179+
STRCMP_EQUAL(paths[1].c_str(), "path2");
180+
STRCMP_EQUAL(paths[2].c_str(), "path3");
181+
}
182+
}
183+
162184
int main(int ac, char **av) {
163185
return CommandLineTestRunner::RunAllTests(ac, av);
164186
}

buildcc/lib/target/cmake/mock_target.cmake

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ target_link_libraries(mock_target PUBLIC
2323
flatbuffers_header_only
2424
Taskflow
2525

26-
mock_env
27-
mock_command
28-
toolchain
26+
mock_toolchain
2927

3028
CppUTest
3129
CppUTestExt

0 commit comments

Comments
 (0)