diff --git a/.gitignore b/.gitignore index 7154fdf2b..2573849b8 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ build/ dist/ local.properties node_modules/ +!test/fixtures/**/node_modules/ package-lock.json report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json xcuserdata/ diff --git a/.rubocop.yml b/.rubocop.yml index 565600e44..73efac37b 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -18,13 +18,11 @@ Metrics/MethodLength: Metrics/CyclomaticComplexity: IgnoredMethods: [ make_project!, - use_react_native!, use_test_app_internal! ] Metrics/PerceivedComplexity: IgnoredMethods: [ - use_react_native!, use_test_app_internal! ] diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index 8e05b67de..9f4dc3d30 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -359,4 +359,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: 2e7c7df6fd87cf74da985794a7d8f186edd6ac1f -COCOAPODS: 1.9.1 +COCOAPODS: 1.9.3 diff --git a/ios/test_app.rb b/ios/test_app.rb index 66aaebac9..26e615bd4 100644 --- a/ios/test_app.rb +++ b/ios/test_app.rb @@ -44,6 +44,10 @@ def flipper_enabled?(react_native_version) react_native_version >= 6200 && @flipper_versions != false end +def flipper_versions + @flipper_versions != false && (@flipper_versions || {}) +end + def nearest_node_modules(project_root) path = find_file('node_modules', project_root) assert(!path.nil?, "Could not find 'node_modules'") @@ -56,6 +60,21 @@ def package_version(package_path) Gem::Version.new(package_json['version']) end +def react_native_pods(version) + v = version.release + if v >= Gem::Version.new('0.63') + 'use_react_native-0.63' + elsif v >= Gem::Version.new('0.62') + 'use_react_native-0.62' + elsif v >= Gem::Version.new('0.61') + 'use_react_native-0.61' + elsif v >= Gem::Version.new('0.60') + 'use_react_native-0.60' + else + raise "Unsupported React Native version: #{version}" + end +end + def resolve_module(request) script = "console.log(path.dirname(require.resolve('#{request}/package.json')));" Pod::Executable.execute_command('node', ['-e', script], true).strip @@ -105,22 +124,12 @@ def use_react_native!(project_root, target_platform) react_native = Pathname.new(resolve_module('react-native')) version = package_version(react_native.to_s) - if version >= Gem::Version.new('0.63') - require_relative('use_react_native-0.63') - elsif version >= Gem::Version.new('0.62') - require_relative('use_react_native-0.62') - elsif version >= Gem::Version.new('0.61') - require_relative('use_react_native-0.61') - elsif version >= Gem::Version.new('0.60') - require_relative('use_react_native-0.60') - else - raise "Unsupported React Native version: #{version}" - end + require_relative(react_native_pods(version)) include_react_native!(react_native: react_native.relative_path_from(project_root).to_s, target_platform: target_platform, project_root: project_root, - flipper_versions: @flipper_versions != false && (@flipper_versions || {})) + flipper_versions: flipper_versions) end def make_project!(xcodeproj, project_root, target_platform) diff --git a/test/fixtures/test_app/node_modules/@react-native-community/cli-platform-ios/package.json b/test/fixtures/test_app/node_modules/@react-native-community/cli-platform-ios/package.json new file mode 100644 index 000000000..bc9160434 --- /dev/null +++ b/test/fixtures/test_app/node_modules/@react-native-community/cli-platform-ios/package.json @@ -0,0 +1,6 @@ +{ + "name": "@react-native-community/cli-platform-ios", + "version": "4.10.1", + "license": "MIT", + "main": "build/index.js" +} diff --git a/test/fixtures/test_app/node_modules/react-native/package.json b/test/fixtures/test_app/node_modules/react-native/package.json new file mode 100644 index 000000000..eddeba069 --- /dev/null +++ b/test/fixtures/test_app/node_modules/react-native/package.json @@ -0,0 +1,10 @@ +{ + "name": "react-native", + "version": "1000.0.0", + "description": "A framework for building native apps using React", + "license": "MIT", + "repository": { + "type": "git", + "url": "git@github.com:facebook/react-native.git" + } +} diff --git a/test/test_test_app.rb b/test/test_test_app.rb index cc010bb21..3c96569dc 100644 --- a/test/test_test_app.rb +++ b/test/test_test_app.rb @@ -18,6 +18,81 @@ def fixture_path(*args) end class TestTestApp < Minitest::Test + def test_flipper_enabled? + refute(flipper_enabled?(6199)) + assert(flipper_enabled?(6200)) + + use_flipper!(false) + + refute(flipper_enabled?(6199)) + refute(flipper_enabled?(6200)) + + use_flipper! + + refute(flipper_enabled?(6199)) + assert(flipper_enabled?(6200)) + ensure + use_flipper!(nil) + end + + def test_flipper_versions + assert_equal({}, flipper_versions) + + use_flipper!(false) + refute(flipper_versions) + + versions = { 'Flipper': '~> 0.41.1' } + use_flipper!(versions) + assert_equal(versions, flipper_versions) + + use_flipper! + assert_equal({}, flipper_versions) + + use_flipper!(false) + refute(flipper_versions) + ensure + use_flipper!(nil) + end + + def test_nearest_node_modules + expected = fixture_path('test_app', 'node_modules') + + assert_equal(expected, nearest_node_modules(fixture_path('test_app'))) + + react_native = fixture_path('test_app', 'node_modules', 'react-native') + assert_equal(expected, nearest_node_modules(react_native)) + + assert_equal(expected, nearest_node_modules(fixture_path('test_app', 'src'))) + end + + def test_package_version + react_native = fixture_path('test_app', 'node_modules', 'react-native') + assert_equal(Gem::Version.new('1000.0.0'), package_version(react_native)) + + cli = fixture_path('test_app', 'node_modules', '@react-native-community', 'cli-platform-ios') + assert_equal(Gem::Version.new('4.10.1'), package_version(cli)) + end + + def test_react_native_pods + assert_equal('use_react_native-0.63', react_native_pods(Gem::Version.new('1000.0.0'))) + + assert_equal('use_react_native-0.63', react_native_pods(Gem::Version.new('0.63.0'))) + assert_equal('use_react_native-0.63', react_native_pods(Gem::Version.new('0.63.0-rc.1'))) + + assert_equal('use_react_native-0.62', react_native_pods(Gem::Version.new('0.62.2'))) + assert_equal('use_react_native-0.62', react_native_pods(Gem::Version.new('0.62.0'))) + + assert_equal('use_react_native-0.61', react_native_pods(Gem::Version.new('0.61.5'))) + assert_equal('use_react_native-0.61', react_native_pods(Gem::Version.new('0.61.0'))) + + assert_equal('use_react_native-0.60', react_native_pods(Gem::Version.new('0.60.6'))) + assert_equal('use_react_native-0.60', react_native_pods(Gem::Version.new('0.60.0'))) + + assert_raises(RuntimeError) do + react_native_pods(Gem::Version.new('0.59.10')) + end + end + %i[ios macos].each do |target| define_method("test_#{target}_resources_pod_returns_spec_path") do assert_nil(resources_pod(Pathname.new('/'), target))