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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
/* Copyright (C) 2012-2024 by László Nagy
This file is part of Bear.
Bear is a tool to generate compilation database for clang tooling.
Bear is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Bear is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "semantic/Tool.h"
#include "semantic/ToolWrapper.h"
#include "report/libexec/Resolver.h"
using namespace cs::semantic;
using ::testing::_;
using ::testing::Eq;
using ::testing::Return;
namespace {
class ResolverMock : public el::Resolver {
public:
MOCK_METHOD(
(rust::Result<const char*, int>),
from_current_directory,
(std::string_view const &),
(override)
);
MOCK_METHOD(
(rust::Result<const char*, int>),
from_path,
(std::string_view const &, const char **),
(override)
);
MOCK_METHOD(
(rust::Result<const char*, int>),
from_search_path,
(std::string_view const &, const char *),
(override)
);
};
TEST(ToolWrapper, is_ccache_call) {
EXPECT_FALSE(ToolWrapper::is_ccache_call("cc"));
EXPECT_FALSE(ToolWrapper::is_ccache_call("/usr/bin/cc"));
EXPECT_FALSE(ToolWrapper::is_ccache_call("gcc"));
EXPECT_FALSE(ToolWrapper::is_ccache_call("/usr/bin/gcc"));
EXPECT_FALSE(ToolWrapper::is_ccache_call("c++"));
EXPECT_FALSE(ToolWrapper::is_ccache_call("/usr/bin/c++"));
EXPECT_FALSE(ToolWrapper::is_ccache_call("g++"));
EXPECT_FALSE(ToolWrapper::is_ccache_call("/usr/bin/g++"));
EXPECT_TRUE(ToolWrapper::is_ccache_call("ccache"));
}
TEST(ToolWrapper, is_ccache_query) {
EXPECT_TRUE(ToolWrapper::is_ccache_query({"ccache"}));
EXPECT_TRUE(ToolWrapper::is_ccache_query({"ccache", "-c"}));
EXPECT_TRUE(ToolWrapper::is_ccache_query({"ccache", "--cleanup"}));
EXPECT_FALSE(ToolWrapper::is_ccache_query({"ccache", "cc", "-c"}));
}
TEST(ToolWrapper, is_distcc_call) {
EXPECT_FALSE(ToolWrapper::is_distcc_call("cc"));
EXPECT_FALSE(ToolWrapper::is_distcc_call("/usr/bin/cc"));
EXPECT_FALSE(ToolWrapper::is_distcc_call("gcc"));
EXPECT_FALSE(ToolWrapper::is_distcc_call("/usr/bin/gcc"));
EXPECT_FALSE(ToolWrapper::is_distcc_call("c++"));
EXPECT_FALSE(ToolWrapper::is_distcc_call("/usr/bin/c++"));
EXPECT_FALSE(ToolWrapper::is_distcc_call("g++"));
EXPECT_FALSE(ToolWrapper::is_distcc_call("/usr/bin/g++"));
EXPECT_TRUE(ToolWrapper::is_distcc_call("distcc"));
}
TEST(ToolWrapper, is_distcc_query) {
EXPECT_TRUE(ToolWrapper::is_ccache_query({"distcc"}));
EXPECT_TRUE(ToolWrapper::is_ccache_query({"distcc", "--help"}));
EXPECT_TRUE(ToolWrapper::is_ccache_query({"distcc", "--show-hosts"}));
EXPECT_TRUE(ToolWrapper::is_ccache_query({"distcc", "-j"}));
EXPECT_FALSE(ToolWrapper::is_ccache_query({"distcc", "cc", "--help"}));
EXPECT_FALSE(ToolWrapper::is_ccache_query({"distcc", "cc", "-c"}));
}
TEST(ToolWrapper, remove_wrapper) {
const Execution input = {
"/usr/bin/ccache",
{"ccache", "cc", "-c", "-o", "source.o", "source.c"},
"/home/user/project",
{{"PATH", "/usr/bin:/usr/sbin"}},
};
const Execution expected = {
"/usr/bin/cc",
{"cc", "-c", "-o", "source.o", "source.c"},
"/home/user/project",
{{"PATH", "/usr/bin:/usr/sbin"}},
};
ResolverMock resolver;
EXPECT_CALL(resolver, from_search_path(Eq(std::string_view("cc")), _))
.Times(1)
.WillOnce(Return(rust::Result<const char*, int>(rust::Ok("/usr/bin/cc"))));
auto result = ToolWrapper::remove_wrapper(resolver, input);
EXPECT_EQ(expected, result);
}
TEST(ToolWrapper, remove_wrapper_fails_to_resolve) {
const Execution input = {
"/usr/bin/ccache",
{"ccache", "cc", "-c", "-o", "source.o", "source.c"},
"/home/user/project",
{{"PATH", "/usr/bin:/usr/sbin"}},
};
const Execution expected = {
"cc",
{"cc", "-c", "-o", "source.o", "source.c"},
"/home/user/project",
{{"PATH", "/usr/bin:/usr/sbin"}},
};
ResolverMock resolver;
EXPECT_CALL(resolver, from_search_path(Eq(std::string_view("cc")), _))
.Times(1)
.WillOnce(Return(rust::Result<const char*, int>(rust::Err(12))));
auto result = ToolWrapper::remove_wrapper(resolver, input);
EXPECT_EQ(expected, result);
}
}
|