fork/rules_rust

git clone https://git.julia.blue/fork/rules_rust

UebelAndreAvoid uses of shell to improve Windows tests (#2912)16cd457b

main
1.5 KiB59 linesraw
1"""Tests for passing configuration to cargo_dep_env rules"""
2
3_SH_WRITER = """\
4#!/usr/bin/env bash
5set -euo pipefail
6echo content > $@
7"""
8
9_BAT_WRITER = """\
10@ECHO OFF
11echo content > %*
12"""
13
14def _create_dep_dir_impl(ctx):
15    is_windows = ctx.attr.platform.values()[0] == "windows"
16    writer = ctx.actions.declare_file("{}.writer.{}".format(
17        ctx.label.name,
18        ".bat" if is_windows else ".sh",
19    ))
20    ctx.actions.write(
21        output = writer,
22        content = _BAT_WRITER if is_windows else _SH_WRITER,
23        is_executable = True,
24    )
25
26    out = ctx.actions.declare_directory("{}.dep_dir".format(ctx.label.name))
27    ctx.actions.run(
28        executable = writer,
29        outputs = [out],
30        arguments = [
31            "{}\\a_file".format(out.path) if is_windows else "{}/a_file".format(out.path),
32        ],
33    )
34
35    return [DefaultInfo(files = depset(direct = [out]))]
36
37_create_dep_dir = rule(
38    implementation = _create_dep_dir_impl,
39    attrs = {
40        "platform": attr.label_keyed_string_dict(
41            doc = "The name of the exec platform",
42            cfg = "exec",
43            mandatory = True,
44        ),
45    },
46)
47
48def create_dep_dir(name, **kwargs):
49    native.filegroup(
50        name = "{}_".format(name),
51    )
52    _create_dep_dir(
53        name = name,
54        platform = select({
55            "@platforms//os:windows": {":{}_".format(name): "windows"},
56            "//conditions:default": {":{}_".format(name): "unix"},
57        }),
58        **kwargs
59    )