Skip to content

Commit

Permalink
Add default per-path stds
Browse files Browse the repository at this point in the history
Unless --no-config is used, add default per-path overrides for stds
if not set already:

files["**/spec/**/*_spec.lua"].std = "+busted"
files["**/test/**/*_spec.lua"].std = "+busted"
files["**/tests/**/*_spec.lua"].std = "+busted"
files["**/*.rockspec"].std = "+rockspec"
files[**/*.luacheckrc"].std = "+luacheckrc"
  • Loading branch information
mpeterv committed Sep 12, 2018
1 parent 66776f5 commit ca83f43
Show file tree
Hide file tree
Showing 15 changed files with 143 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .busted
@@ -0,0 +1,5 @@
return {
_all = {
["exclude-pattern"] = "sample_spec"
}
}
4 changes: 1 addition & 3 deletions .luacheckrc
@@ -1,7 +1,5 @@
std = "min"
cache = true
include_files = {"src", "spec/*.lua", "scripts/*.lua"}
include_files = {"src", "spec/*.lua", "scripts/*.lua", "*.rockspec", "*.luacheckrc"}

files["spec/*_spec.lua"].std = "+busted"
files["src/luacheck/argparse.lua"].max_line_length = 140
files["src/luacheck/unicode_printability_boundaries.lua"].max_line_length = false
4 changes: 3 additions & 1 deletion luacheck-dev-1.rockspec
Expand Up @@ -6,7 +6,9 @@ source = {
description = {
summary = "A static analyzer and a linter for Lua",
detailed = [[
Luacheck is a command-line tool for linting and static analysis of Lua code. It is able to spot usage of undefined global variables, unused local variables and a few other typical problems within Lua programs.
Luacheck is a command-line tool for linting and static analysis of Lua code.
It is able to spot usage of undefined global variables, unused local variables and
a few other typical problems within Lua programs.
]],
homepage = "https://github.com/mpeterv/luacheck",
license = "MIT"
Expand Down
65 changes: 65 additions & 0 deletions spec/cli_spec.lua
Expand Up @@ -1210,6 +1210,71 @@ Total: 4 warnings / 0 errors in 2 files
]], get_output "spec/samples/bad_code.lua spec/samples/unused_code.lua --config=spec/configs/override_config.luacheckrc")
end)

it("adds per-file overrides with default stds", function()
assert.equal(([[
Checking .luacheckrc 3 warnings
.luacheckrc:14:6: accessing undefined variable 'it'
.luacheckrc:14:10: accessing undefined variable 'version'
.luacheckrc:14:25: accessing undefined variable 'newproxy'
Checking default_stds-scm-1.rockspec 3 warnings
default_stds-scm-1.rockspec:13:1: accessing undefined variable 'it'
default_stds-scm-1.rockspec:13:21: accessing undefined variable 'newproxy'
default_stds-scm-1.rockspec:13:37: accessing undefined variable 'new_globals'
Checking nested/spec/sample_spec.lua 3 warnings
nested/spec/sample_spec.lua:1:39: accessing undefined variable 'newproxy'
nested/spec/sample_spec.lua:1:55: accessing undefined variable 'version'
nested/spec/sample_spec.lua:1:64: accessing undefined variable 'read_globals'
Checking normal_file.lua 4 warnings
normal_file.lua:1:1: accessing undefined variable 'it'
normal_file.lua:1:29: accessing undefined variable 'newproxy'
normal_file.lua:1:45: accessing undefined variable 'version'
normal_file.lua:1:54: accessing undefined variable 'read_globals'
Checking sample_spec.lua 4 warnings
sample_spec.lua:1:1: accessing undefined variable 'it'
sample_spec.lua:1:28: accessing undefined variable 'newproxy'
sample_spec.lua:1:44: accessing undefined variable 'version'
sample_spec.lua:1:53: accessing undefined variable 'read_globals'
Checking test/nested_normal_file.lua 4 warnings
test/nested_normal_file.lua:1:1: accessing undefined variable 'it'
test/nested_normal_file.lua:1:47: accessing undefined variable 'newproxy'
test/nested_normal_file.lua:1:63: accessing undefined variable 'version'
test/nested_normal_file.lua:1:72: accessing undefined variable 'read_globals'
Checking test/sample_spec.lua 5 warnings
test/sample_spec.lua:1:1: accessing undefined variable 'it'
test/sample_spec.lua:1:37: accessing undefined variable 'newproxy'
test/sample_spec.lua:1:47: accessing undefined variable 'math'
test/sample_spec.lua:1:53: accessing undefined variable 'version'
test/sample_spec.lua:1:62: accessing undefined variable 'read_globals'
Checking tests/nested/sample_spec.lua 3 warnings
tests/nested/sample_spec.lua:1:44: accessing undefined variable 'newproxy'
tests/nested/sample_spec.lua:1:60: accessing undefined variable 'version'
tests/nested/sample_spec.lua:1:69: accessing undefined variable 'read_globals'
Checking tests/sample_spec.lua 3 warnings
tests/sample_spec.lua:1:17: accessing undefined variable 'newproxy'
tests/sample_spec.lua:1:33: accessing undefined variable 'version'
tests/sample_spec.lua:1:42: accessing undefined variable 'read_globals'
Total: 32 warnings / 0 errors in 9 files
]]):gsub("([a-z])/", "%1" .. package.config:sub(1, 1)), get_output(". --include-files .", "spec/projects/default_stds/"))
end)

it("uses new filename when selecting per-file overrides", function()
assert.equal([[
Checking spec/samples/unused_code.lua OK
Expand Down
14 changes: 14 additions & 0 deletions spec/projects/default_stds/.luacheckrc
@@ -0,0 +1,14 @@
std = "min"

files["**/test/**/*_spec.lua"] = {
std = "none"
}

