From 6d71e2fcc1de774569a1e3810c7b6177af13e839 Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Fri, 10 Nov 2017 14:45:13 -0800 Subject: [PATCH] Fix the recipe version bumping Recipe should have its version bumped based on the version from the previous commit, and not be bumped on the first commit. Fix the code and the tests. --- src/pylorax/api/recipes.py | 26 ++++++++++++++++---------- tests/pylorax/test_recipes.py | 18 +++++++++--------- tests/pylorax/test_server.py | 4 ++-- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/pylorax/api/recipes.py b/src/pylorax/api/recipes.py index fe3c1685..3d99f798 100644 --- a/src/pylorax/api/recipes.py +++ b/src/pylorax/api/recipes.py @@ -74,22 +74,22 @@ class Recipe(dict): """Return the Recipe in TOML format""" return toml.dumps(self).encode("UTF-8") - def bump_version(self, new_version=None): + def bump_version(self, old_version=None): """semver recipe version number bump - :param new_version: An optional new version number - :type new_version: str + :param old_version: An optional old version number + :type old_version: str :returns: The new version number or None :rtype: str :raises: ValueError If neither have a version, 0.0.1 is returned - If there is no previous version the new version is checked and returned - If there is no new version, but there is a previous one, bump its patch level - If the previous and new versions are the same, bump the patch level + If there is no old version the new version is checked and returned + If there is no new version, but there is a old one, bump its patch level + If the old and new versions are the same, bump the patch level If they are different, check and return the new version """ - old_version = self.get("version") + new_version = self.get("version") if not new_version and not old_version: self["version"] = "0.0.1" @@ -385,7 +385,7 @@ def revert_file(repo, branch, filename, commit): message = "Recipe %s reverted to commit %s" % (filename, commit_hash) return repo.create_commit(ref, sig, sig, "UTF-8", message, tree, [parent_commit]) -def commit_recipe(repo, branch, recipe, new_version=None): +def commit_recipe(repo, branch, recipe): """Commit a recipe to a branch :param repo: Open repository @@ -398,7 +398,13 @@ def commit_recipe(repo, branch, recipe, new_version=None): :rtype: Git.OId :raises: Can raise errors from Ggit """ - recipe.bump_version(new_version) + try: + old_recipe = read_recipe_commit(repo, branch, recipe.filename) + old_version = old_recipe["version"] + except Exception: + old_version = None + + recipe.bump_version(old_version) recipe_toml = recipe.toml() message = "Recipe %s, version %s saved." % (recipe["name"], recipe["version"]) return write_commit(repo, branch, recipe.filename, message, recipe_toml) @@ -422,7 +428,7 @@ def commit_recipe_file(repo, branch, filename): except IOError: raise RecipeFileError - return commit_recipe(repo, branch, recipe, new_version=recipe["version"]) + return commit_recipe(repo, branch, recipe) def commit_recipe_directory(repo, branch, directory): """Commit all *.toml files from a directory, if they aren't already in git. diff --git a/tests/pylorax/test_recipes.py b/tests/pylorax/test_recipes.py index 39b2b643..186afe21 100644 --- a/tests/pylorax/test_recipes.py +++ b/tests/pylorax/test_recipes.py @@ -78,13 +78,13 @@ class BasicRecipeTest(unittest.TestCase): self.assertEqual(new_version, "0.0.1") # Original has a version, new does not - recipe = recipes.Recipe("test-recipe", "A recipe used for testing", "0.0.1", None, None) - new_version = recipe.bump_version(None) + recipe = recipes.Recipe("test-recipe", "A recipe used for testing", None, None, None) + new_version = recipe.bump_version("0.0.1") self.assertEqual(new_version, "0.0.2") # Original has no version, new does - recipe = recipes.Recipe("test-recipe", "A recipe used for testing", None, None, None) - new_version = recipe.bump_version("0.1.0") + recipe = recipes.Recipe("test-recipe", "A recipe used for testing", "0.1.0", None, None) + new_version = recipe.bump_version(None) self.assertEqual(new_version, "0.1.0") # New and Original are the same @@ -93,8 +93,8 @@ class BasicRecipeTest(unittest.TestCase): self.assertEqual(new_version, "0.0.2") # New is different from Original - recipe = recipes.Recipe("test-recipe", "A recipe used for testing", "0.0.1", None, None) - new_version = recipe.bump_version("0.1.1") + recipe = recipes.Recipe("test-recipe", "A recipe used for testing", "0.1.1", None, None) + new_version = recipe.bump_version("0.0.1") self.assertEqual(new_version, "0.1.1") @@ -120,14 +120,14 @@ class GitRecipesTest(unittest.TestCase): def test_02_commit_recipe(self): """Test committing a Recipe object""" recipe = recipes.Recipe("test-recipe", "A recipe used for testing", None, None, None) - oid = recipes.commit_recipe(self.repo, "master", recipe, "0.1.0") + oid = recipes.commit_recipe(self.repo, "master", recipe) self.assertNotEqual(oid, None) def test_03_list_recipe(self): """Test listing recipe commits""" commits = recipes.list_commits(self.repo, "master", "test-recipe.toml") self.assertEqual(len(commits), 1, "Wrong number of commits.") - self.assertEqual(commits[0].message, "Recipe test-recipe, version 0.1.0 saved.") + self.assertEqual(commits[0].message, "Recipe test-recipe, version 0.0.1 saved.") self.assertNotEqual(commits[0].timestamp, None, "Timestamp is None") self.assertEqual(len(commits[0].commit), 40, "Commit hash isn't 40 characters") self.assertEqual(commits[0].revision, None, "revision is not None") @@ -195,7 +195,7 @@ class GitRecipesTest(unittest.TestCase): """Test tagging a newer commit of a recipe""" recipe = recipes.read_recipe_commit(self.repo, "master", "http-server.toml") recipe["description"] = "A modified description" - oid = recipes.commit_recipe(self.repo, "master", recipe, "0.1.0") + oid = recipes.commit_recipe(self.repo, "master", recipe) self.assertNotEqual(oid, None) # Tag the new commit diff --git a/tests/pylorax/test_server.py b/tests/pylorax/test_server.py index 2d730d39..246f9eea 100644 --- a/tests/pylorax/test_server.py +++ b/tests/pylorax/test_server.py @@ -71,7 +71,7 @@ class ServerTestCase(unittest.TestCase): "packages": [{"name":"openssh-server", "version": "6.6.*"}, {"name": "rsync", "version": "3.0.*"}, {"name": "tmux", "version": "2.2"}], - "version": "0.0.2"}]} + "version": "0.0.1"}]} resp = self.server.get("/api/v0/recipes/info/http-server") data = json.loads(resp.data) self.assertEqual(data, info_dict_1) @@ -95,7 +95,7 @@ class ServerTestCase(unittest.TestCase): "packages": [{"name":"openssh-server", "version": "6.6.*"}, {"name": "rsync", "version": "3.0.*"}, {"name": "tmux", "version": "2.2"}], - "version": "0.0.2"}, + "version": "0.0.1"}, ]} resp = self.server.get("/api/v0/recipes/info/http-server,glusterfs") data = json.loads(resp.data)