From 2b611b8f907365cd25202297ee91adfbd87cf994 Mon Sep 17 00:00:00 2001
From: Alexey Maslennikov <alexey@maslennikov.me>
Date: Thu, 15 Jun 2017 21:47:12 +0200
Subject: [PATCH] Making --fail-not-set work with partials

The return code was not propagated through nested subshells in case
when files were passed in as arguments.
---
 mo                               | 9 +++++----
 tests/fail-not-set-file.expected | 1 +
 tests/fail-not-set-file.sh       | 9 +++++++++
 tests/fail-not-set-file.template | 1 +
 4 files changed, 16 insertions(+), 4 deletions(-)
 create mode 100644 tests/fail-not-set-file.expected
 create mode 100755 tests/fail-not-set-file.sh
 create mode 100644 tests/fail-not-set-file.template

diff --git a/mo b/mo
index fc168b5..f1d1b31 100755
--- a/mo
+++ b/mo
@@ -696,7 +696,7 @@ moParse() {
 # Returns nothing.
 moPartial() {
     # Namespace variables here to prevent conflicts.
-    local moContent moFilename moIndent moPartial moStandalone
+    local moContent moFilename moIndent moPartial moStandalone moUnindented
 
     if moIsStandalone moStandalone "$2" "$4" "$5"; then
         moStandalone=( $moStandalone )
@@ -715,16 +715,17 @@ moPartial() {
     (
         # TODO:  Remove dirname and use a function instead
         cd "$(dirname -- "$moFilename")" || exit 1
-        moIndentLines moPartial "$moIndent" "$(
+        moUnindented="$(
             moLoadFile moPartial "${moFilename##*/}"
             moParse "${moPartial}" "$6" true
 
             # Fix bash handling of subshells and keep trailing whitespace.
             # This is removed in moIndentLines.
             echo -n "."
-        )"
+        )" || exit 1
+        moIndentLines moPartial "$moIndent" "$moUnindented"
         echo -n "$moPartial"
-    )
+    ) || exit 1
 
     local "$1" && moIndirect "$1" "$moContent"
 }
diff --git a/tests/fail-not-set-file.expected b/tests/fail-not-set-file.expected
new file mode 100644
index 0000000..439982b
--- /dev/null
+++ b/tests/fail-not-set-file.expected
@@ -0,0 +1 @@
+Env variable not set: __NO_SUCH_VAR
diff --git a/tests/fail-not-set-file.sh b/tests/fail-not-set-file.sh
new file mode 100755
index 0000000..0355219
--- /dev/null
+++ b/tests/fail-not-set-file.sh
@@ -0,0 +1,9 @@
+#!/usr/bin/env bash
+
+cd "${0%/*}"
+unset __NO_SUCH_VAR
+../mo --fail-not-set ./fail-not-set-file.template 2>&1
+
+if [[ $? -ne 1 ]]; then
+    echo "Did not return 1"
+fi
diff --git a/tests/fail-not-set-file.template b/tests/fail-not-set-file.template
new file mode 100644
index 0000000..bd8b3ee
--- /dev/null
+++ b/tests/fail-not-set-file.template
@@ -0,0 +1 @@
+* {{__NO_SUCH_VAR}}