pkgmgr_spec.lua 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. --Luanti
  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_texturepath_share()
  46. return txp_dir
  47. end
  48. function core.get_modpath()
  49. return mods_dir
  50. end
  51. function core.get_gamepath()
  52. return games_dir
  53. end
  54. function env.fgettext_ne(fmt, ...)
  55. return fmt
  56. end
  57. setfenv(loadfile("builtin/common/misc_helpers.lua"), env)()
  58. setfenv(loadfile("builtin/mainmenu/content/pkgmgr.lua"), env)()
  59. function env.assert_calls(list)
  60. assert.are.same(list, calls)
  61. end
  62. return env
  63. end
  64. describe("install_dir", function()
  65. it("installs texture pack", function()
  66. local env = reset()
  67. env.pkgmgr.get_base_folder = function()
  68. return { type = "invalid", path = "/tmp/123" }
  69. end
  70. local path, message = env.pkgmgr.install_dir("txp", "/tmp/123", "mytxp", nil)
  71. assert.is.equal(txp_dir .. "/mytxp", path)
  72. assert.is._nil(message)
  73. env.assert_calls({
  74. { "delete_dir", txp_dir .. "/mytxp" },
  75. { "copy_dir", "/tmp/123", txp_dir .. "/mytxp", false },
  76. })
  77. end)
  78. it("prevents installing other as texture pack", function()
  79. local env = reset()
  80. env.pkgmgr.get_base_folder = function()
  81. return { type = "mod", path = "/tmp/123" }
  82. end
  83. local path, message = env.pkgmgr.install_dir("txp", "/tmp/123", "mytxp", nil)
  84. assert.is._nil(path)
  85. assert.is.equal("Unable to install a $1 as a texture pack", message)
  86. end)
  87. it("installs mod", function()
  88. local env = reset()
  89. env.pkgmgr.get_base_folder = function()
  90. return { type = "mod", path = "/tmp/123" }
  91. end
  92. local path, message = env.pkgmgr.install_dir("mod", "/tmp/123", "mymod", nil)
  93. assert.is.equal(mods_dir .. "/mymod", path)
  94. assert.is._nil(message)
  95. env.assert_calls({
  96. { "delete_dir", mods_dir .. "/mymod" },
  97. { "copy_dir", "/tmp/123", mods_dir .. "/mymod", false },
  98. })
  99. end)
  100. it("installs modpack", function()
  101. local env = reset()
  102. env.pkgmgr.get_base_folder = function()
  103. return { type = "modpack", path = "/tmp/123" }
  104. end
  105. local path, message = env.pkgmgr.install_dir("mod", "/tmp/123", "mymod", nil)
  106. assert.is.equal(mods_dir .. "/mymod", path)
  107. assert.is._nil(message)
  108. env.assert_calls({
  109. { "delete_dir", mods_dir .. "/mymod" },
  110. { "copy_dir", "/tmp/123", mods_dir .. "/mymod", false },
  111. })
  112. end)
  113. it("installs game", function()
  114. local env = reset()
  115. env.pkgmgr.get_base_folder = function()
  116. return { type = "game", path = "/tmp/123" }
  117. end
  118. local path, message = env.pkgmgr.install_dir("game", "/tmp/123", "mygame", nil)
  119. assert.is.equal(games_dir .. "/mygame", path)
  120. assert.is._nil(message)
  121. env.assert_calls({
  122. { "delete_dir", games_dir .. "/mygame" },
  123. { "copy_dir", "/tmp/123", games_dir .. "/mygame", false },
  124. })
  125. end)
  126. it("installs mod detects name", function()
  127. local env = reset()
  128. env.pkgmgr.get_base_folder = function()
  129. return { type = "mod", path = "/tmp/123" }
  130. end
  131. local path, message = env.pkgmgr.install_dir("mod", "/tmp/123", nil, nil)
  132. assert.is.equal(mods_dir .. "/123", path)
  133. assert.is._nil(message)
  134. env.assert_calls({
  135. { "delete_dir", mods_dir .. "/123" },
  136. { "copy_dir", "/tmp/123", mods_dir .. "/123", false },
  137. })
  138. end)
  139. it("installs mod detects invalid name", function()
  140. local env = reset()
  141. env.pkgmgr.get_base_folder = function()
  142. return { type = "mod", path = "/tmp/hi!" }
  143. end
  144. local path, message = env.pkgmgr.install_dir("mod", "/tmp/hi!", nil, nil)
  145. assert.is._nil(path)
  146. assert.is.equal("Install: Unable to find suitable folder name for $1", message)
  147. end)
  148. it("installs mod to target (update)", function()
  149. local env = reset()
  150. env.pkgmgr.get_base_folder = function()
  151. return { type = "mod", path = "/tmp/123" }
  152. end
  153. local path, message = env.pkgmgr.install_dir("mod", "/tmp/123", "mymod", "/tmp/alt-target")
  154. assert.is.equal("/tmp/alt-target", path)
  155. assert.is._nil(message)
  156. env.assert_calls({
  157. { "delete_dir", "/tmp/alt-target" },
  158. { "copy_dir", "/tmp/123", "/tmp/alt-target", false },
  159. })
  160. end)
  161. it("checks expected types", function()
  162. local actual_type = "modpack"
  163. local env = reset()
  164. env.pkgmgr.get_base_folder = function()
  165. return { type = actual_type, path = "/tmp/123" }
  166. end
  167. local path, message = env.pkgmgr.install_dir("mod", "/tmp/123", "name", nil)
  168. assert.is._not._nil(path)
  169. assert.is._nil(message)
  170. path, message = env.pkgmgr.install_dir("game", "/tmp/123", "name", nil)
  171. assert.is._nil(path)
  172. assert.is.equal("Unable to install a $1 as a $2", message)
  173. path, message = env.pkgmgr.install_dir("txp", "/tmp/123", "name", nil)
  174. assert.is._nil(path)
  175. assert.is.equal("Unable to install a $1 as a texture pack", message)
  176. actual_type = "game"
  177. path, message = env.pkgmgr.install_dir("mod", "/tmp/123", "name", nil)
  178. assert.is._nil(path)
  179. assert.is.equal("Unable to install a $1 as a $2", message)
  180. path, message = env.pkgmgr.install_dir("game", "/tmp/123", "name", nil)
  181. assert.is._not._nil(path)
  182. assert.is._nil(message)
  183. path, message = env.pkgmgr.install_dir("txp", "/tmp/123", "name", nil)
  184. assert.is._nil(path)
  185. assert.is.equal("Unable to install a $1 as a texture pack", message)
  186. actual_type = "txp"
  187. path, message = env.pkgmgr.install_dir("mod", "/tmp/123", "name", nil)
  188. assert.is._nil(path)
  189. assert.is.equal("Unable to install a $1 as a $2", message)
  190. path, message = env.pkgmgr.install_dir("game", "/tmp/123", "name", nil)
  191. assert.is._nil(path)
  192. assert.is.equal("Unable to install a $1 as a $2", message)
  193. path, message = env.pkgmgr.install_dir("txp", "/tmp/123", "name", nil)
  194. assert.is._not._nil(path)
  195. assert.is._nil(message)
  196. end)
  197. end)