From 853010309d4b0fceda3961eb0914363d91534149 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Tue, 8 Jun 2021 20:34:02 +0200
Subject: [PATCH] ENH: replace tutorials/AutoTest with bin/foamTestTutorial

- additional -serial/-parallel option:
  prefer Allrun-serial or Allrun-parallel if available

- optional -output=DIR to preserve output

ENH: report missing tutorials/ directory in RunFunctions
---
 tutorials/AutoTest => bin/foamTestTutorial | 96 ++++++++++++++++------
 bin/tools/LogFunctions                     |  3 +-
 bin/tools/RunFunctions                     |  5 +-
 etc/openfoam                               |  8 +-
 tutorials/Alltest                          | 60 +++++++-------
 5 files changed, 114 insertions(+), 58 deletions(-)
 rename tutorials/AutoTest => bin/foamTestTutorial (61%)

diff --git a/tutorials/AutoTest b/bin/foamTestTutorial
similarity index 61%
rename from tutorials/AutoTest
rename to bin/foamTestTutorial
index 3a3f41b66a6..e47daecc120 100755
--- a/tutorials/AutoTest
+++ b/bin/foamTestTutorial
@@ -6,17 +6,18 @@
 #   \\  /    A nd           | www.openfoam.com
 #    \\/     M anipulation  |
 #------------------------------------------------------------------------------
-#     Copyright (C) 2020 OpenCFD Ltd.
+#     Copyright (C) 2020-2021 OpenCFD Ltd.
 #------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
 #
 # Script
-#     tutorials/AutoTest dir [.. dirN]
+#     foamTestTutorial dir [.. dirN]
 #
 # Description
 #     Run foamRunTutorials with specified tutorial directories
-#     Creates/destroys a temporary directory for each test.
+#     Creates/destroys a temporary directory for each test unless
+#     an output directory has been specified.
 #
 # Environment
 #     Requires an initialized OpenFOAM environment.
@@ -35,13 +36,18 @@ printHelp() {
 usage: ${0##*/} [OPTION] dir [.. dirN]
 
 options:
-  -1            Modify case controlDict to run only one time step (default)
-  -full         Do not modify controlDict (run tutorial to completion)
-  -debian       Any modifications when running with autopkgtest
+  -1            Run only one time step (modifies controlDict) [default]
+  -full         Run to completion (does not modify controlDict)
+  -force        Force overwrite of existing output directories
+  -debian       Adjust for running with autopkgtest
+  -serial       Prefer Allrun-serial if available
+  -parallel     Prefer Allrun-parallel if available
+  -output=DIR   Output directory (default: a temporary directory)
   -help         Print the usage
 
-Run foamRunTutorials with specified tutorial directories.
-Creates/destroys a temporary directory for each.
+Run foamRunTutorials with specified tutorial directories
+Creates/destroys a temporary directory for each test unless
+an output directory has been specified.
 
 USAGE
     exit 0 # A clean exit
@@ -62,21 +68,22 @@ die()
 
 #------------------------------------------------------------------------------
 
-unset optDebian optVerbose
+unset optAllrun optDebian optForce optVerbose
+unset outputDir
 optRunLimit=1
 
 # Parse options
 while [ "$#" -gt 0 ]
 do
     case "$1" in
-    -h*)
-        printHelp
-        ;;
+    -h* | -help*)   printHelp ;;
 
+    -force)
+        optForce=true
+        ;;
     -1)
         optRunLimit="${1#-}"
         ;;
-
     -full)
         unset optRunLimit
         ;;
@@ -87,6 +94,14 @@ do
         exec 2>&1
         ;;
 
+    -serial | -parallel)
+        optAllrun="Allrun-${1#-}"
+        ;;
+
+    -output=*)
+        outputDir="${1#*=}"
+        ;;
+
     --)
         break
         ;;
@@ -109,8 +124,18 @@ done
 [ -n "$FOAM_TUTORIALS" ] || export FOAM_TUTORIALS="$WM_PROJECT_DIR"/tutorials
 
 [ -d "${WM_PROJECT_DIR:?}" ] || die "No OpenFOAM environment: $WM_PROJECT_DIR"
-[ -d "$FOAM_TUTORIALS" ] || die "No OpenFOAM tutorials : $FOAM_TUTORIALS"
+[ -d "$FOAM_TUTORIALS" ] || die "No OpenFOAM tutorials: $FOAM_TUTORIALS"
+
+
+#-------------------------------------------------------------------------------
+# Resolve the output directory
+if [ -n "$outputDir" ]
+then
+    outputDir="$(cd "$outputDir" 2>/dev/null && pwd -L)" || \
+        die "Cannot resolve output directory"
 
+    [ -w "$outputDir" ] || die "Output directory non-writable: $outputDir"
+fi
 
 #------------------------------------------------------------------------------
 
@@ -149,11 +174,29 @@ do
         echo "Run test: $testdir"
         set -e
 
