summaryrefslogtreecommitdiffhomepage
path: root/test-ymake.cpp
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2024-05-05 11:20:29 +0200
committerRoland Reichwein <mail@reichwein.it>2024-05-05 11:20:29 +0200
commitd137a52a6807f4c74e8e7e32e62ee4acbfe92197 (patch)
treed0a1f48666a752e425b392182b27c54859e6eb95 /test-ymake.cpp
parent39c1729d8dcfffb0f72985411b8b489835d4fd5f (diff)
Tests
Diffstat (limited to 'test-ymake.cpp')
-rw-r--r--test-ymake.cpp151
1 files changed, 151 insertions, 0 deletions
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 <filesystem>
+#include <string>
+
+#include <boost/process.hpp>
+
+#include <gtest/gtest.h>
+
+#include <libreichwein/file.h>
+#include <libreichwein/stringhelper.h>
+
+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<std::string>& 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<char>(is), {});
+ output_string += std::string(std::istreambuf_iterator<char>(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"(
+<ymake>
+ <build>
+ <name>hello</name>
+ <source>hello.cpp</source>
+ </build>
+</ymake>
+)");
+
+ std::vector<std::string> 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"(
+<ymake>
+ <build>
+ <name>hello</name>
+ <source>hello.cpp</source>
+ </build>
+</ymake>
+)");
+
+ 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"(
+<ymake>
+ <build>
+ <name>hello</name>
+ <source>hello.cpp</source>
+ </build>
+</ymake>
+)");
+
+ std::vector<std::string> 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);
+}
+