mirror of https://github.com/Icinga/icinga2.git
Add Utility::ParseVersion() and unit tests
This now uses a regex to extract the short version similar to how Icinga Web 2 does it. Additional unit tests prove the rule.
This commit is contained in:
parent
fcca9643bc
commit
96f62d2d34
|
@ -20,6 +20,7 @@
|
|||
#include <boost/algorithm/string/replace.hpp>
|
||||
#include <boost/uuid/uuid_io.hpp>
|
||||
#include <boost/uuid/uuid_generators.hpp>
|
||||
#include <boost/regex.hpp>
|
||||
#include <ios>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
@ -1172,6 +1173,26 @@ unsigned long Utility::SDBM(const String& str, size_t len)
|
|||
return hash;
|
||||
}
|
||||
|
||||
String Utility::ParseVersion(const String& v)
|
||||
{
|
||||
/*
|
||||
* 2.11.0-0.rc1.1
|
||||
* v2.10.5
|
||||
* r2.10.3
|
||||
* v2.11.0-rc1-58-g7c1f716da
|
||||
*/
|
||||
boost::regex pattern("^[vr]?(2\\.\\d+\\.\\d+).*$");
|
||||
boost::smatch result;
|
||||
|
||||
if (boost::regex_search(v.GetData(), result, pattern)) {
|
||||
String res(result[1].first, result[1].second);
|
||||
return res;
|
||||
}
|
||||
|
||||
// Couldn't not extract anything, return unparsed version
|
||||
return v;
|
||||
}
|
||||
|
||||
int Utility::CompareVersion(const String& v1, const String& v2)
|
||||
{
|
||||
std::vector<String> tokensv1 = v1.Split(".");
|
||||
|
|
|
@ -100,6 +100,7 @@ public:
|
|||
|
||||
static unsigned long SDBM(const String& str, size_t len = String::NPos);
|
||||
|
||||
static String ParseVersion(const String& v);
|
||||
static int CompareVersion(const String& v1, const String& v2);
|
||||
|
||||
static int Random();
|
||||
|
|
|
@ -108,6 +108,7 @@ add_boost_test(base
|
|||
base_type/assign
|
||||
base_type/byname
|
||||
base_type/instantiate
|
||||
base_utility/parse_version
|
||||
base_utility/comparepasswords_works
|
||||
base_utility/comparepasswords_issafe
|
||||
base_utility/validateutf8
|
||||
|
|
|
@ -8,6 +8,16 @@ using namespace icinga;
|
|||
|
||||
BOOST_AUTO_TEST_SUITE(base_utility)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(parse_version)
|
||||
{
|
||||
BOOST_CHECK(Utility::ParseVersion("2.11.0-0.rc1.1") == "2.11.0");
|
||||
BOOST_CHECK(Utility::ParseVersion("v2.10.5") == "2.10.5");
|
||||
BOOST_CHECK(Utility::ParseVersion("r2.11.1") == "2.11.1");
|
||||
BOOST_CHECK(Utility::ParseVersion("v2.11.0-rc1-58-g7c1f716da") == "2.11.0");
|
||||
|
||||
BOOST_CHECK(Utility::ParseVersion("v2.11butactually3.0") == "v2.11butactually3.0");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(comparepasswords_works)
|
||||
{
|
||||
BOOST_CHECK(Utility::ComparePasswords("", ""));
|
||||
|
|
Loading…
Reference in New Issue