tests: Automatically read file paths from configs

Instead of manually redirecting content of specific
section into a file to initialize the configs, change
them to get the file location by use of a file marker.

Originally-Authored-By: Adithya Chakilam
Change-Id: Iae2573e14ab2d0ffaccc68e779e3098aaf6eaebe
diff --git a/src/main/resources/Documentation/test/task_states.md b/src/main/resources/Documentation/test/task_states.md
index 286af97..ef8285f 100644
--- a/src/main/resources/Documentation/test/task_states.md
+++ b/src/main/resources/Documentation/test/task_states.md
@@ -17,6 +17,7 @@
 The config below is expected to be in the `task.config` file in project
 `All-Projects` on ref `refs/meta/config`.
 
+file: `All-Projects:refs/meta/config:task.config`
 ```
 [root "Root N/A"]
   applicable = is:closed # Assumes test query is "is:open"
@@ -2733,8 +2734,7 @@
 
 ```
 
-`task/common.config` file in project `All-Projects` on ref `refs/meta/config`.
-
+file: `All-Projects:refs/meta/config:task/common.config`
 ```
 [task "file task/common.config PASS"]
   applicable = is:open
@@ -2745,8 +2745,7 @@
   fail = is:open
 ```
 
-`task/invalids.config` file in project `All-Projects` on ref `refs/meta/config`.
-
+file: `All-Projects:refs/meta/config:task/invalids.config`
 ```
 [task "No PASS criteria"]
   fail-hint = Invalid without Pass criteria and without subtasks
@@ -2907,8 +2906,7 @@
 
 ```
 
-`task/special.config` file in project `All-Users` on ref `refs/users/self`.
-
+file: `All-Users:refs/meta/config:task/special.config`
 ```
 [task "userfile task/special.config PASS"]
   applicable = is:open
@@ -2921,3 +2919,14 @@
 [task "file task/common.config Preload PASS"]
   preload-task = userfile task/special.config PASS
 ```
+
+file: `All-Users:refs/users/self:task/common.config`
+```
+[task "file task/common.config PASS"]
+  applicable = is:open
+  pass = is:open
+
+[task "file task/common.config FAIL"]
+  applicable = is:open
+  fail = is:open
+```
diff --git a/test/check_task_statuses.sh b/test/check_task_statuses.sh
index d5c965e..c1639f5 100755
--- a/test/check_task_statuses.sh
+++ b/test/check_task_statuses.sh
@@ -17,6 +17,16 @@
 # Usage:
 # All-Projects.git - must have 'Push' rights on refs/meta/config
 
