DataDirectory.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <boost/test/unit_test.hpp>
  16. #include <boost/filesystem.hpp>
  17. #include <algorithm>
  18. #include <fstream>
  19. #include <sstream>
  20. #include "common/DataDirectory.h"
  21. namespace fs = boost::filesystem;
  22. namespace dd = VGears::DataDirectory;
  23. namespace{
  24. /**
  25. * A temporary directory that is removed on destruction.
  26. */
  27. struct Sandbox{
  28. fs::path root;
  29. Sandbox(): root(fs::temp_directory_path() / fs::unique_path("vgears-datadir-%%%%-%%%%")){
  30. fs::create_directories(root);
  31. }
  32. ~Sandbox(){
  33. boost::system::error_code ec;
  34. fs::remove_all(root, ec);
  35. }
  36. void Write(const fs::path& file, const std::string& content) const{
  37. fs::create_directories(file.parent_path());
  38. std::ofstream(file.string()) << content;
  39. }
  40. std::string Read(const fs::path& file) const{
  41. std::ifstream in(file.string());
  42. std::stringstream text;
  43. text << in.rdbuf();
  44. return text.str();
  45. }
  46. /**
  47. * Creates a template with the V-Gears files under root/name.
  48. */
  49. fs::path MakeTemplate(const std::string& name) const{
  50. const fs::path dir = root / name;
  51. Write(dir / "plugins.cfg", "template plugins");
  52. Write(dir / "resources.cfg", "template resources");
  53. Write(dir / "defaults" / "config.cfg", "template config");
  54. Write(dir / "engine" / "scripts" / "menu" / "begin.lua", "template script");
  55. return dir;
  56. }
  57. };
  58. }
  59. BOOST_AUTO_TEST_SUITE(DataDirectoryTests)
  60. BOOST_AUTO_TEST_CASE(IsDataDirectoryNeedsPluginsAndData){
  61. Sandbox box;
  62. BOOST_CHECK(!dd::IsDataDirectory(box.root));
  63. box.Write(box.root / "plugins.cfg", "");
  64. BOOST_CHECK(!dd::IsDataDirectory(box.root));
  65. fs::create_directories(box.root / "engine");
  66. BOOST_CHECK(dd::IsDataDirectory(box.root));
  67. BOOST_CHECK(!dd::IsDataDirectory(box.root / "missing"));
  68. }
  69. BOOST_AUTO_TEST_CASE(RequestedDirectoryWins){
  70. Sandbox box;
  71. const fs::path current = box.MakeTemplate("current");
  72. const fs::path requested = box.MakeTemplate("requested");
  73. const dd::Resolution r = dd::Resolve(requested.string(), current, box.root / "default", {});
  74. BOOST_CHECK_EQUAL(r.error, "");
  75. BOOST_CHECK_EQUAL(r.directory, requested);
  76. BOOST_CHECK(!r.seeded);
  77. }
  78. BOOST_AUTO_TEST_CASE(RelativeRequestIsRelativeToCurrent){
  79. Sandbox box;
  80. const fs::path requested = box.MakeTemplate("requested");
  81. const dd::Resolution r = dd::Resolve("requested", box.root, box.root / "default", {});
  82. BOOST_CHECK_EQUAL(r.error, "");
  83. BOOST_CHECK_EQUAL(r.directory, requested);
  84. }
  85. BOOST_AUTO_TEST_CASE(CurrentDirectoryIsUsedIfItHasTheFiles){
  86. Sandbox box;
  87. const fs::path current = box.MakeTemplate("current");
  88. const dd::Resolution r = dd::Resolve("", current, box.root / "default", {box.MakeTemplate("template")});
  89. BOOST_CHECK_EQUAL(r.error, "");
  90. BOOST_CHECK_EQUAL(r.directory, current);
  91. BOOST_CHECK(!r.seeded);
  92. BOOST_CHECK(!fs::exists(box.root / "default"));
  93. }
  94. BOOST_AUTO_TEST_CASE(DefaultDirectoryIsSeededFromTheFirstValidTemplate){
  95. Sandbox box;
  96. const fs::path not_a_template = box.root / "empty";
  97. fs::create_directories(not_a_template);
  98. const fs::path source = box.MakeTemplate("template");
  99. const fs::path second = box.MakeTemplate("second");
  100. box.Write(second / "plugins.cfg", "second plugins");
  101. const fs::path target = box.root / "home" / ".v-gears";
  102. const dd::Resolution r = dd::Resolve("", box.root / "elsewhere", target, {not_a_template, source, second});
  103. BOOST_CHECK_EQUAL(r.error, "");
  104. BOOST_CHECK_EQUAL(r.directory, target);
  105. BOOST_CHECK(r.seeded);
  106. BOOST_CHECK_EQUAL(r.template_directory, source);
  107. BOOST_CHECK_EQUAL(r.copied_files, 4u);
  108. BOOST_CHECK(dd::IsDataDirectory(target));
  109. BOOST_CHECK_EQUAL(box.Read(target / "plugins.cfg"), "template plugins");
  110. BOOST_CHECK_EQUAL(box.Read(target / "engine" / "scripts" / "menu" / "begin.lua"), "template script");
  111. }
  112. BOOST_AUTO_TEST_CASE(SeedingKeepsExistingFiles){
  113. Sandbox box;
  114. const fs::path source = box.MakeTemplate("template");
  115. // An installation where the installer extracted the game data but the configuration was not copied.
  116. const fs::path target = box.root / "install";
  117. box.Write(target / "user" / "config.cfg", "user config");
  118. box.Write(target / "game" / "fields" / "md1_1.xml", "game data");
  119. const dd::Resolution r = dd::Resolve(target.string(), box.root, box.root / "default", {source});
  120. BOOST_CHECK_EQUAL(r.error, "");
  121. BOOST_CHECK(r.seeded);
  122. BOOST_CHECK_EQUAL(r.copied_files, 4u);
  123. BOOST_CHECK_EQUAL(box.Read(target / "user" / "config.cfg"), "user config");
  124. BOOST_CHECK_EQUAL(box.Read(target / "game" / "fields" / "md1_1.xml"), "game data");
  125. BOOST_CHECK_EQUAL(box.Read(target / "plugins.cfg"), "template plugins");
  126. }
  127. BOOST_AUTO_TEST_CASE(NewInstallationGetsTheConfigurationAndTheDirectoriesOfThePlayer){
  128. Sandbox box;
  129. const fs::path source = box.MakeTemplate("template");
  130. // The template can have player files and logs if the game was run from it: they are not copied.
  131. box.Write(source / "user" / "saves" / "0.xml", "template save");
  132. box.Write(source / "logs" / "v-gears.log", "template log");
  133. const fs::path target = box.root / ".v-gears";
  134. const dd::Resolution r = dd::Resolve("", box.root / "elsewhere", target, {source});
  135. BOOST_CHECK_EQUAL(r.error, "");
  136. BOOST_CHECK_EQUAL(box.Read(target / "user" / "config.cfg"), "template config");
  137. BOOST_CHECK(fs::is_directory(target / "user" / "screenshots"));
  138. BOOST_CHECK(fs::is_directory(target / "logs"));
  139. BOOST_CHECK(!fs::exists(target / "user" / "saves" / "0.xml"));
  140. BOOST_CHECK(!fs::exists(target / "logs" / "v-gears.log"));
  141. }
  142. BOOST_AUTO_TEST_CASE(OldInstallationIsMigrated){
  143. Sandbox box;
  144. const fs::path source = box.MakeTemplate("template");
  145. const fs::path target = box.root / "old";
  146. box.Write(target / "plugins.cfg", "old plugins");
  147. box.Write(target / "data" / "config" / "config.cfg", "old config");
  148. box.Write(target / "data" / "video" / "opening" / "movie.xml", "movie");
  149. box.Write(target / "save" / "0.xml", "save");
  150. box.Write(target / "controls.cfg", "controls");
  151. box.Write(target / "console_history.txt", "history");
  152. box.Write(target / "screenshot_1.png", "image");
  153. const dd::Resolution r = dd::Resolve(target.string(), box.root, box.root / "default", {source});
  154. BOOST_CHECK_EQUAL(r.error, "");
  155. BOOST_CHECK_EQUAL(r.migrated_files, 6u);
  156. BOOST_CHECK(r.seeded);
  157. BOOST_CHECK_EQUAL(box.Read(target / "user" / "config.cfg"), "old config");
  158. BOOST_CHECK_EQUAL(box.Read(target / "user" / "saves" / "0.xml"), "save");
  159. BOOST_CHECK_EQUAL(box.Read(target / "user" / "controls.cfg"), "controls");
  160. BOOST_CHECK_EQUAL(box.Read(target / "user" / "console_history.txt"), "history");
  161. BOOST_CHECK_EQUAL(box.Read(target / "user" / "screenshots" / "screenshot_1.png"), "image");
  162. BOOST_CHECK_EQUAL(box.Read(target / "game" / "video" / "opening" / "movie.xml"), "movie");
  163. BOOST_CHECK(!fs::exists(target / "save"));
  164. BOOST_CHECK_EQUAL(box.Read(target / "plugins.cfg"), "old plugins");
  165. }
  166. BOOST_AUTO_TEST_CASE(MigrationNeverOverwrites){
  167. Sandbox box;
  168. const fs::path target = box.root / "install";
  169. box.Write(target / "plugins.cfg", "plugins");
  170. box.Write(target / "save" / "0.xml", "old save");
  171. box.Write(target / "save" / "1.xml", "other save");
  172. box.Write(target / "user" / "saves" / "0.xml", "new save");
  173. BOOST_CHECK_EQUAL(dd::MigrateLayout(target), 1u);
  174. BOOST_CHECK_EQUAL(box.Read(target / "user" / "saves" / "0.xml"), "new save");
  175. BOOST_CHECK_EQUAL(box.Read(target / "user" / "saves" / "1.xml"), "other save");
  176. BOOST_CHECK_EQUAL(box.Read(target / "save" / "0.xml"), "old save");
  177. }
  178. BOOST_AUTO_TEST_CASE(MigrationIgnoresOtherDirectories){
  179. Sandbox box;
  180. box.Write(box.root / "documents" / "save" / "0.xml", "not ours");
  181. BOOST_CHECK_EQUAL(dd::MigrateLayout(box.root / "documents"), 0u);
  182. BOOST_CHECK(fs::exists(box.root / "documents" / "save" / "0.xml"));
  183. }
  184. BOOST_AUTO_TEST_CASE(UnrelatedNonEmptyDirectoryIsNotFilled){
  185. Sandbox box;
  186. const fs::path source = box.MakeTemplate("template");
  187. const fs::path target = box.root / "documents";
  188. box.Write(target / "letter.txt", "hello");
  189. const dd::Resolution r = dd::Resolve(target.string(), box.root, box.root / "default", {source});
  190. BOOST_CHECK(!r.error.empty());
  191. BOOST_CHECK(r.directory.empty());
  192. BOOST_CHECK(!fs::exists(target / "plugins.cfg"));
  193. }
  194. BOOST_AUTO_TEST_CASE(MissingTemplateIsAnError){
  195. Sandbox box;
  196. const dd::Resolution r = dd::Resolve("", box.root, box.root / "default", {box.root / "nowhere"});
  197. BOOST_CHECK(!r.error.empty());
  198. BOOST_CHECK(r.directory.empty());
  199. }
  200. BOOST_AUTO_TEST_CASE(EmptyDefaultIsAnError){
  201. Sandbox box;
  202. const dd::Resolution r = dd::Resolve("", box.root, fs::path(), {box.MakeTemplate("template")});
  203. BOOST_CHECK(!r.error.empty());
  204. }
  205. BOOST_AUTO_TEST_CASE(TemplateIsNeverTheTargetItself){
  206. Sandbox box;
  207. const fs::path source = box.MakeTemplate("template");
  208. BOOST_CHECK(dd::FindTemplate({source}, source).empty());
  209. BOOST_CHECK_EQUAL(dd::FindTemplate({source}, box.root / "other"), source);
  210. }
  211. BOOST_AUTO_TEST_CASE(TemplateCandidatesCoverBuildTreeAndPackage){
  212. const fs::path exe("/opt/repo/build/bin");
  213. const std::vector<fs::path> c = dd::TemplateCandidates(exe);
  214. auto has = [&](const fs::path& p){return std::find(c.begin(), c.end(), p) != c.end();};
  215. BOOST_CHECK(has("/opt/repo/build/bin/data"));
  216. BOOST_CHECK(has("/opt/repo/data"));
  217. BOOST_CHECK(has("/opt/repo/build/share/v-gears"));
  218. const std::vector<fs::path> installed = dd::TemplateCandidates("/usr/games");
  219. BOOST_CHECK(std::find(installed.begin(), installed.end(), fs::path("/usr/share/v-gears")) != installed.end());
  220. }
  221. BOOST_AUTO_TEST_SUITE_END()