From 856fc13b40b35e7dc0161f876935e81b8dc90933 Mon Sep 17 00:00:00 2001 From: ashleywillard Date: Wed, 30 Oct 2024 13:32:32 -0700 Subject: [PATCH] Fix lib file moves (#164) * Fix moving lib files * bump version * fix sorbet issue --- Gemfile.lock | 2 +- lib/packs/private/file_move_operation.rb | 45 +++++++++++++++++++++--- packs.gemspec | 2 +- spec/packs_spec.rb | 26 ++++++++++++++ 4 files changed, 68 insertions(+), 7 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index cfeab32..7621719 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -17,7 +17,7 @@ GIT PATH remote: . specs: - packs (0.0.44) + packs (0.0.45) bigdecimal code_ownership (>= 1.33.0) packs-specification diff --git a/lib/packs/private/file_move_operation.rb b/lib/packs/private/file_move_operation.rb index 8a5a568..2b5439e 100644 --- a/lib/packs/private/file_move_operation.rb +++ b/lib/packs/private/file_move_operation.rb @@ -53,15 +53,20 @@ 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, @@ -69,8 +74,38 @@ def spec_file_move_operation ) 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( diff --git a/packs.gemspec b/packs.gemspec index 8e4b13e..f009b6e 100644 --- a/packs.gemspec +++ b/packs.gemspec @@ -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 = ['dev@gusto.com'] diff --git a/spec/packs_spec.rb b/spec/packs_spec.rb index 2d7ab98..568be16 100644 --- a/spec/packs_spec.rb +++ b/spec/packs_spec.rb @@ -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