Ejemplo n.º 1
0
  def _SetPbxprojForXctest(self):
    """Sets the dummy project's pbxproj for xctest."""
    pbxproj_plist_obj = plist_util.Plist(self.pbxproj_file_path)
    pbxproj_objects = pbxproj_plist_obj.GetPlistField('objects')

    # Sets the build setting for app under test and unit test bundle signing.
    # 1) If run with iphonesimulator, don't need to set any fields in build
    # setting. xcodebuild will sign bundles with identity '-' and no
    # provisioning profile by default.
    # 2) If runs with iphoneos and the app under test's embedded provisioning
    # profile is 'iOS Team Provisioning Profile: *', set build setting for using
    # Xcode managed provisioning profile to sign bundles.
    # 3) If runs with iphoneos and the app under test's embedded provisioning
    # profile is specific, set build setting with using app under test's
    # embedded provisioning profile.
    if self._sdk == ios_constants.SDK.IPHONEOS:
      aut_build_setting = pbxproj_objects[
          'AppUnderTestBuildConfig']['buildSettings']
      test_build_setting = pbxproj_objects[
          'XCTestBundleBuildConfig']['buildSettings']
      aut_build_setting['CODE_SIGNING_REQUIRED'] = 'YES'
      aut_build_setting['PRODUCT_BUNDLE_IDENTIFIER'] = bundle_util.GetBundleId(
          self._app_under_test_dir)
      embedded_provision = provisioning_profile.ProvisiongProfile(
          os.path.join(self._app_under_test_dir, 'embedded.mobileprovision'),
          self._work_dir,
          keychain_path=self._keychain_path)
      embedded_provision.Install()
      # Case 2)
      if embedded_provision.name.startswith('iOS Team Provisioning Profile: '):
        aut_build_setting['CODE_SIGN_IDENTITY'] = 'iPhone Developer'
        test_build_setting['CODE_SIGN_IDENTITY'] = 'iPhone Developer'
        app_under_test_dev_team = bundle_util.GetDevelopmentTeam(
            self._app_under_test_dir)
        aut_build_setting['DEVELOPMENT_TEAM'] = app_under_test_dev_team
        test_build_setting['DEVELOPMENT_TEAM'] = app_under_test_dev_team
      else:
        # Case 3)
        app_under_test_sign_identity = bundle_util.GetCodesignIdentity(
            self._app_under_test_dir)
        aut_build_setting['CODE_SIGN_IDENTITY'] = app_under_test_sign_identity
        test_build_setting['CODE_SIGN_IDENTITY'] = app_under_test_sign_identity
        (aut_build_setting[
            'PROVISIONING_PROFILE_SPECIFIER']) = embedded_provision.name

    # Sets the app under test and test bundle.
    test_project_build_setting = pbxproj_objects[
        'TestProjectBuildConfig']['buildSettings']
    app_under_test_name = os.path.splitext(
        os.path.basename(self._app_under_test_dir))[0]
    pbxproj_objects['AppUnderTestTarget']['name'] = app_under_test_name
    pbxproj_objects['AppUnderTestTarget']['productName'] = app_under_test_name
    test_project_build_setting['APP_UNDER_TEST_NAME'] = app_under_test_name
    test_bundle_name = os.path.splitext(
        os.path.basename(self._test_bundle_dir))[0]
    pbxproj_objects['XCTestBundleTarget']['name'] = test_bundle_name
    pbxproj_objects['XCTestBundleTarget']['productName'] = test_bundle_name
    test_project_build_setting['XCTEST_BUNDLE_NAME'] = test_bundle_name

    pbxproj_plist_obj.SetPlistField('objects', pbxproj_objects)
