[PATCH 09/20] tests: Ensure proper functioning of Commands.add_tag
Mathieu Bridon
bochecha at fedoraproject.org
Wed Oct 29 12:57:07 UTC 2014
The tests fail!
That's because they just found 1 bug in the code. :)
However, notice the game I'm playing with the EDITOR environment
variable. This is a gross hack, but it is made necessary by a bad
design decision in the pyrpkg API: a library should never do some
interactive stuff in subprocesses like that.
---
test/commands/__init__.py | 14 ++++
test/commands/test_add_tag.py | 156 ++++++++++++++++++++++++++++++++++++++++++
2 files changed, 170 insertions(+)
create mode 100644 test/commands/test_add_tag.py
diff --git a/test/commands/__init__.py b/test/commands/__init__.py
index e55545b..41d513f 100644
--- a/test/commands/__init__.py
+++ b/test/commands/__init__.py
@@ -74,3 +74,17 @@ class CommandTestCase(unittest.TestCase):
# Drop the clone
shutil.rmtree(cloneroot)
+
+ def get_tags(self, gitdir):
+ result = []
+
+ tags = subprocess.check_output(['git', 'tag', '-n1'], cwd=gitdir)
+
+ for line in tags.split('\n'):
+ if not line:
+ continue
+
+ tokens = [x for x in line.split() if x]
+ result.append([tokens[0], ' '.join(tokens[1:])])
+
+ return result
diff --git a/test/commands/test_add_tag.py b/test/commands/test_add_tag.py
new file mode 100644
index 0000000..f780c40
--- /dev/null
+++ b/test/commands/test_add_tag.py
@@ -0,0 +1,156 @@
+import os
+
+from . import CommandTestCase
+
+
+class CommandAddTagTestCase(CommandTestCase):
+ def setUp(self):
+ super(CommandAddTagTestCase, self).setUp()
+ self.old_editor = os.environ['EDITOR']
+
+ def tearDown(self):
+ os.environ['EDITOR'] = self.old_editor
+ super(CommandAddTagTestCase, self).tearDown()
+
+ def test_add_tag(self):
+ self.make_new_git(self.module)
+
+ tag = 'v1.0'
+ message = 'This is a 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
+
+ # `git tag` will call $EDITOR to ask the user to write a message
+ os.environ['EDITOR'] = ('/usr/bin/python -c "import sys; '
+ 'open(sys.argv[1], \'w\').write(\'%s\')"'
+ % message)
+
+ cmd.add_tag(tag)
+
+ self.assertEqual(self.get_tags(moduledir), [[tag, message]])
+
+ def test_add_tag_with_message(self):
+ self.make_new_git(self.module)
+
+ tag = 'v1.0'
+ message = 'This is a 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
+
+ cmd.add_tag(tag, message=message)
+
+ self.assertEqual(self.get_tags(moduledir), [[tag, message]])
+
+ def test_add_tag_with_message_from_file(self):
+ self.make_new_git(self.module)
+
+ tag = 'v1.0'
+ message = 'This is a 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
+
+ message_file = os.path.join(moduledir, 'tag_message')
+
+ with open(message_file, 'w') as f:
+ f.write(message)
+
+ cmd.add_tag(tag, file=message_file)
+
+ self.assertEqual(self.get_tags(moduledir), [[tag, message]])
+
+ def test_add_tag_fails_with_existing(self):
+ self.make_new_git(self.module)
+
+ tag = 'v1.0'
+ message = 'This is a 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
+
+ cmd.add_tag(tag, message=message)
+
+ # Now add the same tag again
+ with self.assertRaises(pyrpkg.rpkgError):
+ cmd.add_tag(tag, message='No, THIS is a release')
+
+ def test_add_tag_force_replace_existing(self):
+ self.make_new_git(self.module)
+
+ tag = 'v1.0'
+ message = 'This is a 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
+
+ cmd.add_tag(tag, message=message)
+
+ # Now add the same tag again by force
+ newmessage = 'No, THIS is a release'
+ cmd.add_tag(tag, message=newmessage, force=True)
+
+ self.assertEqual(self.get_tags(moduledir), [[tag, newmessage]])
+
+ def test_add_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)
+
+ self.assertEqual(self.get_tags(moduledir), tags)
--
2.1.0
More information about the rel-eng
mailing list