|
| 1 | +require 'cocoapods' |
| 2 | +require 'cocoapods/validator' |
| 3 | +require 'fileutils' |
| 4 | + |
| 5 | +class TestRunningValidator < Pod::Validator |
| 6 | + APP_TARGET = 'App' |
| 7 | + TEST_TARGET = 'Tests' |
| 8 | + |
| 9 | + attr_accessor :test_files |
| 10 | + |
| 11 | + def create_app_project |
| 12 | + super.tap do |
| 13 | + project = Xcodeproj::Project.open(validation_dir + "#{APP_TARGET}.xcodeproj") |
| 14 | + create_test_target(project) |
| 15 | + end |
| 16 | + end |
| 17 | + |
| 18 | + def install_pod |
| 19 | + super.tap do |
| 20 | + if local? |
| 21 | + FileUtils.ln_s file.dirname, validation_dir + "Pods/#{spec.name}" |
| 22 | + end |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + def podfile_from_spec(*args) |
| 27 | + super(*args).tap do |pod_file| |
| 28 | + add_test_target(pod_file) |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + def build_pod |
| 33 | + super |
| 34 | + Pod::UI.message "\Testing with xcodebuild.\n".yellow do |
| 35 | + run_tests |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + private |
| 40 | + def create_test_target(project) |
| 41 | + test_target = project.new_target(:unit_test_bundle, TEST_TARGET, consumer.platform_name, deployment_target) |
| 42 | + group = project.new_group(TEST_TARGET) |
| 43 | + test_target.add_file_references(test_files.map { |file| group.new_file(file) }) |
| 44 | + project.save |
| 45 | + create_test_scheme(project, test_target) |
| 46 | + project |
| 47 | + end |
| 48 | + |
| 49 | + def create_test_scheme(project, test_target) |
| 50 | + project.recreate_user_schemes |
| 51 | + test_scheme = Xcodeproj::XCScheme.new(test_scheme_path(project)) |
| 52 | + test_scheme.add_test_target(test_target) |
| 53 | + test_scheme.save! |
| 54 | + end |
| 55 | + |
| 56 | + def test_scheme_path(project) |
| 57 | + Xcodeproj::XCScheme.user_data_dir(project.path) + "#{TEST_TARGET}.xcscheme" |
| 58 | + end |
| 59 | + |
| 60 | + def add_test_target(pod_file) |
| 61 | + app_target = pod_file.target_definitions[APP_TARGET] |
| 62 | + Pod::Podfile::TargetDefinition.new(TEST_TARGET, app_target) |
| 63 | + end |
| 64 | + |
| 65 | + def run_tests |
| 66 | + command = %W(clean test -workspace #{APP_TARGET}.xcworkspace -scheme #{TEST_TARGET} -configuration Debug) |
| 67 | + case consumer.platform_name |
| 68 | + when :ios |
| 69 | + command += %w(CODE_SIGN_IDENTITY=- -sdk iphonesimulator) |
| 70 | + command += Fourflusher::SimControl.new.destination('iPhone 4s', deployment_target) |
| 71 | + when :osx |
| 72 | + command += %w(LD_RUNPATH_SEARCH_PATHS=@loader_path/../Frameworks) |
| 73 | + when :tvos |
| 74 | + command += %w(CODE_SIGN_IDENTITY=- -sdk appletvsimulator) |
| 75 | + command += Fourflusher::SimControl.new.destination('Apple TV 1080p', deployment_target) |
| 76 | + else |
| 77 | + return # skip watchos |
| 78 | + end |
| 79 | + |
| 80 | + output, status = Dir.chdir(validation_dir) { _xcodebuild(command) } |
| 81 | + unless status.success? |
| 82 | + message = 'Returned an unsuccessful exit code.' |
| 83 | + if config.verbose? |
| 84 | + message += "\nXcode output: \n#{output}\n" |
| 85 | + else |
| 86 | + message += ' You can use `--verbose` for more information.' |
| 87 | + end |
| 88 | + error('xcodebuild', message) |
| 89 | + end |
| 90 | + end |
| 91 | +end |
0 commit comments