From 4e959cbc8a27ebcc8dd6ec3643cedf5c1f56fb4c Mon Sep 17 00:00:00 2001
From: nhertz <nhertz@nrao.edu>
Date: Tue, 25 Aug 2020 15:26:53 -0600
Subject: [PATCH] Finished testing for all recipes. Added buildout recipe to
 automatically run tests on other buildout recipes (and itself)

---
 build/recipes/build_pkgs/test/__init__.py     |  0
 build/recipes/build_pkgs/test/conftest.py     | 12 ++++--
 .../build_pkgs/test/test_build_pkgs.py        | 37 +++++++++----------
 build/recipes/setup_to_meta/test/__init__.py  |  0
 build/recipes/setup_to_meta/test/conftest.py  |  6 +++
 .../setup_to_meta/test/test_setup_to_meta.py  | 15 +++++++-
 build/recipes/test_recipes/test/__init__.py   |  0
 .../test_recipes/test/test_test_recipes.py    |  2 +-
 build/recipes/test_recipes/test_recipes.py    | 21 +++++++++--
 9 files changed, 65 insertions(+), 28 deletions(-)
 delete mode 100644 build/recipes/build_pkgs/test/__init__.py
 delete mode 100644 build/recipes/setup_to_meta/test/__init__.py
 delete mode 100644 build/recipes/test_recipes/test/__init__.py

diff --git a/build/recipes/build_pkgs/test/__init__.py b/build/recipes/build_pkgs/test/__init__.py
deleted file mode 100644
index e69de29bb..000000000
diff --git a/build/recipes/build_pkgs/test/conftest.py b/build/recipes/build_pkgs/test/conftest.py
index 8d8263525..e98638c7a 100644
--- a/build/recipes/build_pkgs/test/conftest.py
+++ b/build/recipes/build_pkgs/test/conftest.py
@@ -3,8 +3,14 @@ import zc.buildout.testing
 
 @pytest.fixture(scope='module')
 def recipe():
-    from .. import setup_to_meta
+    """
+    pytest fixture that initializes zc.buildout objects for use in testing.
+    Initializes Buildout, Options, and Recipe objects.
+
+    :return: Initialized recipe object for build_pkgs
+    """
+    from .. import build_pkgs
     buildout = zc.buildout.testing.Buildout()
-    options = buildout.Options(buildout, 'gen_metadata', {'recipe': 'setup_to_meta'})
-    recipe = setup_to_meta.Recipe(buildout=buildout, name=None, options=options)
+    options = buildout.Options(buildout, 'build_pkgs', {'recipe': 'build_pkgs', 'name': 'null'})
+    recipe = build_pkgs.Recipe(buildout=buildout, name=None, options=options)
     return recipe
\ No newline at end of file
diff --git a/build/recipes/build_pkgs/test/test_build_pkgs.py b/build/recipes/build_pkgs/test/test_build_pkgs.py
index 79c2b3220..c0dfae66c 100644
--- a/build/recipes/build_pkgs/test/test_build_pkgs.py
+++ b/build/recipes/build_pkgs/test/test_build_pkgs.py
@@ -1,31 +1,30 @@
-import pytest
-from .. import setup_to_meta
-
-class TestSetupToMeta:
-    def test_del_substrings(self):
-        replaced = setup_to_meta.del_substrings('heallob, woarlcd', ['a', 'b', 'c'])
-        assert replaced == 'hello, world'
+import os
+from .. import build_pkgs
 
+class TestBuildPkgs:
     def test_get_names(self):
+        """
+        Test that build_pkgs correctly gets the package name from
+        :return:
+        """
         d = 'apps/cli/executables/null'
-        assert setup_to_meta.get_names([d]) == ['null']
+        assert build_pkgs.get_names([d]) == ['null']
 
     def test_get_dirs(self):
-        assert './apps/cli/executables/null' in setup_to_meta.get_dirs()
-
-    def test_get_outputs(self):
-        assert setup_to_meta.get_outputs(['null']) == ['build/metadata/null/meta.yaml']
+        """
+        Test that build_pkgs correctly finds and stores directory paths
+        of directories containing setup.py files.
+        """
+        assert './apps/cli/executables/null' in build_pkgs.get_dirs()
 
     def test_output(self, recipe):
         """
-        Test that metadata was successfully created and contains data.
-
-        Checking for 'package' is an arbitrary check for content that will always
-        occur in a correct recipe.
-        :param recipe: Fixture that initializes recipe class in setup_to_meta.py
+        Test that the package specified in the recipe has been built correctly.
         """
         created = recipe.install()
 
         for path in created:
