From 214d35df5e26ab86cd26c3877d8afc1e7f6823a2 Mon Sep 17 00:00:00 2001 From: "Mikhail f. Shiryaev" Date: Mon, 21 Feb 2022 10:33:44 +0100 Subject: [PATCH] Add unit tests for git helper --- tests/ci/git_test.py | 68 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 tests/ci/git_test.py diff --git a/tests/ci/git_test.py b/tests/ci/git_test.py new file mode 100644 index 00000000000..785c9b62cce --- /dev/null +++ b/tests/ci/git_test.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python + +from unittest.mock import patch +import os.path as p +import unittest + +from git_helper import Git, Runner + + +class TestRunner(unittest.TestCase): + def test_init(self): + runner = Runner() + self.assertEqual(runner.cwd, p.realpath(p.dirname(__file__))) + runner = Runner("/") + self.assertEqual(runner.cwd, "/") + + def test_run(self): + runner = Runner() + output = runner.run("echo 1") + self.assertEqual(output, "1") + + +class TestGit(unittest.TestCase): + def setUp(self): + """we use dummy git object""" + run_patcher = patch("git_helper.Runner.run", return_value="") + self.run_mock = run_patcher.start() + self.addCleanup(run_patcher.stop) + update_patcher = patch("git_helper.Git.update") + update_mock = update_patcher.start() + self.addCleanup(update_patcher.stop) + self.git = Git() + update_mock.assert_called_once() + self.run_mock.assert_called_once() + self.git.new_branch = "NEW_BRANCH_NAME" + self.git.new_tag = "v21.12.333.22222-stable" + self.git.branch = "old_branch" + self.git.sha = "" + self.git.sha_short = "" + self.git.latest_tag = "" + self.git.description = "" + self.git.commits_since_tag = 0 + + def test_tags(self): + self.git.new_tag = "v21.12.333.22222-stable" + self.git.latest_tag = "v21.12.333.22222-stable" + for tag_attr in ("new_tag", "latest_tag"): + self.assertEqual(getattr(self.git, tag_attr), "v21.12.333.22222-stable") + setattr(self.git, tag_attr, "") + self.assertEqual(getattr(self.git, tag_attr), "") + for tag in ( + "v21.12.333-stable", + "v21.12.333-prestable", + "21.12.333.22222-stable", + "v21.12.333.22222-production", + ): + with self.assertRaises(Exception): + setattr(self.git, tag_attr, tag) + + def test_tweak(self): + self.git.commits_since_tag = 0 + self.assertEqual(self.git.tweak, 1) + self.git.commits_since_tag = 2 + self.assertEqual(self.git.tweak, 2) + self.git.latest_tag = "v21.12.333.22222-testing" + self.assertEqual(self.git.tweak, 22224) + self.git.commits_since_tag = 0 + self.assertEqual(self.git.tweak, 22222)