build: lint header files with clang-tidy (#7527)

* build: clang-tidy headers when building libtransmission

* chore: revert `= default` workaround

It was introduced in 6909ec0bad to fix build issues with macOS 10.14. We
no longer support that version.

* fix: clang-tidy warnings for libtransmission

* build: clang-tidy headers when building tests

* fix: clang-tidy warnings for tests

* build: clang-tidy headers when building qt

* code review: don't manually edit mime-types.h

* code review: unify variable naming for static private members
This commit is contained in:
Yat Ho
2025-05-08 05:10:16 +08:00
committed by GitHub
parent f0c8fd689a
commit 08ec7fb7c7
78 changed files with 368 additions and 237 deletions

View File

@@ -1,4 +1,6 @@
---
HeaderFilterRegex: .*/tests/libtransmission/.*
# Many of these checks are disabled only because the code hasn't been
# cleaned up yet. Pull requests welcomed.
Checks: >
@@ -24,6 +26,7 @@ Checks: >
-hicpp-signed-bitwise,
misc-*,
-misc-include-cleaner,
-misc-no-recursion,
-misc-non-private-member-variables-in-classes,
modernize-*,
-modernize-use-trailing-return-type,
@@ -39,7 +42,6 @@ CheckOptions:
- { key: readability-identifier-naming.ClassCase, value: CamelCase }
- { key: readability-identifier-naming.ClassMethodCase, value: camelBack }
- { key: readability-identifier-naming.ConstexprVariableCase, value: CamelCase }
- { key: readability-identifier-naming.EnumConstantCase, value: UPPER_CASE }
- { key: readability-identifier-naming.FunctionCase, value: camelBack }
- { key: readability-identifier-naming.GlobalConstantCase, value: CamelCase }
- { key: readability-identifier-naming.MemberConstantCase, value: CamelCase }

View File

@@ -363,7 +363,7 @@ TEST_F(AnnounceListTest, save)
// first, set up a scratch torrent
auto constexpr* const OriginalFile = LIBTRANSMISSION_TEST_ASSETS_DIR "/Android-x86 8.1 r6 iso.torrent";
auto original_content = std::vector<char>{};
auto const sandbox = libtransmission::test::Sandbox::create_sandbox(::testing::TempDir(), "transmission-test-XXXXXX");
auto const sandbox = libtransmission::test::Sandbox::createSandbox(::testing::TempDir(), "transmission-test-XXXXXX");
auto const test_file = tr_pathbuf{ sandbox, "transmission-announce-list-test.torrent"sv };
auto error = tr_error{};
EXPECT_TRUE(tr_file_read(OriginalFile, original_content, &error));

View File

@@ -37,7 +37,7 @@ protected:
static auto constexpr DefaultMaxFileCount = size_t{ 16 };
static auto constexpr DefaultMaxFileSize = size_t{ 1024 };
auto makeRandomFiles(
static auto makeRandomFiles(
std::string_view top,
size_t n_files = std::max(size_t{ 1U }, static_cast<size_t>(tr_rand_int(DefaultMaxFileCount))),
size_t max_size = DefaultMaxFileSize)

View File

@@ -69,9 +69,7 @@ TEST_P(IncompleteDirTest, incompleteDir)
EXPECT_EQ(path, tr_torrentFindFile(tor, 1));
EXPECT_EQ(tor->piece_size(), tr_torrentStat(tor)->leftUntilDone);
// auto constexpr completeness_unset = tr_completeness { -1 };
// auto completeness = completeness_unset;
int completeness = -1;
auto completeness = TR_LEECH;
auto const zeroes_completeness_func =
[](tr_torrent* /*torrent*/, tr_completeness c, bool /*was_running*/, void* vc) noexcept
{
@@ -125,7 +123,7 @@ TEST_P(IncompleteDirTest, incompleteDir)
auto test = [&completeness]()
{
return completeness != -1;
return completeness != TR_LEECH;
};
EXPECT_TRUE(waitFor(test, MaxWaitMsec));
EXPECT_EQ(TR_SEED, completeness);

View File

@@ -172,7 +172,7 @@ protected:
return files;
}
auto createFiles(tr_torrent_files const& files, char const* parent)
static auto createFiles(tr_torrent_files const& files, char const* parent)
{
auto paths = std::set<std::string>{};

View File

@@ -46,13 +46,13 @@ protected:
EXPECT_TRUE(waitFor(test, MaxWaitMsec));
}
void createSingleFileTorrentContents(std::string_view top)
static void createSingleFileTorrentContents(std::string_view top)
{
auto const path = tr_pathbuf{ top, "/hello-world.txt" };
createFileWithContents(path, "hello, world!\n");
}
void createMultifileTorrentContents(std::string_view top)
static void createMultifileTorrentContents(std::string_view top)
{
auto path = tr_pathbuf{ top, "/Felidae/Felinae/Acinonyx/Cheetah/Chester"sv };
createFileWithContents(path, "It ain't easy bein' cheesy.\n");

View File

@@ -37,15 +37,12 @@ inline std::ostream& operator<<(std::ostream& os, tr_error const& err)
return os;
}
namespace libtransmission
{
namespace test
namespace libtransmission::test
{
using file_func_t = std::function<void(char const* filename)>;
static void depthFirstWalk(char const* path, file_func_t func)
static void depthFirstWalk(char const* path, file_func_t const& func)
{
if (auto const info = tr_sys_path_get_info(path); info && info->isFolder())
{
@@ -125,8 +122,8 @@ class Sandbox
{
public:
Sandbox()
: parent_dir_{ get_default_parent_dir() }
, sandbox_dir_{ create_sandbox(parent_dir_, "transmission-test-XXXXXX") }
: parent_dir_{ getDefaultParentDir() }
, sandbox_dir_{ createSandbox(parent_dir_, "transmission-test-XXXXXX") }
{
}
@@ -135,12 +132,17 @@ public:
rimraf(sandbox_dir_);
}
Sandbox(Sandbox const&) = delete;
Sandbox(Sandbox&&) = delete;
Sandbox& operator=(Sandbox const&) = delete;
Sandbox& operator=(Sandbox&&) = delete;
[[nodiscard]] constexpr std::string const& path() const
{
return sandbox_dir_;
}
static std::string create_sandbox(std::string const& parent_dir, std::string const& tmpl)
static std::string createSandbox(std::string const& parent_dir, std::string const& tmpl)
{
auto path = fmt::format(FMT_STRING("{:s}/{:s}"sv), tr_sys_path_resolve(parent_dir), tmpl);
tr_sys_dir_create_temp(std::data(path));
@@ -149,7 +151,7 @@ public:
}
protected:
static std::string get_default_parent_dir()
static std::string getDefaultParentDir()
{
if (auto* const path = getenv("TMPDIR"); path != nullptr)
{
@@ -166,7 +168,7 @@ protected:
{
if (verbose)
{
std::cerr << "cleanup: removing '" << filename << "'" << std::endl;
std::cerr << "cleanup: removing '" << filename << "'\n";
}
tr_sys_path_remove(filename);
@@ -188,7 +190,7 @@ protected:
return sandbox_.path();
}
[[nodiscard]] auto currentTestName() const
[[nodiscard]] static auto currentTestName()
{
auto const* i = ::testing::UnitTest::GetInstance()->current_test_info();
auto child = std::string(i->test_suite_name());
@@ -197,7 +199,7 @@ protected:
return child;
}
void buildParentDir(std::string_view path) const
static void buildParentDir(std::string_view path)
{
auto const tmperr = errno;
@@ -237,7 +239,7 @@ protected:
}
}
void createTmpfileWithContents(char* tmpl, void const* payload, size_t n) const
static void createTmpfileWithContents(char* tmpl, void const* payload, size_t n)
{
auto const tmperr = errno;
@@ -260,7 +262,7 @@ protected:
errno = tmperr;
}
void createFileWithContents(std::string_view path, void const* payload, size_t n) const
static void createFileWithContents(std::string_view path, void const* payload, size_t n)
{
auto const tmperr = errno;
@@ -278,19 +280,19 @@ protected:
errno = tmperr;
}
void createFileWithContents(std::string_view path, std::string_view payload) const
static void createFileWithContents(std::string_view path, std::string_view payload)
{
createFileWithContents(path, std::data(payload), std::size(payload));
}
void createFileWithContents(std::string_view path, void const* payload) const
static void createFileWithContents(std::string_view path, void const* payload)
{
createFileWithContents(path, payload, strlen(static_cast<char const*>(payload)));
}
bool verbose = false;
bool verbose_ = false;
void sync() const
static void sync()
{
#ifndef _WIN32
::sync();
@@ -330,19 +332,19 @@ private:
// fill in any missing settings
settings_map->try_emplace(TR_KEY_port_forwarding_enabled, false);
settings_map->try_emplace(TR_KEY_dht_enabled, false);
settings_map->try_emplace(TR_KEY_message_level, verbose ? TR_LOG_DEBUG : TR_LOG_ERROR);
settings_map->try_emplace(TR_KEY_message_level, verbose_ ? TR_LOG_DEBUG : TR_LOG_ERROR);
return tr_sessionInit(sandboxDir().data(), !verbose, settings);
return tr_sessionInit(sandboxDir().data(), !verbose_, settings);
}
void sessionClose(tr_session* session)
static void sessionClose(tr_session* session)
{
tr_sessionClose(session);
tr_logFreeQueue(tr_logGetQueue());
}
protected:
enum class ZeroTorrentState
enum class ZeroTorrentState : uint8_t
{
NoFiles,
Partial,
@@ -485,7 +487,7 @@ protected:
return settings_.get();
}
virtual void SetUp() override
void SetUp() override
{
SandboxedTest::SetUp();
@@ -494,7 +496,7 @@ protected:
session_ = sessionInit(*settings());
}
virtual void TearDown() override
void TearDown() override
{
sessionClose(session_);
session_ = nullptr;
@@ -509,6 +511,4 @@ private:
std::vector<tr_torrent*> verified_;
};
} // namespace test
} // namespace libtransmission
} // namespace libtransmission::test

View File

@@ -134,7 +134,7 @@ TEST_F(TorrentMetainfoTest, AndroidTorrent)
TEST_F(TorrentMetainfoTest, ctorSaveContents)
{
auto const sandbox = libtransmission::test::Sandbox::create_sandbox(::testing::TempDir(), "transmission-test-XXXXXX");
auto const sandbox = libtransmission::test::Sandbox::createSandbox(::testing::TempDir(), "transmission-test-XXXXXX");
auto const src_filename = tr_pathbuf{ LIBTRANSMISSION_TEST_ASSETS_DIR, "/Android-x86 8.1 r6 iso.torrent"sv };
auto const tgt_filename = tr_pathbuf{ sandbox, "save-contents-test.torrent" };

View File

@@ -262,7 +262,7 @@ TEST_F(UtilsTest, saveFile)
auto filename = tr_pathbuf{};
// save a file to GoogleTest's temp dir
auto const sandbox = libtransmission::test::Sandbox::create_sandbox(::testing::TempDir(), "transmission-test-XXXXXX");
auto const sandbox = libtransmission::test::Sandbox::createSandbox(::testing::TempDir(), "transmission-test-XXXXXX");
filename.assign(sandbox, "filename.txt"sv);
auto contents = "these are the contents"sv;
auto error = tr_error{};

View File

@@ -4,9 +4,7 @@
// License text can be found in the licenses/ folder.
#include <array>
#include <cassert>
#include <cerrno>
#include <cmath> // lrint()
#include <cstddef> // size_t
#include <cstdint> // int64_t
#include <string>
@@ -22,6 +20,7 @@
#include <libtransmission/variant.h>
#include "gtest/gtest.h"
#include "test-fixtures.h"
using namespace std::literals;

View File

@@ -96,7 +96,7 @@ protected:
return watchdir;
}
void createFile(std::string_view dirname, std::string_view basename, std::string_view contents = ""sv)
static void createFile(std::string_view dirname, std::string_view basename, std::string_view contents = ""sv)
{
createFileWithContents(tr_pathbuf{ dirname, '/', basename }, contents);
}