6#include "nix/util/source-accessor.hh"
8#include <gmock/gmock.h>
10namespace nix::testing {
18class HasSubstrIgnoreANSIMatcher
21 explicit HasSubstrIgnoreANSIMatcher(std::string substring)
22 : substring(std::move(substring))
26 bool MatchAndExplain(
const char * s, ::testing::MatchResultListener * listener)
const
28 return s !=
nullptr && MatchAndExplain(std::string(s), listener);
31 template<
typename MatcheeStringType>
32 bool MatchAndExplain(
const MatcheeStringType & s, [[maybe_unused]] ::testing::MatchResultListener * listener)
const
37 void DescribeTo(::std::ostream * os)
const
39 *os <<
"has substring " << substring;
42 void DescribeNegationTo(::std::ostream * os)
const
44 *os <<
"has no substring " << substring;
48 std::string substring;
53inline ::testing::PolymorphicMatcher<internal::HasSubstrIgnoreANSIMatcher>
54HasSubstrIgnoreANSIMatcher(
const std::string & substring)
68 return ::testing::Throws<SysError>(::testing::Field(&SysError::errNo, expected));
81 return ::testing::Throws<SystemError>(
82 ::testing::Property(&SystemError::ec, ::testing::Eq(std::make_error_condition(expected))));
85MATCHER_P2(HasContents, path, expected,
"")
87 auto stat = arg->maybeLstat(path);
89 *result_listener << arg->showPath(path) <<
" does not exist";
92 if (stat->type != SourceAccessor::tRegular) {
93 *result_listener << arg->showPath(path) <<
" is not a regular file";
96 auto actual = arg->readFile(path);
97 if (actual != expected) {
98 *result_listener << arg->showPath(path) <<
" has contents " << ::testing::PrintToString(actual);
104MATCHER_P2(HasSymlink, path, target,
"")
106 auto stat = arg->maybeLstat(path);
108 *result_listener << arg->showPath(path) <<
" does not exist";
111 if (stat->type != SourceAccessor::tSymlink) {
112 *result_listener << arg->showPath(path) <<
" is not a symlink";
115 auto actual = arg->readLink(path);
116 if (actual != target) {
117 *result_listener << arg->showPath(path) <<
" points to " << ::testing::PrintToString(actual);
123MATCHER_P2(HasDirectory, path, dirents,
"")
125 auto stat = arg->maybeLstat(path);
127 *result_listener << arg->showPath(path) <<
" does not exist";
130 if (stat->type != SourceAccessor::tDirectory) {
131 *result_listener << arg->showPath(path) <<
" is not a directory";
134 auto actual = arg->readDirectory(path);
135 std::set<std::string> actualKeys, expectedKeys(dirents.begin(), dirents.end());
136 for (
auto & [k, _] : actual)
137 actualKeys.insert(k);
138 if (actualKeys != expectedKeys) {
139 *result_listener << arg->showPath(path) <<
" has entries " << ::testing::PrintToString(actualKeys);
153inline auto ThrowsWinError(DWORD expected)
155 return ::testing::Throws<windows::WinError>(::testing::Field(&windows::WinError::lastError, expected));
Definition gmock-matchers.hh:19
This file defines two main structs/classes used in nix error handling.
auto ThrowsSysError(int expected)
Definition gmock-matchers.hh:66
auto ThrowsSystemError(std::errc expected)
Definition gmock-matchers.hh:79
Errors from the operating system: SystemError, and its subclasses for the native error codes,...
std::string stripANSIEscapes(std::string_view s)
Definition terminal.cc:185