/
non_atomic_file_operation_spec.rb
176 lines (141 loc) · 5.13 KB
/
non_atomic_file_operation_spec.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Lint::NonAtomicFileOperation, :config do
%i[makedirs mkdir mkdir_p mkpath].each do |make_method|
it 'registers an offense when use `FileTest.exist?` before creating file' do
expect_offense(<<~RUBY)
unless FileTest.exist?(path)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Remove unnecessary existence checks `FileTest.exist?`.
FileUtils.#{make_method}(path)
end
RUBY
expect_correction(<<~RUBY)
#{trailing_whitespace}#{trailing_whitespace}FileUtils.mkdir_p(path)
RUBY
end
end
%i[remove remove_dir remove_entry remove_entry_secure delete unlink
remove_file rm rm_f rm_r rm_rf rmdir rmtree safe_unlink].each do |remove_method|
it 'registers an offense when use `FileTest.exist?` before remove file' do
expect_offense(<<~RUBY)
if FileTest.exist?(path)
^^^^^^^^^^^^^^^^^^^^^^^^ Remove unnecessary existence checks `FileTest.exist?`.
FileUtils.#{remove_method}(path)
end
RUBY
expect_correction(<<~RUBY)
#{trailing_whitespace}#{trailing_whitespace}FileUtils.rm_rf(path)
RUBY
end
end
it 'registers an offense when use `FileTest.exist?` before creating file with an option `force: true`' do
expect_offense(<<~RUBY)
unless FileTest.exists?(path)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Remove unnecessary existence checks `FileTest.exists?`.
FileUtils.makedirs(path, force: true)
end
RUBY
expect_correction(<<~RUBY)
#{trailing_whitespace}#{trailing_whitespace}FileUtils.makedirs(path, force: true)
RUBY
end
it 'does not register an offense when use `FileTest.exist?` before creating file with an option `force: false`' do
expect_no_offenses(<<~RUBY)
unless FileTest.exists?(path)
FileUtils.makedirs(path, force: false)
end
RUBY
end
it 'registers an offense when use `FileTest.exist?` before creating file with an option not `force`' do
expect_offense(<<~RUBY)
unless FileTest.exists?(path)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Remove unnecessary existence checks `FileTest.exists?`.
FileUtils.makedirs(path, verbose: true)
end
RUBY
expect_correction(<<~RUBY)
#{trailing_whitespace}#{trailing_whitespace}FileUtils.mkdir_p(path, verbose: true)
RUBY
end
it 'registers an offense when use `FileTest.exists?` before creating file' do
expect_offense(<<~RUBY)
unless FileTest.exists?(path)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Remove unnecessary existence checks `FileTest.exists?`.
FileUtils.makedirs(path)
end
RUBY
expect_correction(<<~RUBY)
#{trailing_whitespace}#{trailing_whitespace}FileUtils.mkdir_p(path)
RUBY
end
it 'registers an offense when use `FileTest.exist?` with negated `if` before creating file' do
expect_offense(<<~RUBY)
if !FileTest.exist?(path)
^^^^^^^^^^^^^^^^^^^^^^^^^ Remove unnecessary existence checks `FileTest.exist?`.
FileUtils.makedirs(path)
end
RUBY
expect_correction(<<~RUBY)
#{trailing_whitespace}#{trailing_whitespace}FileUtils.mkdir_p(path)
RUBY
end
it 'registers an offense when use file existence checks `unless` by postfix before creating file' do
expect_offense(<<~RUBY)
FileUtils.makedirs(path) unless FileTest.exist?(path)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Remove unnecessary existence checks `FileTest.exist?`.
RUBY
expect_correction(<<~RUBY)
FileUtils.mkdir_p(path)#{trailing_whitespace}
RUBY
end
it 'registers an offense when use file existence checks `if` by postfix before removing file' do
expect_offense(<<~RUBY)
FileUtils.remove(path) if FileTest.exist?(path)
^^^^^^^^^^^^^^^^^^^^^^^^ Remove unnecessary existence checks `FileTest.exist?`.
RUBY
expect_correction(<<~RUBY)
FileUtils.rm_rf(path)#{trailing_whitespace}
RUBY
end
it 'does not register an offense when not checking for the existence' do
expect_no_offenses(<<~RUBY)
FileUtils.mkdir_p(path)
RUBY
end
it 'does not register an offense when checking for the existence of different files' do
expect_no_offenses(<<~RUBY)
FileUtils.mkdir_p(y) unless FileTest.exist?(path)
RUBY
end
it 'does not register an offense when not a method of file operation' do
expect_no_offenses(<<~RUBY)
unless FileUtils.exist?(path)
FileUtils.options_of(:rm)
end
unless FileUtils.exist?(path)
NotFile.remove(path)
end
RUBY
end
it 'does not register an offense when not an exist check' do
expect_no_offenses(<<~RUBY)
unless FileUtils.options_of(:rm)
FileUtils.mkdir_p(path)
end
if FileTest.executable?(path)
FileUtils.remove(path)
end
RUBY
end
it 'does not register an offense when processing other than file operations' do
expect_no_offenses(<<~RUBY)
unless FileTest.exist?(path)
FileUtils.makedirs(path)
do_something
end
unless FileTest.exist?(path)
do_something
FileUtils.makedirs(path)
end
RUBY
end
end