Mercurial > treepkg
diff test/test_builder.py @ 170:bfcb2bbf9a52
Move the pbuilder initialization code from bin/initpbuilder.py to
treepkg/builder.py to make it work again and add some tests.
author | Bernhard Herzog <bh@intevation.de> |
---|---|
date | Mon, 23 Jun 2008 13:21:08 +0000 |
parents | 68d829cac3ff |
children | c0ea6cbb0fd2 |
line wrap: on
line diff
--- a/test/test_builder.py Fri Jun 20 15:51:24 2008 +0000 +++ b/test/test_builder.py Mon Jun 23 13:21:08 2008 +0000 @@ -10,6 +10,7 @@ import sys import os import unittest +import StringIO from treepkg.builder import PBuilder from treepkg.run import call @@ -42,6 +43,102 @@ class TestPBuilder(PBuilderTests): + def test_init_pbuilder(self): + """Tests the PBuilder.init_pbuilder method.""" + basedir = self.create_temp_dir(self.id()) + pbuilderrc = os.path.join(basedir, "pbuilderrc") + builder = PBuilder(pbuilderrc, self.root_command) + old_stdout = sys.stdout + sys.stdout = captured_stdout = StringIO.StringIO() + try: + builder.init_pbuilder(distribution="etch", + mirrorsite="http://example.com/debian", + extramirrors=None) + finally: + sys.stdout = old_stdout + + # check whether the necessary directories were created + missing = [dirname for dirname in ["base", "build", "result", + "aptcache", "extra-pkg"] + if not os.path.isdir(os.path.join(basedir, dirname))] + if missing: + self.fail("init_pbuilder did not create these directories: %s" + % " ".join(missing)) + + # check the pbuilderrc. This test is a little too strict + # because it checks the exact contents of the file. Instread it + # should normalize the contents in some way and check that. + pbuilderrc_contents = ( + "# This file was automatically generated by initpbuilder.py.\n" + "# for the possible settings see \"man pbuilderrc\"\n" + "\n" + "BASETGZ=%(basedir)s/base/base.tgz\n" + "BUILDPLACE=%(basedir)s/build\n" + "USEPROC=yes\n" + "USEDEVPTS=yes\n" + "BUILDRESULT=%(basedir)s/result\n" + "DISTRIBUTION=etch\n" + "APTCACHE=%(basedir)s/base/aptcache\n" + "APTCACHEHARDLINK=yes\n" + "REMOVEPACKAGES=lilo\n" + "MIRRORSITE=\"http://example.com/debian\"\n" + "OTHERMIRROR=\"deb file://%(basedir)s/extra-pkg ./\"\n" + "BINDMOUNTS=\"%(basedir)s/extra-pkg\"\n" + "PKGNAME_LOGFILE=yes\n" % locals()) + self.checkFileContents(pbuilderrc, pbuilderrc_contents) + + # The Packages file is empty for now. + self.checkFileContents(os.path.join(basedir, "extra-pkg", "Packages"), + "") + # check the text written to stdout. This test is a little too + # strict because it checks the exact output. + self.assertEquals(captured_stdout.getvalue(), + "creating directory: '%(basedir_repr)s/base'\n" + "creating directory: '%(basedir_repr)s/build'\n" + "creating directory: '%(basedir_repr)s/result'\n" + "creating directory: '%(basedir_repr)s/aptcache'\n" + "creating directory: '%(basedir_repr)s/extra-pkg'\n" + "creating pbuilderrc: '%(basedir_repr)s/pbuilderrc'\n" + "turning the extra-pkg dir into a debian archive\n" + "running pbuilder create\n" + % dict(basedir_repr=repr(basedir)[1:-1])) + + def test_init_pbuilder_run_twice(self): + """Tests whether PBuilder.init_pbuilder prints an error when run twice. + """ + basedir = self.create_temp_dir(self.id()) + + # run it once + pbuilderrc = os.path.join(basedir, "pbuilderrc") + builder = PBuilder(pbuilderrc, self.root_command) + old_stdout = sys.stdout + sys.stdout = captured_stdout = StringIO.StringIO() + try: + builder.init_pbuilder(distribution="etch", + mirrorsite="http://example.com/debian", + extramirrors=None) + finally: + sys.stdout = old_stdout + + # running it again should not modify anything in the directory + # (which we don't check currently), it should print an error + # message and exit with exit code 1. + old_stderr = sys.stderr + sys.stderr = captured_stderr = StringIO.StringIO() + try: + try: + builder.init_pbuilder(distribution="etch", + mirrorsite="http://example.com/debian", + extramirrors=None) + except SystemExit, exc: + self.assertEquals(exc.code, 1) + finally: + sys.stderr = old_stderr + self.assertEquals("pbuilderrc %r already exists.\n" + % os.path.join(basedir, "pbuilderrc"), + captured_stderr.getvalue()) + + def test_build(self): """Tests the PBuilder.build method. The test checks whether the build method creates the binary_dir