aboutsummaryrefslogtreecommitdiff
path: root/spec/config_spec.lua
blob: f415909d7779a4a4cc054dcb0de2e36c27bb0006 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
local test_env = require("spec.util.test_env")
local lfs = require("lfs")
local run = test_env.run
local testing_paths = test_env.testing_paths
local env_variables = test_env.env_variables
local hardcoded

test_env.unload_luarocks()

describe("LuaRocks config tests #integration", function()
   
   before_each(function()
      test_env.setup_specs()
      test_env.unload_luarocks() -- need to be required here, because hardcoded is created after first loading of specs
      hardcoded = require("luarocks.core.hardcoded")
   end)

   describe("LuaRocks config - basic tests", function()
      it("LuaRocks config with no flags/arguments", function()
         assert.match("rocks_servers", run.luarocks("config"))
      end)
      
      it("LuaRocks config include dir returns a subdir of LUA_DIR", function()
         local output = run.luarocks("config --lua-incdir")
         assert.match(hardcoded.LUA_DIR, output, 1, true)
      end)
      
      it("LuaRocks config library dir returns a subdir of LUA_DIR", function()
         local output = run.luarocks("config --lua-libdir")
         assert.match(hardcoded.LUA_DIR, output, 1, true)
      end)
      
      it("LuaRocks config lua version", function()
         local output = run.luarocks("config --lua-ver")
         local lua_version = _VERSION:gsub("Lua ", "")
         if test_env.LUAJIT_V then
            lua_version = "5.1"
         end
         assert.are.same(lua_version, output)
      end)
      
      it("LuaRocks config rock trees", function()
         assert.is_true(run.luarocks_bool("config --rock-trees"))
      end)
      
      it("LuaRocks config user config", function()
         local user_config_path = run.luarocks("config --user-config")
         assert.is.truthy(lfs.attributes(user_config_path))
      end)
      
      it("LuaRocks config missing user config", function()
         local output = run.luarocks("config --user-config", {LUAROCKS_CONFIG = "missing_file.lua"})
         assert.truthy(output:match("Warning"))
      end)
   end)

   describe("LuaRocks config - more complex tests", function()
      local scdir = testing_paths.testing_lrprefix .. "/etc/luarocks"
      local configfile = scdir .. "/config-" .. env_variables.LUA_VERSION .. ".lua"

      it("LuaRocks fail system config", function()
         os.rename(configfile, configfile .. ".bak")
         finally(function()
            os.rename(configfile .. ".bak", configfile)
         end)
         assert.is_false(run.luarocks_bool("config --system-config"))
      end)
      
      it("LuaRocks system config", function()
         lfs.mkdir(testing_paths.testing_lrprefix)
         lfs.mkdir(testing_paths.testing_lrprefix .. "/etc/")
         lfs.mkdir(scdir)

         local sysconfig = io.open(configfile, "w+")
         sysconfig:write(" ")
         sysconfig:close()
         finally(function()
            os.remove(configfile)
         end)
         
         local output = run.luarocks("config --system-config")
         assert.are.same(configfile, output)
      end)
      
      it("LuaRocks fail system config invalid", function()
         lfs.mkdir(testing_paths.testing_lrprefix)
         lfs.mkdir(testing_paths.testing_lrprefix .. "/etc/")
         lfs.mkdir(scdir)

         local sysconfig = io.open(configfile, "w+")
         sysconfig:write("if if if")
         sysconfig:close()
         finally(function()
            os.remove(configfile)
         end)
         assert.is_false(run.luarocks_bool("config --system-config"))
      end)
   end)
end)