def __init__(self, egtests_app, destination, shards, retries, out_dir=os.path.basename(os.getcwd()), env=None): """Initialize launch command. Args: egtests_app: (EgtestsApp) An egtests_app to run. destination: (str) A destination. shards: (int) A number of shards. retries: (int) A number of retries. out_dir: (str) A folder in which xcodebuild will generate test output. By default it is a current directory. env: (dict) Environment variables. Raises: LaunchCommandCreationError: if one of parameters was not set properly. """ if not isinstance(egtests_app, EgtestsApp): raise test_runner.AppNotFoundError( 'Parameter `egtests_app` is not EgtestsApp: %s' % egtests_app) self.egtests_app = egtests_app self.destination = destination self.shards = shards self.retries = retries self.out_dir = out_dir self.logs = collections.OrderedDict() self.test_results = collections.OrderedDict() self.env = env if distutils.version.LooseVersion('11.0') <= distutils.version.LooseVersion( test_runner.get_current_xcode_info()['version']): self._log_parser = xcode_log_parser.Xcode11LogParser() else: self._log_parser = xcode_log_parser.XcodeLogParser()
def _xcresulttool_get(xcresult_path, ref_id=None): """Runs `xcresulttool get` command and returns JSON output. Xcresult folder contains test result in Xcode Result Types v. 3.19. Documentation of xcresulttool usage is in https://help.apple.com/xcode/mac/current/#/devc38fc7392?sub=dev0fe9c3ea3 Args: xcresult_path: A full path to xcresult folder that must have Info.plist. ref_id: A reference id used in a command and can be used to get test data. If id is from ['timelineRef', 'logRef', 'testsRef', 'diagnosticsRef'] method will run xcresulttool 2 times: 1. to get specific id value running command without id parameter. xcresulttool get --path %xcresul% 2. to get data based on id xcresulttool get --path %xcresul% --id %id% Returns: An output of a command in JSON format. """ xcode_info = test_runner.get_current_xcode_info() folder = os.path.join(xcode_info['path'], 'usr', 'bin') # By default xcresulttool is %Xcode%/usr/bin, # that is not in directories from $PATH # Need to check whether %Xcode%/usr/bin is in a $PATH # and then call xcresulttool if folder not in os.environ['PATH']: os.environ['PATH'] += ':%s' % folder reference_types = [ 'timelineRef', 'logRef', 'testsRef', 'diagnosticsRef' ] if ref_id in reference_types: data = json.loads( Xcode11LogParser._xcresulttool_get(xcresult_path)) # Redefine ref_id to get only the reference data ref_id = data['actions']['_values'][0]['actionResult'][ref_id][ 'id']['_value'] # If no ref_id then xcresulttool will use default(root) id. id_params = ['--id', ref_id] if ref_id else [] xcresult_command = [ 'xcresulttool', 'get', '--format', 'json', '--path', xcresult_path ] + id_params return subprocess.check_output(xcresult_command).strip()