From 6086ec079a31276c81decdd7b5b5daaafdeb58ca Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Thu, 9 May 2024 18:40:42 +0200 Subject: Sort yscan output, tests --- test-ymake.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++-- yscan.cpp | 27 +++++++++++++++++++-------- 2 files changed, 64 insertions(+), 10 deletions(-) diff --git a/test-ymake.cpp b/test-ymake.cpp index dfed871..1df3ce0 100644 --- a/test-ymake.cpp +++ b/test-ymake.cpp @@ -187,14 +187,32 @@ TEST_F(ymakeTest, build_three_cpp) )"); + EXPECT_TRUE(!fs::exists("hello.o")); + EXPECT_TRUE(!fs::exists("second.o")); + EXPECT_TRUE(!fs::exists("third.o")); + EXPECT_TRUE(!fs::exists("hello")); + std::vector output; int result = run_command("../ymake", output); EXPECT_EQ(result, 0); EXPECT_EQ(output.size(), 4); // compile 3, link 1 + + EXPECT_TRUE(fs::exists("hello.o")); + EXPECT_TRUE(fs::exists("second.o")); + EXPECT_TRUE(fs::exists("third.o")); + EXPECT_TRUE(fs::exists("hello")); + + result = run_command("../ymake clean"); + EXPECT_EQ(result, 0); + + EXPECT_TRUE(!fs::exists("hello.o")); + EXPECT_TRUE(!fs::exists("second.o")); + EXPECT_TRUE(!fs::exists("third.o")); + EXPECT_TRUE(!fs::exists("hello")); } -// TODO: test .c .cc .cpp +// TODO: test file extensions .c .cc .cpp // TODO: multiple builds // TODO: test tests // TODO: test g++/clang++ @@ -234,7 +252,32 @@ TEST_F(yscanTest, one_cpp_file) )"); } -// TODO: test multiple files +TEST_F(yscanTest, one_three_files) +{ + create_file("hello.cpp", R"(int main(int argc, char* argv[]) +{ + return 0; +})"); + create_file("second.cpp", R"()"); + create_file("third.cpp", R"()"); + create_file("fourth.cpp", R"()"); + + std::string output; + int result = run_command("../yscan", output); + EXPECT_EQ(result, 0); + + EXPECT_EQ(output, R"( + + testdir1 + fourth.cpp + hello.cpp + second.cpp + third.cpp + + +)"); +} + // TODO: test multiple builds // TODO: test tests diff --git a/yscan.cpp b/yscan.cpp index 2299370..138b67f 100644 --- a/yscan.cpp +++ b/yscan.cpp @@ -2,9 +2,12 @@ #include "file.h" +#include #include #include +#include #include +#include #include @@ -18,24 +21,32 @@ int yscan(int argc, char* argv[]) try { std::string name {fs::current_path().stem().string()}; - std::string sources; - std::string test_sources; + std::vector sources; + std::vector test_sources; for (const auto& i: fs::directory_iterator(".")) { fs::path p{i.path().filename()}; if (is_compile_unit_source_by_extension(p)) { if (p.string().substr(0, 4) == "test") { - test_sources += " " + p.string() + "\n"; + test_sources.push_back(p.string()); } else { - sources += " " + p.string() + "\n"; + sources.push_back(p.string()); } } } - if (!test_sources.empty()) { - test_sources = fmt::format(R"( + std::sort(sources.begin(), sources.end()); + std::sort(test_sources.begin(), test_sources.end()); + + std::string sources_string{std::accumulate(sources.begin(), sources.end(), std::string{}, + [](const std::string& sum, const std::string& i){return sum + " " + i + "\n";})}; + std::string test_sources_string{std::accumulate(test_sources.begin(), test_sources.end(), std::string{}, + [](const std::string& sum, const std::string& i){return sum + " " + i + "\n";})}; + + if (!test_sources_string.empty()) { + test_sources_string = fmt::format(R"( {} {} -)", "test-"s + name, test_sources); +)", "test-"s + name, test_sources_string); } std::string contents {R"( @@ -43,7 +54,7 @@ int yscan(int argc, char* argv[]) {} {} {})"}; - contents = fmt::format(contents, name, sources, test_sources); + contents = fmt::format(contents, name, sources_string, test_sources_string); std::cout << contents << std::endl; } catch (const std::exception& ex) { std::cerr << "yscan: " << ex.what() << std::endl; -- cgit v1.2.3