summaryrefslogtreecommitdiffhomepage
path: root/test-ymake.cpp
blob: a4d9aa42db9d86c3c6d3ef150f9add4a475f852e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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);
}