[build] Add hermetic_tests_package() rule.

This rule is a convenience rule which bundles executable() rules into a
single hermetic tests package. This results in each test running as a
component test in its own environment.

MF-155 #comment [build] Add hermetic_tests_package() rule.
TEST=in future CL

Change-Id: Icedc22b25ca11bb50e45ddc352963c65118b64b5
diff --git a/build/tests_package.gni b/build/tests_package.gni
index 067ad1d..34190a2 100644
--- a/build/tests_package.gni
+++ b/build/tests_package.gni
@@ -3,6 +3,7 @@
 # found in the LICENSE file.
 
 import("//build/package.gni")
+import("//build/test/test_package.gni")
 
 # A variant of package() that only contains test executables. The test
 # executables are specified as the build products of the deps. A tests_package()
@@ -29,3 +30,30 @@
     tests = computed_tests
   }
 }
+
+# A variant of test_package() that only contains test executables. It computes
+# the value of the 'tests' paremeter from 'deps'.
+#
+# This variant, as described in
+# https://fuchsia.git.corp.google.com/docs/+/master/development/tests/test_component.md,
+# requires a .cmx file for each test.
+template("hermetic_tests_package") {
+  computed_tests = []
+  foreach(dep, invoker.deps) {
+    computed_tests += [
+      {
+        name = get_label_info(dep, "name")
+      },
+    ]
+  }
+
+  test_package(target_name) {
+    forward_variables_from(invoker,
+                           [
+                             "deps",
+                             "meta",
+                             "resources",
+                           ])
+    tests = computed_tests
+  }
+}