llvm_runner_test.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 <string>
  8. #include "common/ostream.h"
  9. #include "common/raw_string_ostream.h"
  10. #include "llvm/ADT/ScopeExit.h"
  11. #include "llvm/Support/FormatVariadic.h"
  12. #include "testing/base/capture_std_streams.h"
  13. #include "testing/base/global_exe_path.h"
  14. namespace Carbon {
  15. namespace {
  16. using ::testing::HasSubstr;
  17. using ::testing::StrEq;
  18. TEST(LLVMRunnerTest, Version) {
  19. RawStringOstream test_os;
  20. const auto install_paths =
  21. InstallPaths::MakeForBazelRunfiles(Testing::GetExePath());
  22. LLVMRunner runner(&install_paths, &test_os);
  23. std::string out;
  24. std::string err;
  25. for (LLVMTool tool : LLVMTool::Tools) {
  26. std::string test_flag = "--version";
  27. std::string expected_out = "LLVM version";
  28. // Handle any special requirements for specific tools.
  29. switch (tool) {
  30. case LLVMTool::Addr2Line:
  31. case LLVMTool::BitcodeStrip:
  32. case LLVMTool::Cgdata:
  33. case LLVMTool::DebuginfodFind:
  34. case LLVMTool::Dwp:
  35. case LLVMTool::Gsymutil:
  36. case LLVMTool::Ifs:
  37. case LLVMTool::InstallNameTool:
  38. case LLVMTool::Lipo:
  39. case LLVMTool::Objcopy:
  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