Skip to content

Commit 9f2e770

Browse files
authored
Get CI consistently green (#7)
The goal is to get CI green so that we can make behavior and structural changes with confidence. I've found the following sources of test pollution: - ENV values being written to and not cleaned up between tests - `Annotate` has the instance variable, `@has_set_defaults`, which can persist between tests
1 parent 00c56dd commit 9f2e770

14 files changed

+130
-211
lines changed

lib/annotate_rb/cli.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ def initialize
1212
HELP_MAPPING = %w(-h -? --help).to_set
1313
VERSION_MAPPING = %w(-v --version).to_set
1414

15-
1615
def run(args = ARGV)
1716
_original_argv = ARGV.dup
1817

spec/lib/annotate/annotate_models_annotate_model_file_spec.rb

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,11 @@
11
# encoding: utf-8
2-
require_relative '../../spec_helper'
32
require 'annotate/annotate_models'
43
require 'annotate/active_record_patch'
54
require 'active_support/core_ext/string'
65
require 'files'
76
require 'tmpdir'
87

98
RSpec.describe AnnotateModels do
10-
MAGIC_COMMENTS = [
11-
'# encoding: UTF-8',
12-
'# coding: UTF-8',
13-
'# -*- coding: UTF-8 -*-',
14-
'#encoding: utf-8',
15-
'# encoding: utf-8',
16-
'# -*- encoding : utf-8 -*-',
17-
"# encoding: utf-8\n# frozen_string_literal: true",
18-
"# frozen_string_literal: true\n# encoding: utf-8",
19-
'# frozen_string_literal: true',
20-
'#frozen_string_literal: false',
21-
'# -*- frozen_string_literal : true -*-'
22-
].freeze
23-
249
describe '.annotate_model_file' do
2510
before do
2611
class Foo < ActiveRecord::Base; end

spec/lib/annotate/annotate_models_annotating_a_file_frozen_spec.rb

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# encoding: utf-8
2-
require_relative '../../spec_helper'
32
require 'annotate/annotate_models'
43
require 'annotate/active_record_patch'
54
require 'active_support/core_ext/string'
@@ -9,20 +8,6 @@
98
RSpec.describe AnnotateModels do
109
include AnnotateTestHelpers
1110

12-
MAGIC_COMMENTS = [
13-
'# encoding: UTF-8',
14-
'# coding: UTF-8',
15-
'# -*- coding: UTF-8 -*-',
16-
'#encoding: utf-8',
17-
'# encoding: utf-8',
18-
'# -*- encoding : utf-8 -*-',
19-
"# encoding: utf-8\n# frozen_string_literal: true",
20-
"# frozen_string_literal: true\n# encoding: utf-8",
21-
'# frozen_string_literal: true',
22-
'#frozen_string_literal: false',
23-
'# -*- frozen_string_literal : true -*-'
24-
].freeze
25-
2611
describe 'annotating a file' do
2712
before do
2813
@model_dir = Dir.mktmpdir('annotate_models')
@@ -41,28 +26,8 @@ class User < ActiveRecord::Base
4126
Annotate::Helpers.reset_options(Annotate::Constants::ALL_ANNOTATE_OPTIONS)
4227
end
4328

44-
def write_model(file_name, file_content)
45-
fname = File.join(@model_dir, file_name)
46-
FileUtils.mkdir_p(File.dirname(fname))
47-
File.open(fname, 'wb') { |f| f.write file_content }
48-
49-
[fname, file_content]
50-
end
51-
52-
def annotate_one_file(options = {})
53-
Annotate.set_defaults(options)
54-
options = Annotate.setup_options(options)
55-
AnnotateModels.annotate_one_file(@model_file_name, @schema_info, :position_in_class, options)
56-
57-
# Wipe settings so the next call will pick up new values...
58-
Annotate.instance_variable_set('@has_set_defaults', false)
59-
Annotate::Constants::POSITION_OPTIONS.each { |key| ENV[key.to_s] = '' }
60-
Annotate::Constants::FLAG_OPTIONS.each { |key| ENV[key.to_s] = '' }
61-
Annotate::Constants::PATH_OPTIONS.each { |key| ENV[key.to_s] = '' }
62-
end
63-
6429
# TODO: Check out why this test fails due to test pollution
65-
xdescribe 'frozen option' do
30+
describe 'frozen option' do
6631
it "should abort without existing annotation when frozen: true " do
6732
expect { annotate_one_file frozen: true }.to raise_error SystemExit, /user.rb needs to be updated, but annotate was run with `--frozen`./
6833
end

spec/lib/annotate/annotate_models_annotating_a_file_spec.rb

Lines changed: 67 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# encoding: utf-8
2-
require_relative '../../spec_helper'
32
require 'annotate/annotate_models'
43
require 'annotate/active_record_patch'
54
require 'active_support/core_ext/string'
@@ -8,20 +7,7 @@
87

98
RSpec.describe AnnotateModels do
109
include AnnotateTestHelpers
11-
12-
MAGIC_COMMENTS = [
13-
'# encoding: UTF-8',
14-
'# coding: UTF-8',
15-
'# -*- coding: UTF-8 -*-',
16-
'#encoding: utf-8',
17-
'# encoding: utf-8',
18-
'# -*- encoding : utf-8 -*-',
19-
"# encoding: utf-8\n# frozen_string_literal: true",
20-
"# frozen_string_literal: true\n# encoding: utf-8",
21-
'# frozen_string_literal: true',
22-
'#frozen_string_literal: false',
23-
'# -*- frozen_string_literal : true -*-'
24-
].freeze
10+
include AnnotateTestConstants
2511

2612
describe 'annotating a file' do
2713
before do
@@ -41,36 +27,80 @@ class User < ActiveRecord::Base
4127
Annotate::Helpers.reset_options(Annotate::Constants::ALL_ANNOTATE_OPTIONS)
4228
end
4329

44-
def write_model(file_name, file_content)
45-
fname = File.join(@model_dir, file_name)
46-
FileUtils.mkdir_p(File.dirname(fname))
47-
File.open(fname, 'wb') { |f| f.write file_content }
30+
context "with 'before'" do
31+
let(:position) { 'before' }
32+
33+
it "should put annotation before class if :position == 'before'" do
34+
annotate_one_file position: position
35+
expect(File.read(@model_file_name))
36+
.to eq("#{@schema_info}#{@file_content}")
37+
end
38+
end
39+
40+
context "with :before" do
41+
let(:position) { :before }
4842

49-
[fname, file_content]
43+
it "should put annotation before class if :position == :before" do
44+
annotate_one_file position: position
45+
expect(File.read(@model_file_name))
46+
.to eq("#{@schema_info}#{@file_content}")
47+
end
5048
end
5149

52-
def annotate_one_file(options = {})
53-
Annotate.set_defaults(options)
54-
options = Annotate.setup_options(options)
55-
AnnotateModels.annotate_one_file(@model_file_name, @schema_info, :position_in_class, options)
50+
context "with 'top'" do
51+
let(:position) { 'top' }
5652

57-
# Wipe settings so the next call will pick up new values...
58-
Annotate.instance_variable_set('@has_set_defaults', false)
59-
Annotate::Constants::POSITION_OPTIONS.each { |key| ENV[key.to_s] = '' }
60-
Annotate::Constants::FLAG_OPTIONS.each { |key| ENV[key.to_s] = '' }
61-
Annotate::Constants::PATH_OPTIONS.each { |key| ENV[key.to_s] = '' }
53+
it "should put annotation before class if :position == 'top'" do
54+
annotate_one_file position: position
55+
expect(File.read(@model_file_name))
56+
.to eq("#{@schema_info}#{@file_content}")
57+
end
6258
end
6359

64-
['before', :before, 'top', :top].each do |position|
65-
it "should put annotation before class if :position == #{position}" do
60+
context "with :top" do
61+
let(:position) { :top }
62+
63+
it "should put annotation before class if :position == :top" do
6664
annotate_one_file position: position
6765
expect(File.read(@model_file_name))
6866
.to eq("#{@schema_info}#{@file_content}")
6967
end
7068
end
7169

72-
['after', :after, 'bottom', :bottom].each do |position|
73-
it "should put annotation after class if position: #{position}" do
70+
context "with 'after'" do
71+
let(:position) { 'after' }
72+
73+
it "should put annotation after class if position: 'after'" do
74+
annotate_one_file position: position
75+
expect(File.read(@model_file_name))
76+
.to eq("#{@file_content}\n#{@schema_info}")
77+
end
78+
end
79+
80+
context "with :after" do
81+
let(:position) { :after }
82+
83+
it "should put annotation after class if position: :after" do
84+
annotate_one_file position: position
85+
expect(File.read(@model_file_name))
86+
.to eq("#{@file_content}\n#{@schema_info}")
87+
end
88+
end
89+
90+
context "with 'bottom'" do
91+
let(:position) { 'bottom' }
92+
93+
it "should put annotation after class if position: 'bottom'" do
94+
annotate_one_file position: position
95+
expect(File.read(@model_file_name))
96+
.to eq("#{@file_content}\n#{@schema_info}")
97+
end
98+
end
99+
100+
context "with :bottom" do
101+
let(:position) { :bottom }
102+
103+
it "should put annotation after class if position: :bottom" do
74104
annotate_one_file position: position
75105
expect(File.read(@model_file_name))
76106
.to eq("#{@file_content}\n#{@schema_info}")
@@ -196,7 +226,7 @@ class Foo::User < ActiveRecord::Base
196226
end
197227

198228
it 'should not touch magic comments' do
199-
MAGIC_COMMENTS.each do |magic_comment|
229+
AnnotateTestConstants::MAGIC_COMMENTS.each do |magic_comment|
200230
write_model 'user.rb', <<~EOS
201231
#{magic_comment}
202232
class User < ActiveRecord::Base
@@ -216,7 +246,7 @@ class User < ActiveRecord::Base
216246

217247
it 'adds an empty line between magic comments and annotation (position :before)' do
218248
content = "class User < ActiveRecord::Base\nend\n"
219-
MAGIC_COMMENTS.each do |magic_comment|
249+
AnnotateTestConstants::MAGIC_COMMENTS.each do |magic_comment|
220250
model_file_name, = write_model 'user.rb', "#{magic_comment}\n#{content}"
221251

222252
annotate_one_file position: :before
@@ -228,7 +258,7 @@ class User < ActiveRecord::Base
228258

229259
it 'only keeps a single empty line around the annotation (position :before)' do
230260
content = "class User < ActiveRecord::Base\nend\n"
231-
MAGIC_COMMENTS.each do |magic_comment|
261+
AnnotateTestConstants::MAGIC_COMMENTS.each do |magic_comment|
232262
schema_info = AnnotateModels::SchemaInfo.generate(@klass, '== Schema Info')
233263
model_file_name, = write_model 'user.rb', "#{magic_comment}\n\n\n\n#{content}"
234264

@@ -240,7 +270,7 @@ class User < ActiveRecord::Base
240270

241271
it 'does not change whitespace between magic comments and model file content (position :after)' do
242272
content = "class User < ActiveRecord::Base\nend\n"
243-
MAGIC_COMMENTS.each do |magic_comment|
273+
AnnotateTestConstants::MAGIC_COMMENTS.each do |magic_comment|
244274
model_file_name, = write_model 'user.rb', "#{magic_comment}\n#{content}"
245275

246276
annotate_one_file position: :after

spec/lib/annotate/annotate_models_get_model_class_spec.rb

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,11 @@
11
# encoding: utf-8
2-
require_relative '../../spec_helper'
32
require 'annotate/annotate_models'
43
require 'annotate/active_record_patch'
54
require 'active_support/core_ext/string'
65
require 'tmpdir'
76

87
RSpec.describe AnnotateModels do
9-
MAGIC_COMMENTS = [
10-
'# encoding: UTF-8',
11-
'# coding: UTF-8',
12-
'# -*- coding: UTF-8 -*-',
13-
'#encoding: utf-8',
14-
'# encoding: utf-8',
15-
'# -*- encoding : utf-8 -*-',
16-
"# encoding: utf-8\n# frozen_string_literal: true",
17-
"# frozen_string_literal: true\n# encoding: utf-8",
18-
'# frozen_string_literal: true',
19-
'#frozen_string_literal: false',
20-
'# -*- frozen_string_literal : true -*-'
21-
].freeze
22-
238
describe '.get_model_class' do
24-
before do
25-
AnnotateModels.model_dir = Dir.mktmpdir('annotate_models')
26-
end
27-
28-
# TODO: use 'files' gem instead
299
def create(filename, file_content)
3010
File.join(AnnotateModels.model_dir[0], filename).tap do |path|
3111
FileUtils.mkdir_p(File.dirname(path))
@@ -35,7 +15,8 @@ def create(filename, file_content)
3515
end
3616
end
3717

38-
before :each do
18+
before do
19+
AnnotateModels.model_dir = Dir.mktmpdir('annotate_models')
3920
create(filename, file_content)
4021
end
4122

@@ -254,7 +235,7 @@ class LoadedClass < ActiveRecord::Base
254235
EOS
255236
end
256237

257-
before :each do
238+
before do
258239
path = File.expand_path(filename, AnnotateModels.model_dir[0])
259240
Kernel.load(path)
260241
expect(Kernel).not_to receive(:require)
@@ -269,7 +250,7 @@ class LoadedClass < ActiveRecord::Base
269250
dir = Array.new(8) { (0..9).to_a.sample(random: Random.new) }.join
270251

271252
context "when class SubdirLoadedClass is defined in \"#{dir}/subdir_loaded_class.rb\"" do
272-
before :each do
253+
before do
273254
$LOAD_PATH.unshift(File.join(AnnotateModels.model_dir[0], dir))
274255

275256
path = File.expand_path(filename, AnnotateModels.model_dir[0])
@@ -297,7 +278,7 @@ class SubdirLoadedClass < ActiveRecord::Base
297278
end
298279

299280
context 'when two class exist' do
300-
before :each do
281+
before do
301282
create(filename_2, file_content_2)
302283
end
303284

@@ -319,6 +300,7 @@ class Foo < ActiveRecord::Base
319300

320301
let :file_content_2 do
321302
<<-EOS
303+
module Bar; end
322304
class Bar::Foo
323305
end
324306
EOS

spec/lib/annotate/annotate_models_get_model_files_spec.rb

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,11 @@
11
# encoding: utf-8
2-
require_relative '../../spec_helper'
32
require 'annotate/annotate_models'
43
require 'annotate/active_record_patch'
54
require 'active_support/core_ext/string'
65
require 'files'
76
require 'tmpdir'
87

98
RSpec.describe AnnotateModels do
10-
MAGIC_COMMENTS = [
11-
'# encoding: UTF-8',
12-
'# coding: UTF-8',
13-
'# -*- coding: UTF-8 -*-',
14-
'#encoding: utf-8',
15-
'# encoding: utf-8',
16-
'# -*- encoding : utf-8 -*-',
17-
"# encoding: utf-8\n# frozen_string_literal: true",
18-
"# frozen_string_literal: true\n# encoding: utf-8",
19-
'# frozen_string_literal: true',
20-
'#frozen_string_literal: false',
21-
'# -*- frozen_string_literal : true -*-'
22-
].freeze
23-
249
describe '.get_model_files' do
2510
subject { described_class.get_model_files(options) }
2611

spec/lib/annotate/annotate_models_get_patterns_spec.rb

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,11 @@
11
# encoding: utf-8
2-
require_relative '../../spec_helper'
32
require 'annotate/annotate_models'
43
require 'annotate/active_record_patch'
54
require 'active_support/core_ext/string'
65
require 'files'
76
require 'tmpdir'
87

98
RSpec.describe AnnotateModels do
10-
MAGIC_COMMENTS = [
11-
'# encoding: UTF-8',
12-
'# coding: UTF-8',
13-
'# -*- coding: UTF-8 -*-',
14-
'#encoding: utf-8',
15-
'# encoding: utf-8',
16-
'# -*- encoding : utf-8 -*-',
17-
"# encoding: utf-8\n# frozen_string_literal: true",
18-
"# frozen_string_literal: true\n# encoding: utf-8",
19-
'# frozen_string_literal: true',
20-
'#frozen_string_literal: false',
21-
'# -*- frozen_string_literal : true -*-'
22-
].freeze
23-
249
describe '.get_patterns' do
2510
subject { AnnotateModels.get_patterns(options, pattern_type) }
2611

0 commit comments

Comments
 (0)