local shared_options = {ignore = {"ignored"}}

files["**/spec/**/*_spec.lua"] = shared_options
files["normal_file.lua"] = shared_options

local function sink() end

sink(it, version, math, newproxy)
13 changes: 13 additions & 0 deletions spec/projects/default_stds/default_stds-scm-1.rockspec
@@ -0,0 +1,13 @@
package = "default_stds"
version = "scm-1"
source = {
url = "https://example.com"
}
description = {
summary = "example",
detailed = "example",
homepage = "https://example.com",
license = "MIT"
}
dependencies = {}
it("is a rockspec")(newproxy, math, new_globals)
2 changes: 2 additions & 0 deletions spec/projects/default_stds/nested/spec/sample_spec.lua
@@ -0,0 +1,2 @@
it("is a test in a nested directory")(newproxy, math, version, read_globals)
ignored()
2 changes: 2 additions & 0 deletions spec/projects/default_stds/normal_file.lua
@@ -0,0 +1,2 @@
it("is just a normal file")(newproxy, math, version, read_globals)
ignored()
1 change: 1 addition & 0 deletions spec/projects/default_stds/sample_spec.lua
@@ -0,0 +1 @@
it("is not really a test")(newproxy, math, version, read_globals)
1 change: 1 addition & 0 deletions spec/projects/default_stds/test/nested_normal_file.lua
@@ -0,0 +1 @@
it("is a normal file in a nested directory")(newproxy, math, version, read_globals)
1 change: 1 addition & 0 deletions spec/projects/default_stds/test/sample_spec.lua
@@ -0,0 +1 @@
it("is a test in a test directory")(newproxy, math, version, read_globals)
1 change: 1 addition & 0 deletions spec/projects/default_stds/tests/nested/sample_spec.lua
@@ -0,0 +1 @@
it("is a test in a very nested directory")(newproxy, math, version, read_globals)
1 change: 1 addition & 0 deletions spec/projects/default_stds/tests/sample_spec.lua
@@ -0,0 +1 @@
it("is a test")(newproxy, math, version, read_globals)
3 changes: 2 additions & 1 deletion spec/samples/sample.rockspec
Expand Up @@ -2,6 +2,7 @@ build = {
type = "builtin",
modules = {
good = "spec/samples/good_code.lua",
bad = "spec/samples/bad_code.lua"
bad = "spec/samples/bad_code.lua",
not_even_a_module = some_global
}
}
33 changes: 31 additions & 2 deletions src/luacheck/config.lua
Expand Up @@ -203,6 +203,35 @@ local function remove_env_mt(env, special_values)
utils.update(env, special_values)
end

local function set_default_std(files, pattern, std)
-- Avoid mutating option tables, they may be shared between different patterns.
local pattern_opts = {std = std}

if files[pattern] then
pattern_opts = utils.update(pattern_opts, files[pattern])
end

files[pattern] = pattern_opts
end

local function add_default_path_options(opts)
local files = {}

if opts.files then
files = utils.update(files, opts.files)
end

opts.files = files
set_default_std(files, "**/spec/**/*_spec.lua", "+busted")
set_default_std(files, "**/test/**/*_spec.lua", "+busted")
set_default_std(files, "**/tests/**/*_spec.lua", "+busted")
set_default_std(files, "**/*.rockspec", "+rockspec")
set_default_std(files, "**/*.luacheckrc", "+luacheckrc")
end

local fallback_config = {options = {}}
add_default_path_options(fallback_config.options)

-- Loads config from a file, if possible.
-- `path` and `global_path` can be nil (will use default), false (will disable loading), or a string.
-- Doesn't validate the config.
Expand All @@ -214,7 +243,7 @@ function config.load_config(path, global_path)
if anchor_dir then
return nil, anchor_dir
else
return {options = {}}
return fallback_config
end
end

Expand All @@ -234,7 +263,7 @@ function config.load_config(path, global_path)
end

remove_env_mt(env, special_values)

add_default_path_options(env)
return {options = env, config_path = config_path, anchor_dir = anchor_dir}
end

Expand Down

0 comments on commit ca83f43

Please sign in to comment.