From 9951a251ad40ae25b2ec8c4ccf8e6f69f3e5c026 Mon Sep 17 00:00:00 2001
From: nhertz <nhertz@nrao.edu>
Date: Mon, 24 Aug 2020 15:59:43 -0600
Subject: [PATCH] Added new buildout recipe for testing buildout recipes.

---
 build/recipes/test_recipes/setup.py        |  8 +++
 build/recipes/test_recipes/test_recipes.py | 58 ++++++++++++++++++++++
 buildout.cfg                               |  6 ++-
 3 files changed, 71 insertions(+), 1 deletion(-)
 create mode 100644 build/recipes/test_recipes/setup.py
 create mode 100644 build/recipes/test_recipes/test_recipes.py

diff --git a/build/recipes/test_recipes/setup.py b/build/recipes/test_recipes/setup.py
new file mode 100644
index 000000000..c97dae42a
--- /dev/null
+++ b/build/recipes/test_recipes/setup.py
@@ -0,0 +1,8 @@
+from setuptools import setup
+
+setup(
+    name='test_recipes',
+    version='0.1',
+    py_modules = ['test_recipes'],
+    entry_points = {"zc.buildout": ["default=test_recipes:Recipe"]},
+)
\ No newline at end of file
diff --git a/build/recipes/test_recipes/test_recipes.py b/build/recipes/test_recipes/test_recipes.py
new file mode 100644
index 000000000..a66e349ee
--- /dev/null
+++ b/build/recipes/test_recipes/test_recipes.py
@@ -0,0 +1,58 @@
+import subprocess
+import logging
+
+logger = logging.getLogger("buildout/test_recipes")
+
+def get_recipes():
+    """
+    Get all currently installed buildout recipes (including this one!)
+    :return: Dictionary with format {recipe_name: recipe_path_from_root,}
+    """
+    logger.debug("Getting list of recipes...")
+    recipes = {}
+    find = subprocess.run(['find', './build/recipes', '-name', 'setup.py'],
+                           stdout=subprocess.PIPE)
+    paths = find.stdout.decode('utf-8').split('\n')
+    dirs = []
+    names = []
+    for p in paths:
+        if len(p) > 1:
+            # Exclude empty result from find
+            dirs.append(p.replace('/setup.py', ''))
+            names.append(p.split('/')[-2])
+    for d, n in zip(dirs, names):
+        recipes[n] = d
+    logger.debug("Done getting recipes.")
+
+    return recipes
+
+class Recipe:
+    """
+    Buildout Recipe class.
+    For more detailed information, see the link.
+    http://www.buildout.org/en/latest/topics/writing-recipes.html
+    """
+    def __init__(self, buildout, name, options):
+        """
+        Initializes fields needed for recipe.
+        :param buildout: (Boilerplate) Dictionary of options from buildout section
+        of buildout.cfg
+        :param name: (Boilerplate) Name of section that uses this recipe.
+        :param options: (Boilerplate) Options of section that uses this recipe.
+        """
+        self.name = name
+        self.options = options
+        self.recipes = get_recipes()
+        self.choice = options['name']
+
+    def install(self):
+        """
+        Install method that runs when recipe has components it needs to install.
+
+        In this case, nothing is "installed" per se.
+        :return: Paths to files, as strings, created by the recipe.
+        """
+        if self.choice in self.recipes:
+            logger.debug(f"Running tests for recipe {self.choice}...")
+            subprocess.run(['pytest', '-vv', '--log-level=DEBUG', '--showlocals',
+                            self.recipes[self.choice]])
diff --git a/buildout.cfg b/buildout.cfg
index 5ae4f1cc3..9e37a454e 100644
--- a/buildout.cfg
+++ b/buildout.cfg
@@ -3,7 +3,11 @@
 # e.g. buildout parts=build_pkgs will specify build_pkgs
 # as a part that should be installed
 [buildout]
-develop = build/recipes/setup_to_meta build/recipes/build_pkgs
+develop = build/recipes/setup_to_meta build/recipes/build_pkgs build/recipes/test_recipes
+
+[test]
+recipe = test_recipes
+name = ${buildout:name}
 
 # Section for building internal tools using conda
 # Depends on gen_metadata
-- 
GitLab