llvm_runner_test.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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::Profdata:
  41. case LLVMTool::Sancov:
  42. case LLVMTool::Strip:
  43. case LLVMTool::Symbolizer:
  44. case LLVMTool::Windres:
  45. // TODO: These tools are not well behaved when invoked as a library,
  46. // typically directly calling `exit` on some or all code paths. We
  47. // should see if they can be fixed upstream and re-enable testing.
  48. continue;
  49. case LLVMTool::Dlltool:
  50. case LLVMTool::Rc:
  51. // No good flags to generically test these tools.
  52. continue;
  53. case LLVMTool::Lib:
  54. test_flag = "/help";
  55. expected_out = "LLVM Lib";
  56. break;
  57. case LLVMTool::Ml:
  58. test_flag = "/help";
  59. expected_out = "LLVM MASM Assembler";
  60. break;
  61. case LLVMTool::Mt:
  62. test_flag = "/help";
  63. expected_out = "Manifest Tool";
  64. break;
  65. default:
  66. break;
  67. }
  68. EXPECT_TRUE(Testing::CallWithCapturedOutput(
  69. out, err, [&] { return runner.Run(tool, {test_flag}); }));
  70. // The arguments to the LLVM tool should be part of the verbose log.
  71. EXPECT_THAT(test_os.TakeStr(), HasSubstr(test_flag));
  72. // Nothing should print to stderr here.
  73. EXPECT_THAT(err, StrEq(""));
  74. EXPECT_THAT(out, HasSubstr(expected_out));
  75. }
  76. }
  77. } // namespace
  78. } // namespace Carbon