1 //===-- CompileCommandsTests.cpp ------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "CompileCommands.h"
10 #include "TestFS.h"
11 
12 #include "llvm/ADT/StringExtras.h"
13 
14 #include "gmock/gmock.h"
15 #include "gtest/gtest.h"
16 
17 namespace clang {
18 namespace clangd {
19 namespace {
20 
21 using ::testing::Contains;
22 using ::testing::ElementsAre;
23 using ::testing::HasSubstr;
24 using ::testing::Not;
25 
26 // Sadly, CommandMangler::detect(), which contains much of the logic, is
27 // a bunch of untested integration glue. We test the string manipulation here
28 // assuming its results are correct.
29 
30 // Make use of all features and assert the exact command we get out.
31 // Other tests just verify presence/absence of certain args.
32 TEST(CommandMangler, Everything) {
33   auto Mangler = CommandMangler::forTests();
34   Mangler.ClangPath = testPath("fake/clang");
35   Mangler.ResourceDir = testPath("fake/resources");
36   Mangler.Sysroot = testPath("fake/sysroot");
37   std::vector<std::string> Cmd = {"clang++", "-Xclang", "-load", "-Xclang",
38                                   "plugin",  "-MF",     "dep",   "foo.cc"};
39   Mangler.adjust(Cmd);
40   EXPECT_THAT(Cmd, ElementsAre(testPath("fake/clang++"), "foo.cc",
41                                "-fsyntax-only",
42                                "-resource-dir=" + testPath("fake/resources"),
43                                "-isysroot", testPath("fake/sysroot")));
44 }
45 
46 TEST(CommandMangler, ResourceDir) {
47   auto Mangler = CommandMangler::forTests();
48   Mangler.ResourceDir = testPath("fake/resources");
49   std::vector<std::string> Cmd = {"clang++", "foo.cc"};
50   Mangler.adjust(Cmd);
51   EXPECT_THAT(Cmd, Contains("-resource-dir=" + testPath("fake/resources")));
52 }
53 
54 TEST(CommandMangler, Sysroot) {
55   auto Mangler = CommandMangler::forTests();
56   Mangler.Sysroot = testPath("fake/sysroot");
57 
58   std::vector<std::string> Cmd = {"clang++", "foo.cc"};
59   Mangler.adjust(Cmd);
60   EXPECT_THAT(llvm::join(Cmd, " "),
61               HasSubstr("-isysroot " + testPath("fake/sysroot")));
62 }
63 
64 TEST(CommandMangler, StripPlugins) {
65   auto Mangler = CommandMangler::forTests();
66   std::vector<std::string> Cmd = {"clang++", "-Xclang", "-load",
67                                   "-Xclang", "plugin",  "foo.cc"};
68   Mangler.adjust(Cmd);
69   for (const char* Stripped : {"-Xclang", "-load", "plugin"})
70     EXPECT_THAT(Cmd, Not(Contains(Stripped)));
71 }
72 
73 TEST(CommandMangler, StripOutput) {
74   auto Mangler = CommandMangler::forTests();
75   std::vector<std::string> Cmd = {"clang++", "-MF", "dependency", "-c",
76                                   "foo.cc"};
77   Mangler.adjust(Cmd);
78   for (const char* Stripped : {"-MF", "dependency"})
79     EXPECT_THAT(Cmd, Not(Contains(Stripped)));
80 }
81 
82 TEST(CommandMangler, StripShowIncludes) {
83   auto Mangler = CommandMangler::forTests();
84   std::vector<std::string> Cmd = {"clang-cl", "/showIncludes", "foo.cc"};
85   Mangler.adjust(Cmd);
86   EXPECT_THAT(Cmd, Not(Contains("/showIncludes")));
87 }
88 
89 TEST(CommandMangler, StripShowIncludesUser) {
90   auto Mangler = CommandMangler::forTests();
91   std::vector<std::string> Cmd = {"clang-cl", "/showIncludes:user", "foo.cc"};
92   Mangler.adjust(Cmd);
93   EXPECT_THAT(Cmd, Not(Contains("/showIncludes:user")));
94 }
95 
96 TEST(CommandMangler, ClangPath) {
97   auto Mangler = CommandMangler::forTests();
98   Mangler.ClangPath = testPath("fake/clang");
99 
100   std::vector<std::string> Cmd = {"clang++", "foo.cc"};
101   Mangler.adjust(Cmd);
102   EXPECT_EQ(testPath("fake/clang++"), Cmd.front());
103 
104   Cmd = {"unknown-binary", "foo.cc"};
105   Mangler.adjust(Cmd);
106   EXPECT_EQ("unknown-binary", Cmd.front());
107 
108   Cmd = {testPath("path/clang++"), "foo.cc"};
109   Mangler.adjust(Cmd);
110   EXPECT_EQ(testPath("path/clang++"), Cmd.front());
111 }
112 
113 } // namespace
114 } // namespace clangd
115 } // namespace clang
116 
117