OGS
anonymous_namespace{TestDefinition.cpp} Namespace Reference

Typedefs

using FilePairs = std::vector<std::pair<std::string, std::string>>
 Pairs of reference and output file names to be compared.

Functions

bool isConvertibleToDouble (std::string const &s)
 Test if the given string is convertible to a valid double value, not a NaN.
std::string safeString (std::string const &s)
 Wraps a string into double ticks.
std::string findDiffTool (std::string const &executable_name, std::string const &environment_variable_name)
std::string formatPathForDiffTool (std::string const &filename)
void checkTolerance (std::string const &tolerance_name, std::string const &tolerance)
FilePairs findFilesMatchingRegex (std::string const &regex_string, std::string const &reference_path)
 Collects all file names in reference_path matching the regex.

Typedef Documentation

◆ FilePairs

using anonymous_namespace{TestDefinition.cpp}::FilePairs = std::vector<std::pair<std::string, std::string>>

Pairs of reference and output file names to be compared.

Definition at line 174 of file TestDefinition.cpp.

Function Documentation

◆ checkTolerance()

void anonymous_namespace{TestDefinition.cpp}::checkTolerance ( std::string const & tolerance_name,
std::string const & tolerance )

Definition at line 162 of file TestDefinition.cpp.

164{
165 if (!tolerance.empty() && !isConvertibleToDouble(tolerance))
166 {
167 OGS_FATAL(
168 "The {:s} tolerance value '{:s}' is not convertible to double.",
169 tolerance_name, tolerance);
170 }
171}
#define OGS_FATAL(...)
Definition Error.h:10
bool isConvertibleToDouble(std::string const &s)
Test if the given string is convertible to a valid double value, not a NaN.

References isConvertibleToDouble(), and OGS_FATAL.

◆ findDiffTool()

std::string anonymous_namespace{TestDefinition.cpp}::findDiffTool ( std::string const & executable_name,
std::string const & environment_variable_name )

Tries to find a diff tool executable by testing 'path/tool –version' calls for various paths.

Definition at line 69 of file TestDefinition.cpp.

71{
72 // Try to read the environment variable.
73 if (const char* diff_tool_exe_environment_variable =
74 std::getenv(environment_variable_name.c_str()))
75 {
76 std::string const diff_tool_exe{diff_tool_exe_environment_variable};
77 DBUG("{:s} set to {:s}.", environment_variable_name, diff_tool_exe);
78
79 //
80 // Sanity checks.
81 //
82 { // Check the base name.
83 auto const& base_name =
85 if (base_name != executable_name)
86 {
88 "The {:s} environment variable does not point to '{:s}'. "
89 "{:s}='{:s}'",
90 environment_variable_name, executable_name,
91 environment_variable_name, diff_tool_exe);
92 }
93 }
94 { // Diff tool must exist.
95 if (!BaseLib::IsFileExisting(diff_tool_exe))
96 {
97 OGS_FATAL("The {:s} points to a non-existing file. {:s}='{:s}'",
98 environment_variable_name, environment_variable_name,
99 diff_tool_exe);
100 }
101 }
102
103 //
104 // Test the actual call.
105 //
106 int const return_value =
107 // TODO (naumov) replace system call with output consuming call
108 // (fork + execl seems to be more safe), and extract the vtkdiff
109 // call to common function. Also properly escape all strings in
110 // command lines.
111 // Reference for POSIX and Windows:
112 // https://wiki.sei.cmu.edu/confluence/pages/viewpage.action?pageId=87152177
113 // Take care when using fork, which might copy resources.
114 std::system((diff_tool_exe + " --version").c_str());
115 if (return_value == 0)
116 {
117 return diff_tool_exe;
118 }
119 WARN(
120 "Calling {:s} from the {:s} environment variable didn't work as "
121 "expected. Return value was {:d}.",
122 diff_tool_exe, environment_variable_name, return_value);
123 }
124
125 std::vector<std::string> const paths = {"", "bin"};
126 auto const path =
127 find_if(begin(paths), end(paths),
128 [&executable_name](std::string const& path)
129 {
130 int const return_value =
131 // TODO (naumov) replace system call with output
132 // consuming call as in an above todo comment.
133 std::system((BaseLib::joinPaths(path, executable_name) +
134 " --version")
135 .c_str());
136 return return_value == 0;
137 });
138 if (path == end(paths))
139 {
140 OGS_FATAL("{:s} not found.", executable_name);
141 }
142 return BaseLib::joinPaths(*path, executable_name);
143}
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:22
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:34
bool IsFileExisting(const std::string &strFilename)
Returns true if given file exists.
Definition FileTools.cpp:23
std::string extractBaseNameWithoutExtension(std::string const &pathname)
std::string joinPaths(std::string const &pathA, std::string const &pathB)

