-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathconfig_spec.lua
More file actions
147 lines (128 loc) · 5.69 KB
/
config_spec.lua
File metadata and controls
147 lines (128 loc) · 5.69 KB
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
139
140
141
142
143
144
145
146
147
---@module 'busted'
describe('codesettings.config', function()
before_each(function()
require('codesettings.config').reset() ---@diagnostic disable-line: invisible
end)
describe('schema for local config files', function()
it('loads config for the plugin itself from local config files based on config schema', function()
local Codesettings = require('codesettings')
Codesettings.setup({
config_file_paths = { 'spec/test-config-files/codesettings_plugin_conf.json' },
})
-- from spec/test-config-files/codesettings_plugin_conf.json
local Config = require('codesettings.config')
assert.equal('replace', Config.merge_lists)
assert.equal(false, Config.jsonc_filetype)
assert.equal(false, Config.lua_ls_integration)
assert.equal(false, Config.jsonls_integration)
end)
it('should not run integration setups when disabled in local config', function()
local Codesettings = require('codesettings')
local jsonls_mod = require('codesettings.setup.jsonls')
local lua_ls_mod = require('codesettings.setup.lua_ls')
local jsonc_filetype_mod = require('codesettings.setup.jsonc-filetype')
spy.on(jsonls_mod, 'setup')
spy.on(lua_ls_mod, 'setup')
spy.on(jsonc_filetype_mod, 'setup')
Codesettings.setup({
-- this config file disables all three integrations
config_file_paths = { 'spec/test-config-files/codesettings_plugin_conf.json' },
})
assert.spy(jsonls_mod.setup --[[@as luassert.spy]]).called(0)
assert.spy(lua_ls_mod.setup --[[@as luassert.spy]]).called(0)
assert.spy(jsonc_filetype_mod.setup --[[@as luassert.spy]]).called(0)
end)
end)
describe('config json schema', function()
local schema = require('codesettings.config').jsonschema()
local schema_table = schema:totable()
it('returns a CodesettingsSchema instance', function()
assert.is_not_nil(schema)
assert.is_table(schema)
assert.is_function(schema.totable)
assert.is_function(schema.flatten)
assert.is_function(schema.properties)
end)
it('has the correct flattened structure', function()
assert.is_table(schema_table.properties)
assert.equal('http://json-schema.org/draft-07/schema#', schema_table['$schema'])
assert.is_not_nil(schema_table.properties['codesettings.config_file_paths'])
assert.is_not_nil(schema_table.properties['codesettings.merge_lists'])
assert.is_not_nil(schema_table.properties['codesettings.root_dir'])
end)
it('filters out function types because they are not valid in json schema', function()
local root_dir_prop = schema_table.properties['codesettings.root_dir']
assert.is_not_nil(root_dir_prop)
-- Original type is { 'string', { args = {}, ret = 'string' }, 'null' }
-- Should be filtered to { 'string', 'null' } or similar
local prop_type = root_dir_prop.type
if type(prop_type) == 'table' then
-- Ensure no function type objects remain
for _, t in ipairs(prop_type) do
assert.is_not_table(t)
end
end
end)
end)
describe('ConfigBuilder', function()
local ConfigBuilder = require('codesettings.config.builder')
local Config = require('codesettings.config')
it('creates a new builder with default config', function()
local config = ConfigBuilder.new():build()
assert.is_table(config)
assert.same(Config.config_file_paths, config.config_file_paths)
assert.equal(Config.root_dir, config.root_dir)
assert.same(Config.merge_lists, config.merge_lists)
end)
it('sets config_file_paths with a valid string array', function()
local paths = { 'foo.json', 'bar.json' }
local config = ConfigBuilder.new():config_file_paths(paths):build()
assert.same(paths, config.config_file_paths)
end)
it('errors on invalid config_file_paths', function()
local builder = ConfigBuilder.new()
assert.has_error(function()
builder:config_file_paths('not-a-list') ---@diagnostic disable-line: param-type-mismatch
end)
assert.has_error(function()
builder:config_file_paths({ 123 }) ---@diagnostic disable-line: assign-type-mismatch
end)
end)
it('sets merge_lists correctly', function()
local config = ConfigBuilder.new():merge_lists('replace'):build()
assert.equal('replace', config.merge_lists)
end)
it('errors on invalid merge_lists', function()
local builder = ConfigBuilder.new()
assert.has_error(function()
builder:merge_lists('invalid') ---@diagnostic disable-line: param-type-mismatch
end)
end)
it('sets root_dir correctly', function()
local builder = ConfigBuilder.new():root_dir('/new/path'):build()
assert.equal('/new/path', builder.root_dir)
end)
it('accepts a function as root_dir', function()
local fn = function()
return '/dynamic'
end
local config = ConfigBuilder.new():root_dir(fn):build()
assert.equal(fn, config.root_dir)
end)
it('errors on invalid root_dir', function()
local builder = ConfigBuilder.new()
assert.has_error(function()
builder:root_dir(123) ---@diagnostic disable-line: param-type-mismatch
end)
end)
it('sets loader_extensions correctly', function()
-- test loading by module path
local exts = { 'my.extension', 'another.extension' }
local config = ConfigBuilder.new():loader_extensions(exts):build()
assert.same(exts, config.loader_extensions)
-- test loading by inline extension instances
local config2 = ConfigBuilder.new():loader_extensions({ require('codesettings.extensions.env') }):build()
assert.is_table(config2.loader_extensions)
end)
end)
end)