-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move LocalPodspecPatch to dedicated file (#34025)
Summary: Pull Request resolved: #34025 This diff moves the monkeypatch LocalPodspecPatch to a dedicated ruby file. It also adds test for that ## Changelog [iOS][Changed] - Move LocalPodspecPatch to dedicated file Reviewed By: cortinico Differential Revision: D37069361 fbshipit-source-id: 28fddb197484f45aa20ccac516c874e79448e999
- Loading branch information
1 parent
b6bbbf8
commit 8fe2b59
Showing
7 changed files
with
317 additions
and
44 deletions.
There are no files selected for viewing
171 changes: 171 additions & 0 deletions
171
scripts/cocoapods/__tests__/local_podspec_patch-test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
# 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 "test/unit" | ||
require "json" | ||
require_relative "../local_podspec_patch.rb" | ||
require_relative "./test_utils/FileMock.rb" | ||
require_relative "./test_utils/DirMock.rb" | ||
require_relative "./test_utils/PodMock.rb" | ||
require_relative "./test_utils/LocalPodspecPatchMock.rb" | ||
|
||
class LocalPodspecPatchTests < Test::Unit::TestCase | ||
def setup | ||
File.enable_testing_mode! | ||
Dir.enable_testing_mode! | ||
end | ||
|
||
def teardown | ||
File.reset() | ||
Dir.reset() | ||
end | ||
|
||
# =================== # | ||
# Test - Pods To Update # | ||
# =================== # | ||
|
||
def test_podsToUpdate_whenNoFilesExists_returnLocalPodspecs | ||
# Arrange | ||
react_native_path = "../node_modules/react-native" | ||
globs = ["a/path/to/boost.podspec", "a/path/to/DoubleConversion.podspec"] | ||
mocked_pwd = "a/path/to" | ||
Dir.mocked_existing_globs(globs, "#{react_native_path}/third-party-podspecs/*") | ||
Dir.set_pwd(mocked_pwd) | ||
|
||
# Act | ||
local_podspec = LocalPodspecPatch.pods_to_update(:react_native_path => react_native_path) | ||
|
||
# Assert | ||
assert_equal(local_podspec, []) | ||
assert_equal(Dir.glob_invocation, ["#{react_native_path}/third-party-podspecs/*"]) | ||
assert_equal(File.exist_invocation_params, [ | ||
File.join(mocked_pwd, "Pods/Local Podspecs", "boost.podspec.json"), | ||
File.join(mocked_pwd, "Pods/Local Podspecs", "DoubleConversion.podspec.json"), | ||
]) | ||
end | ||
|
||
def test_podsToUpdate_whenFilesExistsWithSameVersions_returnsEmpty | ||
# Arrange | ||
react_native_path = "../node_modules/react-native" | ||
globs = ["a/path/to/boost.podspec", "a/path/to/DoubleConversion.podspec"] | ||
mocked_pwd = "a/path/to" | ||
prepare_PodsToUpdate_test_withMatchingVersions(react_native_path, globs, mocked_pwd) | ||
|
||
# Act | ||
local_podspec = LocalPodspecPatch.pods_to_update(:react_native_path => react_native_path) | ||
|
||
# Assert | ||
assert_equal(local_podspec, []) | ||
assert_equal(Dir.glob_invocation, ["#{react_native_path}/third-party-podspecs/*"]) | ||
assert_equal(File.exist_invocation_params, [ | ||
File.join(mocked_pwd, "Pods/Local Podspecs", "boost.podspec.json"), | ||
File.join(mocked_pwd, "Pods/Local Podspecs", "DoubleConversion.podspec.json"), | ||
]) | ||
end | ||
|
||
def test_podsToUpdate_whenFilesExistsWithDifferentVersions_returnsThem | ||
# Arrange | ||
react_native_path = "../node_modules/react-native" | ||
globs = ["a/path/to/boost.podspec", "a/path/to/DoubleConversion.podspec"] | ||
mocked_pwd = "a/path/to" | ||
prepare_PodsToUpdate_test_withDifferentVersions(react_native_path, globs, mocked_pwd) | ||
|
||
# Act | ||
local_podspec = LocalPodspecPatch.pods_to_update(:react_native_path => react_native_path) | ||
|
||
# Assert | ||
assert_equal(local_podspec, [ | ||
"boost", | ||
"DoubleConversion" | ||
]) | ||
assert_equal(Dir.glob_invocation, ["#{react_native_path}/third-party-podspecs/*"]) | ||
assert_equal(File.exist_invocation_params, [ | ||
File.join(mocked_pwd, "Pods/Local Podspecs", "boost.podspec.json"), | ||
File.join(mocked_pwd, "Pods/Local Podspecs", "DoubleConversion.podspec.json"), | ||
]) | ||
end | ||
|
||
# ======================================== # | ||
# Test - Patch Detect Changes With Podfile # | ||
# ======================================== # | ||
def test_patchDetectChangesWithPodfile_whenAlreadyChanged_returnSameChangeSet() | ||
local_pods = [ | ||
"boost", | ||
"DoubleConversion" | ||
] | ||
LocalPodspecPatch.mock_local_podspecs(local_pods) | ||
changes = { | ||
:unchanged => ["some_pod"], | ||
:changed => ["boost", "DoubleConversion", "another_pod"] | ||
} | ||
|
||
Pod::Lockfile.prepend(LocalPodspecPatch) | ||
|
||
new_changes = Pod::Lockfile.new().patch_detect_changes_with_podfile(changes) | ||
|
||
assert_equal(new_changes, { | ||
:unchanged => ["some_pod"], | ||
:changed => ["boost", "DoubleConversion", "another_pod"] | ||
}) | ||
end | ||
|
||
def test_patchDetectChangesWithPodfile_whenLocalPodsUnchanged_movesLocalPodsToChangeSet() | ||
pods = [ | ||
"boost", | ||
"DoubleConversion" | ||
] | ||
LocalPodspecPatch.mock_local_podspecs(pods) | ||
changes = { | ||
:unchanged => ["first_pod", "boost", "DoubleConversion"], | ||
:changed => ["another_pod"] | ||
} | ||
|
||
Pod::Lockfile.prepend(LocalPodspecPatch) | ||
|
||
new_changes = Pod::Lockfile.new().patch_detect_changes_with_podfile(changes) | ||
|
||
assert_equal(new_changes, { | ||
:unchanged => ["first_pod"], | ||
:changed => ["another_pod", "boost", "DoubleConversion"] | ||
}) | ||
end | ||
|
||
# ========= # | ||
# Utilities # | ||
# ========= # | ||
def prepare_PodsToUpdate_test_withMatchingVersions(react_native_path, globs, mocked_pwd) | ||
File.mocked_existing_files([ | ||
"a/path/to/Pods/Local Podspecs/boost.podspec.json", | ||
"a/path/to/Pods/Local Podspecs/DoubleConversion.podspec.json" | ||
]) | ||
File.files_to_read({ | ||
"a/path/to/Pods/Local Podspecs/boost.podspec.json" => "{ \"version\": \"0.0.1\"}", | ||
"a/path/to/Pods/Local Podspecs/DoubleConversion.podspec.json" => "{ \"version\": \"1.0.1\"}", | ||
}) | ||
Dir.mocked_existing_globs(globs, "#{react_native_path}/third-party-podspecs/*") | ||
Dir.set_pwd(mocked_pwd) | ||
Pod::Specification.specs_from_file({ | ||
"../node_modules/react-native/third-party-podspecs/boost.podspec" => Pod::PodSpecMock.new(:version => "0.0.1"), | ||
"../node_modules/react-native/third-party-podspecs/DoubleConversion.podspec" => Pod::PodSpecMock.new(:version => "1.0.1"), | ||
}) | ||
end | ||
|
||
def prepare_PodsToUpdate_test_withDifferentVersions(react_native_path, globs, mocked_pwd) | ||
File.mocked_existing_files([ | ||
"a/path/to/Pods/Local Podspecs/boost.podspec.json", | ||
"a/path/to/Pods/Local Podspecs/DoubleConversion.podspec.json" | ||
]) | ||
File.files_to_read({ | ||
"a/path/to/Pods/Local Podspecs/boost.podspec.json" => "{ \"version\": \"0.0.1\"}", | ||
"a/path/to/Pods/Local Podspecs/DoubleConversion.podspec.json" => "{ \"version\": \"1.0.1\"}", | ||
}) | ||
Dir.mocked_existing_globs(globs, "#{react_native_path}/third-party-podspecs/*") | ||
Dir.set_pwd(mocked_pwd) | ||
Pod::Specification.specs_from_file({ | ||
"../node_modules/react-native/third-party-podspecs/boost.podspec" => Pod::PodSpecMock.new(:version => "0.1.1"), | ||
"../node_modules/react-native/third-party-podspecs/DoubleConversion.podspec" => Pod::PodSpecMock.new(:version => "1.1.1"), | ||
}) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
scripts/cocoapods/__tests__/test_utils/LocalPodspecPatchMock.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# 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 LocalPodspecPatch | ||
def self.mock_local_podspecs(pods) | ||
@@local_podspecs = pods | ||
end | ||
|
||
def reset() | ||
@@local_podspecs = [] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# 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. | ||
|
||
# Monkeypatch of `Pod::Lockfile` to ensure automatic update of dependencies integrated with a local podspec when their version changed. | ||
# This is necessary because local podspec dependencies must be otherwise manually updated. | ||
module LocalPodspecPatch | ||
# Returns local podspecs whose versions differ from the one in the `react-native` package. | ||
def self.pods_to_update(react_native_path: "../node_modules/react-native") | ||
@@local_podspecs = Dir.glob("#{react_native_path}/third-party-podspecs/*").map { |file| File.basename(file, ".podspec") } | ||
@@local_podspecs = @@local_podspecs.select do |podspec_name| | ||
|
||
# Read local podspec to determine the cached version | ||
local_podspec_path = File.join( | ||
Dir.pwd, "Pods/Local Podspecs/#{podspec_name}.podspec.json" | ||
) | ||
|
||
# Local podspec cannot be outdated if it does not exist, yet | ||
next unless File.exist?(local_podspec_path) | ||
|
||
local_podspec = File.read(local_podspec_path) | ||
local_podspec_json = JSON.parse(local_podspec) | ||
local_version = local_podspec_json["version"] | ||
|
||
# Read the version from a podspec from the `react-native` package | ||
podspec_path = "#{react_native_path}/third-party-podspecs/#{podspec_name}.podspec" | ||
current_podspec = Pod::Specification.from_file(podspec_path) | ||
current_version = current_podspec.version.to_s | ||
current_version != local_version | ||
end | ||
@@local_podspecs | ||
end | ||
|
||
# Patched `detect_changes_with_podfile` method | ||
def detect_changes_with_podfile(podfile) | ||
Pod::UI.puts "Invoke detect_changes_with_podfile patched method".red | ||
changes = super(podfile) | ||
return patch_detect_changes_with_podfile(changes) | ||
end | ||
|
||
def patch_detect_changes_with_podfile(changes) | ||
@@local_podspecs.each do |local_podspec| | ||
next unless changes[:unchanged].include?(local_podspec) | ||
|
||
changes[:unchanged].delete(local_podspec) | ||
changes[:changed] << local_podspec | ||
end | ||
changes | ||
end | ||
end |
Oops, something went wrong.