Improve Codegen Cleanup (#35642)

Summary:
This PR adds a safety check in case the Cocoapod build script is not able to clean the build folder.
We had evidences where this process failed, and in this way the user has a clear and actionable message to fix its situation.

## Changelog

[iOS][Added] - Add message with instructions about what to do if the cleanup of the build folder fails.

Pull Request resolved: https://github.com/facebook/react-native/pull/35642

Test Plan:
I was not able to reproduce the issue locally.
The fix is not destructive, let's see if the amount of issues decreases.

Reviewed By: dmytrorykun

Differential Revision: D42067939

Pulled By: cipolleschi

fbshipit-source-id: 433dbfaec42a1bf460dc9a48051aa51ec6e12d16
This commit is contained in:
Riccardo Cipolleschi
2022-12-16 04:47:01 -08:00
committed by Facebook GitHub Bot
parent f249bee171
commit 1b7127bb05
4 changed files with 69 additions and 6 deletions
@@ -423,12 +423,59 @@ class CodegenUtilsTests < Test::Unit::TestCase
CodegenUtils.clean_up_build_folder(@base_path, codegen_dir)
# Assert
assert_equal(Dir.exist_invocation_params, [codegen_path])
assert_equal(Dir.glob_invocation, ["#{codegen_path}/*"])
assert_equal(Dir.exist_invocation_params, [codegen_path, codegen_path])
assert_equal(Dir.glob_invocation, ["#{codegen_path}/*", "#{codegen_path}/*"])
assert_equal(FileUtils::FileUtilsStorage.rmrf_invocation_count, 1)
assert_equal(FileUtils::FileUtilsStorage.rmrf_paths, [globs])
assert_equal(CodegenUtils.cleanup_done(), true)
end
# ===================================== #
# Test - Assert Codegen Folder Is Empty #
# ===================================== #
def test_assertCodegenFolderIsEmpty_whenItDoesNotExists_doesNotAbort
# Arrange
codegen_dir = "build/generated/ios"
codegen_path = "#{@base_path}/#{codegen_dir}"
# Act
CodegenUtils.assert_codegen_folder_is_empty(@base_path, codegen_dir)
# Assert
assert_equal(Pod::UI.collected_warns, [])
end
def test_assertCodegenFolderIsEmpty_whenItExistsAndIsEmpty_doesNotAbort
# Arrange
codegen_dir = "build/generated/ios"
codegen_path = "#{@base_path}/#{codegen_dir}"
Dir.mocked_existing_dirs(codegen_path)
Dir.mocked_existing_globs([], "#{codegen_path}/*")
# Act
CodegenUtils.assert_codegen_folder_is_empty(@base_path, codegen_dir)
# Assert
assert_equal(Pod::UI.collected_warns, [])
end
def test_assertCodegenFolderIsEmpty_whenItIsNotEmpty_itAborts
# Arrange
codegen_dir = "build/generated/ios"
codegen_path = "#{@base_path}/#{codegen_dir}"
Dir.mocked_existing_dirs(codegen_path)
Dir.mocked_existing_globs(["#{codegen_path}/MyModuleSpecs/MyModule.mm",], "#{codegen_path}/*")
# Act
assert_raises() {
CodegenUtils.assert_codegen_folder_is_empty(@base_path, codegen_dir)
}
# Assert
assert_equal(Pod::UI.collected_warns, [
"Unable to remove the content of ~/app/ios/build/generated/ios folder. Please run rm -rf ~/app/ios/build/generated/ios and try again."
])
end
private
@@ -49,7 +49,11 @@ class Dir
def self.glob(path)
@@glob_invocation.push(path)
return @@mocked_existing_globs[path]
return @@mocked_existing_globs[path] != nil ? @@mocked_existing_globs[path] : []
end
def self.remove_mocked_paths(path)
@@mocked_existing_globs = @@mocked_existing_globs.select { |k, v| v != path }
end
def self.set_pwd(pwd)
@@ -3,9 +3,9 @@
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
require_relative './DirMock.rb'
module FileUtils
class FileUtilsStorage
@@RMRF_INVOCATION_COUNT = 0
@@RMRF_PATHS = []
@@ -35,5 +35,6 @@ module FileUtils
def self.rm_rf(path)
FileUtilsStorage.push_rmrf_path(path)
FileUtilsStorage.increase_rmrfi_invocation_count
Dir.remove_mocked_paths(path)
end
end
+12 -1
View File
@@ -314,6 +314,17 @@ class CodegenUtils
return if !Dir.exist?(codegen_path)
FileUtils.rm_rf(Dir.glob("#{codegen_path}/*"))
CodegenUtils.set_cleanup_done(true)
CodegenUtils.assert_codegen_folder_is_empty(app_path, codegen_dir)
end
# Need to split this function from the previous one to be able to test it properly.
def self.assert_codegen_folder_is_empty(app_path, codegen_dir)
# double check that the files have actually been deleted.
# Emit an error message if not.
codegen_path = File.join(app_path, codegen_dir)
if Dir.exist?(codegen_path) && Dir.glob("#{codegen_path}/*").length() != 0
Pod::UI.warn "Unable to remove the content of #{codegen_path} folder. Please run rm -rf #{codegen_path} and try again."
abort
end
end
end