Ejemplo n.º 2
0
  def _SetPbxprojForXcuitest(self):
    """Sets the dummy project's pbxproj for xcuitest."""
    pbxproj_plist_obj = plist_util.Plist(self.pbxproj_file_path)
    pbxproj_objects = pbxproj_plist_obj.GetPlistField('objects')

    # Sets the build setting of test bundle for generated XCTRunner.app signing.
    # 1) If run with iphonesimulator, don't need to set any fields in build
    # setting. xcodebuild will sign the XCTRunner.app with identity '-' and no
    # provisioning profile by default.
    # 2) If runs with iphoneos and the app under test's embedded provisioning
    # profile is 'iOS Team Provisioning Profile: *', set build setting for using
    # Xcode managed provisioning profile to sign the XCTRunner.app.
    # 3) If runs with iphoneos and the app under test's embedded provisioning
    # profile is specific, set build setting for using app under test's
    # embedded provisioning profile to sign the XCTRunner.app. If the
    # provisioning profile is not installed in the Mac machine, also installs
    # it.
    # 4) The test bundle's provisioning profile can be overwrited by method
    # SetTestBundleProvisioningProfile.
    if self._sdk == ios_constants.SDK.IPHONEOS:
      build_setting = pbxproj_objects[
          'XCUITestBundleBuildConfig']['buildSettings']
      build_setting['PRODUCT_BUNDLE_IDENTIFIER'] = bundle_util.GetBundleId(
          self._test_bundle_dir)
      build_setting['DEVELOPMENT_TEAM'] = bundle_util.GetDevelopmentTeam(
          self._test_bundle_dir)
      embedded_provision = provisioning_profile.ProvisiongProfile(
          os.path.join(self._app_under_test_dir, 'embedded.mobileprovision'),
          self._work_dir,
          keychain_path=self._keychain_path)
      embedded_provision.Install()
      # Case 2)
      if embedded_provision.name.startswith('iOS Team Provisioning Profile: '):
        build_setting['CODE_SIGN_IDENTITY'] = 'iPhone Developer'
      else:
        # Case 3)
        build_setting['CODE_SIGN_IDENTITY'] = bundle_util.GetCodesignIdentity(
            self._app_under_test_dir)
        (build_setting[
            'PROVISIONING_PROFILE_SPECIFIER']) = embedded_provision.name

    # Sets the app under test and test bundle.
    test_project_build_setting = pbxproj_objects[
        'TestProjectBuildConfig']['buildSettings']
    app_under_test_name = os.path.splitext(
        os.path.basename(self._app_under_test_dir))[0]
    pbxproj_objects['AppUnderTestTarget']['name'] = app_under_test_name
    pbxproj_objects['AppUnderTestTarget']['productName'] = app_under_test_name
    test_project_build_setting['APP_UNDER_TEST_NAME'] = app_under_test_name
    test_bundle_name = os.path.splitext(
        os.path.basename(self._test_bundle_dir))[0]
    pbxproj_objects['XCUITestBundleTarget']['name'] = test_bundle_name
    pbxproj_objects['XCUITestBundleTarget']['productName'] = test_bundle_name
    test_project_build_setting['XCUITEST_BUNDLE_NAME'] = test_bundle_name

    pbxproj_plist_obj.SetPlistField('objects', pbxproj_objects)
Ejemplo n.º 3
0
  def SetTestBundleProvisioningProfile(self, test_bundle_provisioning_profile):
    """Sets the provisioning profile specifier to the test bundle.

    If the given provisioning profile is a path, will also install it in the
    host.

    Args:
      test_bundle_provisioning_profile: string, name/path of the provisioning
        profile of test bundle.
    """
    if not test_bundle_provisioning_profile:
      return
    provisioning_profile_is_file = False
    if (test_bundle_provisioning_profile.startswith('/') and
        os.path.exists(test_bundle_provisioning_profile)):
      provisioning_profile_is_file = True

    if self._sdk != ios_constants.SDK.IPHONEOS:
      logging.warning(
          'Can only set provisioning profile to test bundle in iphoneos SDK. '
          'But current SDK is %s', self._sdk)
      return
    self.GenerateDummyProject()
    if self._test_type == ios_constants.TestType.XCUITEST:
      pbxproj_plist_obj = plist_util.Plist(self.pbxproj_file_path)
      pbxproj_objects = pbxproj_plist_obj.GetPlistField('objects')
      settings = pbxproj_objects['XCUITestBundleBuildConfig']['buildSettings']
      settings['CODE_SIGN_IDENTITY'] = bundle_util.GetCodesignIdentity(
          self._test_bundle_dir)
      if not provisioning_profile_is_file:
        settings[
            'PROVISIONING_PROFILE_SPECIFIER'] = test_bundle_provisioning_profile
      else:
        profile_obj = provisioning_profile.ProvisiongProfile(
            test_bundle_provisioning_profile,
            self._work_dir,
            keychain_path=self._keychain_path)
        profile_obj.Install()
        settings['PROVISIONING_PROFILE_SPECIFIER'] = profile_obj.name
      pbxproj_plist_obj.SetPlistField('objects', pbxproj_objects)
    else:
      logging.warning(
          'Setting provisioning profile specifier to test bundle in test type '
          '%s is not supported.', self._test_type)
