Skip to content

Commit

Permalink
Updating .NET test targets for use with Bazel
Browse files Browse the repository at this point in the history
  • Loading branch information
jimevans committed Jul 1, 2019
1 parent 23eab6a commit 41730f6
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 5 deletions.
143 changes: 143 additions & 0 deletions dotnet/nunit-test.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
load(
"@io_bazel_rules_dotnet//dotnet/private:context.bzl",
"dotnet_context",
)
load(
"@io_bazel_rules_dotnet//dotnet/private:providers.bzl",
"DotnetLibrary",
"DotnetResource",
)

TEST_RUNNER_SCRIPT_CONTENT = """{script_prefix}
{runner} {test} --result={result_location};transform={xslt} {additional_args}
"""

def _is_windows():
return select({
"@bazel_tools//src/conditions:windows": True,
"//conditions:default": False,
})

def _convert_path(input_path, is_windows = False):
if (is_windows):
return input_path.replace("/", "\\")
return input_path

def _nunit_test_impl(ctx):
dotnet = dotnet_context(ctx)
name = ctx.label.name

test_assembly = dotnet.assembly(
dotnet,
name = ctx.label.name,
srcs = ctx.attr.srcs,
deps = ctx.attr.deps,
resources = ctx.attr.resources,
out = ctx.attr.out,
defines = ctx.attr.defines,
unsafe = ctx.attr.unsafe,
data = ctx.attr.data,
executable = False,
keyfile = ctx.attr.keyfile,
)

args = [test_assembly.result.path] + ctx.attr.args

file_inputs = [test_assembly.result]
for dep in ctx.attr.deps:
src_file = dep.files.to_list()[0]
dest_file = ctx.actions.declare_file(src_file.basename)
file_inputs.append(dest_file)
ctx.actions.run(
outputs = [dest_file],
inputs = [src_file],
executable = ctx.attr._copy.files.to_list()[0],
arguments = [dest_file.path, src_file.path],
mnemonic = "CopyDependencyAssembly",
)

runner_executable = None
for runner_file in ctx.attr.test_runner.files.to_list():
dest_runner_file = ctx.actions.declare_file("runner/" + runner_file.basename)
file_inputs.append(dest_runner_file)
if runner_file.basename == "nunit3-console.exe":
runner_executable = dest_runner_file

ctx.actions.run(
outputs = [dest_runner_file],
inputs = [runner_file],
executable = ctx.attr._copy.files.to_list()[0],
arguments = [dest_runner_file.path, runner_file.path],
mnemonic = "CopyTestRunner",
)

# Determining platform isn't available during the analysis phase,
# so there's no opportunity to give the script file a proper extention.
# Luckily, as long as the file is marked with the executable attribute
# in the OS, there's nothing preventing a file named '.bat' being a
# valid executable shell script on non-Windows OSes. If this changes,
# this comment can be removed, and the below line changed to give the
# generated script file a proper extension of '.sh'.
script_file_extension = "bat"
additional_args = "$@"
result_location = "$XML_OUTPUT_FILE"
script_prefix = "#!/bin/bash"
is_windows = _is_windows()
if (is_windows):
script_file_extension = "bat"
additional_args = "%*"
result_location = "%XML_OUTPUT_FILE%"
script_prefix = "@echo off"

script_file_name = "{}.{}".format(name, script_file_extension)
script_file = ctx.actions.declare_file(script_file_name)
script_content = TEST_RUNNER_SCRIPT_CONTENT.format(
script_prefix = script_prefix,
runner = _convert_path(runner_executable.path, is_windows),
test = _convert_path(test_assembly.result.path, is_windows),
result_location = result_location,
xslt = _convert_path(ctx.attr._xslt.files.to_list()[0].path, is_windows),
additional_args = additional_args,
)

ctx.actions.write(script_file, script_content, True)

extra = [] if ctx.attr.data == None else [d.files for d in ctx.attr.data]
runfiles = ctx.runfiles(
files = file_inputs,
transitive_files = depset(transitive = [d[DotnetLibrary].runfiles for d in ctx.attr.deps] + extra),
)

info = DefaultInfo(
files = depset([test_assembly.result, runner_executable, script_file]),
runfiles = runfiles,
executable = script_file,
)

return [
info,
test_assembly,
]

