@@ -53,24 +53,59 @@ def self.destination_pathname_for_new_public_api(origin_pathname)
5353
5454 sig { returns ( FileMoveOperation ) }
5555 def spec_file_move_operation
56+ path_parts = filepath_without_pack_name . split ( '/' )
57+ folder = T . must ( path_parts [ 0 ] )
58+ file_extension = T . must ( filepath_without_pack_name . split ( '.' ) . last )
59+
5660 # This could probably be implemented by some "strategy pattern" where different extension types are handled by different helpers
5761 # Such a thing could also include, for example, when moving a controller, moving its ERB view too.
58- if origin_pathname . extname == '.rake '
59- new_origin_pathname = origin_pathname . sub ( '/lib/' , '/spec/lib/' ) . sub ( %r{^lib/} , 'spec/lib/' ) . sub ( '.rake' , '_spec.rb' )
60- new_destination_pathname = destination_pathname . sub ( '/lib/' , '/spec/lib/' ) . sub ( %r{^lib/} , 'spec/lib/' ) . sub ( '.rake' , '_spec.rb' )
62+ if folder == 'app '
63+ new_origin_pathname = spec_pathname_for_app ( origin_pathname , file_extension )
64+ new_destination_pathname = spec_pathname_for_app ( destination_pathname , file_extension )
6165 else
62- new_origin_pathname = origin_pathname . sub ( '/app/' , '/spec/' ) . sub ( %r{^app/} , 'spec/' ) . sub ( '.rb' , '_spec.rb' )
63- new_destination_pathname = destination_pathname . sub ( '/app/' , '/spec/' ) . sub ( %r{^app/} , 'spec/' ) . sub ( '.rb' , '_spec.rb' )
66+ new_origin_pathname = spec_pathname_for_non_app ( origin_pathname , file_extension , folder )
67+ new_destination_pathname = spec_pathname_for_non_app ( destination_pathname , file_extension , folder )
6468 end
69+
6570 FileMoveOperation . new (
6671 origin_pathname : new_origin_pathname ,
6772 destination_pathname : new_destination_pathname ,
6873 destination_pack : destination_pack
6974 )
7075 end
7176
77+ sig { params ( filepath : Pathname , pack : T . nilable ( Packs ::Pack ) ) . returns ( String ) }
78+ def self . get_filepath_without_pack_name ( filepath , pack )
79+ if pack
80+ filepath . to_s . gsub ( "#{ pack . name } /" , '' )
81+ else
82+ filepath . to_s
83+ end
84+ end
85+
7286 private
7387
88+ sig { returns ( String ) }
89+ def filepath_without_pack_name
90+ self . class . get_filepath_without_pack_name ( origin_pathname , origin_pack )
91+ end
92+
93+ sig { params ( pathname : Pathname , file_extension : String ) . returns ( Pathname ) }
94+ def spec_pathname_for_app ( pathname , file_extension )
95+ pathname
96+ . sub ( '/app/' , '/spec/' )
97+ . sub ( %r{^app/} , 'spec/' )
98+ . sub ( ".#{ file_extension } " , '_spec.rb' )
99+ end
100+
101+ sig { params ( pathname : Pathname , file_extension : String , folder : String ) . returns ( Pathname ) }
102+ def spec_pathname_for_non_app ( pathname , file_extension , folder )
103+ pathname
104+ . sub ( "/#{ folder } /" , "/spec/#{ folder } /" )
105+ . sub ( %r{^#{ folder } /} , "spec/#{ folder } /" )
106+ . sub ( ".#{ file_extension } " , '_spec.rb' )
107+ end
108+
74109 sig { params ( path : Pathname ) . returns ( FileMoveOperation ) }
75110 def relative_to ( path )
76111 FileMoveOperation . new (
0 commit comments