File: config_spec.lua

package info (click to toggle)
luacheck 0.17.1-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 968 kB
  • ctags: 731
  • sloc: python: 35; makefile: 18
file content (138 lines) | stat: -rw-r--r-- 5,784 bytes parent folder | download
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
local config = require "luacheck.config"
local fs = require "luacheck.fs"
local P = fs.normalize
local cur_dir = fs.has_lfs and fs.lfs.currentdir()

local function nest(dir, func)
   if not fs.has_lfs then
      pending("uses lfs")
   end

   local backed = false

   local function back()
      if not backed then
         fs.lfs.chdir(cur_dir)
         backed = true
      end
   end

   finally(back)
   fs.lfs.chdir(dir)
   func()
   back()
end

describe("config", function()
   it("has default path", function()
      assert.is_string(config.default_path)
   end)

   it("loads default config", function()
      local conf = config.load_config()
      assert.is_table(conf)

      nest("spec/configs", function()
         local nested_conf = config.load_config()
         assert.is_table(nested_conf)
         assert.same(config.get_top_options(conf), config.get_top_options(nested_conf))
         assert.same(config.get_options(conf, P"spec/foo.lua"), config.get_options(nested_conf, P"../foo.lua"))
         assert.equal(P"../../bar.lua", config.relative_path(nested_conf, "bar.lua"))
      end)

      assert.not_same(config.get_options(conf, P"spec/foo_spec.lua"), config.get_options(conf, "foo_spec.lua"))
      assert.equal("bar.lua", config.relative_path(conf, "bar.lua"))
   end)

   it("works with empty config", function()
      local conf = config.empty_config
      assert.is_table(conf)
      assert.same({}, config.get_top_options(conf))
      assert.same({}, config.get_options(conf, "bar.lua"))
      assert.equal("bar.lua", config.relative_path(conf, "bar.lua"))
   end)

   it("loads config from path", function()
      local conf = config.load_config(P"spec/configs/override_config.luacheckrc")
      assert.is_table(conf)

      nest("spec/configs/project", function()
         local nested_conf = config.load_config(P"spec/configs/override_config.luacheckrc")
         assert.is_table(nested_conf)
         assert.same(config.get_top_options(conf), config.get_top_options(nested_conf))
         assert.same(
            config.get_options(conf, P"spec/samples/bad_code.lua"),
            config.get_options(nested_conf, P"../../samples/bad_code.lua")
         )
         assert.equal(P"../../../bar.lua", config.relative_path(nested_conf, "bar.lua"))
      end)

      assert.not_same(
         config.get_options(conf, P"spec/samples/bad_code.lua"),
         config.get_options(conf, P"spec/samples/unused_code.lua")
      )
      assert.equal("bar.lua", config.relative_path(conf, "bar.lua"))
   end)

   it("returns nil, error on missing config", function()
      local conf, err = config.load_config(P"spec/configs/config_404.luacheckrc")
      assert.is_nil(conf)
      assert.equal("Couldn't find configuration file "..P"spec/configs/config_404.luacheckrc", err)
   end)

   it("returns nil, error on config with bad syntax", function()
      local conf, err = config.load_config(P"spec/configs/bad_config.luacheckrc")
      assert.is_nil(conf)
      assert.matches("Couldn't load configuration from "..P"spec/configs/bad_config.luacheckrc"..
         ": syntax error %(line 2: .*%)", err)

      nest("spec/configs/project", function()
         local nested_conf, nested_err = config.load_config(P"spec/configs/bad_config.luacheckrc")
         assert.is_nil(nested_conf)
         assert.matches("Couldn't load configuration from "..P"../../../spec/configs/bad_config.luacheckrc"..
            ": syntax error %(line 2: .*%)", nested_err)
      end)
   end)

   it("returns nil, error on config with runtime issues", function()
      local conf, err = config.load_config(P"spec/configs/runtime_bad_config.luacheckrc")
      assert.is_nil(conf)
      assert.equal("Couldn't load configuration from "..P"spec/configs/runtime_bad_config.luacheckrc"..
         ": runtime error (line 1: attempt to call a nil value)", err)

      nest("spec/configs/project", function()
         local nested_conf, nested_err = config.load_config(P"spec/configs/runtime_bad_config.luacheckrc")
         assert.is_nil(nested_conf)
         assert.equal("Couldn't load configuration from "..P"../../../spec/configs/runtime_bad_config.luacheckrc"..
            ": runtime error (line 1: attempt to call a nil value)", nested_err)
      end)
   end)

   it("returns nil, error on invalid config", function()
      local conf, err = config.load_config(P"spec/configs/invalid_config.luacheckrc")
      assert.is_nil(conf)
      assert.equal("Couldn't load configuration from "..P"spec/configs/invalid_config.luacheckrc"..
         ": invalid value of option 'ignore'", err)

      nest("spec/configs/project", function()
         local nested_conf, nested_err = config.load_config(P"spec/configs/invalid_config.luacheckrc")
         assert.is_nil(nested_conf)
         assert.equal("Couldn't load configuration from "..P"../../../spec/configs/invalid_config.luacheckrc"..
            ": invalid value of option 'ignore'", nested_err)
      end)
   end)

   it("returns nil, error on config with invalid override", function()
      local conf, err = config.load_config(P"spec/configs/invalid_override_config.luacheckrc")
      assert.is_nil(conf)
      assert.equal("Couldn't load configuration from "..P"spec/configs/invalid_override_config.luacheckrc"..
         ": invalid value of option 'enable' in options for path 'spec/foo.lua'", err)

      nest("spec/configs/project", function()
         local nested_conf, nested_err = config.load_config(P"spec/configs/invalid_override_config.luacheckrc")
         assert.is_nil(nested_conf)
         assert.equal("Couldn't load configuration from "..P"../../../spec/configs/invalid_override_config.luacheckrc"..
            ": invalid value of option 'enable' in options for path 'spec/foo.lua'", nested_err)
      end)
   end)
end)