How to use matching_platform method in tox

Best Python code snippet using tox_python

deduplicate_tests.py

Source:deduplicate_tests.py Github

copy

Full Screen

1#!/usr/bin/env python2# Copyright (C) 2010 Google Inc. All rights reserved.3#4# Redistribution and use in source and binary forms, with or without5# modification, are permitted provided that the following conditions6# are met:7#8# 1. Redistributions of source code must retain the above copyright9# notice, this list of conditions and the following disclaimer.10# 2. Redistributions in binary form must reproduce the above copyright11# notice, this list of conditions and the following disclaimer in the12# documentation and/or other materials provided with the distribution.13#14# THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY15# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED16# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE17# DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY18# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES19# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;20# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND21# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT22# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF23# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.24"""deduplicate_tests -- lists duplicated between platforms.25If platform/mac-leopard is missing an expected test output, we fall back on26platform/mac. This means it's possible to grow redundant test outputs,27where we have the same expected data in both a platform directory and another28platform it falls back on.29"""30import collections31import fnmatch32import os33import subprocess34import sys35import re36from webkitpy.common.checkout import scm37from webkitpy.common.system import executive38from webkitpy.common.system import logutils39from webkitpy.common.system import ospath40from webkitpy.layout_tests.port import factory as port_factory41_log = logutils.get_logger(__file__)42_BASE_PLATFORM = 'base'43def port_fallbacks():44 """Get the port fallback information.45 Returns:46 A dictionary mapping platform name to a list of other platforms to fall47 back on. All platforms fall back on 'base'.48 """49 fallbacks = {_BASE_PLATFORM: []}50 for port_name in port_factory.all_port_names():51 try:52 platforms = port_factory.get(port_name).baseline_search_path()53 except NotImplementedError:54 _log.error("'%s' lacks baseline_search_path(), please fix."55 % port_name)56 fallbacks[port_name] = [_BASE_PLATFORM]57 continue58 fallbacks[port_name] = [os.path.basename(p) for p in platforms][1:]59 fallbacks[port_name].append(_BASE_PLATFORM)60 return fallbacks61def parse_git_output(git_output, glob_pattern):62 """Parses the output of git ls-tree and filters based on glob_pattern.63 Args:64 git_output: result of git ls-tree -r HEAD LayoutTests.65 glob_pattern: a pattern to filter the files.66 Returns:67 A dictionary mapping (test name, hash of content) => [paths]68 """69 hashes = collections.defaultdict(set)70 for line in git_output.split('\n'):71 if not line:72 break73 attrs, path = line.strip().split('\t')74 if not fnmatch.fnmatch(path, glob_pattern):75 continue76 path = path[len('LayoutTests/'):]77 match = re.match(r'^(platform/.*?/)?(.*)', path)78 test = match.group(2)79 _, _, hash = attrs.split(' ')80 hashes[(test, hash)].add(path)81 return hashes82def cluster_file_hashes(glob_pattern):83 """Get the hashes of all the test expectations in the tree.84 We cheat and use git's hashes.85 Args:86 glob_pattern: a pattern to filter the files.87 Returns:88 A dictionary mapping (test name, hash of content) => [paths]89 """90 # A map of file hash => set of all files with that hash.91 hashes = collections.defaultdict(set)92 # Fill in the map.93 cmd = ('git', 'ls-tree', '-r', 'HEAD', 'LayoutTests')94 try:95 git_output = executive.Executive().run_command(cmd,96 cwd=scm.find_checkout_root())97 except OSError, e:98 if e.errno == 2: # No such file or directory.99 _log.error("Error: 'No such file' when running git.")100 _log.error("This script requires git.")101 sys.exit(1)102 raise e103 return parse_git_output(git_output, glob_pattern)104def dirname_to_platform(dirname):105 if dirname == 'chromium-linux':106 return 'chromium-linux-x86'107 elif dirname == 'chromium-win':108 return 'chromium-win-win7'109 elif dirname == 'chromium-mac':110 return 'chromium-mac-snowleopard'111 return dirname112def extract_platforms(paths):113 """Extracts the platforms from a list of paths matching ^platform/(.*?)/.114 Args:115 paths: a list of paths.116 Returns:117 A dictionary containing all platforms from paths.118 """119 platforms = {}120 for path in paths:121 match = re.match(r'^platform/(.*?)/', path)122 if match:123 platform = dirname_to_platform(match.group(1))124 else:125 platform = _BASE_PLATFORM126 platforms[platform] = path127 return platforms128def has_intermediate_results(test, fallbacks, matching_platform,129 path_exists=os.path.exists):130 """Returns True if there is a test result that causes us to not delete131 this duplicate.132 For example, chromium-linux may be a duplicate of the checked in result,133 but chromium-win may have a different result checked in. In this case,134 we need to keep the duplicate results.135 Args:136 test: The test name.137 fallbacks: A list of platforms we fall back on.138 matching_platform: The platform that we found the duplicate test139 result. We can stop checking here.140 path_exists: Optional parameter that allows us to stub out141 os.path.exists for testing.142 """143 for dirname in fallbacks:144 platform = dirname_to_platform(dirname)145 if platform == matching_platform:146 return False147 test_path = os.path.join('LayoutTests', 'platform', dirname, test)148 if path_exists(test_path):149 return True150 return False151def get_relative_test_path(filename, relative_to,152 checkout_root=scm.find_checkout_root()):153 """Constructs a relative path to |filename| from |relative_to|.154 Args:155 filename: The test file we're trying to get a relative path to.156 relative_to: The absolute path we're relative to.157 Returns:158 A relative path to filename or None if |filename| is not below159 |relative_to|.160 """161 layout_test_dir = os.path.join(checkout_root, 'LayoutTests')162 abs_path = os.path.join(layout_test_dir, filename)163 return ospath.relpath(abs_path, relative_to)164def find_dups(hashes, port_fallbacks, relative_to):165 """Yields info about redundant test expectations.166 Args:167 hashes: a list of hashes as returned by cluster_file_hashes.168 port_fallbacks: a list of fallback information as returned by169 get_port_fallbacks.170 relative_to: the directory that we want the results relative to171 Returns:172 a tuple containing (test, platform, fallback, platforms)173 """174 for (test, hash), cluster in hashes.items():175 if len(cluster) < 2:176 continue # Common case: only one file with that hash.177 # Compute the list of platforms we have this particular hash for.178 platforms = extract_platforms(cluster)179 if len(platforms) == 1:180 continue181 # See if any of the platforms are redundant with each other.182 for platform in platforms.keys():183 if platform not in port_factory.all_port_names():184 continue185 for dirname in port_fallbacks[platform]:186 fallback = dirname_to_platform(dirname)187 if fallback not in platforms.keys():188 continue189 # We have to verify that there isn't an intermediate result190 # that causes this duplicate hash to exist.191 if has_intermediate_results(test, port_fallbacks[platform],192 fallback):193 continue194 # We print the relative path so it's easy to pipe the results195 # to xargs rm.196 path = get_relative_test_path(platforms[platform], relative_to)197 if not path:198 continue199 yield {200 'test': test,201 'platform': platform,202 'fallback': dirname,203 'path': path,204 }205def deduplicate(glob_pattern):206 """Traverses LayoutTests and returns information about duplicated files.207 Args:208 glob pattern to filter the files in LayoutTests.209 Returns:210 a dictionary containing test, path, platform and fallback.211 """212 fallbacks = port_fallbacks()213 hashes = cluster_file_hashes(glob_pattern)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run tox automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful