From 86ffc93d128ef016fcb7b0d43b467159e722c6d2 Mon Sep 17 00:00:00 2001 From: Andrei Toterman Date: Tue, 3 Oct 2023 14:18:58 +0200 Subject: [PATCH] [tests] add mocks for a few util methods --- include/multipass/utils.h | 2 ++ src/utils/utils.cpp | 10 ++++++++++ tests/mock_platform.h | 1 + tests/mock_utils.h | 2 ++ 4 files changed, 15 insertions(+) diff --git a/include/multipass/utils.h b/include/multipass/utils.h index d957ad594f..1b254d70b8 100644 --- a/include/multipass/utils.h +++ b/include/multipass/utils.h @@ -200,6 +200,7 @@ class Utils : public Singleton virtual qint64 filesystem_bytes_available(const QString& data_directory) const; virtual void exit(int code); + virtual std::string contents_of(const multipass::Path& file_path) const; virtual void make_file_with_content(const std::string& file_name, const std::string& content, const bool& overwrite = false); virtual Path make_dir(const QDir& a_dir, const QString& name, @@ -222,6 +223,7 @@ class Utils : public Singleton virtual QString generate_scrypt_hash_for(const QString& passphrase) const; virtual std::vector random_bytes(size_t len); + virtual QString make_uuid(const std::optional& seed = std::nullopt) const; }; } // namespace multipass diff --git a/src/utils/utils.cpp b/src/utils/utils.cpp index d1457e3153..ebb9cfac29 100644 --- a/src/utils/utils.cpp +++ b/src/utils/utils.cpp @@ -479,6 +479,11 @@ std::string mp::utils::contents_of(const multipass::Path& file_path) return stream.str(); } +std::string mp::Utils::contents_of(const multipass::Path& file_path) const +{ + return mp::utils::contents_of(file_path); +} + std::vector mp::Utils::random_bytes(size_t len) { std::vector bytes(len, 0); @@ -497,6 +502,11 @@ std::vector mp::Utils::random_bytes(size_t len) return bytes; } +QString mp::Utils::make_uuid(const std::optional& seed) const +{ + return mp::utils::make_uuid(seed); +} + bool mp::utils::has_only_digits(const std::string& value) { return std::all_of(value.begin(), value.end(), [](char c) { return std::isdigit(c); }); diff --git a/tests/mock_platform.h b/tests/mock_platform.h index 3c34400ba6..550b6fca1d 100644 --- a/tests/mock_platform.h +++ b/tests/mock_platform.h @@ -55,6 +55,7 @@ class MockPlatform : public platform::Platform MOCK_METHOD(QString, default_driver, (), (const, override)); MOCK_METHOD(QString, default_privileged_mounts, (), (const, override)); MOCK_METHOD(bool, is_image_url_supported, (), (const, override)); + MOCK_METHOD(QString, get_username, (), (const, override)); MP_MOCK_SINGLETON_BOILERPLATE(MockPlatform, Platform); }; diff --git a/tests/mock_utils.h b/tests/mock_utils.h index 173886795e..9c4b185b3d 100644 --- a/tests/mock_utils.h +++ b/tests/mock_utils.h @@ -35,6 +35,7 @@ class MockUtils : public Utils MOCK_METHOD(void, exit, (int), (override)); MOCK_METHOD(std::string, run_cmd_for_output, (const QString&, const QStringList&, const int), (const, override)); MOCK_METHOD(bool, run_cmd_for_status, (const QString&, const QStringList&, const int), (const, override)); + MOCK_METHOD(std::string, contents_of, (const multipass::Path&), (const, override)); MOCK_METHOD(void, make_file_with_content, (const std::string&, const std::string&), ()); MOCK_METHOD(void, make_file_with_content, (const std::string&, const std::string&, const bool&), (override)); MOCK_METHOD(Path, make_dir, (const QDir&, const QString&, QFileDevice::Permissions), (override)); @@ -45,6 +46,7 @@ class MockUtils : public Utils MOCK_METHOD(QString, generate_scrypt_hash_for, (const QString&), (const, override)); MOCK_METHOD(bool, client_certs_exist, (const QString&), (const)); MOCK_METHOD(void, copy_client_certs_to_common_dir, (const QString&, const QString&), (const)); + MOCK_METHOD(QString, make_uuid, (const std::optional&), (const, override)); MP_MOCK_SINGLETON_BOILERPLATE(MockUtils, Utils); };