tests: test find_source() with unittest
authorMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Mon, 13 Jan 2014 16:39:05 +0000 (18:39 +0200)
committerMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Tue, 3 Mar 2015 08:07:46 +0000 (10:07 +0200)
Change doctests to unittests in order to have more flexibility in
testing.

Signed-off-by: Markus Lehtonen <markus.lehtonen@linux.intel.com>
gbp/scripts/import_orig.py
tests/test_import_orig.py [new file with mode: 0644]

index d403e9dc57d53031721be6e543b179ad0ff3757c..d6715dfbaa8d14bd3d2dcd10d90a5b4fd2bcfc7d 100644 (file)
@@ -128,21 +128,6 @@ def find_source(use_uscan, args):
     @return: upstream source filename or None if nothing to import
     @rtype: string
     @raise GbpError: raised on all detected errors
-
-    >>> find_source(False, ['too', 'much'])
-    Traceback (most recent call last):
-    ...
-    GbpError: More than one archive specified. Try --help.
-    >>> find_source(False, [])
-    Traceback (most recent call last):
-    ...
-    GbpError: No archive to import specified. Try --help.
-    >>> find_source(True, ['tarball'])
-    Traceback (most recent call last):
-    ...
-    GbpError: you can't pass both --uscan and a filename.
-    >>> find_source(False, ['tarball']).path
-    'tarball'
     """
     if use_uscan:
         if args:
diff --git a/tests/test_import_orig.py b/tests/test_import_orig.py
new file mode 100644 (file)
index 0000000..ce697c0
--- /dev/null
@@ -0,0 +1,64 @@
+# vim: set fileencoding=utf-8 :
+#
+# (C) 2013 Intel Corporation <markus.lehtonen@linux.intel.com>
+#    This program is free software; you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation; either version 2 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program; if not, write to the Free Software
+#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+"""Test import-orig functions"""
+from . import context
+
+import os
+import tarfile
+import unittest
+
+from gbp.errors import GbpError
+from gbp.scripts.import_orig import find_source
+
+
+class TestImportOrigBase(unittest.TestCase):
+    """Base class for handling context"""
+    @classmethod
+    def setup_class(cls):
+        """Class set-up, run only once"""
+        cls._tmpdir = str(context.new_tmpdir(__name__))
+
+    @classmethod
+    def teardown_class(cls):
+        """Class teardown, run only once"""
+        context.teardown()
+
+
+class TestFindSource(TestImportOrigBase):
+    """Test the Debian-specific find_source() function"""
+
+    def test_failure(self):
+        """Test failure modes"""
+        with self.assertRaisesRegexp(GbpError,
+                                     "More than one archive specified"):
+            find_source(False, ['too', 'much'])
+
+        with self.assertRaisesRegexp(GbpError,
+                                     "No archive to import specified"):
+            find_source(False, [])
+
+        with self.assertRaisesRegexp(GbpError,
+                                "you can't pass both --uscan and a filename"):
+            find_source(True, ['tarball'])
+
+    def test_success(self):
+        """Successfully get source archive"""
+        tar_fn = 'tarball.tar'
+        # Create dummy (empty) tarball
+        tarfile.open(tar_fn, 'w' ).close()
+        self.assertEqual(os.path.abspath(tar_fn),
+                         find_source(False, [tar_fn]).path)