Skip to content

Commit f9ebcde

Browse files
committed
use general file resolution for test files as well
the special-purpose file resolution method for test/spec files was failing with namespaced test files (ex. User model attributes would annotate both test/unit/user_test.rb as well as test/unit/foo/user_test.rb, instead of only the former)
1 parent 2eb9714 commit f9ebcde

File tree

1 file changed

+4
-18
lines changed

1 file changed

+4
-18
lines changed

lib/annotate/annotate_models.rb

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ module AnnotateModels
3131
FABRICATORS_SPEC_DIR = File.join("spec", "fabricators")
3232

3333
TEST_PATTERNS = [
34-
[UNIT_TEST_DIR, "%MODEL_NAME%_test.rb"],
35-
[SPEC_MODEL_DIR, "%MODEL_NAME%_spec.rb"],
34+
File.join(UNIT_TEST_DIR, "%MODEL_NAME%_test.rb"),
35+
File.join(SPEC_MODEL_DIR, "%MODEL_NAME%_spec.rb"),
3636
]
3737

3838
FIXTURE_PATTERNS = [
@@ -301,8 +301,7 @@ def annotate(klass, file, header, options={})
301301

302302
unless options[:exclude_tests]
303303
did_annotate = TEST_PATTERNS.
304-
map { |pat| [pat[0], resolve_filename(pat[1], model_name, table_name)] }.
305-
map { |pat| find_test_file(*pat) }.
304+
map { |file| resolve_filename(file, model_name, table_name) }.
306305
map { |file| annotate_one_file(file, info, :position_in_test, options_with_position(options, :position_in_test)) }.
307306
detect { |result| result } || did_annotate
308307
end
@@ -441,16 +440,7 @@ def remove_annotations(options={})
441440
model_file_name = File.join(model_dir, file)
442441
deannotated_klass = true if(remove_annotation_of_file(model_file_name))
443442

444-
TEST_PATTERNS.
445-
map { |pat| [pat[0], resolve_filename(pat[1], model_name, table_name)]}.
446-
map { |pat| find_test_file(*pat) }.each do |file|
447-
if(File.exist?(file))
448-
remove_annotation_of_file(file)
449-
deannotated_klass = true
450-
end
451-
end
452-
453-
(FIXTURE_PATTERNS + FACTORY_PATTERNS).
443+
(TEST_PATTERNS + FIXTURE_PATTERNS + FACTORY_PATTERNS).
454444
map { |file| resolve_filename(file, model_name, table_name) }.
455445
each do |file|
456446
if File.exist?(file)
@@ -468,10 +458,6 @@ def remove_annotations(options={})
468458
puts "Removed annotations from: #{deannotated.join(', ')}"
469459
end
470460

471-
def find_test_file(dir, file_name)
472-
Dir.glob(File.join(dir, "**", file_name)).first || File.join(dir, file_name)
473-
end
474-
475461
def resolve_filename(filename_template, model_name, table_name)
476462
return filename_template.
477463
gsub('%MODEL_NAME%', model_name).

0 commit comments

Comments
 (0)