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

Create Helper to read the package.json from Cocoapods #39390

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
require_relative "./test_utils/InstallerMock.rb"
require_relative "./test_utils/PodMock.rb"
require_relative "./test_utils/SpecMock.rb"
require_relative "./test_utils/FileMock.rb"

class NewArchitectureTests < Test::Unit::TestCase
def teardown
Pod::UI.reset()
FileMock.reset()
end

# ============================= #
Expand Down Expand Up @@ -181,6 +183,119 @@ def test_installModulesDependencies_whenNewArchDisabledAndSearchPathsAndCompiler
]
)
end

# ========================== #
# Test - Is New Arch Enabled #
# ========================== #

def test_isNewArchEnabled_whenOnMainAndFlagTrue_returnTrue
version = '1000.0.0'
new_arch_enabled = true

isEnabled = NewArchitectureHelper.is_new_arch_enabled(new_arch_enabled, version)

assert_equal("1", isEnabled)
end

def test_isNewArchEnabled_whenOnMainAndFlagFalse_returnFalse
version = '1000.0.0'
new_arch_enabled = false

isEnabled = NewArchitectureHelper.is_new_arch_enabled(new_arch_enabled, version)

assert_equal("0", isEnabled)
end

def test_isNewArchEnabled_whenOnStableAndFlagTrue_returnTrue
version = '0.73.0'
new_arch_enabled = true

isEnabled = NewArchitectureHelper.is_new_arch_enabled(new_arch_enabled, version)

assert_equal("1", isEnabled)
end

def test_isNewArchEnabled_whenOnStableAndFlagFalse_returnFalse
version = '0.73.0'
new_arch_enabled = false

isEnabled = NewArchitectureHelper.is_new_arch_enabled(new_arch_enabled, version)

assert_equal("0", isEnabled)
end

def test_isNewArchEnabled_whenOn100AndFlagTrue_returnTrue
version = '1.0.0-prealpha.0'
new_arch_enabled = true

isEnabled = NewArchitectureHelper.is_new_arch_enabled(new_arch_enabled, version)

assert_equal("1", isEnabled)
end

def test_isNewArchEnabled_whenOn100PrealphaWithDotsAndFlagFalse_returnTrue
version = '1.0.0-prealpha.0'
new_arch_enabled = false

isEnabled = NewArchitectureHelper.is_new_arch_enabled(new_arch_enabled, version)

assert_equal("1", isEnabled)
end

def test_isNewArchEnabled_whenOn100PrealphaWithDashAndFlagFalse_returnTrue
version = '1.0.0-prealpha-0'
new_arch_enabled = false

isEnabled = NewArchitectureHelper.is_new_arch_enabled(new_arch_enabled, version)

assert_equal("1", isEnabled)
end

def test_isNewArchEnabled_whenOn100PrealphaOnlyWordsAndFlagFalse_returnTrue
version = '1.0.0-prealpha0'
new_arch_enabled = false

isEnabled = NewArchitectureHelper.is_new_arch_enabled(new_arch_enabled, version)

assert_equal("1", isEnabled)
end

def test_isNewArchEnabled_whenOnGreaterThan100AndFlagFalse_returnTrue
version = '3.2.1'
new_arch_enabled = false

isEnabled = NewArchitectureHelper.is_new_arch_enabled(new_arch_enabled, version)

assert_equal("1", isEnabled)
end

# =================================== #
# Test - Extract React Native Version #
# =================================== #
def test_extractReactNativeVersion_whenFileDoesNotExists_raiseError()
react_native_path = './node_modules/react-native/'

exception = assert_raise(RuntimeError) do
NewArchitectureHelper.extract_react_native_version(react_native_path, :file_manager => FileMock)
end

assert_equal("Couldn't find the React Native package.json file at ./node_modules/react-native/package.json", exception.message)
end

def test_extractReactNativeVersion_whenFileExists_returnTheRightVersion()
react_native_path = "./node_modules/react-native/"
full_path = File.join(react_native_path, "package.json")
json = "{\"version\": \"1.0.0-prealpha.0\"}"
FileMock.mocked_existing_files([full_path])
FileMock.files_to_read({
full_path => json
})

version = NewArchitectureHelper.extract_react_native_version(react_native_path, :file_manager => FileMock)

assert_equal("1.0.0-prealpha.0", version)
end

end

# ================ #
Expand Down
34 changes: 34 additions & 0 deletions packages/react-native/scripts/cocoapods/new_architecture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

require 'json'

class NewArchitectureHelper
@@shared_flags = "-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1"

Expand Down Expand Up @@ -158,4 +160,36 @@ def self.install_modules_dependencies(spec, new_arch_enabled, folly_version)
def self.folly_compiler_flags
return @@folly_compiler_flags
end

def self.extract_react_native_version(react_native_path, file_manager: File, json_parser: JSON)
package_json_file = File.join(react_native_path, "package.json")
if !file_manager.exist?(package_json_file)
raise "Couldn't find the React Native package.json file at #{package_json_file}"
end
package = json_parser.parse(file_manager.read(package_json_file))
return package["version"]
end

def self.is_new_arch_enabled(new_arch_enabled, react_native_version)
# Regex that identify a version with the syntax `<major>.<minor>.<patch>[-<prerelease>[.-]k]
# where
# - major is a number
# - minor is a number
# - patch is a number
# - prerelease is a string (can include numbers)
# - k is a number
version_regex = /^(\d+)\.(\d+)\.(\d+)(?:-(\w+(?:[-.]\d+)?))?$/

if match_data = react_native_version.match(version_regex)

major = match_data[1].to_i

# We want to enforce the new architecture for 1.0.0 and greater,
# but not for 1000 as version 1000 is currently main.
if major > 0 && major < 1000
return "1"
end
end
return new_arch_enabled ? "1" : "0"
end
end