OgrePlugins.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 "common/OgrePlugins.h"
  20. namespace fs = boost::filesystem;
  21. namespace op = VGears::OgrePlugins;
  22. namespace{
  23. const std::string VERSION("1.12.10");
  24. struct Sandbox{
  25. fs::path root;
  26. Sandbox(): root(fs::temp_directory_path() / fs::unique_path("vgears-plugins-%%%%-%%%%")){
  27. fs::create_directories(root);
  28. }
  29. ~Sandbox(){
  30. boost::system::error_code ec;
  31. fs::remove_all(root, ec);
  32. }
  33. fs::path Write(const fs::path& file, const std::string& content = "x") const{
  34. fs::create_directories(file.parent_path());
  35. std::ofstream(file.string()) << content;
  36. return file;
  37. }
  38. /**
  39. * Creates the file of a plugin, with the name this system uses.
  40. */
  41. fs::path Plugin(const fs::path& directory, const std::string& name) const{
  42. return Write(directory / op::FileNames(name, VERSION).front());
  43. }
  44. };
  45. size_t IndexOf(const std::vector<fs::path>& list, const fs::path& item){
  46. return std::find(list.begin(), list.end(), item.lexically_normal()) - list.begin();
  47. }
  48. }
  49. BOOST_AUTO_TEST_SUITE(OgrePluginsTests)
  50. BOOST_AUTO_TEST_CASE(ReadConfigSkipsCommentsAndBlanks){
  51. Sandbox box;
  52. const fs::path file = box.Write(
  53. box.root / "plugins.cfg",
  54. "# Defines plugins to load\n\n# PluginFolder=/nowhere\n PluginFolder = plugins \r\n"
  55. "# Plugin=RenderSystem_Direct3D9\n Plugin=RenderSystem_GL\r\nPlugin=\nPlugin=Codec_STBI\n"
  56. );
  57. op::Config config;
  58. BOOST_REQUIRE(op::ReadConfig(file, config));
  59. BOOST_CHECK_EQUAL(config.folder, "plugins");
  60. BOOST_REQUIRE_EQUAL(config.plugins.size(), 2u);
  61. BOOST_CHECK_EQUAL(config.plugins[0], "RenderSystem_GL");
  62. BOOST_CHECK_EQUAL(config.plugins[1], "Codec_STBI");
  63. }
  64. BOOST_AUTO_TEST_CASE(ReadConfigFailsForMissingFile){
  65. Sandbox box;
  66. op::Config config;
  67. BOOST_CHECK(!op::ReadConfig(box.root / "missing.cfg", config));
  68. }
  69. BOOST_AUTO_TEST_CASE(TheRepositoryConfigListsThePlugins){
  70. op::Config config;
  71. BOOST_REQUIRE(op::ReadConfig(fs::path(VGEARS_SOURCE_DIR) / "data" / "plugins.cfg", config));
  72. BOOST_CHECK_EQUAL(config.folder, "");
  73. BOOST_CHECK(std::find(config.plugins.begin(), config.plugins.end(), "RenderSystem_GL") != config.plugins.end());
  74. BOOST_CHECK(std::find(config.plugins.begin(), config.plugins.end(), "Codec_STBI") != config.plugins.end());
  75. }
  76. BOOST_AUTO_TEST_CASE(SearchOrder){
  77. const fs::path data("/home/user/.v-gears");
  78. const fs::path exe("/opt/v-gears/bin");
  79. const std::vector<fs::path> dirs = op::SearchDirectories(data, "plugins", exe, "/build/ogre/plugins");
  80. const size_t folder = IndexOf(dirs, data / "plugins");
  81. const size_t config = IndexOf(dirs, data);
  82. const size_t executable = IndexOf(dirs, exe);
  83. const size_t bundle = IndexOf(dirs, "/opt/v-gears/lib/OGRE");
  84. const size_t build = IndexOf(dirs, "/build/ogre/plugins");
  85. BOOST_REQUIRE(build < dirs.size());
  86. BOOST_CHECK(folder < config);
  87. BOOST_CHECK(config < executable);
  88. BOOST_CHECK(executable < bundle);
  89. BOOST_CHECK(bundle < build);
  90. }
  91. BOOST_AUTO_TEST_CASE(SearchDirectoriesHaveNoDuplicates){
  92. const fs::path data("/home/user/.v-gears");
  93. const std::vector<fs::path> dirs = op::SearchDirectories(data, ".", data, data.string());
  94. BOOST_CHECK_EQUAL(std::count(dirs.begin(), dirs.end(), data), 1);
  95. }
  96. BOOST_AUTO_TEST_CASE(FindPrefersTheFirstDirectory){
  97. Sandbox box;
  98. const fs::path first = box.root / "first";
  99. const fs::path second = box.root / "second";
  100. box.Plugin(second, "RenderSystem_GL");
  101. BOOST_CHECK_EQUAL(op::Find("RenderSystem_GL", {first, second}, VERSION), box.Plugin(second, "RenderSystem_GL"));
  102. const fs::path in_first = box.Plugin(first, "RenderSystem_GL");
  103. BOOST_CHECK_EQUAL(op::Find("RenderSystem_GL", {first, second}, VERSION), in_first);
  104. BOOST_CHECK(op::Find("Codec_STBI", {first, second}, VERSION).empty());
  105. }
  106. #if !defined(_WIN32) && !defined(__APPLE__)
  107. BOOST_AUTO_TEST_CASE(FindIgnoresPluginsOfOtherOgreVersions){
  108. Sandbox box;
  109. box.Write(box.root / "RenderSystem_GL.so.1.12.9");
  110. box.Write(box.root / "RenderSystem_GL.so");
  111. BOOST_CHECK(op::Find("RenderSystem_GL", {box.root}, VERSION).empty());
  112. box.Write(box.root / "RenderSystem_GL.so.1.12.10");
  113. BOOST_CHECK_EQUAL(op::Find("RenderSystem_GL", {box.root}, VERSION), box.root / "RenderSystem_GL.so.1.12.10");
  114. }
  115. #endif
  116. BOOST_AUTO_TEST_CASE(FindAcceptsAbsolutePaths){
  117. Sandbox box;
  118. const fs::path file = box.Write(box.root / "custom" / "MyPlugin.so");
  119. BOOST_CHECK_EQUAL(op::Find(file.string(), {}, VERSION), file);
  120. BOOST_CHECK(op::Find((box.root / "nothing.so").string(), {}, VERSION).empty());
  121. }
  122. BOOST_AUTO_TEST_SUITE_END()