Skip to content

Commit aaa795b

Browse files
author
Riccardo Cipolleschi
committed
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: #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
1 parent a0ee98d commit aaa795b

File tree

4 files changed

+69
-6
lines changed

4 files changed

+69
-6
lines changed

scripts/cocoapods/__tests__/codegen_utils-test.rb

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,12 +423,59 @@ def testCleanUpCodegenFolder_whenFolderExists_deleteItAndSetCleanupDone
423423
CodegenUtils.clean_up_build_folder(@base_path, codegen_dir)
424424

425425
# Assert
426-
assert_equal(Dir.exist_invocation_params, [codegen_path])
427-
assert_equal(Dir.glob_invocation, ["#{codegen_path}/*"])
426+
assert_equal(Dir.exist_invocation_params, [codegen_path, codegen_path])
427+
assert_equal(Dir.glob_invocation, ["#{codegen_path}/*", "#{codegen_path}/*"])
428428
assert_equal(FileUtils::FileUtilsStorage.rmrf_invocation_count, 1)
429429
assert_equal(FileUtils::FileUtilsStorage.rmrf_paths, [globs])
430430
assert_equal(CodegenUtils.cleanup_done(), true)
431+
end
432+
433+
# ===================================== #
434+
# Test - Assert Codegen Folder Is Empty #
435+
# ===================================== #
436+
437+
def test_assertCodegenFolderIsEmpty_whenItDoesNotExists_doesNotAbort
438+
# Arrange
439+
codegen_dir = "build/generated/ios"
440+
codegen_path = "#{@base_path}/#{codegen_dir}"
441+
442+
# Act
443+
CodegenUtils.assert_codegen_folder_is_empty(@base_path, codegen_dir)
444+
445+
# Assert
446+
assert_equal(Pod::UI.collected_warns, [])
447+
end
431448

449+
def test_assertCodegenFolderIsEmpty_whenItExistsAndIsEmpty_doesNotAbort
450+
# Arrange
451+
codegen_dir = "build/generated/ios"
452+
codegen_path = "#{@base_path}/#{codegen_dir}"
453+
Dir.mocked_existing_dirs(codegen_path)
454+
Dir.mocked_existing_globs([], "#{codegen_path}/*")
455+
456+
# Act
457+
CodegenUtils.assert_codegen_folder_is_empty(@base_path, codegen_dir)
458+
459+
# Assert
460+
assert_equal(Pod::UI.collected_warns, [])
461+
end
462+
463+
def test_assertCodegenFolderIsEmpty_whenItIsNotEmpty_itAborts
464+
# Arrange
465+
codegen_dir = "build/generated/ios"
466+
codegen_path = "#{@base_path}/#{codegen_dir}"
467+
Dir.mocked_existing_dirs(codegen_path)
468+
Dir.mocked_existing_globs(["#{codegen_path}/MyModuleSpecs/MyModule.mm",], "#{codegen_path}/*")
469+
470+
# Act
471+
assert_raises() {
472+
CodegenUtils.assert_codegen_folder_is_empty(@base_path, codegen_dir)
473+
}
474+
475+
# Assert
476+
assert_equal(Pod::UI.collected_warns, [
477+
"Unable to remove the content of ~/app/ios/build/generated/ios folder. Please run rm -rf ~/app/ios/build/generated/ios and try again."
478+
])
432479
end
433480

434481
private

scripts/cocoapods/__tests__/test_utils/DirMock.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ def self.glob_invocation
4949

5050
def self.glob(path)
5151
@@glob_invocation.push(path)
52-
return @@mocked_existing_globs[path]
52+
return @@mocked_existing_globs[path] != nil ? @@mocked_existing_globs[path] : []
53+
end
54+
55+
def self.remove_mocked_paths(path)
56+
@@mocked_existing_globs = @@mocked_existing_globs.select { |k, v| v != path }
5357
end
5458

5559
def self.set_pwd(pwd)

scripts/cocoapods/__tests__/test_utils/FileUtilsMock.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
# This source code is licensed under the MIT license found in the
44
# LICENSE file in the root directory of this source tree.
55

6-
module FileUtils
7-
6+
require_relative './DirMock.rb'
87

8+
module FileUtils
99
class FileUtilsStorage
1010
@@RMRF_INVOCATION_COUNT = 0
1111
@@RMRF_PATHS = []
@@ -35,5 +35,6 @@ def self.reset
3535
def self.rm_rf(path)
3636
FileUtilsStorage.push_rmrf_path(path)
3737
FileUtilsStorage.increase_rmrfi_invocation_count
38+
Dir.remove_mocked_paths(path)
3839
end
3940
end

scripts/cocoapods/codegen_utils.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,17 @@ def self.clean_up_build_folder(app_path, codegen_dir)
314314
return if !Dir.exist?(codegen_path)
315315

316316
FileUtils.rm_rf(Dir.glob("#{codegen_path}/*"))
317-
CodegenUtils.set_cleanup_done(true)
317+
CodegenUtils.assert_codegen_folder_is_empty(app_path, codegen_dir)
318+
end
319+
320+
# Need to split this function from the previous one to be able to test it properly.
321+
def self.assert_codegen_folder_is_empty(app_path, codegen_dir)
322+
# double check that the files have actually been deleted.
323+
# Emit an error message if not.
324+
codegen_path = File.join(app_path, codegen_dir)
325+
if Dir.exist?(codegen_path) && Dir.glob("#{codegen_path}/*").length() != 0
326+
Pod::UI.warn "Unable to remove the content of #{codegen_path} folder. Please run rm -rf #{codegen_path} and try again."
327+
abort
328+
end
318329
end
319330
end

0 commit comments

Comments
 (0)