From d9ad5a13395990b9ca19c60f9c4c5cd3cf32f2d5 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Mon, 30 Sep 2013 15:41:20 +0000
Subject: [PATCH] Expose fail count to shell environment

Also, run the tests in the packager. The packager will automatically run the
regression test suite prior to packaging a release. The package will not
build if there is at least one fail from the regression tests.
---
 setup/cli/package.php           |  8 +++++++
 setup/test/run-tests.php        | 42 ++++++++++++++++++++-------------
 setup/test/tests/class.test.php |  1 +
 3 files changed, 35 insertions(+), 16 deletions(-)

diff --git a/setup/cli/package.php b/setup/cli/package.php
index 83a63d27d..f61d9792d 100755
--- a/setup/cli/package.php
+++ b/setup/cli/package.php
@@ -17,6 +17,10 @@ function get_osticket_root_path() {
     return realpath($start);
 }
 
+function run_tests($root) {
+    return (require "$root/setup/test/run-tests.php");
+}
+
 # Check PHP syntax across all php files
 function glob_recursive($pattern, $flags = 0) {
     $files = glob($pattern, $flags);
@@ -64,6 +68,10 @@ function package($pattern, $destination, $recurse=false, $exclude=false) {
     }
 }
 
+# Run tests before continuing
+if (run_tests($root) > 0)
+    die("Regression tests failed. Cowardly refusing to package\n");
+
 # Create the stage folder for the install files
 if (!is_dir($stage_path))
     mkdir($stage_path);
diff --git a/setup/test/run-tests.php b/setup/test/run-tests.php
index 0f42dd7d0..3a3df3a1d 100644
--- a/setup/test/run-tests.php
+++ b/setup/test/run-tests.php
@@ -4,25 +4,29 @@ if (php_sapi_name() != 'cli') exit();
 
 require_once "tests/class.test.php";
 
-function get_osticket_root_path() {
-    # Hop up to the root folder
-    $start = dirname(__file__);
-    for (;;) {
-        if (file_exists($start . '/main.inc.php')) break;
-        $start .= '/..';
+if (!function_exists('get_osticket_root_path')) {
+    function get_osticket_root_path() {
+        # Hop up to the root folder
+        $start = dirname(__file__);
+        for (;;) {
+            if (file_exists($start . '/main.inc.php')) break;
+            $start .= '/..';
+        }
+        return realpath($start);
     }
-    return realpath($start);
 }
 $root = get_osticket_root_path();
 
-# Check PHP syntax across all php files
-function glob_recursive($pattern, $flags = 0) {
-    $files = glob($pattern, $flags);
-    foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
-        $files = array_merge($files,
-            glob_recursive($dir.'/'.basename($pattern), $flags));
+if (!function_exists('glob_recursive')) {
+    # Check PHP syntax across all php files
+    function glob_recursive($pattern, $flags = 0) {
+        $files = glob($pattern, $flags);
+        foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
+            $files = array_merge($files,
+                glob_recursive($dir.'/'.basename($pattern), $flags));
+        }
+        return $files;
     }
-    return $files;
 }
 
 $fails = array();
@@ -37,6 +41,7 @@ function show_fails() {
             $script = str_replace($root.'/', '', $script);
             print("$test: $message @ $script:$line\n");
         }
+        return count($fails);
     }
 }
 if (function_exists('pcntl_signal')) {
@@ -44,8 +49,7 @@ if (function_exists('pcntl_signal')) {
     function show_fails_on_ctrlc() {
         while (@ob_end_flush());
         print("\n");
-        show_fails();
-        exit();
+        exit(show_fails());
     }
     pcntl_signal(SIGINT, 'show_fails_on_ctrlc');
 }
@@ -64,4 +68,10 @@ foreach (glob_recursive(dirname(__file__)."/tests/test.*.php") as $t) {
 }
 show_fails();
 
+// If executed directly expose the fail count to a shell script
+global $argv;
+if (!strcasecmp(basename($argv[0]), basename(__file__)))
+    exit(count($fails));
+else
+    return count($fails);
 ?>
diff --git a/setup/test/tests/class.test.php b/setup/test/tests/class.test.php
index 80a07b87b..5ce129761 100644
--- a/setup/test/tests/class.test.php
+++ b/setup/test/tests/class.test.php
@@ -10,6 +10,7 @@ class Test {
         '/include/PasswordHash.php',
         '/include/pear/',
         '/include/Spyc.php',
+        '/setup/cli/stage/',
     );
 
     function Test() {
-- 
GitLab