Skip to content

Commit d320334

Browse files
philIipfacebook-github-bot
authored andcommitted
tests for ats settings (#38084)
Summary: Pull Request resolved: #38084 Changelog: [Internal] addings tests for D47041590 Reviewed By: cipolleschi Differential Revision: D46961587 fbshipit-source-id: 6b93a20c8539d692db2f84836a0ab79d899197fa
1 parent c86f15a commit d320334

File tree

4 files changed

+103
-4
lines changed

4 files changed

+103
-4
lines changed

packages/react-native/scripts/cocoapods/__tests__/test_utils/InstallerMock.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,15 @@ class UserProjectMock
114114
attr_reader :path
115115
attr_reader :build_configurations
116116
attr_reader :native_targets
117+
attr_reader :files
117118

118119
attr_reader :save_invocation_count
119120

120-
121-
def initialize(path = "/test/path.xcproj", build_configurations = [], native_targets: [])
122-
@path = Pathname.new(path)
121+
def initialize(path = "/test/path.xcproj", build_configurations = [], parent = "/test", native_targets: [], files: [])
122+
@path = Pathname.new(path, parent)
123123
@build_configurations = build_configurations
124124
@native_targets = native_targets
125+
@files = files
125126
@save_invocation_count = 0
126127
end
127128

@@ -130,6 +131,16 @@ def save()
130131
end
131132
end
132133

134+
class PBXFileRefMock
135+
attr_reader :name
136+
attr_reader :path
137+
138+
def initialize(name)
139+
@name = name
140+
@path = name
141+
end
142+
end
143+
133144
class XCConfigMock
134145
attr_reader :name
135146
attr_accessor :attributes

packages/react-native/scripts/cocoapods/__tests__/test_utils/PathnameMock.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ class Pathname
99

1010
attr_reader :path
1111

12-
def initialize(path)
12+
def initialize(path, parent = "")
1313
@path = path
14+
@parent = parent
1415
end
1516

1617
def realpath
@@ -21,6 +22,10 @@ def relative_path_from(path)
2122
return @path
2223
end
2324

25+
def parent
26+
return @parent
27+
end
28+
2429
def self.pwd!(pwd)
2530
@@pwd = pwd
2631
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
#
3+
# This source code is licensed under the MIT license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
module Xcodeproj
7+
class Plist
8+
@@path_to_file_mapping = Hash.new
9+
def self.read_from_path(path)
10+
return @@path_to_file_mapping[path]
11+
end
12+
13+
def self.write_to_path(hash, path)
14+
@@path_to_file_mapping[path] = hash
15+
end
16+
17+
def self.reset()
18+
@@path_to_file_mapping.clear
19+
end
20+
end
21+
end

packages/react-native/scripts/cocoapods/__tests__/utils-test.rb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
require_relative "./test_utils/systemUtils.rb"
1515
require_relative "./test_utils/PathnameMock.rb"
1616
require_relative "./test_utils/TargetDefinitionMock.rb"
17+
require_relative "./test_utils/XcodeprojMock.rb"
1718

1819
class UtilsTests < Test::Unit::TestCase
1920
def setup
@@ -28,6 +29,7 @@ def teardown
2829
Pod::Config.reset()
2930
SysctlChecker.reset()
3031
Environment.reset()
32+
Xcodeproj::Plist.reset()
3133
ENV['RCT_NEW_ARCH_ENABLED'] = '0'
3234
ENV['USE_HERMES'] = '1'
3335
ENV['USE_FRAMEWORKS'] = nil
@@ -772,6 +774,59 @@ def test_applyFlagsForFabric_whenFabricDisabled_doNothing
772774
assert_equal(config.build_settings["OTHER_CFLAGS"], "$(inherited)")
773775
end
774776
end
777+
778+
# ============================== #
779+
# Test - Apply ATS configuration #
780+
# ============================== #
781+
782+
def test_applyATSConfig_plistNil
783+
# Arrange
784+
user_project_mock = prepare_user_project_mock_with_plists()
785+
pods_projects_mock = PodsProjectMock.new([], {"some_pod" => {}})
786+
installer = InstallerMock.new(pods_projects_mock, [
787+
AggregatedProjectMock.new(user_project_mock)
788+
])
789+
790+
# # Act
791+
ReactNativePodsUtils.apply_ats_config(installer)
792+
793+
# # Assert
794+
assert_equal(user_project_mock.files.length, 2)
795+
user_project_mock.files.each do |file|
796+
path = File.join(user_project_mock.path.parent, file.name)
797+
plist = Xcodeproj::Plist.read_from_path(path)
798+
assert_equal(plist['NSAppTransportSecurity'], {
799+
'NSAllowsArbitraryLoads' => false,
800+
'NSAllowsLocalNetworking' => true,
801+
});
802+
end
803+
end
804+
805+
def test_applyATSConfig_plistNonNil
806+
# Arrange
807+
user_project_mock = prepare_user_project_mock_with_plists()
808+
pods_projects_mock = PodsProjectMock.new([], {"some_pod" => {}})
809+
installer = InstallerMock.new(pods_projects_mock, [
810+
AggregatedProjectMock.new(user_project_mock)
811+
])
812+
Xcodeproj::Plist.write_to_path({}, "/test/Info.plist")
813+
Xcodeproj::Plist.write_to_path({}, "/test/Extension-Info.plist")
814+
815+
# # Act
816+
ReactNativePodsUtils.apply_ats_config(installer)
817+
818+
# # Assert
819+
assert_equal(user_project_mock.files.length, 2)
820+
user_project_mock.files.each do |file|
821+
path = File.join(user_project_mock.path.parent, file.name)
822+
plist = Xcodeproj::Plist.read_from_path(path)
823+
assert_equal(plist['NSAppTransportSecurity'], {
824+
'NSAllowsArbitraryLoads' => false,
825+
'NSAllowsLocalNetworking' => true,
826+
});
827+
end
828+
end
829+
775830
end
776831

777832
# ===== #
@@ -785,6 +840,13 @@ def prepare_empty_user_project_mock
785840
])
786841
end
787842

843+
def prepare_user_project_mock_with_plists
844+
return UserProjectMock.new(:files => [
845+
PBXFileRefMock.new("Info.plist"),
846+
PBXFileRefMock.new("Extension-Info.plist"),
847+
])
848+
end
849+
788850
def prepare_config(config_name)
789851
return BuildConfigurationMock.new(config_name, {"LIBRARY_SEARCH_PATHS" => [
790852
"$(TOOLCHAIN_DIR)/usr/lib/swift-5.0/$(PLATFORM_NAME)",

0 commit comments

Comments
 (0)