llvm_runner_test.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/driver/llvm_runner.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include "common/ostream.h"
  8. #include "common/raw_string_ostream.h"
  9. #include "llvm/ADT/ScopeExit.h"
  10. #include "llvm/Support/FormatVariadic.h"
  11. #include "testing/base/capture_std_streams.h"
  12. #include "testing/base/global_exe_path.h"
  13. namespace Carbon {
  14. namespace {
  15. using ::testing::HasSubstr;
  16. using ::testing::StrEq;
  17. TEST(LLVMRunnerTest, Version) {
  18. RawStringOstream test_os;
  19. const auto install_paths =
  20. InstallPaths::MakeForBazelRunfiles(Testing::GetExePath());
  21. LLVMRunner runner(&install_paths, &test_os);
  22. std::string out;
  23. std::string err;
  24. for (LLVMTool tool : LLVMTool::Tools) {
  25. std::string test_flag = "--version";
  26. std::string expected_out = "LLVM version";
  27. // Handle any special requirements for specific tools.
  28. switch (tool) {
  29. case LLVMTool::Addr2Line:
  30. case LLVMTool::BitcodeStrip:
  31. case LLVMTool::Cgdata:
  32. case LLVMTool::DebuginfodFind:
  33. case LLVMTool::Dwp:
  34. case LLVMTool::Gsymutil:
  35. case LLVMTool::Ifs:
  36. case LLVMTool::InstallNameTool:
  37. case LLVMTool::Lipo:
  38. case LLVMTool::Objcopy:
  39. case LLVMTool::Profdata:
  40. case LLVMTool::Sancov:
  41. case LLVMTool::Strip:
  42. case LLVMTool::Symbolizer:
  43. case LLVMTool::Windres:
  44. // TODO: These tools are not well behaved when invoked as a library,
  45. // typically directly calling `exit` on some or all code paths. We
  46. // should see if they can be fixed upstream and re-enable testing.
  47. continue;
  48. case LLVMTool::Dlltool:
  49. case LLVMTool::Rc:
  50. // No good flags to generically test these tools.
  51. continue;
  52. case LLVMTool::Lib:
  53. test_flag = "/help";
  54. expected_out = "LLVM Lib";
  55. break;
  56. case LLVMTool::Ml:
  57. test_flag = "/help";
  58. expected_out = "LLVM MASM Assembler";
  59. break;
  60. case LLVMTool::Mt:
  61. test_flag = "/help";
  62. expected_out = "Manifest Tool";
  63. break;
  64. default:
  65. break;
  66. }
  67. EXPECT_TRUE(Testing::CallWithCapturedOutput(
  68. out, err, [&] { return runner.Run(tool, {test_flag}); }));
  69. // The arguments to the LLVM tool should be part of the verbose log.
  70. EXPECT_THAT(test_os.TakeStr(), HasSubstr(test_flag));
  71. // Nothing should print to stderr here.
  72. EXPECT_THAT(err, StrEq(""));
  73. EXPECT_THAT(out, HasSubstr(expected_out));
  74. }
  75. }
  76. } // namespace
  77. } // namespace Carbon