Ejemplo n.º 4
0
    def _GenerateTestRootForXctest(self):
        """Generates the test root for XCTest.

    The approach constructs xctestrun.plist from Xcode. Then copies app under
    test, test bundle and xctestrun.plist to test root directory.
    """
        app_under_test_plugins_dir = os.path.join(self._app_under_test_dir,
                                                  'PlugIns')
        if not os.path.exists(app_under_test_plugins_dir):
            os.mkdir(app_under_test_plugins_dir)
        new_test_bundle_path = os.path.join(
            app_under_test_plugins_dir,
            os.path.basename(self._test_bundle_dir))
        # The test bundle under PlugIns can not be symlink since it will cause
        # app installation error.
        if os.path.islink(self._test_bundle_dir):
            shutil.copytree(self._test_bundle_dir, new_test_bundle_path)
            self._test_bundle_dir = new_test_bundle_path
        elif new_test_bundle_path != self._test_bundle_dir:
            self._test_bundle_dir = _MoveAndReplaceFile(
                self._test_bundle_dir, app_under_test_plugins_dir)

        platform_path = xcode_info_util.GetSdkPlatformPath(self._sdk)
        app_under_test_frameworks_dir = os.path.join(self._app_under_test_dir,
                                                     'Frameworks')
        if not os.path.exists(app_under_test_frameworks_dir):
            os.mkdir(app_under_test_frameworks_dir)
        xctest_framework = os.path.join(app_under_test_frameworks_dir,
                                        'XCTest.framework')
        if not os.path.exists(xctest_framework):
            shutil.copytree(
                os.path.join(platform_path,
                             'Developer/Library/Frameworks/XCTest.framework'),
                xctest_framework)
        if xcode_info_util.GetXcodeVersionNumber() < 1000:
            insert_libs_framework = os.path.join(
                app_under_test_frameworks_dir, 'IDEBundleInjection.framework')
            if not os.path.exists(insert_libs_framework):
                shutil.copytree(
                    os.path.join(
                        platform_path, 'Developer/Library/PrivateFrameworks/'
                        'IDEBundleInjection.framework'), insert_libs_framework)
        else:
            insert_libs_framework = os.path.join(
                app_under_test_frameworks_dir, 'libXCTestBundleInject.dylib')
            if not os.path.exists(insert_libs_framework):
                shutil.copyfile(
                    os.path.join(
                        platform_path,
                        'Developer/usr/lib/libXCTestBundleInject.dylib'),
                    insert_libs_framework)

        if self._on_device:
            app_under_test_signing_identity = bundle_util.GetCodesignIdentity(
                self._app_under_test_dir)
            bundle_util.CodesignBundle(
                xctest_framework, identity=app_under_test_signing_identity)
            bundle_util.CodesignBundle(
                insert_libs_framework,
                identity=app_under_test_signing_identity)
            bundle_util.CodesignBundle(self._test_bundle_dir)
            bundle_util.CodesignBundle(self._app_under_test_dir)

        app_under_test_name = os.path.splitext(
            os.path.basename(self._app_under_test_dir))[0]
        platform_name = 'iPhoneOS' if self._on_device else 'iPhoneSimulator'
        if xcode_info_util.GetXcodeVersionNumber() < 1000:
            dyld_insert_libs = (
                '__PLATFORMS__/%s.platform/Developer/Library/'
                'PrivateFrameworks/IDEBundleInjection.framework/'
                'IDEBundleInjection' % platform_name)
        else:
            dyld_insert_libs = ('__PLATFORMS__/%s.platform/Developer/usr/lib/'
                                'libXCTestBundleInject.dylib' % platform_name)
        test_envs = {
            'XCInjectBundleInto':
            os.path.join('__TESTHOST__', app_under_test_name),
            'DYLD_FRAMEWORK_PATH':
            '__TESTROOT__:__PLATFORMS__/%s.platform/Developer/'
            'Library/Frameworks' % platform_name,
            'DYLD_INSERT_LIBRARIES':
            dyld_insert_libs,
            'DYLD_LIBRARY_PATH':
            '__TESTROOT__:__PLATFORMS__/%s.platform/Developer/Library/'
            'Frameworks' % platform_name,
        }
        self._xctestrun_dict = {
            'TestHostPath': self._app_under_test_dir,
            'TestBundlePath': self._test_bundle_dir,
            'IsAppHostedTestBundle': True,
            'TestingEnvironmentVariables': test_envs
        }
