2
0

pkgmgr_spec.lua 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. --Minetest
  2. --Copyright (C) 2022 rubenwardy
  3. --
  4. --This program is free software; you can redistribute it and/or modify
  5. --it under the terms of the GNU Lesser General Public License as published by
  6. --the Free Software Foundation; either version 2.1 of the License, or
  7. --(at your option) any later version.
  8. --
  9. --This program is distributed in the hope that it will be useful,
  10. --but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. --MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. --GNU Lesser General Public License for more details.
  13. --
  14. --You should have received a copy of the GNU Lesser General Public License along
  15. --with this program; if not, write to the Free Software Foundation, Inc.,
  16. --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. local mods_dir = "/tmp/.minetest/mods"
  18. local games_dir = "/tmp/.minetest/games"
  19. local txp_dir = "/tmp/.minetest/textures"
  20. local function reset()
  21. local env = {
  22. core = {},
  23. unpack = table.unpack or unpack,
  24. pkgmgr = {},
  25. DIR_DELIM = "/",
  26. }
  27. env._G = env
  28. setmetatable(env, { __index = _G })
  29. local core = env.core
  30. local calls = {}
  31. function core.get_games()
  32. return {}
  33. end
  34. function core.delete_dir(...)
  35. table.insert(calls, { "delete_dir", ... })
  36. return true
  37. end
  38. function core.copy_dir(...)
  39. table.insert(calls, { "copy_dir", ... })
  40. return true
  41. end
  42. function core.get_texturepath()
  43. return txp_dir
  44. end
  45. function core.get_modpath()
  46. return mods_dir
  47. end
  48. function core.get_gamepath()
  49. return games_dir
  50. end
  51. function env.fgettext(fmt, ...)
  52. return fmt
  53. end
  54. setfenv(loadfile("builtin/common/misc_helpers.lua"), env)()
  55. setfenv(loadfile("builtin/mainmenu/pkgmgr.lua"), env)()
  56. function env.pkgmgr.update_gamelist()
  57. table.insert(calls, { "update_gamelist" })
  58. end
  59. function env.pkgmgr.refresh_globals()
  60. table.insert(calls, { "refresh_globals" })
  61. end
  62. function env.assert_calls(list)
  63. assert.are.same(list, calls)
  64. end
  65. return env
  66. end
  67. describe("install_dir", function()
  68. it("installs texture pack", function()
  69. local env = reset()
  70. env.pkgmgr.get_base_folder = function()
  71. return { type = "invalid", path = "/tmp/123" }
  72. end
  73. local path, message = env.pkgmgr.install_dir("txp", "/tmp/123", "mytxp", nil)
  74. assert.is.equal(txp_dir .. "/mytxp", path)
  75. assert.is._nil(message)
  76. env.assert_calls({
  77. { "delete_dir", txp_dir .. "/mytxp" },
  78. { "copy_dir", "/tmp/123", txp_dir .. "/mytxp", false },
  79. })
  80. end)
  81. it("prevents installing other as texture pack", function()
  82. local env = reset()
  83. env.pkgmgr.get_base_folder = function()
  84. return { type = "mod", path = "/tmp/123" }
  85. end
  86. local path, message = env.pkgmgr.install_dir("txp", "/tmp/123", "mytxp", nil)
  87. assert.is._nil(path)
  88. assert.is.equal("Unable to install a $1 as a texture pack", message)
  89. end)
  90. it("installs mod", function()
  91. local env = reset()
  92. env.pkgmgr.get_base_folder = function()
  93. return { type = "mod", path = "/tmp/123" }
  94. end
  95. local path, message = env.pkgmgr.install_dir("mod", "/tmp/123", "mymod", nil)
  96. assert.is.equal(mods_dir .. "/mymod", path)
  97. assert.is._nil(message)
  98. env.assert_calls({
  99. { "delete_dir", mods_dir .. "/mymod" },
  100. { "copy_dir", "/tmp/123", mods_dir .. "/mymod", false },
  101. { "refresh_globals" },
  102. })
  103. end)
  104. it("installs modpack", function()
  105. local env = reset()
  106. env.pkgmgr.get_base_folder = function()
  107. return { type = "modpack", path = "/tmp/123" }
  108. end
  109. local path, message = env.pkgmgr.install_dir("mod", "/tmp/123", "mymod", nil)
  110. assert.is.equal(mods_dir .. "/mymod", path)
  111. assert.is._nil(message)
  112. env.assert_calls({
  113. { "delete_dir", mods_dir .. "/mymod" },
  114. { "copy_dir", "/tmp/123", mods_dir .. "/mymod", false },
  115. { "refresh_globals" },
  116. })
  117. end)
  118. it("installs game", function()
  119. local env = reset()
  120. env.pkgmgr.get_base_folder = function()
  121. return { type = "game", path = "/tmp/123" }
  122. end
  123. local path, message = env.pkgmgr.install_dir("game", "/tmp/123", "mygame", nil)
  124. assert.is.equal(games_dir .. "/mygame", path)
  125. assert.is._nil(message)
  126. env.assert_calls({
  127. { "delete_dir", games_dir .. "/mygame" },
  128. { "copy_dir", "/tmp/123", games_dir .. "/mygame", false },
  129. { "update_gamelist" },
  130. })
  131. end)
  132. it("installs mod detects name", function()
  133. local env = reset()
  134. env.pkgmgr.get_base_folder = function()
  135. return { type = "mod", path = "/tmp/123" }
  136. end
  137. local path, message = env.pkgmgr.install_dir("mod", "/tmp/123", nil, nil)
  138. assert.is.equal(mods_dir .. "/123", path)
  139. assert.is._nil(message)
  140. env.assert_calls({
  141. { "delete_dir", mods_dir .. "/123" },
  142. { "copy_dir", "/tmp/123", mods_dir .. "/123", false },
  143. { "refresh_globals" },
  144. })
  145. end)
  146. it("installs mod detects invalid name", function()
  147. local env = reset()
  148. env.pkgmgr.get_base_folder = function()
  149. return { type = "mod", path = "/tmp/hi!" }
  150. end
  151. local path, message = env.pkgmgr.install_dir("mod", "/tmp/hi!", nil, nil)
  152. assert.is._nil(path)
  153. assert.is.equal("Install: Unable to find suitable folder name for $1", message)
  154. end)
  155. it("installs mod to target (update)", function()
  156. local env = reset()
  157. env.pkgmgr.get_base_folder = function()
  158. return { type = "mod", path = "/tmp/123" }
  159. end
  160. local path, message = env.pkgmgr.install_dir("mod", "/tmp/123", "mymod", "/tmp/alt-target")
  161. assert.is.equal("/tmp/alt-target", path)
  162. assert.is._nil(message)
  163. env.assert_calls({
  164. { "delete_dir", "/tmp/alt-target" },
  165. { "copy_dir", "/tmp/123", "/tmp/alt-target", false },
  166. { "refresh_globals" },
  167. })
  168. end)
  169. it("checks expected types", function()
  170. local actual_type = "modpack"
  171. local env = reset()
  172. env.pkgmgr.get_base_folder = function()
  173. return { type = actual_type, path = "/tmp/123" }
  174. end
  175. local path, message = env.pkgmgr.install_dir("mod", "/tmp/123", "name", nil)
  176. assert.is._not._nil(path)
  177. assert.is._nil(message)
  178. path, message = env.pkgmgr.install_dir("game", "/tmp/123", "name", nil)
  179. assert.is._nil(path)
  180. assert.is.equal("Unable to install a $1 as a $2", message)
  181. path, message = env.pkgmgr.install_dir("txp", "/tmp/123", "name", nil)
  182. assert.is._nil(path)
  183. assert.is.equal("Unable to install a $1 as a texture pack", message)
  184. actual_type = "game"
  185. path, message = env.pkgmgr.install_dir("mod", "/tmp/123", "name", nil)
  186. assert.is._nil(path)
  187. assert.is.equal("Unable to install a $1 as a $2", message)
  188. path, message = env.pkgmgr.install_dir("game", "/tmp/123", "name", nil)
  189. assert.is._not._nil(path)
  190. assert.is._nil(message)
  191. path, message = env.pkgmgr.install_dir("txp", "/tmp/123", "name", nil)
  192. assert.is._nil(path)
  193. assert.is.equal("Unable to install a $1 as a texture pack", message)
  194. actual_type = "txp"
  195. path, message = env.pkgmgr.install_dir("mod", "/tmp/123", "name", nil)
  196. assert.is._nil(path)
  197. assert.is.equal("Unable to install a $1 as a $2", message)
  198. path, message = env.pkgmgr.install_dir("game", "/tmp/123", "name", nil)
  199. assert.is._nil(path)
  200. assert.is.equal("Unable to install a $1 as a $2", message)
  201. path, message = env.pkgmgr.install_dir("txp", "/tmp/123", "name", nil)
  202. assert.is._not._nil(path)
  203. assert.is._nil(message)
  204. end)
  205. end)