forked from municz/rubocopcop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rubocopcop_test.rb
125 lines (105 loc) · 3.34 KB
/
rubocopcop_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
require 'minitest/autorun'
require 'fileutils'
require 'tmpdir'
require 'yaml'
describe 'rubocopcop' do
def setup
@dir = Dir.mktmpdir
end
def inside_test_dir(&block)
Dir.chdir(@dir, &block)
end
def teardown
FileUtils.remove_entry(@dir)
end
def basedir
File.dirname(__FILE__)
end
def run_rubocopcop
`#{rubocopcop_file}`
end
def rubocopcop_file
File.join(basedir, 'rubocopcop.rb')
end
def read_config(file)
YAML.load_file(file)
end
def default_config
read_config(File.join(basedir, '.rubocop.yml'))
end
def current_config
read_config('.rubocop.yml')
end
def compiled_config
YAML.load(`rubocop --show-cops`)
end
def write_test_config
raise 'rubocop file already exists' if File.exist?('.rubocop.yml')
File.write('.rubocop.yml', <<EOF)
AllCops:
DisabledByDefault: true
Style/MethodDefParentheses:
Description: Old description
StyleGuide: http://github.com/bbatsov/ruby-style-guide#method-parens
Enabled: true
EnforcedStyle: require_no_parentheses
SupportedStyles:
- require_parentheses
- require_no_parentheses
Style/EachWithObject:
Enabled: false
EOF
end
describe 'inside directory without rubocop configuration' do
it "initializes the rubocop environment based on its own defaults" do
inside_test_dir do
run_rubocopcop
assert File.exist?('.rubocop.yml')
end
end
end
describe 'inside directory with existing rubocop configuration' do
it "adds new cops to configuration" do
inside_test_dir do
write_test_config
run_rubocopcop
assert current_config.key?('Style/MethodMissing'), 'configuration is missing new cops'
refute current_config['Style/MethodMissing']['Enabled'], 'new cop is enabled'
end
end
it "keeps AllCops configuration" do
inside_test_dir do
write_test_config
run_rubocopcop
assert current_config.key?('AllCops'), 'AllCops keys is missing'
assert current_config['AllCops']['DisabledByDefault'], 'AllCops configuration is not preserved'
end
end
it "keeps configuration of old cops" do
inside_test_dir do
write_test_config
run_rubocopcop
assert current_config.key?('Style/MethodDefParentheses'), 'configuration is missing old cops'
assert_equal('require_no_parentheses',
current_config['Style/MethodDefParentheses']['EnforcedStyle'],
'old configuration is not preserved')
end
end
it "updates information at the cop" do
inside_test_dir do
write_test_config
run_rubocopcop
assert_equal("Checks if the method definitions have or don't have parentheses.",
current_config['Style/MethodDefParentheses']['Description'],
'Description not updated')
assert_equal("https://github.com/bbatsov/ruby-style-guide#method-parens",
current_config['Style/MethodDefParentheses']['StyleGuide'],
'StyleGuide not updated')
expected_styles = ["require_parentheses", "require_no_parentheses", "require_no_parentheses_except_multiline"]
assert_equal(expected_styles,
current_config['Style/MethodDefParentheses']['SupportedStyles'],
'SupportedStyles not updated')
end
end
end
end