|
9 | 9 | let(:config) {RuboCop::Config.new} |
10 | 10 | let(:cop) {subject.new(config)} |
11 | 11 |
|
12 | | - # Test $! variable |
13 | | - with "code using $!" do |
| 12 | + # Test $! variable in rescue block (allowed - well-defined scope) |
| 13 | + with "code using $! in rescue block" do |
14 | 14 | let(:source) do |
15 | 15 | <<~RUBY |
16 | 16 | begin |
|
21 | 21 | RUBY |
22 | 22 | end |
23 | 23 |
|
24 | | - it "registers an offense" do |
| 24 | + it "does not register an offense" do |
25 | 25 | processed_source = RuboCop::ProcessedSource.new(source, RUBY_VERSION.to_f) |
26 | 26 | investigator = RuboCop::Cop::Commissioner.new([cop], [], raise_error: true) |
27 | 27 | report = investigator.investigate(processed_source) |
28 | 28 | offenses = report.offenses |
29 | | - expect(offenses).not.to be(:empty?) |
30 | | - expect(offenses.first.message).to be(:include?, "$!") |
| 29 | + expect(offenses).to be(:empty?) |
31 | 30 | end |
32 | 31 | end |
33 | 32 |
|
34 | | - # Test $@ variable |
35 | | - with "code using $@" do |
| 33 | + # Test $@ variable in rescue block (allowed - well-defined scope) |
| 34 | + with "code using $@ in rescue block" do |
36 | 35 | let(:source) do |
37 | 36 | <<~RUBY |
38 | 37 | begin |
|
43 | 42 | RUBY |
44 | 43 | end |
45 | 44 |
|
46 | | - it "registers an offense" do |
| 45 | + it "does not register an offense" do |
47 | 46 | processed_source = RuboCop::ProcessedSource.new(source, RUBY_VERSION.to_f) |
48 | 47 | investigator = RuboCop::Cop::Commissioner.new([cop], [], raise_error: true) |
49 | 48 | report = investigator.investigate(processed_source) |
50 | 49 | offenses = report.offenses |
51 | | - expect(offenses).not.to be(:empty?) |
52 | | - expect(offenses.first.message).to be(:include?, "$@") |
| 50 | + expect(offenses).to be(:empty?) |
53 | 51 | end |
54 | 52 | end |
55 | 53 |
|
56 | | - # Test $ERROR_INFO variable |
57 | | - with "code using $ERROR_INFO" do |
| 54 | + # Test $! in ensure block (extremely unsafe - should be flagged) |
| 55 | + with "code using $! in ensure block" do |
58 | 56 | let(:source) do |
59 | 57 | <<~RUBY |
60 | | - require 'English' |
61 | 58 | begin |
62 | 59 | risky_operation |
63 | | - rescue |
64 | | - puts $ERROR_INFO.message |
| 60 | + ensure |
| 61 | + log($!.message) if $! |
65 | 62 | end |
66 | 63 | RUBY |
67 | 64 | end |
68 | 65 |
|
69 | | - it "registers an offense" do |
| 66 | + it "registers offenses with unsafe message" do |
70 | 67 | processed_source = RuboCop::ProcessedSource.new(source, RUBY_VERSION.to_f) |
71 | 68 | investigator = RuboCop::Cop::Commissioner.new([cop], [], raise_error: true) |
72 | 69 | report = investigator.investigate(processed_source) |
73 | 70 | offenses = report.offenses |
74 | | - expect(offenses).not.to be(:empty?) |
75 | | - expect(offenses.first.message).to be(:include?, "$ERROR_INFO") |
| 71 | + expect(offenses.size).to be == 2 |
| 72 | + expect(offenses.first.message).to be(:include?, "extremely unsafe") |
76 | 73 | end |
77 | 74 | end |
78 | 75 |
|
79 | | - # Test $ERROR_POSITION variable |
80 | | - with "code using $ERROR_POSITION" do |
| 76 | + # Test $! in rescue modifier (allowed) |
| 77 | + with "code using $! in rescue modifier" do |
81 | 78 | let(:source) do |
82 | 79 | <<~RUBY |
83 | | - require 'English' |
84 | | - begin |
85 | | - risky_operation |
86 | | - rescue |
87 | | - puts $ERROR_POSITION.first |
| 80 | + result = risky_operation rescue $! |
| 81 | + RUBY |
| 82 | + end |
| 83 | + |
| 84 | + it "does not register an offense" do |
| 85 | + processed_source = RuboCop::ProcessedSource.new(source, RUBY_VERSION.to_f) |
| 86 | + investigator = RuboCop::Cop::Commissioner.new([cop], [], raise_error: true) |
| 87 | + report = investigator.investigate(processed_source) |
| 88 | + offenses = report.offenses |
| 89 | + expect(offenses).to be(:empty?) |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + # Test $! in parameter defaults (allowed - explicitly opting in) |
| 94 | + with "code using $! in parameter defaults" do |
| 95 | + let(:source) do |
| 96 | + <<~RUBY |
| 97 | + def foo(error = $!) |
| 98 | + puts error |
| 99 | + end |
| 100 | + |
| 101 | + def bar(error: $!) |
| 102 | + puts error |
88 | 103 | end |
89 | 104 | RUBY |
90 | 105 | end |
91 | 106 |
|
92 | | - it "registers an offense" do |
| 107 | + it "does not register an offense" do |
93 | 108 | processed_source = RuboCop::ProcessedSource.new(source, RUBY_VERSION.to_f) |
94 | 109 | investigator = RuboCop::Cop::Commissioner.new([cop], [], raise_error: true) |
95 | 110 | report = investigator.investigate(processed_source) |
96 | 111 | offenses = report.offenses |
97 | | - expect(offenses).not.to be(:empty?) |
98 | | - expect(offenses.first.message).to be(:include?, "$ERROR_POSITION") |
| 112 | + expect(offenses).to be(:empty?) |
99 | 113 | end |
100 | 114 | end |
101 | 115 |
|
|
143 | 157 | end |
144 | 158 | end |
145 | 159 |
|
146 | | - # Test multiple uses in same block |
147 | | - with "code using multiple global exception variables" do |
| 160 | + # Test multiple uses in same rescue block (allowed - well-defined scope) |
| 161 | + with "code using multiple global exception variables in rescue block" do |
148 | 162 | let(:source) do |
149 | 163 | <<~RUBY |
150 | 164 | begin |
|
156 | 170 | RUBY |
157 | 171 | end |
158 | 172 |
|
159 | | - it "registers multiple offenses" do |
| 173 | + it "does not register offenses" do |
160 | 174 | processed_source = RuboCop::ProcessedSource.new(source, RUBY_VERSION.to_f) |
161 | 175 | investigator = RuboCop::Cop::Commissioner.new([cop], [], raise_error: true) |
162 | 176 | report = investigator.investigate(processed_source) |
163 | 177 | offenses = report.offenses |
164 | | - expect(offenses.size).to be == 2 |
| 178 | + expect(offenses).to be(:empty?) |
165 | 179 | end |
166 | 180 | end |
167 | 181 |
|
|
0 commit comments