From d137a52a6807f4c74e8e7e32e62ee4acbfe92197 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sun, 5 May 2024 11:20:29 +0200 Subject: Tests --- test-ymake.cpp | 151 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 test-ymake.cpp (limited to 'test-ymake.cpp') diff --git a/test-ymake.cpp b/test-ymake.cpp new file mode 100644 index 0000000..a4d9aa4 --- /dev/null +++ b/test-ymake.cpp @@ -0,0 +1,151 @@ +#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); +} + -- cgit v1.2.3