|
9 | 9 | ::RAILS_6_0_APP_PATH = File.expand_path(RAILS_6_0_APP_NAME, __dir__).freeze
|
10 | 10 |
|
11 | 11 | let!(:git) { Git.open(RAILS_6_0_PROJECT_PATH) }
|
| 12 | + let(:task_model) do |
| 13 | + patch = <<~PATCH |
| 14 | + +# == Schema Information |
| 15 | + +# |
| 16 | + +# Table name: tasks |
| 17 | + +# |
| 18 | + +# id :integer not null, primary key |
| 19 | + +# content :string |
| 20 | + +# count :integer default(0) |
| 21 | + +# status :boolean default(FALSE) |
| 22 | + +# created_at :datetime not null |
| 23 | + +# updated_at :datetime not null |
| 24 | + +# |
| 25 | + PATCH |
| 26 | + |
| 27 | + path = 'app/models/task.rb' |
| 28 | + { |
| 29 | + path: include(path), |
| 30 | + patch: include(patch) |
| 31 | + } |
| 32 | + end |
| 33 | + let(:task_test) do |
| 34 | + patch = <<~PATCH |
| 35 | + +# == Schema Information |
| 36 | + +# |
| 37 | + +# Table name: tasks |
| 38 | + +# |
| 39 | + +# id :integer not null, primary key |
| 40 | + +# content :string |
| 41 | + +# count :integer default(0) |
| 42 | + +# status :boolean default(FALSE) |
| 43 | + +# created_at :datetime not null |
| 44 | + +# updated_at :datetime not null |
| 45 | + +# |
| 46 | + PATCH |
| 47 | + |
| 48 | + path = 'test/models/task_test.rb' |
| 49 | + { |
| 50 | + path: include(path), |
| 51 | + patch: include(patch) |
| 52 | + } |
| 53 | + end |
| 54 | + let(:task_fixture) do |
| 55 | + patch = <<~PATCH |
| 56 | + +# == Schema Information |
| 57 | + +# |
| 58 | + +# Table name: tasks |
| 59 | + +# |
| 60 | + +# id :integer not null, primary key |
| 61 | + +# content :string |
| 62 | + +# count :integer default(0) |
| 63 | + +# status :boolean default(FALSE) |
| 64 | + +# created_at :datetime not null |
| 65 | + +# updated_at :datetime not null |
| 66 | + +# |
| 67 | + PATCH |
| 68 | + |
| 69 | + path = 'test/fixtures/tasks.yml' |
| 70 | + { |
| 71 | + path: include(path), |
| 72 | + patch: include(patch) |
| 73 | + } |
| 74 | + end |
12 | 75 |
|
13 | 76 | before(:all) do
|
14 | 77 | Bundler.with_clean_env do
|
|
19 | 82 | end
|
20 | 83 | end
|
21 | 84 |
|
| 85 | + around(:each) do |example| |
| 86 | + Bundler.with_clean_env do |
| 87 | + Dir.chdir RAILS_6_0_APP_PATH do |
| 88 | + example.run |
| 89 | + end |
| 90 | + end |
| 91 | + end |
| 92 | + |
22 | 93 | after(:each) do
|
23 | 94 | git.reset_hard
|
24 | 95 | end
|
25 | 96 |
|
26 | 97 | describe 'annotate --models' do
|
27 | 98 | let(:command) { 'bundle exec annotate --models' }
|
28 | 99 |
|
29 |
| - let(:task_model) do |
30 |
| - patch = <<~PATCH |
31 |
| - +# == Schema Information |
32 |
| - +# |
33 |
| - +# Table name: tasks |
34 |
| - +# |
35 |
| - +# id :integer not null, primary key |
36 |
| - +# content :string |
37 |
| - +# count :integer default(0) |
38 |
| - +# status :boolean default(FALSE) |
39 |
| - +# created_at :datetime not null |
40 |
| - +# updated_at :datetime not null |
41 |
| - +# |
42 |
| - PATCH |
43 |
| - |
44 |
| - path = 'app/models/task.rb' |
45 |
| - { |
46 |
| - path: include(path), |
47 |
| - patch: include(patch) |
48 |
| - } |
49 |
| - end |
50 |
| - let(:task_test) do |
51 |
| - patch = <<~PATCH |
52 |
| - +# == Schema Information |
53 |
| - +# |
54 |
| - +# Table name: tasks |
55 |
| - +# |
56 |
| - +# id :integer not null, primary key |
57 |
| - +# content :string |
58 |
| - +# count :integer default(0) |
59 |
| - +# status :boolean default(FALSE) |
60 |
| - +# created_at :datetime not null |
61 |
| - +# updated_at :datetime not null |
62 |
| - +# |
63 |
| - PATCH |
64 |
| - |
65 |
| - path = 'test/models/task_test.rb' |
66 |
| - { |
67 |
| - path: include(path), |
68 |
| - patch: include(patch) |
69 |
| - } |
70 |
| - end |
71 |
| - let(:task_fixture) do |
72 |
| - patch = <<~PATCH |
73 |
| - +# == Schema Information |
74 |
| - +# |
75 |
| - +# Table name: tasks |
76 |
| - +# |
77 |
| - +# id :integer not null, primary key |
78 |
| - +# content :string |
79 |
| - +# count :integer default(0) |
80 |
| - +# status :boolean default(FALSE) |
81 |
| - +# created_at :datetime not null |
82 |
| - +# updated_at :datetime not null |
83 |
| - +# |
84 |
| - PATCH |
85 |
| - |
86 |
| - path = 'test/fixtures/tasks.yml' |
87 |
| - { |
88 |
| - path: include(path), |
89 |
| - patch: include(patch) |
90 |
| - } |
91 |
| - end |
92 |
| - |
93 | 100 | it 'annotate models' do
|
94 |
| - Bundler.with_clean_env do |
95 |
| - Dir.chdir RAILS_6_0_APP_PATH do |
96 |
| - expect(git.diff.any?).to be_falsy |
97 |
| - |
98 |
| - puts `#{command}` |
99 |
| - |
100 |
| - expect(git.diff.entries).to contain_exactly( |
101 |
| - an_object_having_attributes(task_model), |
102 |
| - an_object_having_attributes(task_test), |
103 |
| - an_object_having_attributes(task_fixture) |
104 |
| - ) |
105 |
| - end |
106 |
| - end |
| 101 | + expect(git.diff.any?).to be_falsy |
| 102 | + |
| 103 | + puts `#{command}` |
| 104 | + |
| 105 | + expect(git.diff.entries).to contain_exactly( |
| 106 | + an_object_having_attributes(task_model), |
| 107 | + an_object_having_attributes(task_test), |
| 108 | + an_object_having_attributes(task_fixture) |
| 109 | + ) |
107 | 110 | end
|
108 | 111 | end
|
109 | 112 |
|
|
156 | 159 | end
|
157 | 160 |
|
158 | 161 | it 'annotate routes.rb' do
|
159 |
| - Bundler.with_clean_env do |
160 |
| - Dir.chdir RAILS_6_0_APP_PATH do |
161 |
| - expect(git.diff.any?).to be_falsy |
| 162 | + expect(git.diff.any?).to be_falsy |
162 | 163 |
|
163 |
| - puts `#{command}` |
| 164 | + puts `#{command}` |
164 | 165 |
|
165 |
| - expect(git.diff.entries).to contain_exactly(an_object_having_attributes(task_routes)) |
166 |
| - end |
167 |
| - end |
| 166 | + expect(git.diff.entries).to contain_exactly(an_object_having_attributes(task_routes)) |
168 | 167 | end
|
169 | 168 | end
|
170 | 169 |
|
171 | 170 | describe 'rails g annotate:install' do
|
172 | 171 | let(:command) { 'bin/rails g annotate:install' }
|
173 | 172 | let(:rake_file_path) { 'lib/tasks/auto_annotate_models.rake' }
|
| 173 | + let(:full_path) { File.expand_path(rake_file_path) } |
| 174 | + |
| 175 | + after(:each) do |
| 176 | + File.delete(full_path) |
| 177 | + end |
174 | 178 |
|
175 | 179 | it 'generates the rake file' do
|
176 |
| - Bundler.with_clean_env do |
177 |
| - Dir.chdir RAILS_6_0_APP_PATH do |
178 |
| - full_path = File.expand_path(rake_file_path) |
179 |
| - expect { `#{command}` }.to change { File.exist?(rake_file_path) }.from(false).to(true) |
| 180 | + expect { `#{command}` }.to change { File.exist?(rake_file_path) }.from(false).to(true) |
| 181 | + end |
| 182 | + |
| 183 | + context 'with multi-db environment' do |
| 184 | + let(:migrate_command) { 'bin/rails db:migrate:primary' } |
| 185 | + |
| 186 | + it 'hooks database-specific commands and annotates models' do |
| 187 | + expect(git.diff.any?).to be_falsy |
| 188 | + |
| 189 | + system({ 'MULTI_DB' => 'true' }, command) |
| 190 | + system({ 'MULTI_DB' => 'true' }, migrate_command) |
180 | 191 |
|
181 |
| - File.delete(full_path) |
182 |
| - end |
| 192 | + expect(git.diff.entries).to contain_exactly( |
| 193 | + an_object_having_attributes(task_model), |
| 194 | + an_object_having_attributes(task_test), |
| 195 | + an_object_having_attributes(task_fixture) |
| 196 | + ) |
183 | 197 | end
|
184 | 198 | end
|
185 | 199 | end
|
|
0 commit comments