Skip to content

Commit

Permalink
Simplify symlink helper
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Jul 28, 2022
1 parent d16cb7f commit d6e4aaf
Showing 1 changed file with 30 additions and 40 deletions.
70 changes: 30 additions & 40 deletions src/test/shell/bazel/bazel_symlink_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,25 @@ EOF

}

function add_symlink_helper() {
# We use python rather than a simple ln since the latter doesn't handle dangling symlinks on
# Windows.
mkdir -p tools

This comment has been minimized.

Copy link
@alexjski

alexjski Jul 28, 2022

Contributor

I would recommend a more unique name like symlink_helper or tools/symlink_helper directory since tools could clash with things. You can also just always create that since that is an immutable tool from the test case standpoint.

cat > tools/BUILD <<EOF
py_binary(
name = "link",
main = "link.py",
visibility = ["//visibility:public"],
)
EOF

cat > tools/link.py <<EOF
import os
import sys
os.symlink(*sys.argv[1:])
EOF
}

function test_smoke() {
mkdir -p a
cat > a/BUILD <<EOF
Expand Down Expand Up @@ -227,18 +246,9 @@ EOF
}

function test_file_instead_of_symlink() {
mkdir -p a
cat > a/MakeSymlink.java <<'EOF'
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class MakeSymlink {
public static void main(String[] args) throws IOException {
Files.createSymbolicLink(Paths.get(args[0]), Paths.get(args[1]));
}
}
EOF
add_symlink_helper

mkdir -p a
cat > a/a.bzl <<'EOF'
def _bad_symlink_impl(ctx):
symlink = ctx.actions.declare_symlink(ctx.label.name)
Expand Down Expand Up @@ -266,12 +276,6 @@ EOF
cat > a/BUILD <<'EOF'
load(":a.bzl", "bad_symlink", "bad_write")
java_binary(
name = "MakeSymlink",
srcs = ["MakeSymlink.java"],
main_class = "MakeSymlink",
)
bad_symlink(name="bs", link_target="bad/symlink")
genrule(name="bsg", srcs=[":bs"], outs=["bsgo"], cmd="echo BSGO > $@")
Expand All @@ -282,8 +286,8 @@ genrule(
name="bg",
srcs=[],
outs=["bgo"],
exec_tools = [":MakeSymlink"],
cmd = "$(execpath :MakeSymlink) $@ bad/symlink",
cmd = "$(location //tools:link) bad/symlink $@",
tools = ["//tools:link"],
)
EOF

Expand Down Expand Up @@ -328,26 +332,17 @@ EOF
}

function test_symlink_created_from_spawn() {
mkdir -p a
cat > a/MakeSymlink.java <<'EOF'
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class MakeSymlink {
public static void main(String[] args) throws IOException {
Files.createSymbolicLink(Paths.get(args[0]), Paths.get(args[1]));
}
}
EOF
add_symlink_helper

mkdir -p a
cat > a/a.bzl <<'EOF'
def _a_impl(ctx):
symlink = ctx.actions.declare_symlink(ctx.label.name + ".link")
output = ctx.actions.declare_file(ctx.label.name + ".file")
ctx.actions.run(
outputs = [symlink],
executable = ctx.executable._make_symlink,
arguments = [symlink.path, ctx.attr.link_target],
executable = ctx.executable._link,
arguments = [ctx.attr.link_target, symlink.path],
inputs = depset([]),
)
ctx.actions.run_shell(
Expand All @@ -361,23 +356,18 @@ a = rule(
implementation = _a_impl,
attrs = {
"link_target": attr.string(),
"_make_symlink": attr.label(
default = ":MakeSymlink",
"_link": attr.label(
default = "//tools:link",
executable = True,
cfg = "exec",
)
),
}
)
EOF

cat > a/BUILD <<'EOF'
load(":a.bzl", "a")
java_binary(
name = "MakeSymlink",
srcs = ["MakeSymlink.java"],
main_class = "MakeSymlink",
)
a(name="a", link_target="somewhere/over/the/rainbow")
EOF

Expand Down

0 comments on commit d6e4aaf

Please sign in to comment.