Update test to have Windows paths on Windows

This commit is contained in:
Matthieu Gautier 2024-08-22 12:16:09 +02:00
parent b688aa294a
commit 5182a66b19
4 changed files with 73 additions and 9 deletions

View File

@ -15,11 +15,19 @@ struct XMLDoc : pugi::xml_document
} // unnamed namespace } // unnamed namespace
#if _WIN32
# define DATA_ABS_PATH "C:\\data\\zim"
# define ZARA_ABS_PATH "C:\\data\\zim\\zara.zim"
#else
# define DATA_ABS_PATH "/data/zim"
# define ZARA_ABS_PATH "/data/zim/zara.zim"
#endif
TEST(BookTest, updateFromXMLTest) TEST(BookTest, updateFromXMLTest)
{ {
const XMLDoc xml(R"( const XMLDoc xml(R"(
<book id="zara" <book id="zara"
path="./zara.zim" path="zara.zim"
url="https://who.org/zara.zim" url="https://who.org/zara.zim"
title="Catch an infection in 24 hours" title="Catch an infection in 24 hours"
description="Complete guide to contagious diseases" description="Complete guide to contagious diseases"
@ -40,9 +48,9 @@ TEST(BookTest, updateFromXMLTest)
)"); )");
kiwix::Book book; kiwix::Book book;
book.updateFromXml(xml.child("book"), "/data/zim"); book.updateFromXml(xml.child("book"), DATA_ABS_PATH);
EXPECT_EQ(book.getPath(), "/data/zim/zara.zim"); EXPECT_EQ(book.getPath(), ZARA_ABS_PATH);
EXPECT_EQ(book.getUrl(), "https://who.org/zara.zim"); EXPECT_EQ(book.getUrl(), "https://who.org/zara.zim");
EXPECT_EQ(book.getTitle(), "Catch an infection in 24 hours"); EXPECT_EQ(book.getTitle(), "Catch an infection in 24 hours");
EXPECT_EQ(book.getDescription(), "Complete guide to contagious diseases"); EXPECT_EQ(book.getDescription(), "Complete guide to contagious diseases");

View File

@ -267,11 +267,21 @@ const char * sampleOpdsStream = R"(
)"; )";
#ifdef _WIN32
# define ZIMFILE_PATH ".\\zimfile.zim"
# define EXAMPLE_PATH ".\\example.zim"
# define LIBRARY_PATH ".\\test\\library.xml"
#else
# define ZIMFILE_PATH "./zimfile.zim"
# define EXAMPLE_PATH "./example.zim"
# define LIBRARY_PATH "./test/library.xml"
#endif
const char sampleLibraryXML[] = R"( const char sampleLibraryXML[] = R"(
<library version="1.0"> <library version="1.0">
<book <book
id="raycharles" id="raycharles"
path="./zimfile.zim" path=")" ZIMFILE_PATH R"("
url="https://github.com/kiwix/libkiwix/raw/master/test/data/zimfile.zim" url="https://github.com/kiwix/libkiwix/raw/master/test/data/zimfile.zim"
title="Ray Charles" title="Ray Charles"
description="Wikipedia articles about Ray Charles" description="Wikipedia articles about Ray Charles"
@ -287,7 +297,7 @@ const char sampleLibraryXML[] = R"(
></book> ></book>
<book <book
id="example" id="example"
path="./example.zim" path=")" EXAMPLE_PATH R"("
title="An example ZIM archive" title="An example ZIM archive"
description="An eXaMpLe book added to the catalog via XML" description="An eXaMpLe book added to the catalog via XML"
language="deu" language="deu"
@ -383,7 +393,7 @@ class LibraryTest : public ::testing::Test {
void SetUp() override { void SetUp() override {
kiwix::Manager manager(lib); kiwix::Manager manager(lib);
manager.readOpds(sampleOpdsStream, "foo.urlHost"); manager.readOpds(sampleOpdsStream, "foo.urlHost");
manager.readXml(sampleLibraryXML, false, "./test/library.xml", true); manager.readXml(sampleLibraryXML, false, LIBRARY_PATH, true);
} }
kiwix::Bookmark createBookmark(const std::string &id, const std::string& url="", const std::string& title="") { kiwix::Bookmark createBookmark(const std::string &id, const std::string& url="", const std::string& title="") {

View File

@ -25,11 +25,23 @@ TEST(ManagerTest, addBookFromPathAndGetIdTest)
EXPECT_EQ(book.getUrl(), url); EXPECT_EQ(book.getUrl(), url);
} }
#if _WIN32
# define UNITTEST_ZIM_PATH "zimfiles\\unittest.zim"
# define LIB_ABS_PATH "C:\\data\\lib.xml"
# define ZIM_ABS_PATH "C:\\data\\zimfiles\\unittest.zim"
#else
# define UNITTEST_ZIM_PATH "zimfiles/unittest.zim"
# define LIB_ABS_PATH "/data/lib.xml"
# define ZIM_ABS_PATH "/data/zimfiles/unittest.zim"
#endif
const char sampleLibraryXML[] = R"( const char sampleLibraryXML[] = R"(
<library version="1.0"> <library version="1.0">
<book <book
id="0d0bcd57-d3f6-cb22-44cc-a723ccb4e1b2" id="0d0bcd57-d3f6-cb22-44cc-a723ccb4e1b2"
path="zimfiles/unittest.zim" path=")" UNITTEST_ZIM_PATH R"("
url="https://example.com/zimfiles/unittest.zim" url="https://example.com/zimfiles/unittest.zim"
title="Unit Test" title="Unit Test"
description="Wikipedia articles about unit testing" description="Wikipedia articles about unit testing"
@ -51,9 +63,9 @@ TEST(ManagerTest, readXml)
auto lib = kiwix::Library::create(); auto lib = kiwix::Library::create();
kiwix::Manager manager = kiwix::Manager(lib); kiwix::Manager manager = kiwix::Manager(lib);
EXPECT_EQ(true, manager.readXml(sampleLibraryXML, true, "/data/lib.xml", true)); EXPECT_EQ(true, manager.readXml(sampleLibraryXML, true, LIB_ABS_PATH, true));
kiwix::Book book = lib->getBookById("0d0bcd57-d3f6-cb22-44cc-a723ccb4e1b2"); kiwix::Book book = lib->getBookById("0d0bcd57-d3f6-cb22-44cc-a723ccb4e1b2");
EXPECT_EQ("/data/zimfiles/unittest.zim", book.getPath()); EXPECT_EQ(ZIM_ABS_PATH, book.getPath());
EXPECT_EQ("https://example.com/zimfiles/unittest.zim", book.getUrl()); EXPECT_EQ("https://example.com/zimfiles/unittest.zim", book.getUrl());
EXPECT_EQ("Unit Test", book.getTitle()); EXPECT_EQ("Unit Test", book.getTitle());
EXPECT_EQ("Wikipedia articles about unit testing", book.getDescription()); EXPECT_EQ("Wikipedia articles about unit testing", book.getDescription());

View File

@ -7,6 +7,23 @@
namespace namespace
{ {
#if _WIN32
const char libraryXML[] = R"(
<library version="1.0">
<book id="01" path="C:\data\zero_one.zim"> </book>
<book id="02" path="C:\data\zero two.zim"> </book>
<book id="03" path="C:\data\ZERO thrêë.zim"> </book>
<book id="04-2021-10" path="C:\data\zero_four_2021-10.zim"></book>
<book id="04-2021-11" path="C:\data\zero_four_2021-11.zim"></book>
<book id="05-a" path="C:\data\zero_five-a.zim" name="zero_five"></book>
<book id="05-b" path="C:\data\zero_five-b.zim" name="zero_five"></book>
<book id="06+" path="C:\data\zërô + SIX.zim"></book>
<book id="06plus" path="C:\data\zero_plus_six.zim"></book>
<book id="07-super" path="C:\data\zero_seven.zim"></book>
<book id="07-sub" path="C:\data\subdir\zero_seven.zim"></book>
</library>
)";
#else
const char libraryXML[] = R"( const char libraryXML[] = R"(
<library version="1.0"> <library version="1.0">
<book id="01" path="/data/zero_one.zim"> </book> <book id="01" path="/data/zero_one.zim"> </book>
@ -22,6 +39,7 @@ const char libraryXML[] = R"(
<book id="07-sub" path="/data/subdir/zero_seven.zim"></book> <book id="07-sub" path="/data/subdir/zero_seven.zim"></book>
</library> </library>
)"; )";
#endif
class NameMapperTest : public ::testing::Test { class NameMapperTest : public ::testing::Test {
public: public:
@ -61,7 +79,22 @@ public:
operator std::string() const { return buffer.str(); } operator std::string() const { return buffer.str(); }
}; };
#if _WIN32
const std::string ZERO_FOUR_NAME_CONFLICT_MSG =
"Path collision: 'C:\\data\\zero_four_2021-10.zim' and"
" 'C:\\data\\zero_four_2021-11.zim' can't share the same URL path 'zero_four'."
" Therefore, only 'C:\\data\\zero_four_2021-10.zim' will be served.\n";
const std::string ZERO_SIX_NAME_CONFLICT_MSG =
"Path collision: 'C:\\data\\zërô + SIX.zim' and "
"'C:\\data\\zero_plus_six.zim' can't share the same URL path 'zero_plus_six'."
" Therefore, only 'C:\\data\\zërô + SIX.zim' will be served.\n";
const std::string ZERO_SEVEN_NAME_CONFLICT_MSG =
"Path collision: 'C:\\data\\subdir\\zero_seven.zim' and"
" 'C:\\data\\zero_seven.zim' can't share the same URL path 'zero_seven'."
" Therefore, only 'C:\\data\\subdir\\zero_seven.zim' will be served.\n";
#else
const std::string ZERO_FOUR_NAME_CONFLICT_MSG = const std::string ZERO_FOUR_NAME_CONFLICT_MSG =
"Path collision: '/data/zero_four_2021-10.zim' and" "Path collision: '/data/zero_four_2021-10.zim' and"
" '/data/zero_four_2021-11.zim' can't share the same URL path 'zero_four'." " '/data/zero_four_2021-11.zim' can't share the same URL path 'zero_four'."
@ -76,6 +109,7 @@ const std::string ZERO_SEVEN_NAME_CONFLICT_MSG =
"Path collision: '/data/subdir/zero_seven.zim' and" "Path collision: '/data/subdir/zero_seven.zim' and"
" '/data/zero_seven.zim' can't share the same URL path 'zero_seven'." " '/data/zero_seven.zim' can't share the same URL path 'zero_seven'."
" Therefore, only '/data/subdir/zero_seven.zim' will be served.\n"; " Therefore, only '/data/subdir/zero_seven.zim' will be served.\n";
#endif
// Name conflicts in the default mode (without the --nodatealiases is off // Name conflicts in the default mode (without the --nodatealiases is off
const std::string DEFAULT_NAME_CONFLICTS = ZERO_SIX_NAME_CONFLICT_MSG const std::string DEFAULT_NAME_CONFLICTS = ZERO_SIX_NAME_CONFLICT_MSG