Skip to content

Commit

Permalink
Allow passing a config file to bptestrunner (#340)
Browse files Browse the repository at this point in the history
Also, bump rules_apple and subpar to latest versions
  • Loading branch information
ob authored Jun 27, 2019
1 parent e8c8099 commit 7bd3f64
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 19 deletions.
16 changes: 9 additions & 7 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

git_repository(

http_archive(
name = "build_bazel_rules_apple",
remote = "https://github.com/bazelbuild/rules_apple.git",
tag = "0.15.0",
url = "https://github.com/bazelbuild/rules_apple/releases/download/0.17.2/rules_apple.0.17.2.tar.gz",
sha256 = "6efdde60c91724a2be7f89b0c0a64f01138a45e63ba5add2dca2645d981d23a1",
)

load(
Expand All @@ -28,8 +29,9 @@ load(
apple_support_dependencies()

# For packaging python scripts.
git_repository(
http_archive(
name = "subpar",
remote = "https://github.com/google/subpar",
commit = "edb8409041b521959b6ed4b7412c0eec59d4af78",
url = "https://github.com/google/subpar/archive/2.0.0.zip",
sha256 = "8876244a984d75f28b1c64d711b6e5dfab5f992a3b741480e63cfc5e26acba93",
strip_prefix = "subpar-2.0.0",
)
18 changes: 13 additions & 5 deletions bptestrunner/bluepill_test_runner.template.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,33 @@ TMP_DIR="$(mktemp -d "${TMPDIR:-/tmp}/test_runner_work_dir.XXXXXX")"
TEST_BUNDLE_PATH="%(test_bundle_path)s"

if [[ "$TEST_BUNDLE_PATH" == *.xctest ]]; then
runner_flags+=("--test_bundle_path=${TEST_BUNDLE_PATH}")
runner_flags+=("--Xbp=--test_bundle_path=${TEST_BUNDLE_PATH}")
else
TEST_BUNDLE_NAME=$(basename_without_extension "${TEST_BUNDLE_PATH}")
TEST_BUNDLE_TMP_DIR="${TMP_DIR}/${TEST_BUNDLE_NAME}"
unzip -qq -d "${TEST_BUNDLE_TMP_DIR}" "${TEST_BUNDLE_PATH}"
runner_flags+=("--test-bundle-path=${TEST_BUNDLE_TMP_DIR}/${TEST_BUNDLE_NAME}.xctest")
runner_flags+=("--Xbp=--test-bundle-path=${TEST_BUNDLE_TMP_DIR}/${TEST_BUNDLE_NAME}.xctest")
fi

TEST_HOST_PATH="%(test_host_path)s"

if [[ -n "$TEST_HOST_PATH" ]]; then
if [[ "$TEST_HOST_PATH" == *.app ]]; then
runner_flags+=("--app_under_test_path=$TEST_HOST_PATH")
runner_flags+=("--Xbp=--app=$TEST_HOST_PATH")
else
TEST_HOST_NAME=$(basename_without_extension "${TEST_HOST_PATH}")
TEST_HOST_TMP_DIR="${TMP_DIR}/${TEST_HOST_NAME}"
unzip -qq -d "${TEST_HOST_TMP_DIR}" "${TEST_HOST_PATH}"
runner_flags+=("--app=${TEST_HOST_TMP_DIR}/Payload/${TEST_HOST_NAME}.app")
runner_flags+=("--Xbp=--app=${TEST_HOST_TMP_DIR}/Payload/${TEST_HOST_NAME}.app")
fi
fi

ATTR_CONFIG_FILE="%(config_file)s"

if [[ -n "$ATTR_CONFIG_FILE" ]]; then
runner_flags+=("--attr_config_file=$ATTR_CONFIG_FILE")
fi

# Constructs the json string to configure the test env and tests to run.
# It will be written into a temp json file which is passed to the test runner
# flags --config.
Expand Down Expand Up @@ -71,9 +77,11 @@ if [[ -n "${CONFIG_FILE_JSON_STR}" ]]; then
CONFIG_FILE_JSON_STR="{${CONFIG_FILE_JSON_STR}}"
CONFIG_FILE_JSON_PATH="${TMP_DIR}/config.json"
echo "${CONFIG_FILE_JSON_STR}" > "${CONFIG_FILE_JSON_PATH}"
runner_flags+=("-c" "${CONFIG_FILE_JSON_PATH}")
runner_flags+=("--rule_config_file" "${CONFIG_FILE_JSON_PATH}")
fi

runner_flags+=("-v")

cmd=("%(testrunner_binary)s"
"${runner_flags[@]}"
"$@")
Expand Down
18 changes: 17 additions & 1 deletion bptestrunner/ios_bluepill_test_runner.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ def _get_template_substitutions(ctx):
"num_sims": str(ctx.attr.num_sims),
"testrunner_binary": ctx.executable._testrunner.short_path,
}
if ctx.attr.config_file:
# we get a Target in config_file
config_file = ctx.attr.config_file.files.to_list()[0].path
else:
config_file = ""
subs["config_file"] = config_file
return {"%(" + k + ")s": subs[k] for k in subs}

def _get_execution_environment(ctx):
Expand All @@ -33,6 +39,9 @@ def _ios_bluepill_test_runner_impl(ctx):
output = ctx.outputs.test_runner_template,
substitutions = _get_template_substitutions(ctx),
)
runfiles = [ctx.file._testrunner]
if ctx.attr.config_file:
runfiles += ctx.attr.config_file.files.to_list()
return [
AppleTestRunnerInfo(
test_runner_template = ctx.outputs.test_runner_template,
Expand All @@ -41,7 +50,7 @@ def _ios_bluepill_test_runner_impl(ctx):
),
DefaultInfo(
runfiles = ctx.runfiles(
files = [ctx.file._testrunner],
files = runfiles,
),
),
]
Expand Down Expand Up @@ -79,6 +88,13 @@ Spawn simulator by clone from simulator template.
Number of simulators to run in parallel.
""",
),
"config_file": attr.label(
doc = """
A configuration file that will be passed to bluepill. Rule attributes
take precedence over conflicting values in the config file.
""",
allow_single_file = True,
),
"execution_requirements": attr.string_dict(
allow_empty = False,
default = {"requires-darwin": ""},
Expand Down
50 changes: 44 additions & 6 deletions bptestrunner/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
# WITHOUT WARRANTIES OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
from __future__ import print_function

import argparse
import glob
import json
import logging
import os
import pkg_resources
Expand All @@ -25,21 +27,35 @@
def main():
"""Parse the arguments in an xctestrunner-compatible way
"""
args = sys.argv[1:]
if '-v' in args:
parser = argparse.ArgumentParser()
# attr_config_file is a config.json that comes from the rule's attributes (e.g. num_sims)
parser.add_argument('--attr_config_file')
# rule_config_file is the config.json that comes from the 'config_file' rule attribute
parser.add_argument('--rule_config_file')
parser.add_argument('--Xbp', nargs=1, action='append')
parser.add_argument('-v', '--verbose', action='store_true')
args = parser.parse_args()
if args.verbose:
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(message)s')
else:
logging.basicConfig(format='%(asctime)s %(message)s')

logging.debug("PWD: '%s'", os.getcwd())
# flatten Xbp args
bpargs = [a for sl in args.Xbp for a in sl]
# Add xcode path to CLI
args += ['--xcode-path', find_xcode_path()]
bpargs += ['--xcode-path', find_xcode_path()]
output_dir = os.environ['TEST_UNDECLARED_OUTPUTS_DIR']
args += ['--output-dir', output_dir]
bpargs += ['--output-dir', output_dir]
config_file = merge_config_files(args.attr_config_file,
args.rule_config_file)
bpargs += ['-c', config_file]

logging.debug("Running: bluepill %s", ' '.join(args))
rc = run('bluepill', args)
logging.debug("Running: bluepill %s", ' '.join(bpargs))
rc = run('bluepill', bpargs)
if not args.verbose:
os.remove(config_file)
pattern = os.path.join(output_dir, '*.xml')
xml_files = glob.glob(pattern)
final_xml_output = None
Expand All @@ -53,6 +69,28 @@ def main():
sys.exit(rc)


def merge_config_files(config1, config2):
"""Merge two config files. Keys in config2 trump keys in config1,
returns the path of the new merged file.
"""
logging.debug("Merging '{}' '{}'".format(config1, config2))
cfg1 = {}
if config1:
with open(config1) as f:
cfg1 = json.load(f)
cfg2 = {}
if config2:
with open(config2) as f:
cfg2 = json.load(f)
merged_cfg = {key: value for (key, value) in (cfg2.items() + cfg1.items())}
f = tempfile.NamedTemporaryFile(delete=False)
json.dump(merged_cfg, f)
f.close()
logging.debug("merged cfg file: {}".format(f.name))
logging.debug("{}".format(merged_cfg))
return f.name


def find_xcode_path():
"""Return the path to Xcode's Developer directory.
"""
Expand Down

0 comments on commit 7bd3f64

Please sign in to comment.