nunit_test = rule(
implementation = _nunit_test_impl,
attrs = {
"deps": attr.label_list(providers = [DotnetLibrary]),
"resources": attr.label_list(providers = [DotnetResource]),
"srcs": attr.label_list(allow_files = [".cs"]),
"out": attr.string(),
"defines": attr.string_list(),
"unsafe": attr.bool(default = False),
"keyfile": attr.label(allow_files = True),
"data": attr.label_list(allow_files = True),
"dotnet_context_data": attr.label(default = Label("@io_bazel_rules_dotnet//:dotnet_context_data")),
"test_runner": attr.label(
default = Label("//third_party/dotnet/nunit.console-3.10.0/bin/net35:nunitconsole"),
allow_files = True
),
"_copy": attr.label(default = Label("@io_bazel_rules_dotnet//dotnet/tools/copy")),
"_xslt": attr.label(default = Label("@io_bazel_rules_dotnet//tools/converttests:n3.xslt"), allow_files = True),
},
toolchains = ["@io_bazel_rules_dotnet//dotnet:toolchain_net"],
test = True,
)
11 changes: 6 additions & 5 deletions dotnet/test/common/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@io_bazel_rules_dotnet//dotnet:defs.bzl", "net_nunit3_test")
load("//dotnet:nunit-test.bzl", "nunit_test")

SUPPORTED_BROWSERS = [
("chrome", "Chrome"),
Expand All @@ -12,7 +12,7 @@ SUPPORTED_BROWSERS = [
("safaritechpreview", "SafariTechPreview"),
]

[net_nunit3_test(
[nunit_test(
name = "{}".format(target_name),
size = "enormous",
srcs = glob([
Expand All @@ -33,21 +33,22 @@ SUPPORTED_BROWSERS = [
data = [
"appconfig.json",
"//common/src/web",
"//java/client/test/org/openqa/selenium/environment:appserver",
"//java/client/test/org/openqa/selenium/environment:appserver_deploy.jar",
],
dotnet_context_data = "@io_bazel_rules_dotnet//:net_context_data_net47",
visibility = ["//visibility:public"],
deps = [
"//dotnet/src/webdriver:net47assembly",
"//dotnet/src/webdriver:net47",
"@benderproxy//:net47",
"@castle.core//:net45",
"@io_bazel_rules_dotnet//dotnet/stdlib.net:system.dll",
"@io_bazel_rules_dotnet//dotnet/stdlib.net:system.core.dll",
"@io_bazel_rules_dotnet//dotnet/stdlib.net:system.data.dll",
"@io_bazel_rules_dotnet//dotnet/stdlib.net:system.dll",
"@io_bazel_rules_dotnet//dotnet/stdlib.net:system.drawing.dll",
"@io_bazel_rules_dotnet//dotnet/stdlib.net:system.xml.dll",
"@json.net//:net45",
"@moq//:net45",
"@nunit//:net45",
"@system.threading.tasks.extensions//:net45",
],
) for (target_name, config_setting) in SUPPORTED_BROWSERS]
40 changes: 40 additions & 0 deletions dotnet/test/support/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
load("//dotnet:nunit-test.bzl", "nunit_test")

nunit_test(
name = "support",
size = "enormous",
srcs = glob([
"*.cs",
"Events/*.cs",
"Extensions/*.cs",
"UI/*.cs",
]),
out = "WebDriver.Support.Tests.dll",
args = [
"--agents=1",
"--params=ConfigFile=$(location appconfig.json)",
"--params=ActiveWebsiteConfig=HostsFileRedirect",
"--params=ActiveDriverConfig={}".format(config_setting),
],
data = [
"appconfig.json",
"//common/src/web",
"//java/client/test/org/openqa/selenium/environment:appserver_deploy.jar",
],
dotnet_context_data = "@io_bazel_rules_dotnet//:net_context_data_net47",
visibility = ["//visibility:public"],
deps = [
"//dotnet/src/webdriver:net47",
"//dotnet/src/support:net47",
"@io_bazel_rules_dotnet//dotnet/stdlib.net:system.dll",
"@io_bazel_rules_dotnet//dotnet/stdlib.net:system.core.dll",
"@io_bazel_rules_dotnet//dotnet/stdlib.net:system.data.dll",
"@io_bazel_rules_dotnet//dotnet/stdlib.net:system.drawing.dll",
"@io_bazel_rules_dotnet//dotnet/stdlib.net:system.xml.dll",
"@json.net//:net45",
"@moq//:net45",
"@castle.core//:net45",
"@system.threading.tasks.extensions//:net45",
"@nunit//:net45",
],
)

0 comments on commit 41730f6

Please sign in to comment.