1#include "ConfigPreprocessor.h"
2#include <Core/Types.h>
3#include <Poco/Path.h>
4#include <regex>
5namespace DB
6{
7std::vector<XMLConfigurationPtr> ConfigPreprocessor::processConfig(
8 const Strings & tests_tags,
9 const Strings & tests_names,
10 const Strings & tests_names_regexp,
11 const Strings & skip_tags,
12 const Strings & skip_names,
13 const Strings & skip_names_regexp) const
14{
15
16 std::vector<XMLConfigurationPtr> result;
17 for (const auto & path_str : paths)
18 {
19 auto test = XMLConfigurationPtr(new XMLConfiguration(path_str));
20 result.push_back(test);
21
22 const auto path = Poco::Path(path_str);
23 test->setString("path", path.absolute().toString());
24 if (test->getString("name", "") == "")
25 test->setString("name", path.getBaseName());
26 }
27
28 /// Leave tests:
29 removeConfigurationsIf(result, FilterType::Tag, tests_tags, true);
30 removeConfigurationsIf(result, FilterType::Name, tests_names, true);
31 removeConfigurationsIf(result, FilterType::Name_regexp, tests_names_regexp, true);
32
33 /// Skip tests
34 removeConfigurationsIf(result, FilterType::Tag, skip_tags, false);
35 removeConfigurationsIf(result, FilterType::Name, skip_names, false);
36 removeConfigurationsIf(result, FilterType::Name_regexp, skip_names_regexp, false);
37 return result;
38}
39
40void ConfigPreprocessor::removeConfigurationsIf(
41 std::vector<XMLConfigurationPtr> & configs,
42 ConfigPreprocessor::FilterType filter_type,
43 const Strings & values,
44 bool leave) const
45{
46 auto checker = [&filter_type, &values, &leave] (XMLConfigurationPtr & config)
47 {
48 if (values.size() == 0)
49 return false;
50
51 bool remove_or_not = false;
52
53 if (filter_type == FilterType::Tag)
54 {
55 Strings tags_keys;
56 config->keys("tags", tags_keys);
57
58 Strings tags(tags_keys.size());
59 for (size_t i = 0; i != tags_keys.size(); ++i)
60 tags[i] = config->getString("tags.tag[" + std::to_string(i) + "]");
61
62 for (const std::string & config_tag : tags)
63 {
64 if (std::find(values.begin(), values.end(), config_tag) != values.end())
65 remove_or_not = true;
66 }
67 }
68
69 if (filter_type == FilterType::Name)
70 {
71 remove_or_not = (std::find(values.begin(), values.end(), config->getString("name", "")) != values.end());
72 }
73
74 if (filter_type == FilterType::Name_regexp)
75 {
76 std::string config_name = config->getString("name", "");
77 auto regex_checker = [&config_name](const std::string & name_regexp)
78 {
79 std::regex pattern(name_regexp);
80 return std::regex_search(config_name, pattern);
81 };
82
83 remove_or_not = config->has("name") ? (std::find_if(values.begin(), values.end(), regex_checker) != values.end()) : false;
84 }
85
86 if (leave)
87 remove_or_not = !remove_or_not;
88 return remove_or_not;
89 };
90
91 auto new_end = std::remove_if(configs.begin(), configs.end(), checker);
92 configs.erase(new_end, configs.end());
93}
94
95}
96