References DBUG(), BaseLib::extractBaseNameWithoutExtension(), BaseLib::IsFileExisting(), BaseLib::joinPaths(), OGS_FATAL, and WARN().

◆ findFilesMatchingRegex()

FilePairs anonymous_namespace{TestDefinition.cpp}::findFilesMatchingRegex ( std::string const & regex_string,
std::string const & reference_path )

Collects all file names in reference_path matching the regex.

Definition at line 177 of file TestDefinition.cpp.

179{
180 DBUG("regex is '{}'.", regex_string);
181 FilePairs filenames;
182 auto const regex = std::regex(regex_string);
183 for (auto const& p : std::filesystem::directory_iterator(
184 std::filesystem::path(reference_path)))
185 {
186 auto const filename = p.path().filename().string();
187 if (std::regex_match(filename, regex))
188 {
189 DBUG(" -> matched '{}'", filename);
190 filenames.emplace_back(filename, filename);
191 }
192 }
193 return filenames;
194}
std::vector< std::pair< std::string, std::string > > FilePairs
Pairs of reference and output file names to be compared.

References DBUG().

◆ formatPathForDiffTool()

std::string anonymous_namespace{TestDefinition.cpp}::formatPathForDiffTool ( std::string const & filename)

Definition at line 145 of file TestDefinition.cpp.

146{
147#if _WIN32
148 // VTK does not handle Windows long paths:
149 // https://gitlab.kitware.com/vtk/vtk/-/blob/master/Utilities/KWSys/vtksys/SystemTools.cxx#L1519-1521
150 // Workaround is to make the path absolute and prefix with a special
151 // marker and put everything in quotes, see
152 // https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=powershell
153 auto const& long_path_indicator = R"(\\?\)";
154
155 auto const absolute_filename = std::filesystem::absolute(filename).string();
156 return fmt::format("\"{}{}\"", long_path_indicator, absolute_filename);
157#else
158 return safeString(filename);
159#endif
160}
std::string safeString(std::string const &s)
Wraps a string into double ticks.

References safeString().

◆ isConvertibleToDouble()

bool anonymous_namespace{TestDefinition.cpp}::isConvertibleToDouble ( std::string const & s)

Test if the given string is convertible to a valid double value, not a NaN.

Definition at line 30 of file TestDefinition.cpp.

31{
32 std::size_t pos = 0;
33 double value;
34 try
35 {
36 value = std::stod(s, &pos);
37 }
38 catch (...)
39 {
40 ERR("The given string '{:s}' is not convertible to double.", s);
41 return false;
42 }
43 if (pos != s.size())
44 {
45 ERR("Only {:d} characters were used for double conversion of string "
46 "'{:s}'",
47 pos, s);
48 return false;
49 }
50
51 if (std::isnan(value))
52 {
53 ERR("The given string '{:s}' results in a NaN value.", s);
54 return false;
55 }
56 return true;
57}
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40

References ERR().

Referenced by checkTolerance().

◆ safeString()

std::string anonymous_namespace{TestDefinition.cpp}::safeString ( std::string const & s)

Wraps a string into double ticks.

Definition at line 60 of file TestDefinition.cpp.

61{
62 std::stringstream ss;
63 ss << std::quoted(s);
64 return ss.str();
65}

Referenced by formatPathForDiffTool().