[PATCH 15/20] tests: Ensure functioning of Commands.list_tag

Mathieu Bridon bochecha at fedoraproject.org
Wed Oct 29 12:57:13 UTC 2014


From: Mathieu Bridon <bochecha at daitauha.fr>

This is pretty horrible though. The library function is just a poorly
thought-out API. A proper API would return the results, and let the
caller handle them (for example, printing them to stdout, or in a web
interface, or...).

However, the rpkg API instead directly prints it to stdout, which not
only prevents consumers from easily doing something nice with it, but
also prevents **unit tests** (!!) from easily verifying the behaviour of
the function.

As a result, if we want unit tests, we need to do something pretty
horrible: hijack the stdout of the function, and parse it.
---
 test/commands/__init__.py      |  20 ++++++
 test/commands/test_list_tag.py | 159 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 179 insertions(+)
 create mode 100644 test/commands/test_list_tag.py

diff --git a/test/commands/__init__.py b/test/commands/__init__.py
index 41d513f..d199f31 100644
--- a/test/commands/__init__.py
+++ b/test/commands/__init__.py
@@ -1,6 +1,7 @@
 import os
 import shutil
 import subprocess
+import sys
 import tempfile
 import unittest
 
@@ -88,3 +89,22 @@ class CommandTestCase(unittest.TestCase):
             result.append([tokens[0], ' '.join(tokens[1:])])
 
         return result
+
+    def hijack_stdout(self):
+        class cm(object):
+            def __enter__(self):
+                from cStringIO import StringIO
+
+                self.old_stdout = sys.stdout
+                self.out = StringIO()
+                sys.stdout = self.out
+
+                return self.out
+
+            def __exit__(self, *args):
+                sys.stdout.flush()
+                sys.stdout = self.old_stdout
+
+                self.out.seek(0)
+
+        return cm()
diff --git a/test/commands/test_list_tag.py b/test/commands/test_list_tag.py
new file mode 100644
index 0000000..8135f5e
--- /dev/null
+++ b/test/commands/test_list_tag.py
@@ -0,0 +1,159 @@
+import os
+
+from . import CommandTestCase
+
+
+class CommandListTagTestCase(CommandTestCase):
+    def test_list_tag_no_tags(self):
+        self.make_new_git(self.module)
+
+        import pyrpkg
+        cmd = pyrpkg.Commands(self.path, self.lookaside, self.lookasidehash,
+                              self.lookaside_cgi, self.gitbaseurl,
+                              self.anongiturl, self.branchre, self.kojiconfig,
+                              self.build_client, self.user, self.dist,
+                              self.target, self.quiet)
+        cmd.clone(self.module, anon=True)
+
+        moduledir = os.path.join(self.path, self.module)
+        cmd.path = moduledir
+
+        with self.hijack_stdout() as out:
+            cmd.list_tag()
+
+        self.assertEqual(out.read().strip(), '')
+
+    def test_list_tag_many(self):
+        self.make_new_git(self.module)
+
+        tags = [['v1.0', 'This is a release'],
+                ['v2.0', 'This is another release']]
+
+        import pyrpkg
+        cmd = pyrpkg.Commands(self.path, self.lookaside, self.lookasidehash,
+                              self.lookaside_cgi, self.gitbaseurl,
+                              self.anongiturl, self.branchre, self.kojiconfig,
+                              self.build_client, self.user, self.dist,
+                              self.target, self.quiet)
+        cmd.clone(self.module, anon=True)
+
+        moduledir = os.path.join(self.path, self.module)
+        cmd.path = moduledir
+
+        for tag, message in tags:
+            cmd.add_tag(tag, message=message)
+
+        with self.hijack_stdout() as out:
+            cmd.list_tag()
+
+        result = out.read().strip().split('\n')
+
+        self.assertEqual(result, [t for (t, m) in tags])
+
+    def test_list_tag_specific(self):
+        self.make_new_git(self.module)
+
+        tags = [['v1.0', 'This is a release'],
+                ['v2.0', 'This is another release']]
+
+        import pyrpkg
+        cmd = pyrpkg.Commands(self.path, self.lookaside, self.lookasidehash,
+                              self.lookaside_cgi, self.gitbaseurl,
+                              self.anongiturl, self.branchre, self.kojiconfig,
+                              self.build_client, self.user, self.dist,
+                              self.target, self.quiet)
+        cmd.clone(self.module, anon=True)
+
+        moduledir = os.path.join(self.path, self.module)
+        cmd.path = moduledir
+
+        for tag, message in tags:
+            cmd.add_tag(tag, message=message)
+
+        with self.hijack_stdout() as out:
+            cmd.list_tag(tagname='v1.0')
+
+        result = out.read().strip().split('\n')
+
+        self.assertEqual(result, ['v1.0'])
+
+    def test_list_tag_inexistent(self):
+        self.make_new_git(self.module)
+
+        tags = [['v1.0', 'This is a release'],
+                ['v2.0', 'This is another release']]
+
+        import pyrpkg
+        cmd = pyrpkg.Commands(self.path, self.lookaside, self.lookasidehash,
+                              self.lookaside_cgi, self.gitbaseurl,
+                              self.anongiturl, self.branchre, self.kojiconfig,
+                              self.build_client, self.user, self.dist,
+                              self.target, self.quiet)
+        cmd.clone(self.module, anon=True)
+
+        moduledir = os.path.join(self.path, self.module)
+        cmd.path = moduledir
+
+        for tag, message in tags:
+            cmd.add_tag(tag, message=message)
+
+        with self.hijack_stdout() as out:
+            cmd.list_tag(tagname='v1.1')
+
+        result = out.read().strip().split('\n')
+
+        self.assertEqual(result, [''])
+
+    def test_list_tag_glob(self):
+        self.make_new_git(self.module)
+
+        tags = [['v1.0', 'This is a release'],
+                ['v2.0', 'This is another release']]
+
+        import pyrpkg
+        cmd = pyrpkg.Commands(self.path, self.lookaside, self.lookasidehash,
+                              self.lookaside_cgi, self.gitbaseurl,
+                              self.anongiturl, self.branchre, self.kojiconfig,
+                              self.build_client, self.user, self.dist,
+                              self.target, self.quiet)
+        cmd.clone(self.module, anon=True)
+
+        moduledir = os.path.join(self.path, self.module)
+        cmd.path = moduledir
+
+        for tag, message in tags:
+            cmd.add_tag(tag, message=message)
+
+        with self.hijack_stdout() as out:
+            cmd.list_tag(tagname='v1*')
+
+        result = out.read().strip().split('\n')
+
+        self.assertEqual(result, ['v1.0'])
+
+    def test_list_tag_wildcard(self):
+        self.make_new_git(self.module)
+
+        tags = [['v1.0', 'This is a release'],
+                ['v2.0', 'This is another release']]
+
+        import pyrpkg
+        cmd = pyrpkg.Commands(self.path, self.lookaside, self.lookasidehash,
+                              self.lookaside_cgi, self.gitbaseurl,
+                              self.anongiturl, self.branchre, self.kojiconfig,
+                              self.build_client, self.user, self.dist,
+                              self.target, self.quiet)
+        cmd.clone(self.module, anon=True)
+
+        moduledir = os.path.join(self.path, self.module)
+        cmd.path = moduledir
+
+        for tag, message in tags:
+            cmd.add_tag(tag, message=message)
+
+        with self.hijack_stdout() as out:
+            cmd.list_tag(tagname='*')
+
+        result = out.read().strip().split('\n')
+
+        self.assertEqual(result, [t for (t, m) in tags])
-- 
2.1.0



More information about the rel-eng mailing list