+create_configs_from_task_states() {
+    for marker in $(md_file_markers "$DOC_STATES") ; do
+        local project="$OUT/$(md_file_marker_project "$marker")"
+        local file="$(md_file_marker_file "$marker")"
+
+        mkdir -p "$(dirname "$project/$file")"
+        md_marker_content "$DOC_STATES" "$marker" | replace_user | testdoc_2_cfg > "$project/$file"
+    done
+}
+
 test_2generated() { # name task_args...
     local name=$1 ; shift
     local out=$(query "$@")
@@ -53,6 +63,7 @@
 MYPROG=$(basename -- "$0")
 
 source "$MYDIR/lib/lib_helper.sh"
+source "$MYDIR/lib/lib_md.sh"
 
 DOCS=$MYDIR/.././src/main/resources/Documentation/test
 OUT=$MYDIR/../target/tests
@@ -67,10 +78,6 @@
 ACTUAL=$OUT/actual
 
 ROOT_CFG=$ALL/task.config
-COMMON_CFG=$ALL_TASKS/common.config
-USER_COMMON_CFG=$USER_TASKS/common.config
-INVALIDS_CFG=$ALL_TASKS/invalids.config
-USER_SPECIAL_CFG=$USER_TASKS/special.config
 
 # --- Args ----
 
@@ -120,11 +127,7 @@
 DOC_PREVIEW=$(replace_tokens < "$DOCS/preview.md")
 DOC_PATHS=$(replace_tokens < "$DOCS/paths.md")
 
-example "$DOC_STATES" 2 | testdoc_2_cfg > "$ROOT_CFG"
-example "$DOC_STATES" 3 > "$COMMON_CFG"
-example "$DOC_STATES" 3 > "$USER_COMMON_CFG"
-example "$DOC_STATES" 4 > "$INVALIDS_CFG"
-example "$DOC_STATES" 5 > "$USER_SPECIAL_CFG"
+create_configs_from_task_states
 
 ROOTS=$(config_section_keys "root") || err "Invalid ROOTS"
 
diff --git a/test/check_task_visibility.sh b/test/check_task_visibility.sh
index 553918e..c5855e2 100755
--- a/test/check_task_visibility.sh
+++ b/test/check_task_visibility.sh
@@ -19,6 +19,7 @@
 MYPROG=$(basename -- "$0")
 
 source "$MYDIR/lib/lib_helper.sh"
+source "$MYDIR/lib/lib_md.sh"
 
 # Visibility tests cases are described using a markdown file.
 # Each file has a list of config files specified by file
@@ -33,89 +34,6 @@
 # part of diff, we use diff indicators (+/-) instead of a leading
 # space.
 
-# Example markdown file:
-# (Using block comment to better understand the file syntax.)
-
-: <<'END'
-# Test case description header
-
-file: `All-Projects.git:refs/meta/config:task.config`
-```
-[root "Test root"]
-    applicable = "is:open"
-    pass = True
-```
-
-file: `All-Users:refs/users/some_ref:task/sample.config`
-```
- [task "NON-SECRET task"]
-     applicable = is:open
-     pass = Fail
-+    subtasks-external = SECRET
-
-+[external "SECRET"]
-+    user = {secret_user}
-+    file = secret.config
-```
-
-json:
-```
-{
-   {
-     "some": "example"
-   }
-}
-END
-
-# (For example above)
-# out:
-# `All-Projects.git:refs/meta/config:task.config`
-# `All-Users:refs/users/some_ref:task/sample.config`
-get_file_markers() {
-    echo "$TEST_DOC" | grep -o "^file: .*" | cut -f2 -d'`'
-}
-
-# (For example above)
-# in: `All-Projects.git:refs/meta/config:task.config`
-# out:
-#[root "Test root"]
-#    applicable = "is:open"
-#    pass = True
-#
-# in: json:
-# out :
-# {
-#    {
-#      "some": "example"
-#    }
-# }
-get_marker_content() { # marker
-    local start_line=$(echo "$TEST_DOC" | grep -n "$1" | cut -f1 -d':')
-    echo "$TEST_DOC" | tail -n+"$start_line" | \
-        sed '1,/```/d;/```/,$d' | grep -v '```'
-}
-
-# file_marker > project
-# in: `All-Projects.git:refs/meta/config:task/task.config`
-# out: All-Projects.git
-get_project_from_marker() {
-    echo "$WORKSPACE_DIR/$(echo "$1" | cut -f1 -d':')"
-}
-
-# file_marker > ref
-# in: `All-Projects.git:refs/meta/config:task/task.config`
-# out: refs/meta/config
-get_ref_from_marker() {
-    echo "$1" | cut -f2 -d':'
-}
-
-# file_marker > file
-# in: `All-Projects.git:refs/meta/config:task/task.config`
-#out: task/task.config
-get_file_from_marker() {
-    echo "$1" | cut -f3 -d':'
-}
-
 # Example input for all diff functions:
 #
 #  [root "Root Preview SECRET external"]
