#include #include #include #include #include #include namespace fs = std::filesystem; namespace bp = boost::process; namespace { const fs::path testpath{"testdir1"}; } class BuildTest: public ::testing::Test { private: fs::path original_path; protected: BuildTest() { } ~BuildTest() override { } void SetUp() override { original_path = fs::current_path(); fs::remove_all(testpath); fs::create_directory(testpath); fs::current_path(testpath); } void TearDown() override { fs::current_path(original_path); fs::remove_all(testpath); } }; void create_file(const fs::path& path, const std::string& contents) { Reichwein::File::setFile(path, contents); } int run_command(const std::string& command) { return bp::system(command, bp::std_out > bp::null, bp::std_err > bp::null); } int run_command(const std::string& command, std::vector& output) { bp::ipstream is; bp::ipstream es; int result = bp::system(command, bp::std_out > is, bp::std_err > es); std::string output_string(std::istreambuf_iterator(is), {}); output_string += std::string(std::istreambuf_iterator(es), {}); output = Reichwein::Stringhelper::split(output_string, "\r\n"); return result; } TEST_F(BuildTest, build_one_cpp) { create_file("hello.cpp", R"(int main(int argc, char* argv[]) { return 0; })"); create_file("YMakefile", R"( hello hello.cpp )"); std::vector output; int result = run_command("../ymake", output); EXPECT_EQ(result, 0); EXPECT_EQ(output.size(), 2); // compile, link } TEST_F(BuildTest, build_one_cpp_compile_error) { create_file("hello.cpp", R"(int main(int argc, char* argv[]) { retrn 0; })"); create_file("YMakefile", R"( hello hello.cpp )"); int result = run_command("../ymake"); EXPECT_NE(result, 0); } TEST_F(BuildTest, clean_one_cpp) { create_file("hello.cpp", R"(int main(int argc, char* argv[]) { return 0; })"); create_file("YMakefile", R"( hello hello.cpp )"); std::vector output; int result = run_command("../ymake clean", output); EXPECT_EQ(output.size(), 0); ASSERT_EQ(result, 0); result = run_command("../ymake", output); EXPECT_EQ(output.size(), 2); // compile, link ASSERT_EQ(result, 0); result = run_command("../ymake clean", output); EXPECT_EQ(output.size(), 3); // rm .o, .d, executable ASSERT_EQ(result, 0); } TEST_F(BuildTest, YMakefile_missing) { int result = run_command("../ymake"); EXPECT_NE(result, 0); result = run_command("../ymake clean"); EXPECT_NE(result, 0); }