github.com/bazelbuild/rules_webtesting@v0.2.0/web/internal/metadata.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  """A library of functions for working with web_test metadata files."""
    15  
    16  load(":files.bzl", "files")
    17  
    18  def _merge_files(ctx, merger, output, inputs):
    19      """Produces a merged web test metadata file.
    20  
    21      Args:
    22          ctx: a Skylark rule context.
    23          merger: the WTL metadata merger executable.
    24          output: a File object for the output file.
    25          inputs: a list of File objects. These files are in order of priority;
    26            i.e. values in the first file will take precedence over values in the
    27            second file, etc.
    28      """
    29      paths = [i.path for i in reversed(inputs)]
    30      short_paths = [i.short_path for i in inputs]
    31      args = ["--output", output.path] + paths
    32  
    33      ctx.actions.run(
    34          outputs = [output],
    35          inputs = inputs,
    36          executable = merger,
    37          arguments = args,
    38          mnemonic = "METADATAMERGER",
    39          progress_message = "merging %s" % (", ".join(short_paths)),
    40      )
    41  
    42  def _create_file(
    43          ctx,
    44          output,
    45          browser_label = None,
    46          capabilities = None,
    47          config_label = None,
    48          environment = None,
    49          label = None,
    50          test_label = None,
    51          web_test_files = None,
    52          extension = None):
    53      """Generates a web_test metadata file with specified contents.
    54  
    55      Args:
    56          ctx: a Skylark rule context.
    57          output: File object. The file to write the metadata to.
    58          browser_label: Label. The label for a browser rule.
    59          capabilities: struct; Browser capabilities.
    60          config_label: Label. The label for the web_test_config rule.
    61          environment: string. The Web Test Launcher environment name.
    62          label: Label. The label for this target.
    63          test_label: Label. The label for the test being executed.
    64          web_test_files: sequence of web_test_file structs.
    65          extension: map or struct defining additional fields that should be added
    66            metadata file.
    67      """
    68      fields = {}
    69      if browser_label:
    70          fields["browserLabel"] = str(browser_label)
    71      if capabilities:
    72          fields["capabilities"] = capabilities
    73      if config_label:
    74          fields["configLabel"] = str(config_label)
    75      if environment:
    76          fields["environment"] = environment
    77      if label:
    78          fields["label"] = str(label)
    79      if test_label:
    80          fields["testLabel"] = str(test_label)
    81      if web_test_files:
    82          fields["webTestFiles"] = web_test_files
    83      if extension:
    84          if type(extension) == type({}):
    85              extension = struct(**extension)
    86          fields["extension"] = extension
    87  
    88      ctx.actions.write(
    89          output = output,
    90          content = struct(**fields).to_json(),
    91          is_executable = False,
    92      )
    93  
    94  def _web_test_files(ctx, archive_file = None, named_files = None, strip_prefix = ""):
    95      """Build a web_test_files struct.
    96  
    97      Args:
    98          ctx: a Skylark rule context.
    99          archive_file: a File object. The archive file where the named_files will be
   100            found. If absent, the named_files are located directly in the runfiles.
   101          named_files: a dict of strings to strings or File objects. The mapping of
   102            names to file path. If archive_file is absent, the values should be
   103            File objects for files that will be in the runfiles of the test. If
   104            archive_file is present, the values should be string paths referencing
   105            files in archive_file.
   106          strip_prefix: string. A prefix to strip from the begining of paths in an archive.
   107  
   108      Returns:
   109          A web_test_files struct.
   110      """
   111      named_files = named_files or {}
   112      for k, v in named_files.items():
   113          if type(v) != type(""):
   114              named_files[k] = files.long_path(ctx, v)
   115      if archive_file:
   116          archive_file = files.long_path(ctx, archive_file)
   117      return struct(
   118          archiveFile = archive_file,
   119          namedFiles = struct(**named_files),
   120          stripPrefix = strip_prefix,
   121      )
   122  
   123  metadata = struct(
   124      create_file = _create_file,
   125      merge_files = _merge_files,
   126      web_test_files = _web_test_files,
   127  )