-        TESTDIR="$(mktemp --directory --suffix=".$suffix")"
-        trap 'rm -rf $TESTDIR' 0 INT QUIT ABRT PIPE TERM
-
+        if [ -n "$outputDir" ]
+        then
+            TESTDIR="$outputDir/$suffix"
+            if [ -d "$TESTDIR" ]
+            then
+                if [ "$optForce" = true ]
+                then
+                    rm -rf "$TESTDIR"  # Remove old directory
+                else
+                    echo "Directory exists: $TESTDIR" 1>&2
+                    exit
+                fi
+            fi
+            mkdir "$TESTDIR"
+        else
+            TESTDIR="$(mktemp --directory --suffix=".$suffix")"
+            trap 'rm -rf $TESTDIR' 0 INT QUIT ABRT PIPE TERM
+        fi
         cp -r "$FOAM_TUTORIALS/$testdir"/* "$TESTDIR"/
-        cd "$TESTDIR"
+        cd "$TESTDIR" || exit
+
+        # In case the input already had results
+        foamCleanTutorials  > /dev/null 2>&1
 
         if [ -n "$optRunLimit" ]
         then
@@ -162,19 +205,26 @@ do
             set -e
         fi
 
-        nInput="$(ls | wc -l)"
-        foamRunTutorials
-        nOutput="$(ls | wc -l)"
+        nFilesBefore="$(ls | wc -l)"
+
+        if [ -n "$optAllrun" ] && [ -f ./"$optAllrun" ]
+        then
+            ./"$optAllrun"
+        else
+            foamRunTutorials
+        fi
+        nFilesAfter="$(ls | wc -l)"
 
-        if [ "$nInput" = 0 ]
+        if [ "$nFilesBefore" = 0 ]
         then
             echo "No input for $testdir" 1>&2
             exit 1
-        elif [ "$nOutput" = "$nInput" ]
+        elif [ "$nFilesBefore" = "$nFilesAfter" ]
         then
             echo "Run failure for $testdir" 1>&2
             exit 1
         else
+            # And/or grep for FatalError in log files
             echo "run: OK"
         fi
     ) && nPassed=$((nPassed + 1))
diff --git a/bin/tools/LogFunctions b/bin/tools/LogFunctions
index 50de322e360..4bac22234b3 100644
--- a/bin/tools/LogFunctions
+++ b/bin/tools/LogFunctions
@@ -8,8 +8,7 @@
 #     Copyright (C) 2017-2019 OpenCFD Ltd.
 #------------------------------------------------------------------------------
 # License
-#     This file is part of OpenFOAM, licensed under GNU General Public License
-#     <http://www.gnu.org/licenses/>.
+#     This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
 #
 # Script
 #     LogFunctions
diff --git a/bin/tools/RunFunctions b/bin/tools/RunFunctions
index ccb2bc1c103..12077a00b2a 100644
--- a/bin/tools/RunFunctions
+++ b/bin/tools/RunFunctions
@@ -20,7 +20,10 @@
 #------------------------------------------------------------------------------
 
 # The normal locations
-[ -n "$FOAM_TUTORIALS" ] || export FOAM_TUTORIALS=$WM_PROJECT_DIR/tutorials
+[ -n "$FOAM_TUTORIALS" ] || export FOAM_TUTORIALS="$WM_PROJECT_DIR"/tutorials
+
+# Basic sanity checks
+[ -d "$FOAM_TUTORIALS" ] || echo "No OpenFOAM tutorials? : $FOAM_TUTORIALS" 1>&2
 
 
 #------------------------------------------------------------------------------
diff --git a/etc/openfoam b/etc/openfoam
index da2b025b688..87191ad7168 100755
--- a/etc/openfoam
+++ b/etc/openfoam
@@ -6,7 +6,7 @@
 #   \\  /    A nd           | www.openfoam.com
 #    \\/     M anipulation  |
 #------------------------------------------------------------------------------
-#     Copyright (C) 2019-2020 OpenCFD Ltd.
+#     Copyright (C) 2019-2021 OpenCFD Ltd.
 #------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
@@ -59,7 +59,7 @@ options:
   -show-api | -version  Print META-INFO api value and exit
   -show-patch           Print META-INFO patch value and exit
   -show-prefix          Print project directory and exit
-  -test-tutorial        Forward arguments to tutorials/AutoTest
+  -test-tutorial        Forward arguments to bin/foamTestTutorial
   -init=FILE            Alternative initialization file (expert option)
   -verbose              Set FOAM_VERBOSE=true (interactive only)
   -help                 Print the usage
@@ -126,7 +126,7 @@ do
         exit $?
         ;;
 
-    -test-tutorial)  # Run tutorials/AutoTest
+    -test-tutorial)  # Run bin/foamTestTutorial
         optTestTut=true
         ;;
 
@@ -294,7 +294,7 @@ if [ -n "$optTestTut" ]
 then
 
     sourceBashrc
-    exec "$WM_PROJECT_DIR/tutorials/AutoTest" "$@"
+    exec "$WM_PROJECT_DIR"/bin/foamTestTutorial "$@"
     exit $? # Safety
 fi
 
diff --git a/tutorials/Alltest b/tutorials/Alltest
index a7621d01674..0cdebe8b44d 100755
--- a/tutorials/Alltest
+++ b/tutorials/Alltest
@@ -7,11 +7,10 @@
 #    \\/     M anipulation  |
 #------------------------------------------------------------------------------
 #     Copyright (C) 2011-2016 OpenFOAM Foundation
-#     Copyright (C) 2017-2019 OpenCFD Ltd.
+#     Copyright (C) 2017-2021 OpenCFD Ltd.
 #------------------------------------------------------------------------------
 # License
-#     This file is part of OpenFOAM, licensed under GNU General Public License
-#     <http://www.gnu.org/licenses/>.
+#     This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
 #
 # Script
 #     Alltest
@@ -25,10 +24,7 @@
 #------------------------------------------------------------------------------
 cd "${0%/*}" || exit  # Run from this directory
 
-usage()
-{
-    exec 1>&2
-    while [ "$#" -ge 1 ]; do echo "$1"; shift; done
+printHelp() {
     cat<<USAGE
 
 usage: ${0##*/} [OPTION]
