Skip to content

Commit

Permalink
Merge #3245
Browse files Browse the repository at this point in the history
3245: [tests] add mocks for a few util methods r=townsend2010 a=andrei-toterman

This is a prerequisite of canonical/multipass-private#508, but the changes should appear in public as well

Co-authored-by: Andrei Toterman <[email protected]>
  • Loading branch information
bors[bot] and andrei-toterman authored Oct 18, 2023
2 parents 24ca05e + 86ffc93 commit ef40bb5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/multipass/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ class Utils : public Singleton<Utils>

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,
Expand All @@ -222,6 +223,7 @@ class Utils : public Singleton<Utils>
virtual QString generate_scrypt_hash_for(const QString& passphrase) const;

virtual std::vector<uint8_t> random_bytes(size_t len);
virtual QString make_uuid(const std::optional<std::string>& seed = std::nullopt) const;
};
} // namespace multipass

Expand Down
10 changes: 10 additions & 0 deletions src/utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t> mp::Utils::random_bytes(size_t len)
{
std::vector<uint8_t> bytes(len, 0);
Expand All @@ -497,6 +502,11 @@ std::vector<uint8_t> mp::Utils::random_bytes(size_t len)
return bytes;
}

QString mp::Utils::make_uuid(const std::optional<std::string>& 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); });
Expand Down
1 change: 1 addition & 0 deletions tests/mock_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
2 changes: 2 additions & 0 deletions tests/mock_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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<std::string>&), (const, override));

MP_MOCK_SINGLETON_BOILERPLATE(MockUtils, Utils);
};
Expand Down

0 comments on commit ef40bb5

Please sign in to comment.