diff --git a/packages/react-native/scripts/cocoapods/__tests__/test_utils/InstallerMock.rb b/packages/react-native/scripts/cocoapods/__tests__/test_utils/InstallerMock.rb index 9b96f82a22b..6c0bfc7a353 100644 --- a/packages/react-native/scripts/cocoapods/__tests__/test_utils/InstallerMock.rb +++ b/packages/react-native/scripts/cocoapods/__tests__/test_utils/InstallerMock.rb @@ -114,14 +114,15 @@ class UserProjectMock attr_reader :path attr_reader :build_configurations attr_reader :native_targets + attr_reader :files attr_reader :save_invocation_count - - def initialize(path = "/test/path.xcproj", build_configurations = [], native_targets: []) - @path = Pathname.new(path) + def initialize(path = "/test/path.xcproj", build_configurations = [], parent = "/test", native_targets: [], files: []) + @path = Pathname.new(path, parent) @build_configurations = build_configurations @native_targets = native_targets + @files = files @save_invocation_count = 0 end @@ -130,6 +131,16 @@ class UserProjectMock end end +class PBXFileRefMock + attr_reader :name + attr_reader :path + + def initialize(name) + @name = name + @path = name + end +end + class XCConfigMock attr_reader :name attr_accessor :attributes diff --git a/packages/react-native/scripts/cocoapods/__tests__/test_utils/PathnameMock.rb b/packages/react-native/scripts/cocoapods/__tests__/test_utils/PathnameMock.rb index 19e9a5b282c..883aeea077f 100644 --- a/packages/react-native/scripts/cocoapods/__tests__/test_utils/PathnameMock.rb +++ b/packages/react-native/scripts/cocoapods/__tests__/test_utils/PathnameMock.rb @@ -9,8 +9,9 @@ class Pathname attr_reader :path - def initialize(path) + def initialize(path, parent = "") @path = path + @parent = parent end def realpath @@ -21,6 +22,10 @@ class Pathname return @path end + def parent + return @parent + end + def self.pwd!(pwd) @@pwd = pwd end diff --git a/packages/react-native/scripts/cocoapods/__tests__/test_utils/XcodeprojMock.rb b/packages/react-native/scripts/cocoapods/__tests__/test_utils/XcodeprojMock.rb new file mode 100644 index 00000000000..7737a1bba45 --- /dev/null +++ b/packages/react-native/scripts/cocoapods/__tests__/test_utils/XcodeprojMock.rb @@ -0,0 +1,21 @@ +# 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. + +module Xcodeproj + class Plist + @@path_to_file_mapping = Hash.new + def self.read_from_path(path) + return @@path_to_file_mapping[path] + end + + def self.write_to_path(hash, path) + @@path_to_file_mapping[path] = hash + end + + def self.reset() + @@path_to_file_mapping.clear + end + end +end diff --git a/packages/react-native/scripts/cocoapods/__tests__/utils-test.rb b/packages/react-native/scripts/cocoapods/__tests__/utils-test.rb index 39a74d189a6..1d65edb1f97 100644 --- a/packages/react-native/scripts/cocoapods/__tests__/utils-test.rb +++ b/packages/react-native/scripts/cocoapods/__tests__/utils-test.rb @@ -14,6 +14,7 @@ require_relative "./test_utils/FileMock.rb" require_relative "./test_utils/systemUtils.rb" require_relative "./test_utils/PathnameMock.rb" require_relative "./test_utils/TargetDefinitionMock.rb" +require_relative "./test_utils/XcodeprojMock.rb" class UtilsTests < Test::Unit::TestCase def setup @@ -28,6 +29,7 @@ class UtilsTests < Test::Unit::TestCase Pod::Config.reset() SysctlChecker.reset() Environment.reset() + Xcodeproj::Plist.reset() ENV['RCT_NEW_ARCH_ENABLED'] = '0' ENV['USE_HERMES'] = '1' ENV['USE_FRAMEWORKS'] = nil @@ -772,6 +774,59 @@ class UtilsTests < Test::Unit::TestCase assert_equal(config.build_settings["OTHER_CFLAGS"], "$(inherited)") end end + + # ============================== # + # Test - Apply ATS configuration # + # ============================== # + + def test_applyATSConfig_plistNil + # Arrange + user_project_mock = prepare_user_project_mock_with_plists() + pods_projects_mock = PodsProjectMock.new([], {"some_pod" => {}}) + installer = InstallerMock.new(pods_projects_mock, [ + AggregatedProjectMock.new(user_project_mock) + ]) + + # # Act + ReactNativePodsUtils.apply_ats_config(installer) + + # # Assert + assert_equal(user_project_mock.files.length, 2) + user_project_mock.files.each do |file| + path = File.join(user_project_mock.path.parent, file.name) + plist = Xcodeproj::Plist.read_from_path(path) + assert_equal(plist['NSAppTransportSecurity'], { + 'NSAllowsArbitraryLoads' => false, + 'NSAllowsLocalNetworking' => true, + }); + end + end + + def test_applyATSConfig_plistNonNil + # Arrange + user_project_mock = prepare_user_project_mock_with_plists() + pods_projects_mock = PodsProjectMock.new([], {"some_pod" => {}}) + installer = InstallerMock.new(pods_projects_mock, [ + AggregatedProjectMock.new(user_project_mock) + ]) + Xcodeproj::Plist.write_to_path({}, "/test/Info.plist") + Xcodeproj::Plist.write_to_path({}, "/test/Extension-Info.plist") + + # # Act + ReactNativePodsUtils.apply_ats_config(installer) + + # # Assert + assert_equal(user_project_mock.files.length, 2) + user_project_mock.files.each do |file| + path = File.join(user_project_mock.path.parent, file.name) + plist = Xcodeproj::Plist.read_from_path(path) + assert_equal(plist['NSAppTransportSecurity'], { + 'NSAllowsArbitraryLoads' => false, + 'NSAllowsLocalNetworking' => true, + }); + end + end + end # ===== # @@ -785,6 +840,13 @@ def prepare_empty_user_project_mock ]) end +def prepare_user_project_mock_with_plists + return UserProjectMock.new(:files => [ + PBXFileRefMock.new("Info.plist"), + PBXFileRefMock.new("Extension-Info.plist"), + ]) +end + def prepare_config(config_name) return BuildConfigurationMock.new(config_name, {"LIBRARY_SEARCH_PATHS" => [ "$(TOOLCHAIN_DIR)/usr/lib/swift-5.0/$(PLATFORM_NAME)",