Ejemplo n.º 5
0
    def _GenerateTestRootForXcuitest(self):
        """Generates the test root for XCUITest.

    The approach constructs xctestrun.plist and uitest runner app from Xcode.
    Then copies app under test, test bundle, xctestrun.plist and uitest
    runner app to test root directory.
    """
        platform_library_path = os.path.join(
            xcode_info_util.GetSdkPlatformPath(self._sdk), 'Developer/Library')
        uitest_runner_app = self._GetUitestRunnerAppFromXcode(
            platform_library_path)

        runner_app_frameworks_dir = os.path.join(uitest_runner_app,
                                                 'Frameworks')
        os.mkdir(runner_app_frameworks_dir)
        xctest_framework = os.path.join(runner_app_frameworks_dir,
                                        'XCTest.framework')
        shutil.copytree(
            os.path.join(platform_library_path, 'Frameworks/XCTest.framework'),
            xctest_framework)
        if xcode_info_util.GetXcodeVersionNumber() >= 900:
            xct_automation_framework = os.path.join(
                runner_app_frameworks_dir, 'XCTAutomationSupport.framework')
            shutil.copytree(
                os.path.join(
                    platform_library_path,
                    'PrivateFrameworks/XCTAutomationSupport.framework'),
                xct_automation_framework)

        self._PrepareUitestInRunerApp(uitest_runner_app)

        if self._on_device:
            runner_app_embedded_provision = os.path.join(
                uitest_runner_app, 'embedded.mobileprovision')
            use_customized_provision = False
            if self._signing_options:
                customized_runner_app_provision = self._signing_options.get(
                    'xctrunner_app_provisioning_profile')
                if customized_runner_app_provision:
                    shutil.copyfile(customized_runner_app_provision,
                                    runner_app_embedded_provision)
                    use_customized_provision = True
                if self._signing_options.get(
                        'xctrunner_app_enable_ui_file_sharing'):
                    try:
                        # Don't resign the uitest runner app here since it will be resigned
                        # with passing entitlements and identity later.
                        bundle_util.EnableUIFileSharing(uitest_runner_app,
                                                        resigning=False)
                    except ios_errors.BundleError as e:
                        logging.warning(str(e))

            # If customized runner app provision is not provided, runner app will
            # use app under test's embedded provision as embedded provision.
            if not use_customized_provision:
                app_under_test_embedded_provision = os.path.join(
                    self._app_under_test_dir, 'embedded.mobileprovision')
                shutil.copyfile(app_under_test_embedded_provision,
                                runner_app_embedded_provision)

            test_bundle_team_id = bundle_util.GetDevelopmentTeam(
                self._test_bundle_dir)
            full_test_bundle_id = '%s.%s' % (test_bundle_team_id,
                                             bundle_util.GetBundleId(
                                                 self._test_bundle_dir))
            entitlements_dict = {
                'application-identifier': full_test_bundle_id,
                'com.apple.developer.team-identifier': test_bundle_team_id,
                'get-task-allow': True,
                'keychain-access-groups': [full_test_bundle_id],
            }
            entitlements_plist_path = os.path.join(uitest_runner_app,
                                                   'RunnerEntitlements.plist')
            plist_util.Plist(entitlements_plist_path).SetPlistField(
                None, entitlements_dict)

            test_bundle_signing_identity = bundle_util.GetCodesignIdentity(
                self._test_bundle_dir)
            bundle_util.CodesignBundle(xctest_framework,
                                       identity=test_bundle_signing_identity)
            if xcode_info_util.GetXcodeVersionNumber() >= 900:
                bundle_util.CodesignBundle(
                    xct_automation_framework,
                    identity=test_bundle_signing_identity)
            bundle_util.CodesignBundle(
                uitest_runner_app,
                entitlements_plist_path=entitlements_plist_path,
                identity=test_bundle_signing_identity)

            bundle_util.CodesignBundle(self._test_bundle_dir)
            bundle_util.CodesignBundle(self._app_under_test_dir)

        platform_name = 'iPhoneOS' if self._on_device else 'iPhoneSimulator'
        test_envs = {
            'DYLD_FRAMEWORK_PATH':
            '__TESTROOT__:__PLATFORMS__/%s.platform/Developer/'
            'Library/Frameworks' % platform_name,
            'DYLD_LIBRARY_PATH':
            '__TESTROOT__:__PLATFORMS__/%s.platform/Developer/'
            'Library/Frameworks' % platform_name
        }
        self._xctestrun_dict = {
            'IsUITestBundle': True,
            'SystemAttachmentLifetime': 'keepNever',
            'TestBundlePath': self._test_bundle_dir,
            'TestHostPath': uitest_runner_app,
            'UITargetAppPath': self._app_under_test_dir,
            'UserAttachmentLifetime': 'keepNever',
            'TestingEnvironmentVariables': test_envs
        }
