/
builds_config_test.rb
142 lines (115 loc) · 5.05 KB
/
builds_config_test.rb
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
require "test_helper"
class Standard::BuildsConfigTest < UnitTest
DEFAULT_OPTIONS = {
auto_correct: false,
safe_auto_correct: false,
formatters: [["Standard::Formatter", nil]],
parallel: false,
todo_file: nil,
todo_ignore_files: []
}.freeze
def setup
@subject = Standard::BuildsConfig.new
end
def test_no_argv_and_no_standard_dot_yml
result = @subject.call([], "/")
assert_equal :rubocop, result.runner
assert_equal DEFAULT_OPTIONS, result.rubocop_options
assert_equal config_store, result.rubocop_config_store.for("").to_h
end
def test_custom_argv_with_fix_set
result = @subject.call(["--only", "Standard/SemanticBlocks", "--fix", "--parallel"])
assert_equal DEFAULT_OPTIONS.merge(
auto_correct: true,
safe_auto_correct: true,
parallel: true,
only: ["Standard/SemanticBlocks"]
), result.rubocop_options
end
def test_blank_standard_yaml
result = @subject.call([], path("test/fixture/config/z"))
assert_equal DEFAULT_OPTIONS, result.rubocop_options
assert_equal config_store("test/fixture/config/z"), result.rubocop_config_store.for("").to_h
end
def test_decked_out_standard_yaml
result = @subject.call([], path("test/fixture/config/y"))
assert_equal DEFAULT_OPTIONS.merge(
auto_correct: true,
safe_auto_correct: true,
parallel: true,
formatters: [["progress", nil]]
), result.rubocop_options
expected_config = RuboCop::ConfigStore.new.tap do |config_store|
config_store.options_config = path("config/ruby-1.8.yml")
options_config = config_store.instance_variable_get("@options_config")
options_config["AllCops"]["Exclude"] |= [path("test/fixture/config/y/monkey/**/*")]
options_config["Fake/Lol"] = {"Exclude" => [path("test/fixture/config/y/neat/cool.rb")]}
options_config["Fake/Kek"] = {"Exclude" => [path("test/fixture/config/y/neat/cool.rb")]}
end.for("").to_h
assert_equal expected_config, result.rubocop_config_store.for("").to_h
end
def test_single_line_ignore
result = @subject.call([], path("test/fixture/config/x"))
assert_equal DEFAULT_OPTIONS, result.rubocop_options
assert_equal config_store("test/fixture/config/x").dup.tap { |config_store|
config_store["AllCops"]["Exclude"] |= [path("test/fixture/config/x/pants/**/*")]
}, result.rubocop_config_store.for("").to_h
end
def test_19
result = @subject.call([], path("test/fixture/config/w"))
assert_equal DEFAULT_OPTIONS, result.rubocop_options
assert_equal config_store("test/fixture/config/w", "config/ruby-1.9.yml", 2.5), result.rubocop_config_store.for("").to_h
end
def test_specified_standard_yaml_overrides_local
result = @subject.call(["--config", "test/fixture/lol.standard.yml"], path("test/fixture/config/z"))
assert_equal DEFAULT_OPTIONS.merge(
auto_correct: true,
safe_auto_correct: true
), result.rubocop_options
assert_equal config_store("test/fixture"), result.rubocop_config_store.for("").to_h
end
def test_specified_standard_yaml_raises
err = assert_raises(StandardError) {
@subject.call(["--config", "fake.file"], path("test/fixture/config/z"))
}
assert_match(/Configuration file ".*fake\.file" not found/, err.message)
end
def test_todo_merged
result = @subject.call([], path("test/fixture/config/u"))
assert_equal DEFAULT_OPTIONS.merge(
todo_file: path("test/fixture/config/u/.standard_todo.yml"),
todo_ignore_files: %w[todo_file_one.rb todo_file_two.rb]
), result.rubocop_options
assert_equal config_store("test/fixture/config/u").dup.tap { |config_store|
config_store["AllCops"]["Exclude"] |= [path("test/fixture/config/u/none_todo_path/**/*")]
config_store["AllCops"]["Exclude"] |= [path("test/fixture/config/u/none_todo_file.rb")]
config_store["AllCops"]["Exclude"] |= [path("test/fixture/config/u/todo_file_one.rb")]
config_store["AllCops"]["Exclude"] |= [path("test/fixture/config/u/todo_file_two.rb")]
}, result.rubocop_config_store.for("").to_h
end
private
def config_store(config_root = nil, rubocop_yml = highest_compatible_yml_version, ruby_version = RUBY_VERSION)
RuboCop::ConfigStore.new.tap do |config_store|
config_store.options_config = path(rubocop_yml)
options_config = config_store.instance_variable_get("@options_config")
options_config["AllCops"]["TargetRubyVersion"] = ruby_version.to_f
options_config["AllCops"]["Exclude"] |= standard_default_ignores(config_root)
end.for("").to_h
end
def highest_compatible_yml_version
non_latest_ruby = Dir["config/*.yml"]
.map { |n| n.match(/ruby-(.*)\.yml/) }.compact
.map { |m| Gem::Version.new(m[1]) }.sort
.find { |v| Gem::Version.new(RUBY_VERSION) < v }
if non_latest_ruby
"config/ruby-#{non_latest_ruby}.yml"
else
"config/base.yml"
end
end
def standard_default_ignores(config_root)
Standard::CreatesConfigStore::ConfiguresIgnoredPaths::DEFAULT_IGNORES.map { |(path, _)|
File.expand_path(File.join(config_root || Dir.pwd, path))
}
end
end