forked from valerian-roche/rules_docker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make transitions optional (bazelbuild#2068)
* Make transitions optional Transitions, introduced in bazelbuild#1963 can now be disabled by calling bazel with `--@io_bazel_rules_docker//transitions:enable=no`. Added tests to verify the behaviour. * Update ubuntu image to try to bust the bazel cache
- Loading branch information
1 parent
3c94264
commit 071eb9b
Showing
9 changed files
with
233 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
schemaVersion: 2.0.0 | ||
|
||
fileContentTests: | ||
- name: "validate architecture" | ||
path: "/Files/got_arch.txt" | ||
expectedContents: ["%WANT_ARCH%"] | ||
- name: "validate os" | ||
path: "/Files/got_os.txt" | ||
expectedContents: ["%WANT_OS%"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
schemaVersion: 2.0.0 | ||
|
||
fileContentTests: | ||
- name: "validate architecture" | ||
path: "/Files/got_arch.txt" | ||
expectedContents: ["arm64"] | ||
- name: "validate os" | ||
path: "/Files/got_os.txt" | ||
expectedContents: ["windows"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
""" | ||
Rules for running tests with a specific value of the //transitions:enabled flag | ||
""" | ||
|
||
def _templated_file_impl(ctx): | ||
out = ctx.outputs.out | ||
ctx.actions.expand_template( | ||
template = ctx.file.template, | ||
output = out, | ||
substitutions = dict([s.split("=", 1) for s in ctx.attr.substitutions]), | ||
) | ||
return DefaultInfo( | ||
files = depset([out]), | ||
) | ||
|
||
# Replaces substitutions split on `=` in the template. Substitution is a list | ||
# so that is usable with multiple selects, | ||
templated_file = rule( | ||
attrs = { | ||
"template": attr.label( | ||
mandatory = True, | ||
allow_single_file = True, | ||
), | ||
"substitutions": attr.string_list(), | ||
"out": attr.output(), | ||
}, | ||
implementation = _templated_file_impl, | ||
) | ||
|
||
def _enable_transition_impl(settings, attr): | ||
_ = settings | ||
return {"@io_bazel_rules_docker//transitions:enable": attr.transitions_enabled} | ||
|
||
_enable_transition = transition( | ||
implementation = _enable_transition_impl, | ||
inputs = [], | ||
outputs = [ | ||
"@io_bazel_rules_docker//transitions:enable", | ||
], | ||
) | ||
|
||
def _transitioned_test(ctx): | ||
source_info = ctx.attr.actual[DefaultInfo] | ||
|
||
# Bazel wants the executable to be generated by this rule, let's oblige by | ||
# just copying the actual runner. | ||
executable = None | ||
if source_info.files_to_run and source_info.files_to_run.executable: | ||
executable = ctx.actions.declare_file("{}_{}".format(ctx.file.actual.basename, "on" if ctx.attr.transitions_enabled else "off")) | ||
ctx.actions.run_shell( | ||
command = "cp {} {}".format(source_info.files_to_run.executable.path, executable.path), | ||
inputs = [source_info.files_to_run.executable], | ||
outputs = [executable], | ||
) | ||
return [DefaultInfo( | ||
files = depset(ctx.files.actual), | ||
runfiles = source_info.default_runfiles.merge(source_info.data_runfiles), | ||
executable = executable, | ||
)] | ||
|
||
# Defines a test that runs with a specific value of the | ||
# @io_bazel_rules_docker//transitions:enable flag. | ||
transition_test = rule( | ||
attrs = { | ||
"actual": attr.label( | ||
mandatory = True, | ||
allow_single_file = True, | ||
), | ||
"transitions_enabled": attr.bool(), | ||
"_allowlist_function_transition": attr.label( | ||
default = "@bazel_tools//tools/allowlists/function_transition_allowlist", | ||
), | ||
}, | ||
cfg = _enable_transition, | ||
test = True, | ||
implementation = _transitioned_test, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Copyright 2022 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag") | ||
|
||
package( | ||
default_visibility = ["//visibility:public"], | ||
) | ||
|
||
bool_flag( | ||
name = "enable", | ||
build_setting_default = True, | ||
) | ||
|
||
config_setting( | ||
name = "enabled", | ||
flag_values = {"@io_bazel_rules_docker//transitions:enable": "true"}, | ||
) | ||
|
||
config_setting( | ||
name = "disabled", | ||
flag_values = {"@io_bazel_rules_docker//transitions:enable": "false"}, | ||
) |