12 #ifndef ZYPP_MISC_YAMLTESTCASEHELPERS_H
13 #define ZYPP_MISC_YAMLTESTCASEHELPERS_H
19 #include <yaml-cpp/yaml.h>
21 #include <type_traits>
27 auto &target = t.
data();
28 MIL <<
"Parsing setup node " << std::endl;
29 for ( YAML::const_iterator it = setup.begin(); it != setup.end(); it++ ) {
31 const std::string &key = it->first.as<std::string>();
32 const auto &data = it->second;
34 MIL <<
"Found key " << key << std::endl;
37 auto readListInlineOrFromFile = [&](
const auto &cb , std::string *err ) ->
bool {
38 if ( data.Type() == YAML::NodeType::Sequence ) {
40 for (
const auto &
node: data ) {
41 if ( !cb(
node, err ) )
return false;
44 MIL <<
"Loaded " << cnt <<
" Elements inline" << std::endl;
46 const std::string &fName = data.as<std::string>();
47 MIL <<
"Trying to load list from file " << fName << std::endl;
49 auto doc = YAML::LoadFile( fName );
50 if ( doc.Type() != YAML::NodeType::Sequence ) {
51 if ( err ) *err =
"Expected the top node to be a sequence in external file for key: ";
56 for (
const auto &
node : doc ) {
57 if ( !cb(
node, err ) )
return false;
60 MIL <<
"Loaded " << cnt <<
" Elements from file" << std::endl;
61 }
catch ( YAML::Exception &e ) {
62 if ( err ) *err = e.what();
65 if ( err ) *err =
zypp::str::Str() <<
"Unknown error when parsing the file for " << key;
72 if ( key ==
"resolverFlags" ) {
73 #define if_SolverFlag( N ) if ( data[#N] ) { target.N = data[#N].as<bool>(); }
74 if_SolverFlag( ignorealreadyrecommended )
if ( data[
"ignorealready"] ) { target.ignorealreadyrecommended = data[
"ignorealready"].as<
bool>(); }
75 if_SolverFlag( onlyRequires )
if ( data[
"ignorerecommended"] ) { target.onlyRequires = data[
"ignorerecommended"].as<
bool>(); }
90 if ( data[
"focus"] ) {
91 target.resolverFocus = zypp::resolverFocusFromString( data[
"focus"].as<std::string>() );
93 }
else if ( key == (
"system") ) {
95 zypp::misc::testcase::TestcaseRepoType::Testtags,
98 data[
"file"].as<std::string>()
101 else if ( key == (
"hardwareInfo") ) {
102 target.hardwareInfoFile = data.as<std::string>();
104 else if ( key == (
"modalias") ) {
105 bool success = readListInlineOrFromFile( [&target](
const YAML::Node &dataNode,
auto ){
106 target.modaliasList.push_back( dataNode.as<std::string>() );
109 if ( !success )
return false;
111 else if ( key == (
"multiversion") ) {
112 bool success = readListInlineOrFromFile( [&target](
const YAML::Node &dataNode,
auto ){
113 target.multiversionSpec.insert( dataNode.as<std::string>() );
116 if ( !success )
return false;
118 else if (key == (
"channels")) {
119 bool success = readListInlineOrFromFile( [&target](
const YAML::Node &dataNode,
auto ){
120 std::string name = dataNode[
"alias"].as<std::string>();
121 std::string file = dataNode[
"file"].as<std::string>();
124 if ( dataNode[
"priority"] )
125 prio = dataNode[
"priority"].as<
unsigned>();
128 zypp::misc::testcase::TestcaseRepoType::Testtags,
135 if ( !success )
return false;
137 else if ( key == (
"sources") )
139 bool success = readListInlineOrFromFile( [&target](
const YAML::Node &dataNode,
auto ){
140 std::string
url = dataNode[
"url"].as<std::string>();
141 std::string alias = dataNode[
"name"].as<std::string>();
150 if ( !success )
return false;
152 else if ( key == (
"force-install") )
154 bool success = readListInlineOrFromFile( [&target](
const YAML::Node &dataNode,
auto ){
156 dataNode[
"channel"].as<std::string>(),
157 dataNode[
"package"].as<std::string>(),
158 dataNode[
"kind"].as<std::string>()
162 if ( !success )
return false;
164 else if ( key == (
"mediaid") )
166 target.show_mediaid = data.as<
bool>();
168 else if ( key == (
"arch") ) {
169 std::string architecture = data.as<std::string>();
170 if ( architecture.empty() ) {
171 if (err) *err =
zypp::str::Str() <<
"Property 'arch' in setup can not be empty." << std::endl;
175 MIL <<
"Setting architecture to '" << architecture <<
"'" << std::endl;
176 target.architecture =
zypp::Arch( architecture );
179 else if ( key == (
"locales") )
181 bool success = readListInlineOrFromFile( [&target](
const YAML::Node &dataNode, std::string *err ){
183 std::string fate = dataNode[
"fate"].as<std::string>();
185 if (err) *err =
zypp::str::Str() <<
"Bad or missing name in locale..." << std::endl;
188 else if ( fate ==
"added" ) {
189 target.localesTracker.added().insert( loc );
191 else if ( fate ==
"removed" ) {
192 target.localesTracker.removed().insert( loc );
195 target.localesTracker.current().insert( loc );
199 if ( !success )
return false;
201 else if ( key == (
"vendors") )
203 bool success = readListInlineOrFromFile( [&target](
const YAML::Node & dataNode, std::string * err ) {
204 std::vector<std::string> vlist;
205 for (
const auto &
node : dataNode )
206 vlist.push_back(
node.as<std::string>() );
207 if ( ! vlist.empty() )
208 target.vendorLists.push_back( std::move(vlist) );
211 if ( !success )
return false;
213 else if ( key == (
"autoinst") ) {
214 bool success = readListInlineOrFromFile( [&](
const YAML::Node &dataNode,
auto ){
215 target.autoinstalled.push(
zypp::IdString( dataNode.as<std::string>() ).
id() );
218 if ( !success )
return false;
220 else if ( key == (
"systemCheck") ) {
221 target.systemCheck = data.as<std::string>();
223 else if ( key == (
"setlicencebit") ) {
224 target.set_licence = data.as<
bool>();
227 ERR <<
"Ignoring unrecognized tag '" << key <<
"' in setup" << std::endl;
233 template <
typename T>
234 bool parseJobs (
const YAML::Node &trial, std::vector<T> &target, std::string *err );
236 template <
typename T>
237 bool parseSingleJob (
const YAML::Node &jobNode, std::vector<T> &target, std::string *err ) {
239 constexpr
bool isSubNode = std::is_same_v<T, std::shared_ptr<zypp::misc::testcase::TestcaseTrial::Node>>;
240 if ( jobNode[
"include"] ) {
242 const auto &fName = jobNode[
"include"].as<std::string>();
243 MIL <<
"Including file " << fName << std::endl;
245 auto doc = YAML::LoadFile( fName );
248 MIL <<
"Including file " << fName <<
"was successfull" << std::endl;
249 }
catch ( YAML::Exception &e ) {
250 if ( err ) *err = e.what();
253 if ( err ) *err =
zypp::str::Str() <<
"Unknown error when parsing the file: " << fName;
260 if ( !jobNode[
"job"] ) {
263 const auto &mark = jobNode.Mark();
264 errStr <<
"'job' key missing from trial node.";
265 if ( !mark.is_null() ) {
266 errStr <<
" Line: " << mark.line <<
" Col: " << mark.column <<
" pos: " << mark.pos;
273 for (
const auto &elem : jobNode ) {
274 const std::string &key = elem.first.as<std::string>();
275 const auto &data = elem.second;
276 if ( key ==
"job" ) {
277 n.
name() = data.as<std::string>();
278 }
else if ( key ==
"__content") {
279 n.
value() = data.as<std::string>();
281 if( data.IsScalar() ) {
282 n.
properties().insert( { key, data.as<std::string>() } );
283 }
else if ( data.IsSequence() ) {
289 }
else if ( data.IsMap() ) {
296 ERR <<
"Ignoring field " << key <<
" with unsupported type." << std::endl;
300 if constexpr ( isSubNode ) {
301 target.push_back( std::make_shared<zypp::misc::testcase::TestcaseTrial::Node>( std::move(n) ) );
303 target.push_back( std::move(n) );
308 template <
typename T>
309 bool parseJobs (
const YAML::Node &trial, std::vector<T> &target, std::string *err ) {
310 for (
const auto &jobNode : trial ) {
318 MIL <<
"Parsing trials." << std::endl;
323 #endif // ZYPP_MISC_YAMLTESTCASEHELPERS_H