Ejemplo n.º 6
0
    def _GenerateTestRootForXctest(self):
        """Generates the test root for XCTest.

    The approach constructs xctestrun.plist from Xcode. Then copies app under
    test, test bundle and xctestrun.plist to test root directory.
    """
        app_under_test_plugins_dir = os.path.join(self._app_under_test_dir,
                                                  'PlugIns')
        if not os.path.exists(app_under_test_plugins_dir):
            os.mkdir(app_under_test_plugins_dir)
        new_test_bundle_path = os.path.join(
            app_under_test_plugins_dir,
            os.path.basename(self._test_bundle_dir))
        # The test bundle under PlugIns can not be symlink since it will cause
        # app installation error.
        if os.path.islink(self._test_bundle_dir):
            shutil.copytree(self._test_bundle_dir, new_test_bundle_path)
            self._test_bundle_dir = new_test_bundle_path
        elif new_test_bundle_path != self._test_bundle_dir:
            self._test_bundle_dir = _MoveAndReplaceFile(
                self._test_bundle_dir, app_under_test_plugins_dir)

        if self._on_device:
            platform_path = xcode_info_util.GetSdkPlatformPath(self._sdk)
            app_under_test_frameworks_dir = os.path.join(
                self._app_under_test_dir, 'Frameworks')
            if not os.path.exists(app_under_test_frameworks_dir):
                os.mkdir(app_under_test_frameworks_dir)
            app_under_test_signing_identity = bundle_util.GetCodesignIdentity(
                self._app_under_test_dir)
            _CopyAndSignFramework(
                os.path.join(platform_path,
                             'Developer/Library/Frameworks/XCTest.framework'),
                app_under_test_frameworks_dir, app_under_test_signing_identity)
            xcode_version_num = xcode_info_util.GetXcodeVersionNumber()
            if xcode_version_num < 1000:
                bundle_injection_lib = os.path.join(
                    platform_path, 'Developer/Library/PrivateFrameworks/'
                    'IDEBundleInjection.framework')
                _CopyAndSignFramework(bundle_injection_lib,
                                      app_under_test_frameworks_dir,
                                      app_under_test_signing_identity)
            else:
                bundle_injection_lib = os.path.join(
                    platform_path,
                    'Developer/usr/lib/libXCTestBundleInject.dylib')
                _CopyAndSignLibFile(bundle_injection_lib,
                                    app_under_test_frameworks_dir,
                                    app_under_test_signing_identity)
            if xcode_version_num >= 1100:
                _CopyAndSignFramework(
                    os.path.join(
                        platform_path, 'Developer/Library/PrivateFrameworks/'
                        'XCTAutomationSupport.framework'),
                    app_under_test_frameworks_dir,
                    app_under_test_signing_identity)
                _CopyAndSignLibFile(
                    os.path.join(platform_path,
                                 _LIB_XCTEST_SWIFT_RELATIVE_PATH),
                    app_under_test_frameworks_dir,
                    app_under_test_signing_identity)
            bundle_util.CodesignBundle(self._test_bundle_dir)
            bundle_util.CodesignBundle(self._app_under_test_dir)

        app_under_test_name = os.path.splitext(
            os.path.basename(self._app_under_test_dir))[0]
        platform_name = 'iPhoneOS' if self._on_device else 'iPhoneSimulator'
        developer_path = '__PLATFORMS__/%s.platform/Developer' % platform_name
        if xcode_info_util.GetXcodeVersionNumber() < 1000:
            dyld_insert_libs = (
                '%s/Library/PrivateFrameworks/'
                'IDEBundleInjection.framework/IDEBundleInjection' %
                developer_path)
        else:
            dyld_insert_libs = ('%s/usr/lib/libXCTestBundleInject.dylib' %
                                developer_path)
        test_envs = {
            'XCInjectBundleInto':
            os.path.join('__TESTHOST__', app_under_test_name),
            'DYLD_FRAMEWORK_PATH':
            '__TESTROOT__:{developer}/Library/Frameworks:'
            '{developer}/Library/PrivateFrameworks'.format(
                developer=developer_path),
            'DYLD_INSERT_LIBRARIES':
            dyld_insert_libs,
            'DYLD_LIBRARY_PATH':
            '__TESTROOT__:%s/usr/lib:' % developer_path
        }

        # Fixes failures for test targets that depend on Swift libraries when running with Xcode 11
        # on pre-iOS 12.2 simulators.
        # Example failure message this resolves: "The bundle couldn’t be loaded because it is damaged
        # or missing necessary resources."
        swift5FallbackLibsDir = xcode_info_util.GetSwift5FallbackLibsDir()
        if swift5FallbackLibsDir:
            test_envs["DYLD_FALLBACK_LIBRARY_PATH"] = swift5FallbackLibsDir

        self._xctestrun_dict = {
            'TestHostPath': self._app_under_test_dir,
            'TestBundlePath': self._test_bundle_dir,
            'IsAppHostedTestBundle': True,
            'TestingEnvironmentVariables': test_envs
        }
