@@ -53,24 +53,59 @@ def self.destination_pathname_for_new_public_api(origin_pathname)
53
53
54
54
sig { returns ( FileMoveOperation ) }
55
55
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
+
56
60
# This could probably be implemented by some "strategy pattern" where different extension types are handled by different helpers
57
61
# 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 )
61
65
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 )
64
68
end
69
+
65
70
FileMoveOperation . new (
66
71
origin_pathname : new_origin_pathname ,
67
72
destination_pathname : new_destination_pathname ,
68
73
destination_pack : destination_pack
69
74
)
70
75
end
71
76
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
+
72
86
private
73
87
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
+
74
109
sig { params ( path : Pathname ) . returns ( FileMoveOperation ) }
75
110
def relative_to ( path )
76
111
FileMoveOperation . new (
0 commit comments