[gegl] tests: Replace *.xml.sh with a python script



commit 3b88f8f0a8cc9e656f21e8972838c4d39f192cda
Author: Daniel Sabo <DanielSabo gmail com>
Date:   Sat Oct 19 23:58:04 2013 -0700

    tests: Replace *.xml.sh with a python script

 tests/compositions/.gitignore          |    2 -
 tests/compositions/Makefile.am         |  146 +++++++------------------
 tests/compositions/run-compositions.py |  190 ++++++++++++++++++++++++++++++++
 3 files changed, 229 insertions(+), 109 deletions(-)
---
diff --git a/tests/compositions/.gitignore b/tests/compositions/.gitignore
index 34224d0..3d8106f 100644
--- a/tests/compositions/.gitignore
+++ b/tests/compositions/.gitignore
@@ -1,5 +1,3 @@
-*~
 /output
-/run-*.xml.sh
 Makefile
 Makefile.in
diff --git a/tests/compositions/Makefile.am b/tests/compositions/Makefile.am
index d431bf4..f9e09e0 100644
--- a/tests/compositions/Makefile.am
+++ b/tests/compositions/Makefile.am
@@ -10,125 +10,57 @@
 
 SUBDIRS=data
 
-EXTRA_DIST=$(wildcard $(srcdir)/*.xml) $(wildcard $(srcdir)/reference/*)
-
-# Env vars to make binaries use builddir stuff
-builddir_gegl_env = GEGL_SWAP=RAM GEGL_PATH=$(top_builddir)/operations
-
-# Binary paths
-gegl_bin = $(top_builddir)/bin/gegl$(EXEEXT)
-img_cmp_bin = $(top_builddir)/tools/img_cmp$(EXEEXT)
-detect_opencl_bin = $(top_builddir)/tools/detect_opencl$(EXEEXT)
-
-# Commands run in a builddir env
-builddir_gegl = $(builddir_gegl_env) $(gegl_bin)
-builddir_img_cmp = $(builddir_gegl_env) $(img_cmp_bin)
-builddir_detect_opencl = $(builddir_gegl_env) $(detect_opencl_bin)
-
-TESTS_ENVIRONMENT = \
-       GEGL_TEST_HAVE_OPENCL=$(shell $(builddir_detect_opencl))
-
-# These strings must not be inside XML file names; we use $(subst ...)
-opencl_testprefix = run-opencl-
-testprefix = run-
-testsuffix = .sh
+EXTRA_DIST=$(wildcard $(srcdir)/*.xml) $(wildcard $(srcdir)/reference/*) run-compositions.py
 
 # List of tests. The .sh suffix allows us to use $(wildcard $(srcdir)/*.xml) in
 # EXTRA_DISTS
 TESTS = \
-  run-affine-with-negative.xml.sh        \
-  run-opencl-affine-with-negative.xml.sh \
-  run-checkerboard.xml.sh                \
-  run-opencl-checkerboard.xml.sh         \
-  run-clones.xml.sh                      \
-  run-opencl-clones.xml.sh               \
-  run-colors.xml.sh                      \
-  run-opencl-colors.xml.sh               \
-  run-composite-transform.xml.sh         \
-  run-opencl-composite-transform.xml.sh  \
-  run-edge-laplace.xml.sh                \
-  run-opencl-edge-laplace.xml.sh         \
-  run-edge-sobel.xml.sh                  \
-  run-opencl-edge-sobel.xml.sh           \
-  run-fattal02.xml.sh                    \
-  run-opencl-fattal02.xml.sh             \
-  run-gamma.xml.sh                       \
-  run-opencl-gamma.xml.sh                \
-  run-hdr-color.xml.sh                   \
-  run-opencl-hdr-color.xml.sh            \
-  run-mantiuk06.xml.sh                   \
-  run-opencl-mantiuk06.xml.sh            \
-  run-pixelize.xml.sh                    \
-  run-opencl-pixelize.xml.sh             \
-  run-reflect.xml.sh                     \
-  run-opencl-reflect.xml.sh              \
-  run-reflect2.xml.sh                    \
-  run-opencl-reflect2.xml.sh             \
-  run-reinhard05.xml.sh                  \
-  run-opencl-reinhard05.xml.sh           \
-  run-rgb-params.xml.sh                  \
-  run-opencl-rgb-params.xml.sh           \
-  run-rotate.xml.sh                      \
-  run-opencl-rotate.xml.sh               \
-  run-simple-scale.xml.sh                \
-  run-opencl-simple-scale.xml.sh         \
-  run-softglow.xml.sh                    \
-  run-opencl-softglow.xml.sh             \
-  run-transform.xml.sh                   \
-  run-opencl-transform.xml.sh            \
-  run-rectangles.xml.sh                  \
-  run-opencl-rectangles.xml.sh
+  affine-with-negative.xml        \
+  checkerboard.xml                \
+  clones.xml                      \
+  colors.xml                      \
+  composite-transform.xml         \
+  edge-laplace.xml                \
+  edge-sobel.xml                  \
+  fattal02.xml                    \
+  gamma.xml                       \
+  hdr-color.xml                   \
+  mantiuk06.xml                   \
+  pixelize.xml                    \
+  reflect.xml                     \
+  reflect2.xml                    \
+  reinhard05.xml                  \
+  rgb-params.xml                  \
+  rotate.xml                      \
+  simple-scale.xml                \
+  softglow.xml                    \
+  transform.xml                   \
+  rectangles.xml
+
+  #run-matting-global.xml.sh
+  #run-matting-global-big.xml.sh
 
 NO_OPENCL_TESTS = \
-  run-rgbe-load.xml.sh                 \
-  run-rgbe-save.xml.sh                 \
-  run-pnm-raw-load.xml.sh              \
-  run-pnm-ascii-load.xml.sh
-
-  #run-matting-global.xml.sh           
-  #run-matting-global-big.xml.sh     
-
-TESTS += $(NO_OPENCL_TESTS)
+  rgbe-load.xml                 \
+  rgbe-save.xml                 \
+  pnm-raw-load.xml              \
+  pnm-ascii-load.xml
 
 # Conditional tests
 if HAVE_JASPER
-TESTS += run-jp2-load.xml.sh
+NO_OPENCL_TESTS += jp2-load.xml
 endif
 if HAVE_UMFPACK
-TESTS += run-matting-levin.xml.sh
+TESTS += matting-levin.xml
 endif
 
-
-
-# Create a separate executable script for each composition test to run
-opencl_test_to_xml = $(abs_srcdir)/$(subst $(testsuffix),,$(subst $(opencl_testprefix),,$(1)))
-opencl_test_to_ref = $(wildcard $(abs_srcdir)/reference/$(basename $(notdir $(call 
opencl_test_to_xml,$(1)))).*)
-opencl_test_to_out = $(abs_builddir)/output/opencl-$(notdir $(call opencl_test_to_ref,$(1)))
-$(opencl_testprefix)%.xml.sh: Makefile.am $(call opencl_test_to_xml,$@) $(call opencl_test_to_ref,$@)
-       @xml_file=$(call opencl_test_to_xml,$@)                            ;\
-       ref_img=$(call opencl_test_to_ref,$@)                              ;\
-       out_img=$(call opencl_test_to_out,$@)                              ;\
-       echo "#!/bin/bash" > $@                                            ;\
-       echo "if [ \"x\$$GEGL_TEST_HAVE_OPENCL\" = \"xno\" ]; then exit 77; fi" > $@ ;\
-       echo "mkdir -p $(abs_builddir)/output" >> $@                       ;\
-       echo "if [ -a $$out_img ]; then rm $$out_img ; fi" >> $@           ;\
-       echo "$(builddir_gegl) $$xml_file -o $$out_img" >> $@              ;\
-       echo "GEGL_USE_OPENCL=no $(builddir_img_cmp) $$ref_img $$out_img" >> $@ ;\
-       chmod +x $@
-
-test_to_xml = $(abs_srcdir)/$(subst $(testsuffix),,$(subst $(testprefix),,$(1)))
-test_to_ref = $(wildcard $(abs_srcdir)/reference/$(basename $(notdir $(call test_to_xml,$(1)))).*)
-test_to_out = $(abs_builddir)/output/$(notdir $(call test_to_ref,$(1)))
-$(testprefix)%.xml.sh: Makefile.am $(call test_to_xml,$@) $(call test_to_ref,$@)
-       @xml_file=$(call test_to_xml,$@)                                   ;\
-       ref_img=$(call test_to_ref,$@)                                     ;\
-       out_img=$(call test_to_out,$@)                                     ;\
-       echo "#!/bin/bash" > $@                                            ;\
-       echo "mkdir -p $(abs_builddir)/output" >> $@                       ;\
-       echo "if [ -a $$out_img ]; then rm $$out_img ; fi" >> $@           ;\
-       echo "GEGL_USE_OPENCL=no $(builddir_gegl) $$xml_file -o $$out_img" >> $@  ;\
-       echo "GEGL_USE_OPENCL=no $(builddir_img_cmp) $$ref_img $$out_img" >> $@   ;\
-       chmod +x $@
+check-TESTS: $(TESTS)
+       $(PYTHON) $(srcdir)/run-compositions.py \
+         --build-dir=$(top_builddir) --src-dir=$(top_srcdir) --xml-dir=$(srcdir) \
+         $(TESTS)
+       $(PYTHON) $(srcdir)/run-compositions.py --without-opencl \
+         --build-dir=$(top_builddir) --src-dir=$(top_srcdir) --xml-dir=$(srcdir) \
+         $(NO_OPENCL_TESTS)
 
 clean-local:
-       rm -f $(testprefix)*.xml$(testsuffix) output/*
+       rm -f output/*
diff --git a/tests/compositions/run-compositions.py b/tests/compositions/run-compositions.py
new file mode 100755
index 0000000..2e726ae
--- /dev/null
+++ b/tests/compositions/run-compositions.py
@@ -0,0 +1,190 @@
+#!/usr/bin/env python
+from __future__ import print_function
+
+import os
+import sys
+import argparse
+import subprocess
+
+from glob import glob
+from pprint import pprint
+
+if sys.stdout.isatty() and "TERM" in os.environ:
+  blue  = "\033[1;34m"
+  green = "\033[1;32m"
+  red   = "\033[1;31m"
+  end   = "\033[0m"
+  PASS_STR = green + "PASS" + end
+  FAIL_STR = red + "FAIL" + end
+  SKIP_STR = blue + "SKIP" + end
+else:
+  PASS_STR = "PASS"
+  FAIL_STR = "FAIL"
+  SKIP_STR = "SKIP"
+
+VERBOSE = False
+if ("V" in os.environ and os.environ["V"] != "0") or \
+   ("VERBOSE" in os.environ and os.environ["VERBOSE"] != "0"):
+  VERBOSE = True
+
+class Context():
+  def __init__(self):
+    self.src_dir   = os.path.realpath(os.path.join(os.path.dirname(__file__), "..", ".."))
+    self.build_dir = self.src_dir
+
+  def prep(self):
+    self.fail_count = 0
+    self.pass_count = 0
+    self.skip_count = 0
+
+    if sys.platform == "win32":
+      exe_suffix = ".exe"
+    else:
+      exe_suffix = ""
+
+    self.gegl_bin = os.path.join(self.build_dir, "bin", "gegl" + exe_suffix)
+    self.img_cmp_bin = os.path.join(self.build_dir, "tools", "img_cmp" + exe_suffix)
+    self.detect_opencl_bin = os.path.join(self.build_dir, "tools", "detect_opencl" + exe_suffix)
+
+    for prog in [self.gegl_bin, self.img_cmp_bin, self.detect_opencl_bin]:
+      if not os.path.isfile(prog):
+        raise RuntimeError("Could not locate required file: %s" % prog)
+
+    FNULL = open(os.devnull, 'w')
+    self.opencl_available = not subprocess.call(self.detect_opencl_bin, stdout=FNULL)
+
+    self.ref_dir = os.path.join(self.src_dir, "tests", "compositions", "reference")
+    self.output_dir = os.path.join(self.build_dir, "tests", "compositions", "output")
+
+    if not os.path.isdir(self.output_dir):
+      os.makedirs(self.output_dir)
+
+  def find_reference_image(self, test_name):
+    images = glob(os.path.join(self.ref_dir, test_name + ".*"))
+    if len(images) == 1:
+      return os.path.basename(images[0])
+    elif len(images) == 0:
+      return test_name + ".png"
+    else:
+      raise RuntimeError("Ambiguous reference image for %s" % test_name)
+
+  def run_xml_test(self, test_xml_filename, use_opencl):
+    result_name_str = "%s" % os.path.basename(test_xml_filename)
+
+    if use_opencl:
+      result_name_str = "%s (OpenCL)" % os.path.basename(test_xml_filename)
+
+    if use_opencl and not self.opencl_available:
+      print(SKIP_STR, result_name_str)
+      self.skip_count += 1
+      return True
+
+    test_env = os.environ.copy()
+
+    if not use_opencl:
+      test_env["GEGL_USE_OPENCL"] = "no"
+    test_env["GEGL_SWAP"] = "RAM"
+    test_env["GEGL_PATH"] = os.path.join(self.build_dir, "operations")
+    img_cmp_env = test_env.copy()
+    img_cmp_env["GEGL_USE_OPENCL"] = "no"
+
+    test_name = os.path.basename(test_xml_filename)
+    if test_name.endswith(".xml"):
+      test_name = test_name[:-4]
+    else:
+      print (FAIL_STR, "Bad test name: %s" % test_name)
+      self.fail_count += 1
+      return False
+
+    out_image_name = self.find_reference_image(test_name)
+
+    if use_opencl:
+      out_image_path = os.path.join(self.output_dir, "opencl-" + out_image_name)
+    else:
+      out_image_path = os.path.join(self.output_dir, out_image_name)
+
+    ref_image_path = os.path.join(self.ref_dir, out_image_name)
+
+    if os.path.isfile(out_image_path):
+      os.remove(out_image_path)
+
+    xml_graph_path = os.path.realpath(test_xml_filename)
+
+    try:
+      if VERBOSE:
+        print(" ".join([self.gegl_bin, xml_graph_path, "-o", out_image_path]))
+      subprocess.check_call([self.gegl_bin, xml_graph_path, "-o", out_image_path], env=test_env)
+
+      if VERBOSE:
+        print(" ".join([self.img_cmp_bin, ref_image_path, out_image_path]))
+      subprocess.check_call([self.img_cmp_bin, ref_image_path, out_image_path], env=img_cmp_env)
+    except KeyboardInterrupt:
+      raise
+    except subprocess.CalledProcessError:
+      print (FAIL_STR, result_name_str)
+      self.fail_count += 1
+      return False
+    print (PASS_STR, result_name_str)
+    self.pass_count += 1
+    return True
+
+def main():
+  parser = argparse.ArgumentParser()
+  parser.add_argument("--without-opencl",
+                      action="store_true")
+  parser.add_argument("--xml-dir")
+  parser.add_argument("--build-dir")
+  parser.add_argument("--src-dir")
+  parser.add_argument("FILES",
+                      nargs="*")
+# Use argparse to set --without-opencl --output-dir= --build-dir= --verbose
+
+  args = parser.parse_args()
+
+  context = Context()
+
+  if args.src_dir:
+    context.src_dir = os.path.realpath(args.src_dir)
+
+  if args.build_dir:
+    context.build_dir = os.path.realpath(args.build_dir)
+
+  run_opencl_tests = not args.without_opencl
+
+  tests = args.FILES
+
+  if not tests:
+    sys.exit(0)
+
+  context.prep()
+
+  xml_dir = None
+  if args.xml_dir:
+    xml_dir = os.path.join(context.src_dir, "tests", "compositions")
+
+  for testfile in tests:
+    if xml_dir:
+      testfile = os.path.join(xml_dir, testfile)
+
+    context.run_xml_test(testfile, False)
+
+    sys.stdout.flush() # Keep our ouput in sync with subprocess if redirected
+
+    if run_opencl_tests:
+      context.run_xml_test(testfile, True)
+
+    sys.stdout.flush() # Keep our ouput in sync with subprocess if redirected
+
+  print ("=== Test Results ===")
+  print (" tests passed:  %d" % context.pass_count)
+  print (" tests skipped: %d" % context.skip_count)
+  print (" tests failed:  %d" % context.fail_count)
+
+  if context.fail_count == 0:
+    print ("======  %s  ======" % PASS_STR)
+    sys.exit(0)
+  else:
+    print ("======  %s  ======" % FAIL_STR)
+    sys.exit(1)
+
+main()
\ No newline at end of file


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]