commit 662c3af003784c3e8baedfe6b075c3d91039a219
parent 1985eca6237fb0ff90a8a0027773b51b8530dffd
Author: cfillion <cfillion@users.noreply.github.com>
Date: Thu, 26 Nov 2015 23:07:35 -0500
support multiple source url for each package version and platforms
Diffstat:
7 files changed, 291 insertions(+), 6 deletions(-)
diff --git a/src/database_v1.cpp b/src/database_v1.cpp
@@ -75,5 +75,19 @@ VersionPtr LoadVersionV1(TiXmlElement *verNode)
VersionPtr ver = make_shared<Version>(name);
+ TiXmlElement *node = verNode->FirstChildElement("source");
+
+ while(node) {
+ const char *platform = node->Attribute("platform");
+ if(!platform) platform = "all";
+
+ const char *url = node->GetText();
+ if(!url) url = "";
+
+ ver->addSource(make_shared<Source>(Source::convertPlatform(platform), url));
+
+ node = node->NextSiblingElement("source");
+ }
+
return ver;
}
diff --git a/src/version.cpp b/src/version.cpp
@@ -31,7 +31,65 @@ Version::Version(const std::string &str)
}
}
+void Version::addSource(SourcePtr source)
+{
+ const Source::Platform p = source->platform();
+
+#ifdef __APPLE__
+ if(p != Source::GenericPlatform && p < Source::DarwinPlatform)
+ return;
+
+#ifdef __x86_64__
+ if(p == Source::Darwin32Platform)
+ return;
+#else
+ if(p == Source::Darwin64Platform)
+ return;
+#endif
+
+#elif _WIN32
+ if(p == Source::UnknownPlatform || p > Source::Win64Platform)
+ return;
+
+#ifdef _WIN64
+ if(p == Source::Win32Platform)
+ return;
+#else
+ if(p == Source::Win64Platform)
+ return;
+#endif
+#endif
+ m_sources.push_back(source);
+}
+
bool Version::operator<(const Version &o)
{
return m_code < o.code();
}
+
+Source::Platform Source::convertPlatform(const char *platform)
+{
+ if(!strcmp(platform, "all"))
+ return GenericPlatform;
+ else if(!strcmp(platform, "windows"))
+ return WindowsPlatform;
+ else if(!strcmp(platform, "win32"))
+ return Win32Platform;
+ else if(!strcmp(platform, "win64"))
+ return Win64Platform;
+ else if(!strcmp(platform, "darwin"))
+ return DarwinPlatform;
+ else if(!strcmp(platform, "darwin32"))
+ return Darwin32Platform;
+ else if(!strcmp(platform, "darwin64"))
+ return Darwin64Platform;
+ else
+ return UnknownPlatform;
+}
+
+Source::Source(const Platform platform, const std::string &url)
+ : m_platform(platform), m_url(url)
+{
+ if(m_url.empty())
+ throw reapack_error("empty source url");
+}
diff --git a/src/version.hpp b/src/version.hpp
@@ -3,10 +3,14 @@
#include <set>
#include <string>
+#include <vector>
class Version;
typedef std::shared_ptr<Version> VersionPtr;
+class Source;
+typedef std::shared_ptr<Source> SourcePtr;
+
class Version {
public:
Version(const std::string &);
@@ -14,11 +18,16 @@ public:
const std::string &name() const { return m_name; }
int code() const { return m_code; }
+ void addSource(SourcePtr source);
+ const std::vector<SourcePtr> &sources() const { return m_sources; }
+ SourcePtr source(const int i) const { return m_sources[i]; }
+
bool operator<(const Version &);
private:
std::string m_name;
int m_code;
+ std::vector<SourcePtr> m_sources;
};
class VersionCompare {
@@ -31,4 +40,33 @@ public:
typedef std::set<VersionPtr, VersionCompare> VersionSet;
+class Source {
+public:
+ enum Platform {
+ UnknownPlatform,
+ GenericPlatform,
+
+ // windows
+ WindowsPlatform,
+ Win32Platform,
+ Win64Platform,
+
+ // os x
+ DarwinPlatform,
+ Darwin32Platform,
+ Darwin64Platform,
+ };
+
+ static Platform convertPlatform(const char *);
+
+ Source(const Platform, const std::string &source);
+
+ Platform platform() const { return m_platform; }
+ const std::string &url() const { return m_url; }
+
+private:
+ Platform m_platform;
+ std::string m_url;
+};
+
#endif
diff --git a/test/database_v1.cpp b/test/database_v1.cpp
@@ -85,3 +85,19 @@ TEST_CASE("null package version", M) {
}
}
+TEST_CASE("null source url", M) {
+ try {
+ Database::load(DBPATH "missing_source_url.xml");
+ FAIL();
+ }
+ catch(const reapack_error &e) {
+ REQUIRE(string(e.what()) == "empty source url");
+ }
+}
+
+TEST_CASE("default platform", M) {
+ DatabasePtr db = Database::load(DBPATH "missing_platform.xml");
+
+ REQUIRE(db->category(0)->package(0)->version(0)->source(0)->platform()
+ == Source::GenericPlatform);
+}
diff --git a/test/db/v1/missing_platform.xml b/test/db/v1/missing_platform.xml
@@ -0,0 +1,9 @@
+<index version="1">
+ <category name="hello">
+ <reapack name="a" type="script" author="abc">
+ <version name="1">
+ <source>hello</source>
+ </version>
+ </reapack>
+ </category>
+</index>
diff --git a/test/db/v1/missing_source_url.xml b/test/db/v1/missing_source_url.xml
@@ -0,0 +1,9 @@
+<index version="1">
+ <category name="hello">
+ <reapack name="a" type="script" author="abc">
+ <version name="1">
+ <source platform="all" />
+ </version>
+ </reapack>
+ </category>
+</index>
diff --git a/test/version.cpp b/test/version.cpp
@@ -8,7 +8,8 @@
using namespace std;
static const char *M = "[database]";
-TEST_CASE("invalid") {
+
+TEST_CASE("invalid", M) {
try {
Version ver("hello");
FAIL();
@@ -18,32 +19,172 @@ TEST_CASE("invalid") {
}
}
-TEST_CASE("major minor patch") {
+TEST_CASE("major minor patch version", M) {
Version ver("1.2.3");
REQUIRE(ver.name() == "1.2.3");
REQUIRE(ver.code() == 1002003);
}
-TEST_CASE("major minor") {
+TEST_CASE("major minor version", M) {
Version ver("1.2");
REQUIRE(ver.name() == "1.2");
REQUIRE(ver.code() == 1002000);
}
-TEST_CASE("major") {
+TEST_CASE("major version", M) {
Version ver("1");
REQUIRE(ver.name() == "1");
REQUIRE(ver.code() == 1000000);
}
-TEST_CASE("string suffix") {
+TEST_CASE("version with string suffix", M) {
Version ver("1.2pre3");
REQUIRE(ver.name() == "1.2pre3");
REQUIRE(ver.code() == 1002003);
}
-TEST_CASE("extra integer") {
+TEST_CASE("version with extra integer", M) {
Version ver("1.2.3.4");
REQUIRE(ver.name() == "1.2.3.4");
REQUIRE(ver.code() == 1002003004);
}
+
+TEST_CASE("convert platforms", M) {
+ SECTION("unknown") {
+ REQUIRE(Source::convertPlatform("hello") == Source::UnknownPlatform);
+ }
+
+ SECTION("generic") {
+ REQUIRE(Source::convertPlatform("all") == Source::GenericPlatform);
+ }
+
+ SECTION("generic windows") {
+ REQUIRE(Source::convertPlatform("windows") == Source::WindowsPlatform);
+ }
+
+ SECTION("windows 32-bit") {
+ REQUIRE(Source::convertPlatform("win32") == Source::Win32Platform);
+ }
+
+ SECTION("windows 64-bit") {
+ REQUIRE(Source::convertPlatform("win64") == Source::Win64Platform);
+ }
+
+ SECTION("generic os x") {
+ REQUIRE(Source::convertPlatform("darwin") == Source::DarwinPlatform);
+ }
+
+ SECTION("os x 32-bit") {
+ REQUIRE(Source::convertPlatform("darwin32") == Source::Darwin32Platform);
+ }
+
+ SECTION("os x 64-bit") {
+ REQUIRE(Source::convertPlatform("darwin64") == Source::Darwin64Platform);
+ }
+}
+
+TEST_CASE("empty source url", M) {
+ try {
+ Source source(Source::UnknownPlatform, string());
+ FAIL();
+ }
+ catch(const reapack_error &e) {
+ REQUIRE(string(e.what()) == "empty source url");
+ }
+}
+
+TEST_CASE("drow sources for unknown platforms") {
+ Version ver("1");
+ ver.addSource(make_shared<Source>(Source::UnknownPlatform, "a"));
+
+ REQUIRE(ver.sources().size() == 0);
+}
+
+#ifdef __APPLE__
+TEST_CASE("drop windows sources on os x", M) {
+ Version ver("1");
+ ver.addSource(make_shared<Source>(Source::WindowsPlatform, "a"));
+ ver.addSource(make_shared<Source>(Source::Win32Platform, "a"));
+ ver.addSource(make_shared<Source>(Source::Win64Platform, "a"));
+
+ REQUIRE(ver.sources().size() == 0);
+}
+
+#ifdef __x86_64__
+TEST_CASE("drop 32-bit sources on os x 64-bit", M) {
+ Version ver("1");
+ ver.addSource(make_shared<Source>(Source::Darwin32Platform, "a"));
+
+ REQUIRE(ver.sources().size() == 0);
+}
+
+TEST_CASE("valid sources for os x 64-bit", M) {
+ Version ver("1");
+ ver.addSource(make_shared<Source>(Source::GenericPlatform, "a"));
+ ver.addSource(make_shared<Source>(Source::DarwinPlatform, "a"));
+ ver.addSource(make_shared<Source>(Source::Darwin64Platform, "a"));
+
+ REQUIRE(ver.sources().size() == 3);
+}
+#else
+TEST_CASE("drop 64-bit sources on os x 32-bit", M) {
+ Version ver("1");
+ ver.addSource(make_shared<Source>(Source::Darwin64Platform, "a"));
+
+ REQUIRE(ver.sources().size() == 0);
+}
+
+TEST_CASE("valid sources for os x 32-bit", M) {
+ Version ver("1");
+ ver.addSource(make_shared<Source>(Source::GenericPlatform, "a"));
+ ver.addSource(make_shared<Source>(Source::DarwinPlatform, "a"));
+ ver.addSource(make_shared<Source>(Source::Darwin32Platform, "a"));
+
+ REQUIRE(ver.sources().size() == 3);
+}
+#endif
+
+#elif _WIN32
+TEST_CASE("drop os x sources on windows", M) {
+ Version ver("1");
+ ver.addSource(make_shared<Source>(Source::DarwinPlatform, "a"));
+ ver.addSource(make_shared<Source>(Source::Darwin32Platform, "a"));
+ ver.addSource(make_shared<Source>(Source::Darwin64Platform, "a"));
+
+ REQUIRE(ver.sources().size() == 0);
+}
+
+#ifdef _WIN64
+TEST_CASE("drop 32-bit sources on windows 64-bit", M) {
+ Version ver("1");
+ ver.addSource(make_shared<Source>(Source::Win32Platform, "a"));
+
+ REQUIRE(ver.sources().size() == 0);
+}
+
+TEST_CASE("valid sources for windows 64-bit", M) {
+ Version ver("1");
+ ver.addSource(make_shared<Source>(Source::GenericPlatform, "a"));
+ ver.addSource(make_shared<Source>(Source::WindowsPlatform, "a"));
+ ver.addSource(make_shared<Source>(Source::Win64Platform, "a"));
+
+ REQUIRE(ver.sources().size() == 3);
+}
+#else
+TEST_CASE("drop 64-bit sources on windows 32-bit", M) {
+ Version ver("1");
+ ver.addSource(make_shared<Source>(Source::Win64Platform, "a"));
+
+ REQUIRE(ver.sources().size() == 0);
+}
+
+TEST_CASE("valid sources for windows 32-bit", M) {
+ Version ver("1");
+ ver.addSource(make_shared<Source>(Source::GenericPlatform, "a"));
+ ver.addSource(make_shared<Source>(Source::WindowsPlatform, "a"));
+ ver.addSource(make_shared<Source>(Source::Win32Platform, "a"));
+
+ REQUIRE(ver.sources().size() == 3);
+}
+#endif
+#endif