@@ -39,14 +35,14 @@ options:
   -default      Sets up a default scheme on all schemes
   -git          Use git to retrieve the tutorials
   -no-git       Do not use git to retrieve the tutorials
-  -root dir     Root directory to start tests from (non-git only)
+  -root=DIR     Root directory to start tests from (non-git only)
   -help         Print the usage
 
 Quickly tests the tutorials and writes out the scheme/solver information.
 Detects and uses 'git' to obtain a fresh set of files when possible.
 
 USAGE
-    exit 1
+    exit 0 # A clean exit
 }
 
 # Report error and exit
@@ -74,12 +70,13 @@ unset optForce
 while [ "$#" -gt 0 ]
 do
     case "$1" in
-    -h | -help)
-        usage
-        ;;
-    -f | -force)
-        optForce=true
+    -h* | -help*)   printHelp ;;
+    -f | -force)   optForce=true ;;
+
+    -root=*)
+        rootDir="${1#*=}"
         ;;
+
     -r | -root)
         [ "$#" -ge 2 ] || die "'$1' option requires an argument"
         rootDir="$2"
@@ -141,10 +138,27 @@ snGradSchemes { default corrected; }
 EOF
 }
 
-
 #
-# Locate the user or project controlDict to adjust
+# Modify case controlDicts to run only one time step
 #
+modifyCaseControlDict()
+{
+    for dict in $(find . -name "controlDict*" -type f)
+    do
+        cp -f "${dict}" "${dict}.orig"
+        sed \
+            -e 's/\(startFrom[ \t]*\)\([A-Za-z]*\);/\1 latestTime;/' \
+            -e 's/\(stopAt[ \t]*\)\([A-Za-z]*\);/\1 nextWrite;/' \
+            -e 's/\(writeControl[ \t]*\)\([A-Za-z]*\);/\1 timeStep;/' \
+            -e 's/\(writeInterval[ \t]*\)\([-.0-9A-Za-z]*\);/\1 '"$optRunLimit"';/' \
+            "${dict}.orig" > "${dict}"
+    done
+}
+
+
+#------------------------------------------------------------------------------
+
+# Locate the user or project controlDict to adjust
 unset ETC_CONTROL_DICT
 if [ "$adjustDebugSwitches" = true ]
 then
@@ -194,7 +208,7 @@ then
     if [ "$optForce" = true ]
     then
         echo "Removing old directory: $TEST_RUN_DIR" 1>&2
-        rm -rf $TEST_RUN_DIR
+        rm -rf "$TEST_RUN_DIR"
     else
         echo "Directory already exists: $TEST_RUN_DIR" 1>&2
         echo "    use -force to remove" 1>&2
@@ -273,17 +287,7 @@ fi
 
 echo "Modifying the case controlDicts to run only one time step" 1>&2
 echo 1>&2
-
-for CD in $(find . -name "controlDict*" -type f)
-do
-    cp -f "${CD}" "${CD}.orig"
-    sed \
-        -e 's/\(startFrom[ \t]*\)\([a-zA-Z]*\);/\1 latestTime;/g' \
-        -e 's/\(stopAt[ \t]*\)\([a-zA-Z]*\);/\1 nextWrite;/g' \
-        -e 's/\(writeControl[ \t]*\)\([a-zA-Z]*\);/\1 timeStep;/g' \
-        -e 's/\(writeInterval[ \t]*\)\([0-9a-zA-Z.-]*\);/\1 1;/g' \
-        "${CD}.orig" > "${CD}"
-done
+modifyCaseControlDict
 
 if [ "$useDefaultSchemes" = true ]
 then
-- 
GitLab