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