From 4a75b2d2adcc33ff13ea8ecabdcc662ec5dffdd4 Mon Sep 17 00:00:00 2001 From: Philip Patsch Date: Wed, 24 Oct 2018 14:55:01 +0200 Subject: [PATCH] Improve documentation of `build_file` and add tests Adds some user documentation on how to generate bazel targets for the repositories generated by `nixpkgs_package`. Also adds two tests to check whether this actually works as documented. --- README.md | 44 +++++++++++++++++++++++++------ WORKSPACE | 19 ++++++++++++++ tests/BUILD | 62 +++++++++++++++++++++++++++++++++----------- tests/output.nix | 13 ++++++++++ tests/test_output.sh | 15 +++++++++++ 5 files changed, 130 insertions(+), 23 deletions(-) create mode 100644 tests/output.nix create mode 100755 tests/test_output.sh diff --git a/README.md b/README.md index dbf334b47..cda93b09e 100644 --- a/README.md +++ b/README.md @@ -168,9 +168,8 @@ nixpkgs clone in `nix_file` or `nix_file_content`.

Label-keyed String dict; optional

A dictionary mapping repositoriy labels to `NIX_PATH` entries.

-

Setting it to

-repositories = { "myrepo" : "//:myrepo" }
-
+

Setting it to +

repositories = { "myrepo" : "//:myrepo" }
for example would replace all instances of <myrepo> in the called nix code by the path to the target "//:myrepo". See the @@ -182,17 +181,46 @@ repositories = { "myrepo" : "//:myrepo" } build_file -

String; optional

-

The file to use as the BUILD file for this repository. This - attribute is a label relative to the main workspace. The - file does not need to be named BUILD, but can be.

+

Label; optional

+

The file to use as the BUILD file for this repository. + Its contents are copied copied into the file + BUILD in root of the nix output folder. + The Label does not need to be named BUILD, but can be. +

+

For common use cases we provide filegroups that expose + certain files as targets: +

+
:bin
+
Everything in the bin/ directory.
+
:lib
+
All .so and .a files + that can be found in subdirectories of + lib/.
+
:include
+
All .h files + that can be found in subdirectories of + bin/.
+
+

+

If you need different files from the nix package, + you can reference them like this:

package(default_visibility = [ "//visibility:public" ])
+filegroup(
+  name = "our-docs",
+  srcs = glob(["share/doc/ourpackage/**/*"]),
+)
+ See the bazel documentation of + filegroup + and + glob. +

build_file_content

String; optional

-

The content for the BUILD file for this repository.

+

Like build_file, but a string of the contents + instead of a file name.

diff --git a/WORKSPACE b/WORKSPACE index afa34586a..4a19e8f23 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -55,3 +55,22 @@ nixpkgs_package( nix_file_deps = ["//tests:pkgname.nix"], repositories = { "nixpkgs": "//:nixpkgs.nix" }, ) + +nixpkgs_package( + name = "output-filegroup-test", + nix_file = "//tests:output.nix", + repositories = { "nixpkgs": "//:nixpkgs.nix" }, +) + +nixpkgs_package( + name = "output-filegroup-manual-test", + nix_file = "//tests:output.nix", + repositories = { "nixpkgs": "//:nixpkgs.nix" }, + build_file_content = """ +package(default_visibility = [ "//visibility:public" ]) +filegroup( + name = "manual-filegroup", + srcs = glob(["hi-i-exist", "hi-i-exist-too", "bin/*"]), +) +""", +) diff --git a/tests/BUILD b/tests/BUILD index 373d2bb56..ba338fb8c 100644 --- a/tests/BUILD +++ b/tests/BUILD @@ -1,17 +1,49 @@ package(default_testonly = 1) -[sh_test( - name= "run-{0}".format(test), - srcs = ["test_bin.sh"], - args=["$(location @{0}//:bin)".format(test)], - data = ["@{0}//:bin".format(test)], - timeout = "short", -) for test in [ - "hello", - "expr-test", - "attribute-test", - "expr-attribute-test", - "nix-file-test", - "nix-file-deps-test", - "nixpkgs-git-repository-test", -]] +[ + # All of these tests use the "hello" binary to see + # whether different invocations of `nixpkgs_package` + # produce a valid bazel repository. + sh_test( + name= "run-{0}".format(test), + srcs = ["test_bin.sh"], + args=["$(location @{0}//:bin)".format(test)], + data = ["@{0}//:bin".format(test)], + timeout = "short", + ) for test in [ + "hello", + "expr-test", + "attribute-test", + "expr-attribute-test", + "nix-file-test", + "nix-file-deps-test", + "nixpkgs-git-repository-test", + ] +] \ ++ \ +[ + # These tests use the nix package generated by ./output.nix + + # Checks whether the `:include` filegroup of `nixpkgs_package` + # repositories works as intended + # (that the expected number of files are inside the target) + sh_test( + name = "run-test-include", + srcs = ["test_output.sh"], + data = ["@output-filegroup-test//:include"], + args = ["2", "$(locations @output-filegroup-test//:include)"], + timeout = "short", + ), + + # Checks whether specifying a manual filegroup in the + # `nixpkgs_package` BUILD file works as well. + sh_test( + name = "run-test-manual-filegroup", + srcs = ["test_output.sh"], + data = ["@output-filegroup-manual-test//:manual-filegroup"], + args = [ + "3", + "$(locations @output-filegroup-manual-test//:manual-filegroup)"], + timeout = "short", + ) +] diff --git a/tests/output.nix b/tests/output.nix new file mode 100644 index 000000000..a0269adb6 --- /dev/null +++ b/tests/output.nix @@ -0,0 +1,13 @@ +with import {}; + +runCommand "some-output" { + preferLocalBuild = true; + allowSubstitutes = false; +} '' + mkdir -p $out/{bin,include/mylib} + touch $out/hi-i-exist + touch $out/hi-i-exist-too + touch $out/bin/im-a-binary + touch $out/include/mylib/im-a-header.h + touch $out/include/mylib/im-also-a-header.h +'' diff --git a/tests/test_output.sh b/tests/test_output.sh new file mode 100755 index 000000000..29f2735cf --- /dev/null +++ b/tests/test_output.sh @@ -0,0 +1,15 @@ +#!/bin/sh +# first param is the expected number of files given by `locations` +expected_length="$1" + +# rest of the arguments are files +shift +no_of_files=$# + +if [ "$no_of_files" -ne "$expected_length" ]; then + echo "Should have received $expected_length files, but got $no_of_files:" + for f in "$@"; do + echo "$f" + done + exit 1 +fi