def _test(value, expect, open_fp): actual = mx._safe_path(value) if actual != expect: nl = os.linesep assert False, 'Failed safe_path test{} input: {} (len={}){}expect: {} (len={}){}actual: {} (len={})'.format(nl, value, len(value), nl, expect, len(expect), nl, actual, len(actual)) if open_fp and value != open_fp.name: try: with mx.open(value, 'w') as fp: fp.write('blah') with mx.open(value, 'r') as fp: contents = fp.read() assert contents == 'blah', contents finally: if os.path.exists(value): os.unlink(value)
def testdownstream(suite, repoUrls, relTargetSuiteDir, mxCommands, branch=None): """ Tests a downstream repo against the current working directory state of `suite`. :param mx.Suite suite: the suite to test against the downstream repo :param list repoUrls: URLs of downstream repos to clone, the first of which is the repo being tested :param str relTargetSuiteDir: directory of the downstream suite to test relative to the top level directory of the downstream repo being tested :param list mxCommands: argument lists for the mx commands run in downstream suite being tested :param str branch: name(s) of branch to look for in downstream repo(s) """ assert len(repoUrls) > 0 repoUrls = [mx_urlrewrites.rewriteurl(url) for url in repoUrls] workDir = join(suite.get_output_root(), 'testdownstream') # A mirror of each suites in the same repo as `suite` is created via copying rel_mirror = os.path.relpath(suite.dir, mx.SuiteModel.siblings_dir(suite.dir)) in_subdir = os.sep in rel_mirror suites_in_repo = [suite] if in_subdir: base = os.path.dirname(suite.dir) for e in os.listdir(base): candidate = join(base, e) if candidate != suite.dir: mxDir = mx._is_suite_dir(candidate) if mxDir: matches = [s for s in mx.suites() if s.dir == candidate] if len(matches) == 0: suites_in_repo.append( mx.SourceSuite(mxDir, primary=False, load=False)) else: suites_in_repo.append(matches[0]) if suite.vc: vc_metadir = mx._safe_path(mx.VC.get_vc(suite.vc_dir).metadir()) blacklist = {suite.vc_dir: [join(suite.vc_dir, vc_metadir)]} else: blacklist = {} for suite_in_repo in suites_in_repo: output_root = mx._safe_path(suite_in_repo.get_output_root()) blacklist.setdefault(dirname(output_root), []).append(output_root) def omitted_dirs(d, names): mx.log('Copying ' + d) to_omit = [] for blacklisted_dir in blacklist.get(d, []): mx.log('Omitting ' + blacklisted_dir) to_omit.append(basename(blacklisted_dir)) return to_omit if suite.vc_dir and suite.dir != suite.vc_dir: mirror = join(workDir, basename(suite.vc_dir)) else: mirror = join(workDir, suite.name) if exists(mirror): mx.rmtree(mirror) mx.copytree(suite.vc_dir, mirror, ignore=omitted_dirs, symlinks=True) targetDir = None for repoUrl in repoUrls: # Deduce a target name from the target URL url = _urllib_parse.urlparse(repoUrl) targetName = url.path if targetName.rfind('/') != -1: targetName = targetName[targetName.rfind('/') + 1:] if targetName.endswith('.git'): targetName = targetName[0:-len('.git')] repoWorkDir = join(workDir, targetName) git = mx.GitConfig() if exists(repoWorkDir): git.pull(repoWorkDir) else: git.clone(repoUrl, repoWorkDir) if branch is None: branch = [] elif isinstance(branch, str): branch = [branch] else: assert isinstance(branch, list) # fall back to the branch of the main repo active_branch = git.active_branch(suite.dir, abortOnError=False) if active_branch: branch.append(active_branch) updated = False for branch_name in branch: if git.update_to_branch(repoWorkDir, branch_name, abortOnError=False): updated = True break if not updated: mx.warn('Could not update {} to any of the following branches: {}'. format(repoWorkDir, ', '.join(branch))) if not targetDir: targetDir = repoWorkDir assert not isabs(relTargetSuiteDir) targetSuiteDir = join(targetDir, relTargetSuiteDir) assert targetSuiteDir.startswith(targetDir) mxpy = None if suite != mx._mx_suite else join(mirror, 'mx.py') for command in mxCommands: mx.logv('[running "mx ' + ' '.join(command) + '" in ' + targetSuiteDir + ']') mx.run_mx(command, targetSuiteDir, mxpy=mxpy)
def testdownstream(suite, repoUrls, relTargetSuiteDir, mxCommands, branch=None): """ Tests a downstream repo against the current working directory state of `suite`. :param mx.Suite suite: the suite to test against the downstream repo :param list repoUrls: URLs of downstream repos to clone, the first of which is the repo being tested :param str relTargetSuiteDir: directory of the downstream suite to test relative to the top level directory of the downstream repo being tested :param list mxCommands: argument lists for the mx commands run in downstream suite being tested :param str branch: name of branch to look for in downstream repo(s) """ assert len(repoUrls) > 0 repoUrls = [mx_urlrewrites.rewriteurl(url) for url in repoUrls] workDir = join(suite.get_output_root(), 'testdownstream') # A mirror of each suites in the same repo as `suite` is created via copying rel_mirror = os.path.relpath(suite.dir, mx.SuiteModel.siblings_dir(suite.dir)) in_subdir = os.sep in rel_mirror suites_in_repo = [suite] if in_subdir: base = os.path.dirname(suite.dir) for e in os.listdir(base): candidate = join(base, e) if candidate != suite.dir: mxDir = mx._is_suite_dir(candidate) if mxDir: matches = [s for s in mx.suites() if s.dir == candidate] if len(matches) == 0: suites_in_repo.append( mx.SourceSuite(mxDir, primary=False, load=False)) else: suites_in_repo.append(matches[0]) for suite_in_repo in suites_in_repo: if suite_in_repo.vc_dir and suite_in_repo.dir != suite_in_repo.vc_dir: mirror = join(workDir, basename(suite_in_repo.vc_dir), suite_in_repo.name) else: mirror = join(workDir, suite_in_repo.name) if exists(mirror): mx.rmtree(mirror) output_root = mx._safe_path(suite_in_repo.get_output_root()) def ignore_output_root(d, names): mx.log('Copying ' + d) if d == os.path.dirname(output_root): mx.log('Omitting ' + output_root) return [os.path.basename(output_root)] return [] mx.copytree(suite_in_repo.dir, mirror, ignore=ignore_output_root, symlinks=True) targetDir = None for repoUrl in repoUrls: # Deduce a target name from the target URL url = _urllib_parse.urlparse(repoUrl) targetName = url.path if targetName.rfind('/') != -1: targetName = targetName[targetName.rfind('/') + 1:] if targetName.endswith('.git'): targetName = targetName[0:-len('.git')] repoWorkDir = join(workDir, targetName) git = mx.GitConfig() if exists(repoWorkDir): git.pull(repoWorkDir) else: git.clone(repoUrl, repoWorkDir) # See if there's a matching (non-master) branch and use it if there is if not branch: branch = git.git_command( suite.dir, ['rev-parse', '--abbrev-ref', 'HEAD']).strip() if branch != 'master': git.git_command(repoWorkDir, ['checkout', branch], abortOnError=False) if not targetDir: targetDir = repoWorkDir assert not isabs(relTargetSuiteDir) targetSuiteDir = join(targetDir, relTargetSuiteDir) assert targetSuiteDir.startswith(targetDir) mxpy = None if suite != mx._mx_suite else join(mirror, 'mx.py') for command in mxCommands: mx.logv('[running "mx ' + ' '.join(command) + '" in ' + targetSuiteDir + ']') mx.run_mx(command, targetSuiteDir, mxpy=mxpy)