Skip to content

Commit 5ed6c9e

Browse files
authored
Merge pull request #3486 from hnakamur/v3/do_not_add_newline_to_test_body_lines
V3/do not add newline to test body lines
2 parents 64d43d4 + 07c487b commit 5ed6c9e

File tree

203 files changed

+25333
-20969
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

203 files changed

+25333
-20969
lines changed

test/common/modsecurity_test.cc

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,22 @@ bool ModSecurityTest<T>::load_test_json(const std::string &file) {
6868
return false;
6969
}
7070

71-
size_t num_tests = node->u.array.len;
72-
for ( int i = 0; i < num_tests; i++ ) {
73-
yajl_val obj = node->u.array.values[i];
74-
75-
auto u = std::unique_ptr<T>(T::from_yajl_node(obj));
71+
if (m_format) {
72+
auto u = T::from_yajl_node(node);
7673
u->filename = file;
7774

78-
const auto key = u->filename + ":" + u->name;
79-
(*this)[key].push_back(std::move(u));
75+
(*this)[file].push_back(std::move(u));
76+
} else {
77+
size_t num_tests = node->u.array.len;
78+
for ( int i = 0; i < num_tests; i++ ) {
79+
yajl_val obj = node->u.array.values[i];
80+
81+
auto u = T::from_yajl_node(obj);
82+
u->filename = file;
83+
84+
const auto key = u->filename + ":" + u->name;
85+
(*this)[key].push_back(std::move(u));
86+
}
8087
}
8188

8289
yajl_tree_free(node);
@@ -140,6 +147,13 @@ void ModSecurityTest<T>::cmd_options(int argc, char **argv) {
140147
i++;
141148
m_test_multithreaded = true;
142149
}
150+
if (argc > i && strcmp(argv[i], "format") == 0) {
151+
i++;
152+
m_format = true;
153+
}
154+
if (std::getenv("UPDATE_CONTENT_LENGTH")) {
155+
m_update_content_length = true;
156+
}
143157
if (std::getenv("AUTOMAKE_TESTS")) {
144158
m_automake_output = true;
145159
}

test/common/modsecurity_test.h

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include <utility>
1818
#include <string>
1919
#include <vector>
20-
#include <unordered_map>
20+
#include <map>
2121

2222
#ifndef TEST_COMMON_MODSECURITY_TEST_H_
2323
#define TEST_COMMON_MODSECURITY_TEST_H_
@@ -29,13 +29,9 @@ extern std::string default_test_path;
2929
namespace modsecurity_test {
3030

3131
template <class T> class ModSecurityTest :
32-
public std::unordered_map<std::string, std::vector<std::unique_ptr<T>>> {
32+
public std::map<std::string, std::vector<std::unique_ptr<T>>> {
3333
public:
34-
ModSecurityTest()
35-
: m_test_number(0),
36-
m_automake_output(false),
37-
m_count_all(false),
38-
m_test_multithreaded(false) { }
34+
ModSecurityTest() = default;
3935

4036
std::string header();
4137
void cmd_options(int, char **);
@@ -44,12 +40,14 @@ template <class T> class ModSecurityTest :
4440
bool load_test_json(const std::string &file);
4541

4642
std::string target;
47-
bool verbose = false;
48-
bool color = false;
49-
int m_test_number;
50-
bool m_automake_output;
51-
bool m_count_all;
52-
bool m_test_multithreaded;
43+
bool verbose{false};
44+
bool color{false};
45+
int m_test_number{0};
46+
bool m_automake_output{false};
47+
bool m_count_all{false};
48+
bool m_test_multithreaded{false};
49+
bool m_format{false};
50+
bool m_update_content_length{false};
5351
};
5452

5553
} // namespace modsecurity_test

test/common/modsecurity_test_results.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
*/
1515

1616
#include <iostream>
17-
#include <unordered_map>
1817
#include <vector>
1918
#include <string>
2019

test/regression/regression.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ using modsecurity_test::CustomDebugLog;
4141
using modsecurity_test::ModSecurityTest;
4242
using modsecurity_test::ModSecurityTestResults;
4343
using modsecurity_test::RegressionTest;
44+
using modsecurity_test::RegressionTests;
4445
using modsecurity_test::RegressionTestResult;
4546

4647
using modsecurity::Utils::regex_search;
@@ -436,6 +437,35 @@ int main(int argc, char **argv)
436437
return 0;
437438
#else
438439
test.cmd_options(argc, argv);
440+
441+
if (test.m_format) {
442+
#ifdef WITH_YAJL
443+
std::cout << "start formatting test case JSON files" << std::endl;
444+
ModSecurityTest<RegressionTests> test2;
445+
test2.cmd_options(argc, argv);
446+
test2.load_tests();
447+
for (const auto &[name, tests] : test2) {
448+
std::ofstream ofs{name};
449+
if (!ofs.is_open()) {
450+
std::cerr << "cannot open " << name << " for writing." << std::endl;
451+
return 1;
452+
}
453+
if (test2.m_update_content_length) {
454+
tests[0]->update_content_lengths();
455+
}
456+
ofs << tests[0]->toJSON();
457+
ofs.close();
458+
std::cout << "written formatted JSON to " << name << std::endl;
459+
}
460+
std::cout << "finished formatting files." << std::endl;
461+
return 0;
462+
#else
463+
std::cout << "Test utility cannot format test case JSON files without being built with YAJL." \
464+
<< std::endl;
465+
return 1;
466+
#endif
467+
}
468+
439469
if (!test.m_automake_output && !test.m_count_all) {
440470
std::cout << test.header();
441471
}

0 commit comments

Comments
 (0)