github.com/bazelbuild/rules_webtesting@v0.2.0/web/web.bzl (about)

     1  # Copyright 2016 Google Inc.
     2  #
     3  # Licensed under the Apache License, Version 2.0 (the "License");
     4  # you may not use this file except in compliance with the License.
     5  # You may obtain a copy of the License at
     6  #
     7  #      http://www.apache.org/licenses/LICENSE-2.0
     8  #
     9  # Unless required by applicable law or agreed to in writing, software
    10  # distributed under the License is distributed on an "AS IS" BASIS,
    11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  # See the License for the specific language governing permissions and
    13  # limitations under the License.
    14  """Public definitions for web_test related build rules."""
    15  
    16  load(
    17      "//web/internal:browser.bzl",
    18      browser_alias = "browser",
    19  )
    20  load("//web/internal:collections.bzl", "lists", "maps")
    21  load("//web/internal:constants.bzl", "DEFAULT_TEST_SUITE_TAGS")
    22  load(
    23      "//web/internal:web_test.bzl",
    24      web_test_alias = "web_test",
    25  )
    26  load(
    27      "//web/internal:web_test_archive.bzl",
    28      web_test_archive_alias = "web_test_archive",
    29  )
    30  load(
    31      "//web/internal:web_test_config.bzl",
    32      web_test_config_alias = "web_test_config",
    33  )
    34  load(
    35      "//web/internal:web_test_files.bzl",
    36      web_test_files_alias = "web_test_files",
    37  )
    38  load(
    39      "//web/internal:web_test_named_executable.bzl",
    40      web_test_named_executable_alias = "web_test_named_executable",
    41  )
    42  load(
    43      "//web/internal:web_test_named_file.bzl",
    44      web_test_named_file_alias = "web_test_named_file",
    45  )
    46  
    47  def web_test_suite(
    48          name,
    49          browsers,
    50          browser_overrides = None,
    51          test_suite_tags = None,
    52          visibility = None,
    53          **kwargs):
    54      """Defines a test_suite of web_test targets to be run.
    55  
    56      Args:
    57          name: Name; required. A unique name for this rule.
    58          browsers: List of labels; required. The browsers with which to run the test.
    59          browser_overrides: Dictionary; optional; default is an empty dictionary. A
    60            dictionary mapping from browser names to browser-specific web_test
    61            attributes, such as shard_count, flakiness, timeout, etc. For example:
    62            {'//browsers:chrome-native': {'shard_count': 3, 'flaky': 1}
    63             '//browsers:firefox-native': {'shard_count': 1, 'timeout': 100}}.
    64          test_suite_tags: List of strings; tags for the generated test_suite rule.
    65          visibility: List of labels; optional.
    66          **kwargs: Additional arguments for web_test rule.
    67      """
    68      if not lists.is_list_like(browsers):
    69          fail("expected a sequence type for attribute 'browsers' but got '%s'" %
    70               type(browsers))
    71      if not browsers:
    72          fail("expected non-empty value for attribute 'browsers'")
    73  
    74      # Check explicitly for None so that users can set this to the empty list.
    75      if test_suite_tags == None:
    76          test_suite_tags = DEFAULT_TEST_SUITE_TAGS
    77  
    78      tests = []
    79      browser_overrides = browser_overrides or {}
    80  
    81      for browser in browsers:  # pylint: disable=redefined-outer-name
    82          unqualified_browser = browser.split(":", 2)[1]
    83          test_name = name + "_" + unqualified_browser
    84  
    85          # Replace current browser attributes with those specified in the browser
    86          # overrides.
    87          overrides = browser_overrides.get(browser) or browser_overrides.get(
    88              unqualified_browser,
    89          ) or {}
    90          overridden_kwargs = _apply_browser_overrides(kwargs, overrides)
    91          if not "tags" in overridden_kwargs:
    92              overridden_kwargs["tags"] = []
    93          overridden_kwargs["tags"] = lists.clone(overridden_kwargs["tags"]) + ["browser:" + unqualified_browser]
    94  
    95          web_test(
    96              name = test_name,
    97              browser = browser,
    98              visibility = visibility,
    99              **overridden_kwargs
   100          )
   101          tests += [test_name]
   102  
   103      native.test_suite(
   104          name = name,
   105          tests = tests,
   106          tags = test_suite_tags,
   107          visibility = visibility,
   108      )
   109  
   110  def _apply_browser_overrides(kwargs, overrides):
   111      """Handles browser-specific options that override the top-level definitions.
   112  
   113      Args:
   114          kwargs: A dictionary of arguments that will be overridden.
   115          overrides: A dictionary of attributes with the new attributes that should
   116            replace the top-level definitions.
   117  
   118      Returns:
   119          A dictionary of updated attributes.  For example:
   120          {'shard_count': 4, 'size': 'medium', 'timeout': 100, 'flaky': 1}
   121      """
   122      overridden_kwargs = maps.clone(kwargs)
   123      overridden_kwargs.update(overrides)
   124  
   125      return overridden_kwargs
   126  
   127  def browser(testonly = True, **kwargs):
   128      """Wrapper around browser to correctly set defaults."""
   129      browser_alias(testonly = testonly, **kwargs)
   130  
   131  def web_test(config = None, launcher = None, size = None, **kwargs):
   132      """Wrapper around web_test to correctly set defaults."""
   133      config = config or str(Label("//web:default_config"))
   134      launcher = launcher or str(Label("//go/wtl/main"))
   135      size = size or "large"
   136      web_test_alias(config = config, launcher = launcher, size = size, **kwargs)
   137  
   138  def web_test_config(testonly = True, **kwargs):
   139      """Wrapper around web_test_config to correctly set defaults."""
   140      web_test_config_alias(testonly = testonly, **kwargs)
   141  
   142  def web_test_named_executable(testonly = True, **kwargs):
   143      """Wrapper around web_test_named_executable to correctly set defaults."""
   144      web_test_named_executable_alias(testonly = testonly, **kwargs)
   145  
   146  def web_test_named_file(testonly = True, **kwargs):
   147      """Wrapper around web_test_named_file to correctly set defaults."""
   148      web_test_named_file_alias(testonly = testonly, **kwargs)
   149  
   150  def web_test_archive(testonly = True, **kwargs):
   151      """Wrapper around web_test_archive to correctly set defaults."""
   152      web_test_archive_alias(testonly = testonly, **kwargs)
   153  
   154  def web_test_files(testonly = True, **kwargs):
   155      """Wrapper around web_test_files to correctly set defaults."""
   156      web_test_files_alias(testonly = testonly, **kwargs)