-            with open(path, 'r') as f:
-                assert 'package' in f.read()
+            print(path)
+            if len(path) > 0:
+                assert path is not None, "conda build failed to build package"
+                assert os.path.exists(path)
diff --git a/build/recipes/setup_to_meta/test/__init__.py b/build/recipes/setup_to_meta/test/__init__.py
deleted file mode 100644
index e69de29bb..000000000
diff --git a/build/recipes/setup_to_meta/test/conftest.py b/build/recipes/setup_to_meta/test/conftest.py
index 8d8263525..2f6dfb21d 100644
--- a/build/recipes/setup_to_meta/test/conftest.py
+++ b/build/recipes/setup_to_meta/test/conftest.py
@@ -3,6 +3,12 @@ import zc.buildout.testing
 
 @pytest.fixture(scope='module')
 def recipe():
+    """
+    pytest fixture that initializes zc.buildout objects for use in testing.
+    Initializes Buildout, Options, and Recipe objects.
+
+    :return: Initialized recipe object for setup_to_meta
+    """
     from .. import setup_to_meta
     buildout = zc.buildout.testing.Buildout()
     options = buildout.Options(buildout, 'gen_metadata', {'recipe': 'setup_to_meta'})
diff --git a/build/recipes/setup_to_meta/test/test_setup_to_meta.py b/build/recipes/setup_to_meta/test/test_setup_to_meta.py
index 79c2b3220..dc6a201f9 100644
--- a/build/recipes/setup_to_meta/test/test_setup_to_meta.py
+++ b/build/recipes/setup_to_meta/test/test_setup_to_meta.py
@@ -1,19 +1,32 @@
-import pytest
 from .. import setup_to_meta
 
 class TestSetupToMeta:
     def test_del_substrings(self):
+        """
+        Test that del_substrings function properly deletes substrings from a given string
+        """
         replaced = setup_to_meta.del_substrings('heallob, woarlcd', ['a', 'b', 'c'])
         assert replaced == 'hello, world'
 
     def test_get_names(self):
+        """
+        Test that setup_to_meta correctly gets the package name from the package path
+        """
         d = 'apps/cli/executables/null'
         assert setup_to_meta.get_names([d]) == ['null']
 
     def test_get_dirs(self):
+        """
+        Test that setup_to_meta correctly finds and stores directory paths
+        of packages containing setup.py files
+        """
         assert './apps/cli/executables/null' in setup_to_meta.get_dirs()
 
     def test_get_outputs(self):
+        """
+        Test that tests that setup_to_meta correctly generates a list of output paths given
+        a list of package names.
+        """
         assert setup_to_meta.get_outputs(['null']) == ['build/metadata/null/meta.yaml']
 
     def test_output(self, recipe):
diff --git a/build/recipes/test_recipes/test/__init__.py b/build/recipes/test_recipes/test/__init__.py
deleted file mode 100644
index e69de29bb..000000000
diff --git a/build/recipes/test_recipes/test/test_test_recipes.py b/build/recipes/test_recipes/test/test_test_recipes.py
index 788ef9217..57034eefd 100644
--- a/build/recipes/test_recipes/test/test_test_recipes.py
+++ b/build/recipes/test_recipes/test/test_test_recipes.py
@@ -6,7 +6,7 @@ class TestRecipes:
         Test that test_recipes is able to successfully retrieve names and paths
         for all buildout recipes.
         """
-        recipe_names = ['setup_to_meta', 'build_packages', 'test_recipes']
+        recipe_names = ['setup_to_meta', 'build_pkgs', 'test_recipes']
         recipes = test_recipes.get_recipes()
         for recipe in recipe_names:
             assert recipe in recipes
diff --git a/build/recipes/test_recipes/test_recipes.py b/build/recipes/test_recipes/test_recipes.py
index a66e349ee..479cc0514 100644
--- a/build/recipes/test_recipes/test_recipes.py
+++ b/build/recipes/test_recipes/test_recipes.py
@@ -32,6 +32,16 @@ class Recipe:
     For more detailed information, see the link.
     http://www.buildout.org/en/latest/topics/writing-recipes.html
     """
+
+    def run_test(self, recipe):
+        """
+        Run test for given recipe.
+        :param recipe: Name of recipe to be run.
+        """
+        logger.debug(f"Running tests for recipe {recipe}...")
+        subprocess.run(['pytest', '-vv', '--log-level=DEBUG', '--showlocals',
+                        self.recipes[recipe]])
+
     def __init__(self, buildout, name, options):
         """
         Initializes fields needed for recipe.
@@ -52,7 +62,10 @@ class Recipe:
         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]])
+        if self.choice == 'all':
+            # Run tests for all recipes
+            for recipe in self.recipes:
+                self.run_test(recipe)
+        else:
+            if self.choice in self.recipes:
+                self.run_test(self.choice)
\ No newline at end of file
-- 
GitLab