-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatarecorder.hpp
More file actions
480 lines (402 loc) · 16.4 KB
/
Copy pathdatarecorder.hpp
File metadata and controls
480 lines (402 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// Copyright (c) 2025 Steinwurf ApS
// All Rights Reserved
//
// Distributed under the "BSD License". See the accompanying LICENSE.rst file.
#pragma once
#include <filesystem>
#include <fstream>
#include <functional>
#include <optional>
#include <regex>
#include <string>
#include <vector>
#include <gtest/gtest.h>
#include <poke/make_error.hpp>
#include <poke/monitor.hpp>
#include <tl/expected.hpp>
#include <verify/verify.hpp>
#include "mismatch_info.hpp"
#include "to_json_property.hpp"
namespace datarecorder
{
/// This class is used to record data and check for mismatches.
///
/// Example:
/// datarecorder recorder;
/// recorder.set_recording_dir("test/recordings/mytest1.json");
/// recorder.on_mismatch([](datarecorder::mismatch_info mismatch)
/// {
/// std::cout << "Mismatch found!" << std::endl;
/// std::cout << "Recording data: " << mismatch.recording_data
/// << std::endl;
/// std::cout << "Mismatch data: " << mismatch.mismatch_data
/// << std::endl;
/// std::cout << "Mismatch path: " << mismatch.mismatch_path
/// << std::endl;
/// });
/// recorder.record("test data");
///
class datarecorder
{
public:
/// Default Constructor
datarecorder() : m_monitor("datarecorder")
{
}
/// Sets the recording directory where data will be stored.
///
/// Requirements:
///
/// * If a directory is specified, it must already exist.
/// * Both absolute and relative paths are supported.
///
/// Path Resolution:
///
/// * If the given path is relative, `data_recorder` will attempt to resolve
/// it to an absolute path.
/// * The resolution process searches backward from the current working
/// directory (cwd), moving up the directory tree until it finds an existing
/// matching directory.
///
/// Example: Given a cwd of `/home/user/project/build`, calling:
///
/// data_recorder recorder;
/// recorder.set_recording_dir("test/recordings");
///
/// Will attempt to find the "test/recordings" directory in the following
/// order:
///
/// - `/home/user/project/build/test/recordings/`
/// - `/home/user/project/test/recordings/`
/// - `/home/user/test/recordings/`
/// - `/home/test/recordings/`
/// - `/test/recordings/`
///
/// If the directory is found the recording file will be created in that
/// directory.
///
/// Note, that resolving this may not be the best in all use-cases. So we
/// may want to revisit this in the future. Additional options like setting
/// the recording path via an environment variable or as an argument to the
/// test may be added.
void set_recording_dir(std::filesystem::path recording_dir)
{
VERIFY(!recording_dir.empty(), "Recording path must not be empty",
recording_dir);
// Check if the path is absolute
if (recording_dir.is_absolute())
{
m_recording_dir = recording_dir;
return;
}
// Find the recording directory by iterating backwards from the cwd
// until we find the first directory that exists
auto find_result = find_relative_path(recording_dir);
VERIFY(find_result, "Could not find recording path", recording_dir);
m_recording_dir = *find_result;
}
/// Set the recording filename. If not set the filename will be derived
/// from the current test name (assuming this is used in a Google Test
/// environment).
void set_recording_filename(std::string filename)
{
// The file extension should be 2 or more characters ".something"
VERIFY(!filename.empty(), "Recording filename must not be empty",
filename);
m_recording_filename = filename;
}
/// Set the callback that will be called when a mismatch is found.
///
/// If no mismatch handler is set, a default mismatch handler will be used.
/// The callback should return a `poke::error` object describing the
/// mismatch.
///
/// Example:
/// recorder.on_mismatch([](datarecorder::mismatch_info mismatch)
/// {
/// std::cout << "Mismatch found!" << std::endl;
/// std::cout << "Recording data: " << mismatch.recording_data
/// << std::endl;
/// std::cout << "Mismatch data: " << mismatch.mismatch_data
/// << std::endl;
/// return poke::make_error(
//// std::make_error_code(std::errc::invalid_argument),
/// poke::log::str{"recording_data",
/// mismatch.recording_data},
/// poke::log::str{"mismatch_data",
/// mismatch.mismatch_data});
/// });
void on_mismatch(std::function<poke::error(mismatch_info)> callback)
{
m_on_mismatch = callback;
}
/// This is the base function that will record the data. Other convenience
/// functions will call this function. But, before they must serialize their
/// data to a single string.
auto record(const std::string& data) -> tl::expected<void, poke::error>
{
// Check if we have a missmatch handler
if (!m_on_mismatch)
{
determine_mismatch_handler();
}
// Check if the recording path is set
VERIFY(m_recording_dir);
if (!m_recording_filename)
{
m_recording_filename = testname_as_filename();
m_monitor.log(
poke::log_level::debug,
poke::log::str{"message", "Recording filename not set"},
poke::log::str{"test_name", *m_recording_filename});
}
std::filesystem::path recording_path =
m_recording_dir.value() / m_recording_filename.value();
// Check if the file exists
if (std::filesystem::exists(recording_path))
{
m_monitor.log(
poke::log_level::debug,
poke::log::str{"message", "Recording file already exists"},
poke::log::str{"path", recording_path.string()});
// Read the data from the recording path
std::string recording_data = read_data(recording_path);
// Compare the data
return compare_data(data, recording_data);
}
else
{
m_monitor.log(
poke::log_level::debug,
poke::log::str{"message", "Recording file does not exist"},
poke::log::str{"path", recording_path.string()});
// If it does not exist we create it
write_data(recording_path, data);
}
// If we get here we are good
return {};
}
/// Convenience function to record a vector of strings.
auto record(const std::vector<std::string>& data)
-> tl::expected<void, poke::error>
{
// We have to build a single string from the vector
std::string data_string;
for (const auto& data : data)
{
data_string += data + "\n";
}
return record(data_string);
}
auto monitor() -> poke::monitor&
{
return m_monitor;
}
private:
auto testname_as_filename() -> std::string
{
// Get the current test name
auto* test_info =
::testing::UnitTest::GetInstance()->current_test_info();
std::string test_case = test_info->test_case_name();
std::string test_name = test_info->name();
VERIFY(!test_case.empty());
VERIFY(!test_name.empty());
std::string filename = test_case + "_" + test_name + ".data";
return filename;
}
void determine_mismatch_handler()
{
auto visualizer = find_relative_path("visualizer/recording_diff.html");
if (visualizer)
{
m_monitor.log(poke::log_level::debug,
poke::log::str{"message", "Using diff visualizer"},
poke::log::str{"path", visualizer->string()});
m_on_mismatch = [this, visualizer](mismatch_info mismatch)
{
// Call the diff handler
return diff_mismatch_handler(*visualizer, mismatch);
};
}
else
{
m_monitor.log(
poke::log_level::debug,
poke::log::str{"message", "Using default mismatch handler"},
poke::log::str{"path", visualizer.error().message()});
m_on_mismatch = [this](mismatch_info mismatch)
{
// Call the default handler
return default_mismatch_handler(mismatch);
};
}
}
auto determine_mismatch_dir() -> std::filesystem::path
{
VERIFY(m_recording_dir, "Recording dir must not be empty");
// Put the mismatch in /tmp/cppmismatch-N/file_name where N is
// a concecutive number incremented if already exists
std::filesystem::path tmp_dir = std::filesystem::temp_directory_path();
std::filesystem::path mismatch_dir = tmp_dir / "cppmismatch-0";
std::size_t i = 0;
while (std::filesystem::exists(mismatch_dir))
{
++i;
mismatch_dir = tmp_dir / ("cppmismatch-" + std::to_string(i));
}
// Create the directory
std::error_code ec;
bool created = std::filesystem::create_directory(mismatch_dir, ec);
VERIFY(created, "Could not create directory", ec);
return mismatch_dir;
}
void write_data(const std::filesystem::path& path, const std::string& data)
{
std::ofstream file(path, std::ios::out | std::ios::trunc);
VERIFY(file.is_open(), "Could not open file for writing", errno, path);
file << data;
file.close();
VERIFY(file.good(), "Could not write to file", errno);
}
auto read_data(const std::filesystem::path& path) -> std::string
{
std::ifstream file(path, std::ios::in);
VERIFY(file.is_open(), "Could not open file for reading", errno);
// Read all data from the file
std::string data{std::istreambuf_iterator<char>(file),
std::istreambuf_iterator<char>()};
file.close();
return data;
}
auto compare_data(const std::string& data,
const std::string& recording_data)
-> tl::expected<void, poke::error>
{
VERIFY(m_recording_filename.has_value(),
"Recording filename must not be empty");
if (data != recording_data)
{
// If it exists we check for a mismatch
std::filesystem::path mismatch_dir = determine_mismatch_dir();
m_monitor.log(poke::log_level::debug,
poke::log::str{"message", "Mismatch found"});
// We have a mismatch
mismatch_info mismatch;
mismatch.recording_data = recording_data;
mismatch.mismatch_data = data;
mismatch.mismatch_dir = mismatch_dir;
VERIFY(m_recording_filename.has_value());
VERIFY(m_recording_dir.has_value());
mismatch.recording_path =
m_recording_dir.value() / m_recording_filename.value();
VERIFY(m_on_mismatch, "Mismatch handler not set");
return tl::make_unexpected(m_on_mismatch.value()(mismatch));
}
else
{
m_monitor.log(poke::log_level::debug,
poke::log::str{"message", "No mismatch found"});
return {};
}
}
auto find_relative_path(const std::filesystem::path& path) const
-> tl::expected<std::filesystem::path, poke::error>
{
// We'll store where we looked for the path - just for
// debugging purposes
std::vector<std::filesystem::path> searched_paths;
// Iterate backwards from the current working directory until we
// find the first directory that exists
// Iterate backwards from the current working directory until we
// find the first directory that exists
auto current_path = std::filesystem::current_path();
std::filesystem::path root_path = current_path.root_directory();
while (!current_path.empty() && current_path != root_path)
{
searched_paths.push_back(current_path / path);
if (std::filesystem::exists(current_path / path))
{
return current_path / path;
}
current_path = current_path.parent_path();
}
// Handle the case where the root directory is reached
if (current_path == root_path &&
std::filesystem::exists(current_path / path))
{
return current_path / path;
}
// If we get here, we could not find the path
std::string searched_paths_str;
for (const auto& path : searched_paths)
{
searched_paths_str += path.string() + "\n";
}
return tl::make_unexpected(poke::make_error(
std::make_error_code(std::errc::no_such_file_or_directory),
poke::log::str{"searched_paths", searched_paths_str},
poke::log::str{"path", path.string()}));
}
auto diff_mismatch_handler(std::filesystem::path recording_diff_html,
mismatch_info mismatch) -> poke::error
{
m_monitor.log(
poke::log_level::debug,
poke::log::str{"message", "Using diff mismatch handler"},
poke::log::str{"recording_diff_html", recording_diff_html.string()},
mismatch);
auto escape_dollar_bracs = [](const std::string& input)
{
// When we insert the string in the HTML file we need to escape
// the dollar brackets. Since this is interpreted as a template
// string litteral in javascript. So we need to escape it with a
// backslash.
static const std::regex pattern(R"(\$\{[^}]+\})");
return std::regex_replace(input, pattern, R"(\$&)");
};
std::string escaped_recording_data =
escape_dollar_bracs(mismatch.recording_data);
std::string escaped_mismatch_data =
escape_dollar_bracs(mismatch.mismatch_data);
std::string file_content = read_data(recording_diff_html);
std::regex oldTextPattern(R"((const\s+oldText\s*=\s*`)([^`]*)(`;))");
std::regex newTextPattern(R"((const\s+newText\s*=\s*`)([^`]*)(`;))");
file_content = std::regex_replace(file_content, oldTextPattern,
"$1" + escaped_recording_data + "$3");
file_content = std::regex_replace(file_content, newTextPattern,
"$1" + escaped_mismatch_data + "$3");
// Output file
std::filesystem::path output_file =
mismatch.mismatch_dir / recording_diff_html.filename();
// Write the modified content back to the file
write_data(output_file, file_content);
// Also write the mismatch data to the mismatch dir
std::filesystem::path mismatch_path =
mismatch.mismatch_dir / mismatch.recording_path.filename();
write_data(mismatch_path, mismatch.mismatch_data);
return poke::make_error(
std::make_error_code(std::errc::invalid_argument),
poke::log::str{"message", "Mismatch found"},
poke::log::str{"recording_data:", mismatch.recording_data},
poke::log::str{"mismatch_data:", mismatch.mismatch_data},
poke::log::str{"recording_path:", mismatch.recording_path.string()},
poke::log::str{"mismatch_path:", mismatch_path.string()},
poke::log::str{"html_diff", output_file.string()});
}
auto default_mismatch_handler(mismatch_info mismatch) -> poke::error
{
/// We just return the mismatch as strings
return poke::make_error(
std::make_error_code(std::errc::invalid_argument),
poke::log::str{"recording_data:", mismatch.recording_data},
poke::log::str{"mismatch_data:", mismatch.mismatch_data});
}
private:
/// Monitor for logging
poke::monitor m_monitor;
std::optional<std::string> m_recording_filename;
std::optional<std::filesystem::path> m_recording_dir;
std::optional<std::function<poke::error(mismatch_info)>> m_on_mismatch;
};
}