Files
react-native/scripts/cocoapods/__tests__/test_utils/FileUtilsMock.rb
T
Riccardo Cipolleschi 88a1b8e18d Use FileMock and DirMock instead of Monkey Patching (#35792)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/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
2023-01-10 06:43:39 -08:00

41 lines
974 B
Ruby

# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# 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 = []
def self.rmrf_invocation_count
return @@RMRF_INVOCATION_COUNT
end
def self.increase_rmrfi_invocation_count
@@RMRF_INVOCATION_COUNT += 1
end
def self.rmrf_paths
return @@RMRF_PATHS
end
def self.push_rmrf_path(path)
@@RMRF_PATHS.push(path)
end
def self.reset
@@RMRF_INVOCATION_COUNT = 0
@@RMRF_PATHS = []
end
end
def self.rm_rf(path)
FileUtilsStorage.push_rmrf_path(path)
FileUtilsStorage.increase_rmrfi_invocation_count
DirMock.remove_mocked_paths(path)
end
end