Ejemplo n.º 7
0
    def _GenerateTestRootForXcuitest(self):
        """Generates the test root for XCUITest.

    The approach constructs xctestrun.plist and uitest runner app from Xcode.
    Then copies app under test, test bundle, xctestrun.plist and uitest
    runner app to test root directory.
    """
        platform_path = xcode_info_util.GetSdkPlatformPath(self._sdk)
        platform_library_path = os.path.join(platform_path,
                                             'Developer/Library')
        uitest_runner_app = self._GetUitestRunnerAppFromXcode(
            platform_library_path)
        self._PrepareUitestInRunerApp(uitest_runner_app)

        if self._on_device:
            runner_app_embedded_provision = os.path.join(
                uitest_runner_app, 'embedded.mobileprovision')
            use_customized_provision = False
            if self._signing_options:
                customized_runner_app_provision = self._signing_options.get(
                    'xctrunner_app_provisioning_profile')
                if customized_runner_app_provision:
                    shutil.copyfile(customized_runner_app_provision,
                                    runner_app_embedded_provision)
                    use_customized_provision = True
                if self._signing_options.get(
                        'xctrunner_app_enable_ui_file_sharing'):
                    try:
                        # Don't resign the uitest runner app here since it will be resigned
                        # with passing entitlements and identity later.
                        bundle_util.EnableUIFileSharing(uitest_runner_app,
                                                        resigning=False)
                    except ios_errors.BundleError as e:
                        logging.warning(str(e))

            # If customized runner app provision is not provided, runner app will
            # use app under test's embedded provision as embedded provision.
            if not use_customized_provision:
                app_under_test_embedded_provision = os.path.join(
                    self._app_under_test_dir, 'embedded.mobileprovision')
                shutil.copyfile(app_under_test_embedded_provision,
                                runner_app_embedded_provision)

            test_bundle_team_id = bundle_util.GetDevelopmentTeam(
                self._test_bundle_dir)
            full_test_bundle_id = '%s.%s' % (test_bundle_team_id,
                                             bundle_util.GetBundleId(
                                                 self._test_bundle_dir))
            entitlements_dict = {
                'application-identifier': full_test_bundle_id,
                'com.apple.developer.team-identifier': test_bundle_team_id,
                'get-task-allow': True,
                'keychain-access-groups': [full_test_bundle_id],
            }
            entitlements_plist_path = os.path.join(uitest_runner_app,
                                                   'RunnerEntitlements.plist')
            plist_util.Plist(entitlements_plist_path).SetPlistField(
                None, entitlements_dict)

            test_bundle_signing_identity = bundle_util.GetCodesignIdentity(
                self._test_bundle_dir)

            runner_app_frameworks_dir = os.path.join(uitest_runner_app,
                                                     'Frameworks')
            os.mkdir(runner_app_frameworks_dir)
            _CopyAndSignFramework(
                os.path.join(platform_library_path,
                             'Frameworks/XCTest.framework'),
                runner_app_frameworks_dir, test_bundle_signing_identity)
            xcode_version_num = xcode_info_util.GetXcodeVersionNumber()
            if xcode_version_num >= 900:
                _CopyAndSignFramework(
                    os.path.join(
                        platform_library_path,
                        'PrivateFrameworks/XCTAutomationSupport.framework'),
                    runner_app_frameworks_dir, test_bundle_signing_identity)
            if xcode_version_num >= 1100:
                _CopyAndSignLibFile(
                    os.path.join(platform_path,
                                 _LIB_XCTEST_SWIFT_RELATIVE_PATH),
                    runner_app_frameworks_dir, test_bundle_signing_identity)
            bundle_util.CodesignBundle(
                uitest_runner_app,
                entitlements_plist_path=entitlements_plist_path,
                identity=test_bundle_signing_identity)

            bundle_util.CodesignBundle(self._test_bundle_dir)
            bundle_util.CodesignBundle(self._app_under_test_dir)

        platform_name = 'iPhoneOS' if self._on_device else 'iPhoneSimulator'
        developer_path = '__PLATFORMS__/%s.platform/Developer' % platform_name
        test_envs = {
            'DYLD_FRAMEWORK_PATH':
            '__TESTROOT__:{developer}/Library/Frameworks:'
            '{developer}/Library/PrivateFrameworks'.format(
                developer=developer_path),
            'DYLD_LIBRARY_PATH':
            '__TESTROOT__:%s/usr/lib' % developer_path
        }

        # Fixes failures for UI test targets that depend on Swift libraries when running with Xcode 11
        # on pre-iOS 12.2 simulators.
        # Example failure message this resolves: "The bundle couldn’t be loaded because it is damaged
        # or missing necessary resources."
        swift5FallbackLibsDir = xcode_info_util.GetSwift5FallbackLibsDir()
        if swift5FallbackLibsDir:
            test_envs["DYLD_FALLBACK_LIBRARY_PATH"] = swift5FallbackLibsDir

        self._xctestrun_dict = {
            'IsUITestBundle':
            True,
            'SystemAttachmentLifetime':
            'keepAlways',
            'TestBundlePath':
            self._test_bundle_dir,
            'TestHostPath':
            uitest_runner_app,
            'UITargetAppPath':
            self._app_under_test_dir,
            'UserAttachmentLifetime':
            'keepAlways',
            'TestingEnvironmentVariables':
            test_envs,
            'DependentProductPaths':
            [self._app_under_test_dir, self._test_bundle_dir],
        }
