Skip to content

Commit

Permalink
Fix lib file moves (#164)
Browse files Browse the repository at this point in the history
* Fix moving lib files

* bump version

* fix sorbet issue
  • Loading branch information
ashleywillard authored Oct 30, 2024
1 parent f61b80d commit 856fc13
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ GIT
PATH
remote: .
specs:
packs (0.0.44)
packs (0.0.45)
bigdecimal
code_ownership (>= 1.33.0)
packs-specification
Expand Down
45 changes: 40 additions & 5 deletions lib/packs/private/file_move_operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,59 @@ def self.destination_pathname_for_new_public_api(origin_pathname)

sig { returns(FileMoveOperation) }
def spec_file_move_operation
path_parts = filepath_without_pack_name.split('/')
folder = T.must(path_parts[0])
file_extension = T.must(filepath_without_pack_name.split('.').last)

# This could probably be implemented by some "strategy pattern" where different extension types are handled by different helpers
# Such a thing could also include, for example, when moving a controller, moving its ERB view too.
if origin_pathname.extname == '.rake'
new_origin_pathname = origin_pathname.sub('/lib/', '/spec/lib/').sub(%r{^lib/}, 'spec/lib/').sub('.rake', '_spec.rb')
new_destination_pathname = destination_pathname.sub('/lib/', '/spec/lib/').sub(%r{^lib/}, 'spec/lib/').sub('.rake', '_spec.rb')
if folder == 'app'
new_origin_pathname = spec_pathname_for_app(origin_pathname, file_extension)
new_destination_pathname = spec_pathname_for_app(destination_pathname, file_extension)
else
new_origin_pathname = origin_pathname.sub('/app/', '/spec/').sub(%r{^app/}, 'spec/').sub('.rb', '_spec.rb')
new_destination_pathname = destination_pathname.sub('/app/', '/spec/').sub(%r{^app/}, 'spec/').sub('.rb', '_spec.rb')
new_origin_pathname = spec_pathname_for_non_app(origin_pathname, file_extension, folder)
new_destination_pathname = spec_pathname_for_non_app(destination_pathname, file_extension, folder)
end

FileMoveOperation.new(
origin_pathname: new_origin_pathname,
destination_pathname: new_destination_pathname,
destination_pack: destination_pack
)
end

sig { params(filepath: Pathname, pack: T.nilable(Packs::Pack)).returns(String) }
def self.get_filepath_without_pack_name(filepath, pack)
if pack
filepath.to_s.gsub("#{pack.name}/", '')
else
filepath.to_s
end
end

private

sig { returns(String) }
def filepath_without_pack_name
self.class.get_filepath_without_pack_name(origin_pathname, origin_pack)
end

sig { params(pathname: Pathname, file_extension: String).returns(Pathname) }
def spec_pathname_for_app(pathname, file_extension)
pathname
.sub('/app/', '/spec/')
.sub(%r{^app/}, 'spec/')
.sub(".#{file_extension}", '_spec.rb')
end

sig { params(pathname: Pathname, file_extension: String, folder: String).returns(Pathname) }
def spec_pathname_for_non_app(pathname, file_extension, folder)
pathname
.sub("/#{folder}/", "/spec/#{folder}/")
.sub(%r{^#{folder}/}, "spec/#{folder}/")
.sub(".#{file_extension}", '_spec.rb')
end

sig { params(path: Pathname).returns(FileMoveOperation) }
def relative_to(path)
FileMoveOperation.new(
Expand Down
2 changes: 1 addition & 1 deletion packs.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |spec|
spec.name = 'packs'
spec.version = '0.0.44'
spec.version = '0.0.45'
spec.authors = ['Gusto Engineers']
spec.email = ['[email protected]']

Expand Down
26 changes: 26 additions & 0 deletions spec/packs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,32 @@ def write_codeownership_config
end
end

context 'files moved are ruby files in lib' do
it 'can move files from lib from one pack to another pack' do
write_package_yml('packs/my_pack')
write_package_yml('packs/organisms')
write_file('packs/organisms/lib/my_ruby_file.rb')
write_file('packs/organisms/spec/lib/my_ruby_file_spec.rb')

Packs.move_to_pack!(
pack_name: 'packs/my_pack',
paths_relative_to_root: [
'packs/organisms/lib/my_ruby_file.rb'
]
)

expect_files_to_not_exist([
'packs/organisms/lib/my_ruby_file.rb',
'packs/organisms/spec/lib/my_ruby_file_spec.rb'
])

expect_files_to_exist([
'packs/my_pack/lib/my_ruby_file.rb',
'packs/my_pack/spec/lib/my_ruby_file_spec.rb'
])
end
end

describe 'RubocopPostProcessor' do
context 'moving file listed in top-level .rubocop_todo.yml' do
it 'modifies an application-specific file, .rubocop_todo.yml, correctly' do
Expand Down

0 comments on commit 856fc13

Please sign in to comment.