Home | History | Annotate | Download | only in host_driven
      1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 
      5 """Annotations for host-driven tests."""
      6 # pylint: disable=W0212
      7 
      8 import os
      9 
     10 
     11 class AnnotatedFunctions(object):
     12   """A container for annotated methods."""
     13   _ANNOTATED = {}
     14 
     15   @staticmethod
     16   def _AddFunction(annotation, function):
     17     """Adds an annotated function to our container.
     18 
     19     Args:
     20       annotation: the annotation string.
     21       function: the function.
     22     Returns:
     23       The function passed in.
     24     """
     25     module_name = os.path.splitext(os.path.basename(
     26         function.__globals__['__file__']))[0]
     27     qualified_function_name = '.'.join([module_name, function.func_name])
     28     function_list = AnnotatedFunctions._ANNOTATED.get(annotation, [])
     29     function_list.append(qualified_function_name)
     30     AnnotatedFunctions._ANNOTATED[annotation] = function_list
     31     return function
     32 
     33   @staticmethod
     34   def IsAnnotated(annotation, qualified_function_name):
     35     """True if function name (module.function) contains the annotation.
     36 
     37     Args:
     38       annotation: the annotation string.
     39       qualified_function_name: the qualified function name.
     40     Returns:
     41       True if module.function contains the annotation.
     42     """
     43     return qualified_function_name in AnnotatedFunctions._ANNOTATED.get(
     44         annotation, [])
     45 
     46   @staticmethod
     47   def GetTestAnnotations(qualified_function_name):
     48     """Returns a list containing all annotations for the given function.
     49 
     50     Args:
     51       qualified_function_name: the qualified function name.
     52     Returns:
     53       List of all annotations for this function.
     54     """
     55     return [annotation
     56             for annotation, tests in AnnotatedFunctions._ANNOTATED.iteritems()
     57             if qualified_function_name in tests]
     58 
     59 
     60 # The following functions are annotations used for the host-driven tests.
     61 def Smoke(function):
     62   return AnnotatedFunctions._AddFunction('Smoke', function)
     63 
     64 
     65 def SmallTest(function):
     66   return AnnotatedFunctions._AddFunction('SmallTest', function)
     67 
     68 
     69 def MediumTest(function):
     70   return AnnotatedFunctions._AddFunction('MediumTest', function)
     71 
     72 
     73 def LargeTest(function):
     74   return AnnotatedFunctions._AddFunction('LargeTest', function)
     75 
     76 
     77 def EnormousTest(function):
     78   return AnnotatedFunctions._AddFunction('EnormousTest', function)
     79 
     80 
     81 def FlakyTest(function):
     82   return AnnotatedFunctions._AddFunction('FlakyTest', function)
     83 
     84 
     85 def DisabledTest(function):
     86   return AnnotatedFunctions._AddFunction('DisabledTest', function)
     87 
     88 
     89 def Feature(feature_list):
     90   def _AddFeatures(function):
     91     for feature in feature_list:
     92       AnnotatedFunctions._AddFunction('Feature:%s' % feature, function)
     93     return AnnotatedFunctions._AddFunction('Feature', function)
     94   return _AddFeatures
     95