Skip to content

Commit 09189a0

Browse files
sharder996ricab
authored andcommitted
[tests] mock convert_to_localtime() to print in UTC time in tests
1 parent 3f08ad2 commit 09189a0

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

tests/mock_format_utils.h

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (C) Canonical, Ltd.
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; version 3.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
*
16+
*/
17+
18+
#ifndef MULTIPASS_MOCK_FORMAT_UTILS_H
19+
#define MULTIPASS_MOCK_FORMAT_UTILS_H
20+
21+
#include "mock_singleton_helpers.h"
22+
23+
#include <multipass/cli/format_utils.h>
24+
25+
namespace multipass::test
26+
{
27+
class MockFormatUtils : public FormatUtils
28+
{
29+
public:
30+
using FormatUtils::FormatUtils;
31+
32+
MOCK_METHOD(std::string, convert_to_localtime, (const google::protobuf::Timestamp&), (const, override));
33+
34+
MP_MOCK_SINGLETON_BOILERPLATE(MockFormatUtils, FormatUtils);
35+
};
36+
} // namespace multipass::test
37+
38+
#endif // MULTIPASS_MOCK_FORMAT_UTILS_H

tests/test_output_formatter.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717

1818
#include "common.h"
19+
#include "mock_format_utils.h"
1920
#include "mock_settings.h"
2021

2122
#include <multipass/cli/csv_formatter.h>
@@ -748,6 +749,11 @@ class BaseFormatterSuite : public testing::Test
748749
// The tests expected output are for the default C locale
749750
std::locale::global(std::locale("C"));
750751
EXPECT_CALL(mock_settings, get(Eq(mp::petenv_key))).WillRepeatedly(Return("pet"));
752+
753+
// Timestamps in tests need to be in a consistent locale
754+
EXPECT_CALL(mock_format_utils, convert_to_localtime(_)).WillRepeatedly([](const auto timestamp) {
755+
return google::protobuf::util::TimeUtil::ToString(timestamp);
756+
});
751757
}
752758

753759
~BaseFormatterSuite()
@@ -759,6 +765,9 @@ class BaseFormatterSuite : public testing::Test
759765
mpt::MockSettings::GuardedMock mock_settings_injection = mpt::MockSettings::inject<StrictMock>();
760766
mpt::MockSettings& mock_settings = *mock_settings_injection.first;
761767

768+
mpt::MockFormatUtils::GuardedMock mock_format_utils_injection = mpt::MockFormatUtils::inject<NiceMock>();
769+
mpt::MockFormatUtils& mock_format_utils = *mock_format_utils_injection.first;
770+
762771
private:
763772
std::locale saved_locale;
764773
};

0 commit comments

Comments
 (0)