@@ -169,7 +87,7 @@
 # Gets json from the preview doc and creates
 # expected json in workspace to assert later.
 create_expected_json() {
-    local json=$(get_marker_content "json:")
+    local json=$(md_marker_content "$TEST_DOC" "json:")
 
     echo "$json" | remove_suites "non-secret" | \
       testdoc_2_pjson | ensure json_pp > "$EXPECTED_SECRET"
@@ -191,11 +109,11 @@
 }
 
 init_configs() {
-    for marker in $(get_file_markers) ; do
-        local project="$(get_project_from_marker "$marker")"
-        local ref="$(get_ref_from_marker "$marker")"
-        local file="$(get_file_from_marker "$marker")"
-        local content="$(get_marker_content "$marker")"
+    for marker in $(md_file_markers "$TEST_DOC") ; do
+        local project="$OUT/$(md_file_marker_project "$marker")"
+        local ref="$(md_file_marker_ref "$marker")"
+        local file="$(md_file_marker_file "$marker")"
+        local content="$(md_marker_content "$TEST_DOC" "$marker")"
         local tip_content
 
         q_setup setup_repo "$project" "$(get_remote "$project")" "$ref"
@@ -216,9 +134,9 @@
 }
 
 test_change() {
-    local project="$(get_project_from_marker "$CHANGE_FILE_MARKER")"
-    local ref="$(get_ref_from_marker "$CHANGE_FILE_MARKER")"
-    local file="$(get_file_from_marker "$CHANGE_FILE_MARKER")"
+    local project="$OUT/$(md_file_marker_project "$CHANGE_FILE_MARKER")"
+    local ref="$(md_file_marker_ref "$CHANGE_FILE_MARKER")"
+    local file="$(md_file_marker_file "$CHANGE_FILE_MARKER")"
     q_setup setup_repo "$project" "$(get_remote "$project")" "$ref"
 
     echo "$CHANGE_CONTENT"  > "$project/$file"
@@ -260,11 +178,11 @@
 RESULT=0
 PORT=29418
 HTTP_PORT=8080
-WORKSPACE_DIR=$MYDIR/../target/preview
-EXPECTED_SECRET="$WORKSPACE_DIR/expected-secret"
-EXPECTED_NON_SECRET="$WORKSPACE_DIR/expected-non-secret"
-ACTUAL_SECRET="$WORKSPACE_DIR/actual-secret"
-ACTUAL_NON_SECRET="$WORKSPACE_DIR/actual-non-secret"
+OUT=$MYDIR/../target/preview
+EXPECTED_SECRET="$OUT/expected-secret"
+EXPECTED_NON_SECRET="$OUT/expected-non-secret"
+ACTUAL_SECRET="$OUT/actual-secret"
+ACTUAL_NON_SECRET="$OUT/actual-non-secret"
 TEST_DOC_DIR="$MYDIR/../src/main/resources/Documentation/test/task-preview/"
 
 declare -A USERS
@@ -274,8 +192,8 @@
 USERS["{non_secret_user}"]="$NON_SECRET_USER"
 USER_REFS["{non_secret_user_ref}"]="$(get_user_ref "$NON_SECRET_USER")"
 
-mkdir -p "$WORKSPACE_DIR"
-trap 'rm -rf "$WORKSPACE_DIR"' EXIT
+mkdir -p "$OUT"
+trap 'rm -rf "$OUT"' EXIT
 
 TESTS=(
 "new_root_with_original_with_external_secret_ref.md"
diff --git a/test/lib/lib_md.sh b/test/lib/lib_md.sh
new file mode 100644
index 0000000..2838163
--- /dev/null
+++ b/test/lib/lib_md.sh
@@ -0,0 +1,100 @@
+#!/usr/bin/env bash
+#
+# Copyright (C) 2021 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# ---- Markdown Format Helpers ----
+
+# Example markdown file:
+# (Using block comment to better understand the file syntax.)
+
+: <<'END'
+# Test case description header
+
+file: `All-Projects.git:refs/meta/config:task.config`
+```
+[root "Test root"]
+    applicable = "is:open"
+    pass = True
+```
+
+file: `All-Users:refs/users/some_ref:task/sample.config`
+```
+ [task "NON-SECRET task"]
+     applicable = is:open
+     pass = Fail
++    subtasks-external = SECRET
+
++[external "SECRET"]
++    user = {secret_user}
++    file = secret.config
+```
+
+json:
+```
+{
+   {
+     "some": "example"
+   }
+}
+END
+
+# (For example above)
+# out:
+# `All-Projects.git:refs/meta/config:task.config`
+# `All-Users:refs/users/some_ref:task/sample.config`
+md_file_markers() { # DOC_CONTENT
+    echo "$1" | grep -o "^file: .*" | cut -f2 -d'`'
+}
+
+# (For example above)
+# in: `All-Projects.git:refs/meta/config:task.config`
+# out:
+#[root "Test root"]
+#    applicable = "is:open"
+#    pass = True
+#
+# in: json:
+# out :
+# {
+#    {
+#      "some": "example"
+#    }
+# }
+md_marker_content() { # DOC marker
+    local start_line=$(echo "$1" | grep -n "$2" | cut -f1 -d':')
+    echo "$1" | tail -n+"$start_line" | \
+        sed '1,/```/d;/```/,$d' | grep -v '```'
+}
+
+# file_marker > project
+# in: `All-Projects.git:refs/meta/config:task/task.config`
+# out: All-Projects.git
+md_file_marker_project() {
+    echo "$1" | cut -f1 -d':'
+}
+
+# file_marker > ref
+# in: `All-Projects.git:refs/meta/config:task/task.config`
+# out: refs/meta/config
+md_file_marker_ref() {
+    echo "$1" | cut -f2 -d':'
+}
+
+# file_marker > file
+# in: `All-Projects.git:refs/meta/config:task/task.config`
+#out: task/task.config
+md_file_marker_file() {
+    echo "$1" | cut -f3 -d':'
+}
\ No newline at end of file