Ejemplo n.º 8
0
    def _GenerateTestRootForXctest(self):
        """Generates the test root for XCTest.

    The approach constructs xctestrun.plist from Xcode. Then copies app under
    test, test bundle and xctestrun.plist to test root directory.
    """
        app_under_test_plugins_dir = os.path.join(self._app_under_test_dir,
                                                  'PlugIns')
        if not os.path.exists(app_under_test_plugins_dir):
            os.mkdir(app_under_test_plugins_dir)
        new_test_bundle_path = os.path.join(
            app_under_test_plugins_dir,
            os.path.basename(self._test_bundle_dir))
        # The test bundle under PlugIns can not be symlink since it will cause
        # app installation error.
        if os.path.islink(self._test_bundle_dir):
            shutil.copytree(self._test_bundle_dir, new_test_bundle_path)
            self._test_bundle_dir = new_test_bundle_path
        elif new_test_bundle_path != self._test_bundle_dir:
            self._test_bundle_dir = _MoveAndReplaceFile(
                self._test_bundle_dir, app_under_test_plugins_dir)

        if self._on_device:
            platform_path = xcode_info_util.GetSdkPlatformPath(self._sdk)
            app_under_test_frameworks_dir = os.path.join(
                self._app_under_test_dir, 'Frameworks')
            if not os.path.exists(app_under_test_frameworks_dir):
                os.mkdir(app_under_test_frameworks_dir)
            app_under_test_signing_identity = bundle_util.GetCodesignIdentity(
                self._app_under_test_dir)
            _CopyAndSignFramework(
                os.path.join(platform_path,
                             'Developer/Library/Frameworks/XCTest.framework'),
                app_under_test_frameworks_dir, app_under_test_signing_identity)
            bundle_injection_lib = os.path.join(
                platform_path, 'Developer/usr/lib/libXCTestBundleInject.dylib')
            _CopyAndSignLibFile(bundle_injection_lib,
                                app_under_test_frameworks_dir,
                                app_under_test_signing_identity)
            if xcode_info_util.GetXcodeVersionNumber() >= 1100:
                _CopyAndSignFramework(
                    os.path.join(
                        platform_path, 'Developer/Library/PrivateFrameworks/'
                        'XCTAutomationSupport.framework'),
                    app_under_test_frameworks_dir,
                    app_under_test_signing_identity)
                _CopyAndSignLibFile(
                    os.path.join(platform_path,
                                 _LIB_XCTEST_SWIFT_RELATIVE_PATH),
                    app_under_test_frameworks_dir,
                    app_under_test_signing_identity)
            if xcode_info_util.GetXcodeVersionNumber() >= 1300:
                _CopyAndSignFramework(
                    os.path.join(
                        platform_path, 'Developer/Library/PrivateFrameworks/'
                        'XCUIAutomation.framework'),
                    app_under_test_frameworks_dir,
                    app_under_test_signing_identity)
                _CopyAndSignFramework(
                    os.path.join(
                        platform_path, 'Developer/Library/PrivateFrameworks/'
                        'XCTestCore.framework'), app_under_test_frameworks_dir,
                    app_under_test_signing_identity)
                _CopyAndSignFramework(
                    os.path.join(
                        platform_path, 'Developer/Library/PrivateFrameworks/'
                        'XCUnit.framework'), app_under_test_frameworks_dir,
                    app_under_test_signing_identity)
            bundle_util.CodesignBundle(self._test_bundle_dir)
            bundle_util.CodesignBundle(self._app_under_test_dir)

        app_under_test_name = os.path.splitext(
            os.path.basename(self._app_under_test_dir))[0]
        platform_name = 'iPhoneOS' if self._on_device else 'iPhoneSimulator'
        developer_path = '__PLATFORMS__/%s.platform/Developer' % platform_name

        if self._on_device:
            dyld_insert_libs = '__TESTHOST__/Frameworks/libXCTestBundleInject.dylib'
        else:
            dyld_insert_libs = ('%s/usr/lib/libXCTestBundleInject.dylib' %
                                developer_path)
        test_envs = {
            'XCInjectBundleInto':
            os.path.join('__TESTHOST__', app_under_test_name),
            'DYLD_FRAMEWORK_PATH':
            '__TESTROOT__:{developer}/Library/Frameworks:'
            '{developer}/Library/PrivateFrameworks'.format(
                developer=developer_path),
            'DYLD_INSERT_LIBRARIES':
            dyld_insert_libs,
            'DYLD_LIBRARY_PATH':
            '__TESTROOT__:%s/usr/lib:' % developer_path
        }
        self._xctestrun_dict = {
            'ProductModuleName': self._test_name,
            'TestHostPath': self._app_under_test_dir,
            'TestBundlePath': self._test_bundle_dir,
            'IsAppHostedTestBundle': True,
            'TestingEnvironmentVariables': test_envs
        }