Skip to content

Commit 88a1b8e

Browse files
cipolleschifacebook-github-bot
authored andcommitted
Use FileMock and DirMock instead of Monkey Patching (#35792)
Summary: Pull Request resolved: #35792 This Diff fixes a problem we have when running Ruby tests. The previous approach was monkey-patching the Ruby File and Dir classes to override some behaviours we needed during tests. However, these classes are also used by the test runners to properly read and run the tests, therefore when the tests were failing, the stream weren't closed properly and we received the wrong errors. This problem was also preventing us from adopting other Ruby tools like SimpleCov to compute code coverage. ## Changelog: [internal] - refactor Ruby tests not to monkey patch Dir and File Reviewed By: dmytrorykun Differential Revision: D42414717 fbshipit-source-id: 879b9928da1a083ebf9c81b1f510eaa039376042
1 parent 8bef1b3 commit 88a1b8e

13 files changed

+170
-196
lines changed

scripts/cocoapods/__tests__/codegen-test.rb

+29-31
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ class CodegenTests < Test::Unit::TestCase
2020
:tmp_schema_list_file
2121

2222
def setup
23-
File.enable_testing_mode!
24-
Dir.enable_testing_mode!
2523
Pod::Config.reset()
2624

2725
@prefix = "../.."
@@ -38,8 +36,8 @@ def teardown
3836
Pod::UI.reset()
3937
Pod::Executable.reset()
4038
Pathname.reset()
41-
File.reset()
42-
Dir.reset()
39+
FileMock.reset()
40+
DirMock.reset()
4341
end
4442

4543
# ============================================== #
@@ -48,90 +46,90 @@ def teardown
4846
def testCheckAndGenerateEmptyThirdPartyProvider_whenFileAlreadyExists_doNothing()
4947

5048
# Arrange
51-
File.mocked_existing_files([
49+
FileMock.mocked_existing_files([
5250
@base_path + "/build/" + @third_party_provider_header,
5351
@base_path + "/build/" + @third_party_provider_implementation,
5452
])
5553

5654
# Act
57-
checkAndGenerateEmptyThirdPartyProvider!(@prefix, false, 'build')
55+
checkAndGenerateEmptyThirdPartyProvider!(@prefix, false, 'build', dir_manager: DirMock, file_manager: FileMock)
5856

5957
# Assert
6058
assert_equal(Pathname.pwd_invocation_count, 1)
6159
assert_equal(Pod::Config.instance.installation_root.relative_path_from_invocation_count, 1)
62-
assert_equal(File.exist_invocation_params, [
60+
assert_equal(FileMock.exist_invocation_params, [
6361
@base_path + "/build/" + @third_party_provider_header,
6462
@base_path + "/build/" + @third_party_provider_implementation,
6563
])
66-
assert_equal(Dir.exist_invocation_params, [])
64+
assert_equal(DirMock.exist_invocation_params, [])
6765
assert_equal(Pod::UI.collected_messages, [])
6866
assert_equal($collected_commands, [])
69-
assert_equal(File.open_files.length, 0)
67+
assert_equal(FileMock.open_files.length, 0)
7068
assert_equal(Pod::Executable.executed_commands.length, 0)
7169
end
7270

7371
def testCheckAndGenerateEmptyThirdPartyProvider_whenHeaderMissingAndCodegenMissing_raiseError()
7472

7573
# Arrange
76-
File.mocked_existing_files([
74+
FileMock.mocked_existing_files([
7775
@base_path + "/build/" + @third_party_provider_implementation,
7876
])
7977

8078
# Act
8179
assert_raise {
82-
checkAndGenerateEmptyThirdPartyProvider!(@prefix, false, 'build')
80+
checkAndGenerateEmptyThirdPartyProvider!(@prefix, false, 'build', dir_manager: DirMock, file_manager: FileMock)
8381
}
8482

8583
# Assert
8684
assert_equal(Pathname.pwd_invocation_count, 1)
8785
assert_equal(Pod::Config.instance.installation_root.relative_path_from_invocation_count, 1)
88-
assert_equal(File.exist_invocation_params, [
86+
assert_equal(FileMock.exist_invocation_params, [
8987
@base_path + "/build/" + @third_party_provider_header
9088
])
91-
assert_equal(Dir.exist_invocation_params, [
89+
assert_equal(DirMock.exist_invocation_params, [
9290
@base_path + "/"+ @prefix + "/packages/react-native-codegen",
9391
@base_path + "/"+ @prefix + "/../@react-native/codegen",
9492
])
9593
assert_equal(Pod::UI.collected_messages, [])
9694
assert_equal($collected_commands, [])
97-
assert_equal(File.open_files.length, 0)
95+
assert_equal(FileMock.open_files.length, 0)
9896
assert_equal(Pod::Executable.executed_commands.length, 0)
9997
end
10098

10199
def testCheckAndGenerateEmptyThirdPartyProvider_whenImplementationMissingAndCodegenrepoExists_dontBuildCodegen()
102100

103101
# Arrange
104-
File.mocked_existing_files([
102+
FileMock.mocked_existing_files([
105103
@base_path + "/build/" + @third_party_provider_header,
106104
@base_path + "/build/tmpSchemaList.txt"
107105
])
108106

109-
Dir.mocked_existing_dirs([
107+
DirMock.mocked_existing_dirs([
110108
@base_path + "/"+ @prefix + "/packages/react-native-codegen",
111109
@base_path + "/"+ @prefix + "/packages/react-native-codegen/lib"
112110
])
113111

114112
# Act
115-
checkAndGenerateEmptyThirdPartyProvider!(@prefix, false, 'build')
113+
checkAndGenerateEmptyThirdPartyProvider!(@prefix, false, 'build', dir_manager: DirMock, file_manager: FileMock)
116114

117115
# Assert
118116
assert_equal(Pathname.pwd_invocation_count, 1)
119117
assert_equal(Pod::Config.instance.installation_root.relative_path_from_invocation_count, 1)
120-
assert_equal(File.exist_invocation_params, [
118+
assert_equal(FileMock.exist_invocation_params, [
121119
@base_path + "/build/" + @third_party_provider_header,
122120
@base_path + "/build/" + @third_party_provider_implementation,
123121
@base_path + "/build/tmpSchemaList.txt",
124122
])
125-
assert_equal(Dir.exist_invocation_params, [
123+
assert_equal(DirMock.exist_invocation_params, [
126124
@base_path + "/"+ @prefix + "/packages/react-native-codegen",
127125
@base_path + "/"+ @prefix + "/packages/react-native-codegen/lib",
128126
])
129127
assert_equal(Pod::UI.collected_messages, ["[Codegen] generating an empty RCTThirdPartyFabricComponentsProvider"])
130128
assert_equal($collected_commands, [])
131-
assert_equal(File.open_invocation_count, 1)
132-
assert_equal(File.open_files_with_mode[@base_path + "/build/tmpSchemaList.txt"], 'w')
133-
assert_equal(File.open_files[0].collected_write, ["[]"])
134-
assert_equal(File.open_files[0].fsync_invocation_count, 1)
129+
assert_equal(FileMock.open_invocation_count, 1)
130+
assert_equal(FileMock.open_files_with_mode[@base_path + "/build/tmpSchemaList.txt"], 'w')
131+
assert_equal(FileMock.open_files[0].collected_write, ["[]"])
132+
assert_equal(FileMock.open_files[0].fsync_invocation_count, 1)
135133
assert_equal(Pod::Executable.executed_commands[0], {
136134
"command" => "node",
137135
"arguments" => [
@@ -141,27 +139,27 @@ def testCheckAndGenerateEmptyThirdPartyProvider_whenImplementationMissingAndCode
141139
"--outputDir", @base_path + "/build"
142140
]
143141
})
144-
assert_equal(File.delete_invocation_count, 1)
145-
assert_equal(File.deleted_files, [@base_path + "/build/tmpSchemaList.txt"])
142+
assert_equal(FileMock.delete_invocation_count, 1)
143+
assert_equal(FileMock.deleted_files, [@base_path + "/build/tmpSchemaList.txt"])
146144
end
147145

148146
def testCheckAndGenerateEmptyThirdPartyProvider_whenBothMissing_buildCodegen()
149147
# Arrange
150148
codegen_cli_path = @base_path + "/" + @prefix + "/../@react-native/codegen"
151-
Dir.mocked_existing_dirs([
149+
DirMock.mocked_existing_dirs([
152150
codegen_cli_path,
153151
])
154152
# Act
155-
checkAndGenerateEmptyThirdPartyProvider!(@prefix, false, 'build')
153+
checkAndGenerateEmptyThirdPartyProvider!(@prefix, false, 'build', dir_manager: DirMock, file_manager: FileMock)
156154

157155
# Assert
158156
assert_equal(Pathname.pwd_invocation_count, 1)
159157
assert_equal(Pod::Config.instance.installation_root.relative_path_from_invocation_count, 1)
160-
assert_equal(File.exist_invocation_params, [
158+
assert_equal(FileMock.exist_invocation_params, [
161159
@base_path + "/build/" + @third_party_provider_header,
162160
@base_path + "/build/" + @tmp_schema_list_file
163161
])
164-
assert_equal(Dir.exist_invocation_params, [
162+
assert_equal(DirMock.exist_invocation_params, [
165163
@base_path + "/" + @prefix + "/packages/react-native-codegen",
166164
codegen_cli_path,
167165
codegen_cli_path + "/lib",
@@ -171,8 +169,8 @@ def testCheckAndGenerateEmptyThirdPartyProvider_whenBothMissing_buildCodegen()
171169
"[Codegen] generating an empty RCTThirdPartyFabricComponentsProvider"
172170
])
173171
assert_equal($collected_commands, ["~/app/ios/../../../@react-native/codegen/scripts/oss/build.sh"])
174-
assert_equal(File.open_files[0].collected_write, ["[]"])
175-
assert_equal(File.open_files[0].fsync_invocation_count, 1)
172+
assert_equal(FileMock.open_files[0].collected_write, ["[]"])
173+
assert_equal(FileMock.open_files[0].fsync_invocation_count, 1)
176174
assert_equal(Pod::Executable.executed_commands[0], {
177175
"command" => "node",
178176
"arguments" => [

0 commit comments

Comments
 (0)