Merge "Create separate bazel test target for each directory's template test"
diff --git a/polygerrit-ui/README.md b/polygerrit-ui/README.md
index 5aa918c..e47d246 100644
--- a/polygerrit-ui/README.md
+++ b/polygerrit-ui/README.md
@@ -176,10 +176,19 @@
 To run on all files, execute the following command:
 
 ```sh
-bazel test //polygerrit-ui/app:template_test
+bazel test //polygerrit-ui/app:all --test_tag_filters=template --test_output errors
 ```
 
-To run on a specific file (ex: gr-list-view), execute the following command:
+To run on a specific top level directory (ex: change-list)
 ```sh
-bazel test //polygerrit-ui/app:template_test --test_arg=gr-list-view
+bazel test //polygerrit-ui/app:template_test_change-list --test_output errors
+```
+
+To run on a specific file (ex: gr-change-list-view), execute the following command:
+```sh
+bazel test //polygerrit-ui/app:template_test_<TOP_LEVEL_DIRECTORY> --test_arg=<VIEW_NAME> --test_output errors
+```
+
+```sh
+bazel test //polygerrit-ui/app:template_test_change-list --test_arg=gr-change-list-view  --test_output errors
 ```
\ No newline at end of file
diff --git a/polygerrit-ui/app/BUILD b/polygerrit-ui/app/BUILD
index 59a9a6d..a00ded7 100644
--- a/polygerrit-ui/app/BUILD
+++ b/polygerrit-ui/app/BUILD
@@ -125,21 +125,33 @@
     ],
 )
 
-sh_test(
-    name = "template_test",
+DIRECTORIES = [
+    "change",
+    "change-list",
+    "core",
+    "diff",
+    "plugins",
+    "settings",
+    "shared",
+    "gr-app",
+]
+
+[sh_test(
+    name = "template_test_" + directory,
     size = "large",
     srcs = ["template_test.sh"],
+    args = [directory],
     data = [
         ":pg_code",
         ":template_test_srcs",
         "//polygerrit-ui:polygerrit_components.bower_components.zip",
     ],
-    # Should not run sandboxed.
     tags = [
+        # Should not run sandboxed.
         "local",
-        "manual",
+        "template",
     ],
-)
+) for directory in DIRECTORIES]
 
 # Embed bundle
 polygerrit_bundle(
diff --git a/polygerrit-ui/app/template_test.sh b/polygerrit-ui/app/template_test.sh
index 0ed6d94..5ffc434 100755
--- a/polygerrit-ui/app/template_test.sh
+++ b/polygerrit-ui/app/template_test.sh
@@ -35,4 +35,4 @@
 unzip -o polygerrit-ui/polygerrit_components.bower_components.zip -d polygerrit-ui/app
 python $TEST_SRCDIR/gerrit/polygerrit-ui/app/template_test_srcs/convert_for_template_tests.py
 # Pass a file name argument from the --test_args (example: --test_arg=gr-list-view)
-${node_bin} $TEST_SRCDIR/gerrit/polygerrit-ui/app/template_test_srcs/template_test.js $1
+${node_bin} $TEST_SRCDIR/gerrit/polygerrit-ui/app/template_test_srcs/template_test.js $1 $2
diff --git a/polygerrit-ui/app/template_test_srcs/convert_for_template_tests.py b/polygerrit-ui/app/template_test_srcs/convert_for_template_tests.py
index d293b69..b128d65 100644
--- a/polygerrit-ui/app/template_test_srcs/convert_for_template_tests.py
+++ b/polygerrit-ui/app/template_test_srcs/convert_for_template_tests.py
@@ -55,7 +55,8 @@
   if "gr-reporting" in file:
     return
   if not root in elements:
-    elements[root] = {}
+    # gr-app doesn't have an additional level
+    elements[root] = {"directory": 'gr-app' if len(root.split("/")) < 4 else root.split("/")[3]}
   if file.endswith(".html") and not file.endswith("_test.html"):
     # gr-navigation is treated like a behavior rather than a standard element
     # because of the way it added to the Gerrit object.
diff --git a/polygerrit-ui/app/template_test_srcs/template_test.js b/polygerrit-ui/app/template_test_srcs/template_test.js
index e5dae8b..ae26316 100644
--- a/polygerrit-ui/app/template_test_srcs/template_test.js
+++ b/polygerrit-ui/app/template_test_srcs/template_test.js
@@ -45,8 +45,21 @@
   let mappings = JSON.parse(fs.readFileSync(
       `./polygerrit-ui/temp/map.json`, 'utf-8'));
 
+  // The directory is passed as arg2 by the test target.
+  const directory = process.argv[2];
+  if (directory) {
+    const mappingSpecificDirectory = {};
+
+    for (key of Object.keys(mappings)) {
+      if (directory === mappings[key].directory) {
+        mappingSpecificDirectory[key] = mappings[key];
+      }
+    }
+    mappings = mappingSpecificDirectory;
+  }
+
   // If a particular file was passed by the user, don't test everything.
-  const file = process.argv[2];
+  const file = process.argv[3];
   if (file) {
     const mappingSpecificFile = {};
     for (key of Object.keys(mappings)) {