Skip to content

Commit 856fc13

Browse files
Fix lib file moves (#164)
* Fix moving lib files * bump version * fix sorbet issue
1 parent f61b80d commit 856fc13

File tree

4 files changed

+68
-7
lines changed

4 files changed

+68
-7
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ GIT
1717
PATH
1818
remote: .
1919
specs:
20-
packs (0.0.44)
20+
packs (0.0.45)
2121
bigdecimal
2222
code_ownership (>= 1.33.0)
2323
packs-specification

lib/packs/private/file_move_operation.rb

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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(

packs.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |spec|
22
spec.name = 'packs'
3-
spec.version = '0.0.44'
3+
spec.version = '0.0.45'
44
spec.authors = ['Gusto Engineers']
55
spec.email = ['[email protected]']
66

spec/packs_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,32 @@ def write_codeownership_config
567567
end
568568
end
569569

570+
context 'files moved are ruby files in lib' do
571+
it 'can move files from lib from one pack to another pack' do
572+
write_package_yml('packs/my_pack')
573+
write_package_yml('packs/organisms')
574+
write_file('packs/organisms/lib/my_ruby_file.rb')
575+
write_file('packs/organisms/spec/lib/my_ruby_file_spec.rb')
576+
577+
Packs.move_to_pack!(
578+
pack_name: 'packs/my_pack',
579+
paths_relative_to_root: [
580+
'packs/organisms/lib/my_ruby_file.rb'
581+
]
582+
)
583+
584+
expect_files_to_not_exist([
585+
'packs/organisms/lib/my_ruby_file.rb',
586+
'packs/organisms/spec/lib/my_ruby_file_spec.rb'
587+
])
588+
589+
expect_files_to_exist([
590+
'packs/my_pack/lib/my_ruby_file.rb',
591+
'packs/my_pack/spec/lib/my_ruby_file_spec.rb'
592+
])
593+
end
594+
end
595+
570596
describe 'RubocopPostProcessor' do
571597
context 'moving file listed in top-level .rubocop_todo.yml' do
572598
it 'modifies an application-specific file, .rubocop_todo.yml, correctly' do

0 commit comments

Comments
 (0)