Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion test/build_defs/test.build_defs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ def please_repo_e2e_test(
name: str,
plz_command: str,
repo: str,
defer_cmd: list = [],
data: dict={},
deps: list=[],
tools: dict={},
Expand All @@ -12,7 +13,6 @@ def please_repo_e2e_test(
expect_output_doesnt_contain: dict = {},
labels: list = [],
):
plz_command = plz_command.replace("plz ", "$TOOLS_PLEASE ")
if expected_failure:
plz_command += "; [ ! $? -eq 0 ]"

Expand All @@ -31,7 +31,11 @@ def please_repo_e2e_test(
if expect_output_doesnt_contain:
test_cmd += [f'_STR="$(cat {o})" _SUBSTR="{c}" && if [ -z "${_STR##*$_SUBSTR*}" ]; then echo "$_STR"; exit 1; fi' for o, c in expect_output_doesnt_contain.items()]

# defer commands should be added last
test_cmd += defer_cmd

test_cmd = ' && '.join(test_cmd)
test_cmd = test_cmd.replace("plz ", "$TOOLS_PLEASE ")

data["REPO"] = [repo]
data["BASE_CONFIG"] = ["//test/build_defs:base_config"]
Expand Down
121 changes: 120 additions & 1 deletion test/export/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,124 @@
subinclude("//test/build_defs")
filegroup(
name = "export_e2e_test_build_def",
srcs = ["please_export_e2e_test.build_defs"],
)

subinclude(":export_e2e_test_build_def")

# Export a target generated by a native genrule target and trim unused
# build statements.
please_export_e2e_test(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please could you move each of these please_export_e2e_test targets to a BUILD file alongside the test/expected repos?

name = "export_native_genrule",
dir = "test_builtins",
export_targets = ["//:native_genrule"],
)

# Export a target generated by a custom build def.
please_export_e2e_test(
name = "export_simple_custom_target",
dir = "test_custom_def",
export_targets = ["//:simple_custom_target"],
)

# Export a target generated by a custom build def defined in the same
# BUILD file.
please_export_e2e_test(
name = "export_custom_target_in_file",
dir = "test_custom_in_file_def",
export_targets = ["//:simple_custom_target"],
)

# Export a target generated by a custom build def.
please_export_e2e_test(
name = "export_standard_children_custom_target",
cmd_on_export = [
# Child of build def
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's avoid referring to children, as that's not well-defined terminology. adjacent_targets_in_build_def is probably better naming for this case, and the fact that one is a "child" of the other doesn't make a difference.

What you're essentially capturing here is that a target A created in a build def alongside the target B that we're trying to export will result in both A and B being buildable

"build //:standard_children_custom_target#child",
],
dir = "test_custom_def_children",
export_targets = ["//:standard_children_custom_target"],
)

# Export a target that depends on another target.
please_export_e2e_test(
name = "export_deps",
dir = "test_deps",
export_targets = ["//:dep2"],
)

# Test multiple targets.
please_export_e2e_test(
name = "export_multiple_targets",
dir = "test_multiple_targets",
export_targets = [
"//:target1",
"//:target2",
],
)

# Export a target from a repo that preloads a build def.
# This test purposely doesn't use the custom def but checks that the source files are still included.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# This test purposely doesn't use the custom def but checks that the source files are still included.
# The exported target doesn't use the preloaded build def, but the preloaded build def is still present in the export.

please_export_e2e_test(
name = "export_preload_genrule",
dir = "test_preload",
export_targets = ["//:native_target"],
)

# Export a custom target from a repo that preloads the build def used.
please_export_e2e_test(
name = "export_preload_use_preloaded_def",
dir = "test_use_preloaded",
export_targets = ["//:use_preloaded_def"],
)

# # Test go binary target.
please_export_e2e_test(
name = "export_go_binary",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than the target being exported moving to a subdirectory, I don't see any behavioural difference between this and export_go_target_with_go_dep

dir = "test_go_binary",
export_targets = ["//pkg:go_bin"],
include_go = True,
)

# Test native target with go third_party dependency.
please_export_e2e_test(
name = "export_native_target_with_go_dep",
dir = "test_native_target_with_go_dep",
export_targets = ["//:genrule_go_dep"],
include_go = True,
)

# Test go target with go third_party dependency.
please_export_e2e_test(
name = "export_go_target_with_go_dep",
dir = "test_go_bin_with_go_dep",
export_targets = ["//:bin_go_dep"],
include_go = True,
)

# Test outputs export.
please_repo_e2e_test(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you not use please_export_e2e_test here? I think both would be clearer as golden-master tests

name = "export_outputs",
defer_cmd = [
'test -f "plz-out/plzexport/some_output.txt"',
'test ! -f "plz-out/plzexport/BUILD_FILE"',
],
plz_command = "plz export outputs --output plz-out/plzexport //:out_target",
repo = "test_outputs",
)

# Test --notrim flag.
please_repo_e2e_test(
name = "export_notrim",
defer_cmd = [
'test -f "plz-out/plzexport/file2.txt"',
'test -f "plz-out/plzexport/BUILD_FILE"',
'grep -q \'name = "unrelated"\' "plz-out/plzexport/BUILD_FILE"',
],
plz_command = "plz export --notrim --output plz-out/plzexport //:target1",
repo = "test_notrim",
)

# Generic catch-all test on internal repo.
plz_e2e_test(
name = "export_src_please_test",
cmd = "plz export --output plz-out/plzexport //src/core && plz --repo_root=$(plz query reporoot)/plz-out/plzexport build //src/core",
Expand Down
66 changes: 66 additions & 0 deletions test/export/please_export_e2e_test.build_defs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
subinclude("//test/build_defs:build_defs")

def please_export_e2e_test(
name:str,
dir:str,
export_targets:list,
cmd_on_export:list=[],
include_go:bool=False):
"""
Runs an export e2e test. To use it, create a directory with both a `test_repo` with the targets
to be exported and an `expected_repo` with the expected resulting files.
The validation steps for the e2e test are:
* It performs a repo-wide diff between the exported files and the `expected_repo`
* Builds the exported target to ensure the exported repo, and consequently the
`expected_repo`, include valid sources and dependencies to build the target.
* Optionally you can specify additional commands to run on the exported directory by
passing them to `cmd_on_export`.

Args:
name (str): Name of the test.
dir (str): A relative path to the directory containing `test_repo` and `expected_repo`.
export_targets (list): Targets to export as part of the test.
cmd_on_export (list): Optional. Additional commands to run on the exported directory.
include_go (bool): Optional. A flag to include Go dependencies in the e2e test. Use when
testing repos that include Go targets.
"""
EXPORT_DIR = "plz-out/plzexport"
test_repo = f"{dir}/test_repo"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can we call this source_repo rather than test_repo throughout please

expected_repo = f"{dir}/expected_repo"
exported_repo = f"$DATA_TEST_REPO/{EXPORT_DIR}"
data = {}
tools = {}

targets = " ".join(export_targets)

config_override = ""
if include_go:
config_override += "-o plugin.go.gotool:$TOOLS_GO"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't we just put this in the .plzconfig in the source repo?

tools["GO"] = [CONFIG.GO.GO_TOOL]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this necessary at all?


test_cmd = [
# Export
f'plz {config_override} --repo_root="$DATA_TEST_REPO" export --output "{EXPORT_DIR}" {targets}',
# Golden-Master validation with expected repo. Done before any building to avoid plz-out
f'diff -r "{exported_repo}" "$DATA_EXPECTED_REPO"',
# Tests building the exported target which in turn ensures the sources are included
f'plz {config_override} --repo_root="{exported_repo}" build {targets}',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense for this to just be plz build //...? Each repo should be tiny, so building everything shouldn't be a problem, and it asserts our requirement that the entire export should be buildable?

] + [f'plz {config_override} --repo_root="{exported_repo}" {cmd}' for cmd in cmd_on_export]

test_cmd = [cmd.replace("plz ", "$TOOLS_PLEASE ") for cmd in test_cmd]
test_cmd = " && ".join(test_cmd)

data["TEST_REPO"] = [test_repo]
data["EXPECTED_REPO"] = [expected_repo]

tools["PLEASE"] = ["//package:installed_files|please"]

return gentest(
name = name,
data = data,
labels = ["plz_e2e_test", "e2e"],
no_test_output = True,
sandbox = False,
test_cmd = test_cmd,
test_tools = tools,
)
3 changes: 3 additions & 0 deletions test/export/test_builtins/expected_repo/.plzconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[Parse]

BuildFileName = BUILD_FILE
6 changes: 6 additions & 0 deletions test/export/test_builtins/expected_repo/BUILD_FILE
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
genrule(
name = "native_genrule",
srcs = ["file.txt"],
outs = ["file.wordcount"],
cmd = "wc $SRCS > $OUT",
)
1 change: 1 addition & 0 deletions test/export/test_builtins/expected_repo/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test source file
3 changes: 3 additions & 0 deletions test/export/test_builtins/test_repo/.plzconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[Parse]

BuildFileName = BUILD_FILE
13 changes: 13 additions & 0 deletions test/export/test_builtins/test_repo/BUILD_FILE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
genrule(
name = "native_genrule",
srcs = ["file.txt"],
outs = ["file.wordcount"],
cmd = "wc $SRCS > $OUT",
)

genrule(
name = "dummy_target_to_be_trimmed",
srcs = ["dummy.txt"],
outs = ["dummy"],
cmd = "cat $SRCS > $OUT",
)
Empty file.
1 change: 1 addition & 0 deletions test/export/test_builtins/test_repo/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test source file
3 changes: 3 additions & 0 deletions test/export/test_custom_def/expected_repo/.plzconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[Parse]

BuildFileName = BUILD_FILE
7 changes: 7 additions & 0 deletions test/export/test_custom_def/expected_repo/BUILD_FILE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
subinclude("//build_defs:simple_build_def")

simple_custom_target(
name = "simple_custom_target",
srcs = ["file.txt"],
outs = ["file_simple.out"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
filegroup(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either this should contain an entry for dummy.build_defs, or dummy.build_defs shouldn't exist

name = "simple_build_def",
srcs = ["simple.build_defs"],
visibility = ["PUBLIC"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def simple_custom_target(
name:str,
srcs:list=[],
outs:list=[]):
return genrule(
name = name,
srcs = srcs,
outs = outs,
cmd = "cat $SRCS > $OUT",
)
1 change: 1 addition & 0 deletions test/export/test_custom_def/expected_repo/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test source file
3 changes: 3 additions & 0 deletions test/export/test_custom_def/test_repo/.plzconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[Parse]

BuildFileName = BUILD_FILE
7 changes: 7 additions & 0 deletions test/export/test_custom_def/test_repo/BUILD_FILE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
subinclude("//build_defs:simple_build_def")

simple_custom_target(
name = "simple_custom_target",
srcs = ["file.txt"],
outs = ["file_simple.out"],
)
5 changes: 5 additions & 0 deletions test/export/test_custom_def/test_repo/build_defs/BUILD_FILE
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
filegroup(
name = "simple_build_def",
srcs = ["simple.build_defs"],
visibility = ["PUBLIC"],
)
10 changes: 10 additions & 0 deletions test/export/test_custom_def/test_repo/build_defs/dummy.build_defs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def dummy_target(
name:str,
srcs:list=[],
outs:list=[]):
return genrule(
name = name,
srcs = srcs,
outs = outs,
cmd = "echo dummy > $OUT",
)
10 changes: 10 additions & 0 deletions test/export/test_custom_def/test_repo/build_defs/simple.build_defs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def simple_custom_target(
name:str,
srcs:list=[],
outs:list=[]):
return genrule(
name = name,
srcs = srcs,
outs = outs,
cmd = "cat $SRCS > $OUT",
)
1 change: 1 addition & 0 deletions test/export/test_custom_def/test_repo/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test source file
3 changes: 3 additions & 0 deletions test/export/test_custom_def_children/expected_repo/.plzconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[Parse]

BuildFileName = BUILD_FILE
8 changes: 8 additions & 0 deletions test/export/test_custom_def_children/expected_repo/BUILD_FILE
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
subinclude("//build_defs:standard_children_build_def")

standard_children_custom_target(
name = "standard_children_custom_target",
srcs = ["file.txt"],
outs = ["file_standard.out"],
outs_child = ["file_child.out"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
filegroup(
name = "standard_children_build_def",
srcs = ["standard_children.build_defs"],
visibility = ["PUBLIC"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def standard_children_custom_target(
name:str,
srcs:list=[],
outs:list=[],
outs_child:list=[]):
genrule(
name = f"{name}#child",
srcs = srcs,
outs = outs_child,
cmd = "echo 'child' > $OUT && cat $SRCS >> $OUT ",
)
return genrule(
name = name,
srcs = srcs,
outs = outs,
cmd = "echo 'main' > $OUT && cat $SRCS >> $OUT ",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test source file
3 changes: 3 additions & 0 deletions test/export/test_custom_def_children/test_repo/.plzconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[Parse]

BuildFileName = BUILD_FILE
8 changes: 8 additions & 0 deletions test/export/test_custom_def_children/test_repo/BUILD_FILE
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
subinclude("//build_defs:standard_children_build_def")

standard_children_custom_target(
name = "standard_children_custom_target",
srcs = ["file.txt"],
outs = ["file_standard.out"],
outs_child = ["file_child.out"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
filegroup(
name = "standard_children_build_def",
srcs = ["standard_children.build_defs"],
visibility = ["PUBLIC"],
)
Loading