|
1 | 1 | # frozen_string_literal: true
|
2 | 2 |
|
3 | 3 | RSpec.describe RuboCop::Cop::Rails::TransactionExitStatement, :config do
|
4 |
| - it 'registers an offense when `return` is used in transactions' do |
5 |
| - expect_offense(<<~RUBY) |
6 |
| - ApplicationRecord.transaction do |
7 |
| - return if user.active? |
8 |
| - ^^^^^^ Exit statement `return` is not allowed. Use `raise` (rollback) or `next` (commit). |
9 |
| - end |
10 |
| - RUBY |
11 |
| - end |
| 4 | + shared_examples 'flags transaction exit statements' do |method| |
| 5 | + it 'registers an offense when `return` is used in transactions' do |
| 6 | + expect_offense(<<~RUBY, method: method) |
| 7 | + ApplicationRecord.%{method} do |
| 8 | + return if user.active? |
| 9 | + ^^^^^^ Exit statement `return` is not allowed. Use `raise` (rollback) or `next` (commit). |
| 10 | + end |
| 11 | + RUBY |
| 12 | + end |
12 | 13 |
|
13 |
| - it 'registers an offense when `break` is used in transactions' do |
14 |
| - expect_offense(<<~RUBY) |
15 |
| - ApplicationRecord.transaction do |
16 |
| - break if user.active? |
17 |
| - ^^^^^ Exit statement `break` is not allowed. Use `raise` (rollback) or `next` (commit). |
18 |
| - end |
19 |
| - RUBY |
20 |
| - end |
| 14 | + it 'registers an offense when `break` is used in transactions' do |
| 15 | + expect_offense(<<~RUBY, method: method) |
| 16 | + ApplicationRecord.%{method} do |
| 17 | + break if user.active? |
| 18 | + ^^^^^ Exit statement `break` is not allowed. Use `raise` (rollback) or `next` (commit). |
| 19 | + end |
| 20 | + RUBY |
| 21 | + end |
21 | 22 |
|
22 |
| - it 'registers an offense when `throw` is used in transactions' do |
23 |
| - expect_offense(<<~RUBY) |
24 |
| - ApplicationRecord.transaction do |
25 |
| - throw if user.active? |
26 |
| - ^^^^^ Exit statement `throw` is not allowed. Use `raise` (rollback) or `next` (commit). |
27 |
| - end |
28 |
| - RUBY |
29 |
| - end |
| 23 | + it 'registers an offense when `throw` is used in transactions' do |
| 24 | + expect_offense(<<~RUBY, method: method) |
| 25 | + ApplicationRecord.%{method} do |
| 26 | + throw if user.active? |
| 27 | + ^^^^^ Exit statement `throw` is not allowed. Use `raise` (rollback) or `next` (commit). |
| 28 | + end |
| 29 | + RUBY |
| 30 | + end |
30 | 31 |
|
31 |
| - it 'registers an offense when `return` is used in `with_lock` transactions' do |
32 |
| - expect_offense(<<~RUBY) |
33 |
| - user.with_lock do |
34 |
| - return if user.active? |
35 |
| - ^^^^^^ Exit statement `return` is not allowed. Use `raise` (rollback) or `next` (commit). |
36 |
| - end |
37 |
| - RUBY |
38 |
| - end |
| 32 | + it 'does not register an offense when `next` is used in transactions' do |
| 33 | + expect_no_offenses(<<~RUBY) |
| 34 | + ApplicationRecord.#{method} do |
| 35 | + next if user.active? |
| 36 | + end |
| 37 | + RUBY |
| 38 | + end |
39 | 39 |
|
40 |
| - it 'does not register an offense when `next` is used in transactions' do |
41 |
| - expect_no_offenses(<<~RUBY) |
42 |
| - ApplicationRecord.transaction do |
43 |
| - next if user.active? |
44 |
| - end |
45 |
| - RUBY |
46 |
| - end |
| 40 | + it 'does not register an offense when `raise` is used in transactions' do |
| 41 | + expect_no_offenses(<<~RUBY) |
| 42 | + ApplicationRecord.#{method} do |
| 43 | + raise if user.active? |
| 44 | + end |
| 45 | + RUBY |
| 46 | + end |
47 | 47 |
|
48 |
| - it 'does not register an offense when `raise` is used in transactions' do |
49 |
| - expect_no_offenses(<<~RUBY) |
50 |
| - ApplicationRecord.transaction do |
51 |
| - raise if user.active? |
52 |
| - end |
53 |
| - RUBY |
54 |
| - end |
| 48 | + it 'registers an offense when `return` is used in `loop` in transactions' do |
| 49 | + expect_offense(<<~RUBY, method: method) |
| 50 | + ApplicationRecord.%{method} do |
| 51 | + loop do |
| 52 | + return if condition |
| 53 | + ^^^^^^ Exit statement `return` is not allowed. Use `raise` (rollback) or `next` (commit). |
| 54 | + end |
| 55 | + end |
| 56 | + RUBY |
| 57 | + end |
55 | 58 |
|
56 |
| - it 'registers an offense when `return` is used in `loop` in transactions' do |
57 |
| - expect_offense(<<~RUBY) |
58 |
| - ApplicationRecord.transaction do |
59 |
| - loop do |
60 |
| - return if condition |
61 |
| - ^^^^^^ Exit statement `return` is not allowed. Use `raise` (rollback) or `next` (commit). |
| 59 | + it 'registers an offense when `throw` is used in `loop` in transactions' do |
| 60 | + expect_offense(<<~RUBY, method: method) |
| 61 | + ApplicationRecord.%{method} do |
| 62 | + loop do |
| 63 | + throw if condition |
| 64 | + ^^^^^ Exit statement `throw` is not allowed. Use `raise` (rollback) or `next` (commit). |
| 65 | + end |
62 | 66 | end
|
63 |
| - end |
64 |
| - RUBY |
65 |
| - end |
| 67 | + RUBY |
| 68 | + end |
66 | 69 |
|
67 |
| - it 'registers an offense when `throw` is used in `loop` in transactions' do |
68 |
| - expect_offense(<<~RUBY) |
69 |
| - ApplicationRecord.transaction do |
70 |
| - loop do |
71 |
| - throw if condition |
72 |
| - ^^^^^ Exit statement `throw` is not allowed. Use `raise` (rollback) or `next` (commit). |
| 70 | + it 'does not register an offense when `break` is used in `loop` in transactions' do |
| 71 | + expect_no_offenses(<<~RUBY) |
| 72 | + ApplicationRecord.#{method} do |
| 73 | + loop do |
| 74 | + break if condition |
| 75 | + end |
73 | 76 | end
|
74 |
| - end |
75 |
| - RUBY |
76 |
| - end |
| 77 | + RUBY |
| 78 | + end |
77 | 79 |
|
78 |
| - it 'does not register an offense when `break` is used in `loop` in transactions' do |
79 |
| - expect_no_offenses(<<~RUBY) |
80 |
| - ApplicationRecord.transaction do |
81 |
| - loop do |
82 |
| - break if condition |
| 80 | + it 'registers an offense when `return` is used in `rescue`' do |
| 81 | + expect_offense(<<~RUBY, method: method) |
| 82 | + ApplicationRecord.%{method} do |
| 83 | + rescue |
| 84 | + return do_something |
| 85 | + ^^^^^^^^^^^^^^^^^^^ Exit statement `return` is not allowed. Use `raise` (rollback) or `next` (commit). |
83 | 86 | end
|
84 |
| - end |
85 |
| - RUBY |
86 |
| - end |
| 87 | + RUBY |
| 88 | + end |
87 | 89 |
|
88 |
| - it 'registers an offense when `return` is used in `rescue`' do |
89 |
| - expect_offense(<<~RUBY) |
90 |
| - ApplicationRecord.transaction do |
91 |
| - rescue |
92 |
| - return do_something |
93 |
| - ^^^^^^^^^^^^^^^^^^^ Exit statement `return` is not allowed. Use `raise` (rollback) or `next` (commit). |
94 |
| - end |
95 |
| - RUBY |
96 |
| - end |
| 90 | + it 'registers an offense when `return` is used outside of a `rescue`' do |
| 91 | + expect_offense(<<~RUBY, method: method) |
| 92 | + ApplicationRecord.%{method} do |
| 93 | + return if user.active? |
| 94 | + ^^^^^^ Exit statement `return` is not allowed. Use `raise` (rollback) or `next` (commit). |
| 95 | + rescue |
| 96 | + pass |
| 97 | + end |
| 98 | + RUBY |
| 99 | + end |
97 | 100 |
|
98 |
| - it 'registers an offense when `return` is used outside of a `rescue`' do |
99 |
| - expect_offense(<<~RUBY) |
100 |
| - ApplicationRecord.transaction do |
101 |
| - return if user.active? |
102 |
| - ^^^^^^ Exit statement `return` is not allowed. Use `raise` (rollback) or `next` (commit). |
103 |
| - rescue |
104 |
| - pass |
105 |
| - end |
106 |
| - RUBY |
| 101 | + it 'does not register an offense when transaction block is empty' do |
| 102 | + expect_no_offenses(<<~RUBY) |
| 103 | + ApplicationRecord.#{method} do |
| 104 | + end |
| 105 | + RUBY |
| 106 | + end |
107 | 107 | end
|
108 | 108 |
|
109 |
| - it 'does not register an offense when transaction block is empty' do |
110 |
| - expect_no_offenses(<<~RUBY) |
111 |
| - ApplicationRecord.transaction do |
112 |
| - end |
113 |
| - RUBY |
114 |
| - end |
| 109 | + it_behaves_like 'flags transaction exit statements', :transaction |
| 110 | + it_behaves_like 'flags transaction exit statements', :with_lock |
115 | 111 | end
|
0 commit comments