Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplicate entries in HEADER_SEARCH_PATHS when running react_native_post_install script #46262

Closed
68 changes: 68 additions & 0 deletions packages/react-native/scripts/cocoapods/__tests__/utils-test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,74 @@ def test_updateSearchPaths_whenNotUseFrameworks_addsSearchPaths
end
end

# ====================================== #
# Test - Add Search Path If Not Included #
# ====================================== #
# Tests for string input
def test_add_search_path_if_not_included_adds_new_path_to_string
current_paths = "/path/to/headers /another/path"
new_path = "/new/path"
result = ReactNativePodsUtils.add_search_path_if_not_included(current_paths, new_path)
assert_equal("/path/to/headers /another/path /new/path", result)
end

def test_add_search_path_if_not_included_does_not_add_existing_path_to_string
current_paths = "/path/to/headers /another/path"
new_path = "/another/path"
result = ReactNativePodsUtils.add_search_path_if_not_included(current_paths, new_path)
assert_equal("/path/to/headers /another/path", result)
end

def test_add_search_path_if_not_included_does_not_add_existing_path_with_leading_space_to_string
current_paths = " /path/with/leading/space /another/path"
new_path = "/path/with/leading/space"
result = ReactNativePodsUtils.add_search_path_if_not_included(current_paths, new_path)
assert_equal("/path/with/leading/space /another/path", result)
end

def test_add_search_path_if_not_included_handles_path_with_spaces_in_string
current_paths = "/path/to/headers /another/path"
new_path = '"/path/with spaces/lib"'
result = ReactNativePodsUtils.add_search_path_if_not_included(current_paths, new_path)
assert_equal('/path/to/headers /another/path "/path/with spaces/lib"', result)
end

# Tests for array input
def test_add_search_path_if_not_included_adds_new_path_to_array
current_paths = ["/path/to/headers", "/another/path"]
new_path = "/new/path"
result = ReactNativePodsUtils.add_search_path_if_not_included(current_paths, new_path)
assert_equal(["/path/to/headers", "/another/path", "/new/path"], result)
end

def test_add_search_path_if_not_included_does_not_add_existing_path_to_array
current_paths = ["/path/to/headers", "/another/path"]
new_path = "/another/path"
result = ReactNativePodsUtils.add_search_path_if_not_included(current_paths, new_path)
assert_equal(["/path/to/headers", "/another/path"], result)
end

def test_add_search_path_if_not_included_strips_leading_and_trailing_spaces_in_array
current_paths = ["/path/to/headers", " /another/path"]
new_path = "/another/path"
result = ReactNativePodsUtils.add_search_path_if_not_included(current_paths, new_path)
assert_equal(["/path/to/headers", "/another/path"], result)
end

def test_add_search_path_if_not_included_handles_path_with_spaces_in_array
current_paths = [" /path/to/headers ", "/another/path"]
new_path = '"/path/with spaces/lib"'
result = ReactNativePodsUtils.add_search_path_if_not_included(current_paths, new_path)
assert_equal(["/path/to/headers", "/another/path", '"/path/with spaces/lib"'], result)
end

def test_add_search_path_if_not_included_adds_to_empty_array
current_paths = []
new_path = "/new/path"
result = ReactNativePodsUtils.add_search_path_if_not_included(current_paths, new_path)
assert_equal(["/new/path"], result)
end

# =============================================== #
# Test - Create Header Search Path For Frameworks #
# =============================================== #
Expand Down
15 changes: 12 additions & 3 deletions packages/react-native/scripts/cocoapods/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -538,10 +538,19 @@ def self.remove_flag_for_key(config, flag, key)
end

def self.add_search_path_if_not_included(current_search_paths, new_search_path)
if !current_search_paths.include?(new_search_path)
current_search_paths << " #{new_search_path}"
new_search_path = new_search_path.strip

if current_search_paths.is_a?(String)
current_search_paths = current_search_paths.strip
return "#{current_search_paths} #{new_search_path}" unless current_search_paths.include?(new_search_path)
end

if current_search_paths.is_a?(Array)
current_search_paths = current_search_paths.map(&:strip)
return current_search_paths << new_search_path unless current_search_paths.include?(new_search_path)
end
return current_search_paths

current_search_paths
end

def self.update_header_paths_if_depends_on(target_installation_result, dependency_name, header_paths)
Expand Down