|
| 1 | +#include <gtest/gtest.h> |
| 2 | +#include <string> |
| 3 | +#include <vector> |
| 4 | +#include <format> |
| 5 | +#include <ranges> |
| 6 | +#include <filesystem> |
| 7 | +#include <fstream> |
| 8 | +#include <boost/process.hpp> |
| 9 | +#include "tools/cpp/runfiles/runfiles.h" |
| 10 | + |
| 11 | +using bazel::tools::cpp::runfiles::Runfiles; |
| 12 | +using namespace std::string_literals; |
| 13 | +namespace fs = std::filesystem; |
| 14 | +namespace bp = boost::process; |
| 15 | + |
| 16 | +TEST(Codegen, Stdout) { |
| 17 | + auto runfiles_err = std::string{}; |
| 18 | + auto runfiles = Runfiles::CreateForTest(&runfiles_err); |
| 19 | + auto test_ecsact_cli = std::getenv("TEST_ECSACT_CLI"); |
| 20 | + auto test_codegen_plugin_path = std::getenv("TEST_CODEGEN_PLUGIN_PATH"); |
| 21 | + auto test_ecsact_file_path = std::getenv("TEST_ECSACT_FILE_PATH"); |
| 22 | + |
| 23 | + ASSERT_NE(test_ecsact_cli, nullptr); |
| 24 | + ASSERT_NE(test_codegen_plugin_path, nullptr); |
| 25 | + ASSERT_NE(test_ecsact_file_path, nullptr); |
| 26 | + ASSERT_NE(runfiles, nullptr) << runfiles_err; |
| 27 | + ASSERT_TRUE(fs::exists(test_codegen_plugin_path)); |
| 28 | + ASSERT_TRUE(fs::exists(test_ecsact_file_path)); |
| 29 | + |
| 30 | + // this file should NOT be generated |
| 31 | + auto bad_generated_file_path = fs::path{"test.txt"s}; |
| 32 | + if(fs::exists(bad_generated_file_path)) { |
| 33 | + fs::remove(bad_generated_file_path); |
| 34 | + } |
| 35 | + |
| 36 | + auto proc_stdout = bp::ipstream{}; |
| 37 | + auto proc = bp::child{ |
| 38 | + bp::exe(test_ecsact_cli), |
| 39 | + bp::args({ |
| 40 | + "codegen"s, |
| 41 | + std::string{test_ecsact_file_path}, |
| 42 | + std::format("--plugin={}", test_codegen_plugin_path), |
| 43 | + "--print-output-files"s, |
| 44 | + }), |
| 45 | + bp::std_out > proc_stdout |
| 46 | + }; |
| 47 | + |
| 48 | + proc.wait(); |
| 49 | + |
| 50 | + auto exit_code = proc.exit_code(); |
| 51 | + ASSERT_EQ(exit_code, 0); |
| 52 | + |
| 53 | + auto output_files = std::vector<std::string>{}; |
| 54 | + auto line = std::string{}; |
| 55 | + while(std::getline(proc_stdout, line)) { |
| 56 | + if(line.ends_with("\r")) { |
| 57 | + line.pop_back(); |
| 58 | + } |
| 59 | + output_files.emplace_back(line); |
| 60 | + } |
| 61 | + |
| 62 | + EXPECT_EQ(output_files.size(), 2); |
| 63 | + |
| 64 | + EXPECT_FALSE( |
| 65 | + std::ranges::find(output_files, "test.txt") == output_files.end() |
| 66 | + ); |
| 67 | + EXPECT_FALSE( |
| 68 | + std::ranges::find(output_files, "test.zomsky") == output_files.end() |
| 69 | + ); |
| 70 | + EXPECT_FALSE(fs::exists(bad_generated_file_path)) |
| 71 | + << bad_generated_file_path.string() |
| 72 | + << " was generated even with --print-output-files option!"; |
| 73 | +} |
0 commit comments