From f109b4bd1ea72a3603baebdf1d6d4c6e1b4189ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eray=20=C3=96zkural?= Date: Wed, 26 Oct 2005 23:28:14 +0000 Subject: [PATCH] * use a testcase class for proper API init/shutdown, if that doesn't work, nothing will anyway :) - this new class uses the tmp dir * make the test cases that access pisi dirs, etc. derive from the new testcase class * a silly Option class in config module for those who want to give options from API. We should have default values there, too (it seems!) * several fixes in tests, remove old style config accesses etc. --- pisi.e3p | 16 ++++++++++++---- pisi/config.py | 12 ++++++++++-- tests/actionsapitests.py | 5 ++++- tests/archivetests.py | 15 +++------------ tests/configfiletests.py | 1 + tests/fetchertests.py | 5 +++-- tests/graphtests.py | 1 - tests/installdbtests.py | 10 ++-------- tests/metadatatests.py | 11 +++++------ tests/packagedbtests.py | 9 +++------ tests/packagetests.py | 9 +++++---- tests/sourcedbtests.py | 5 +++-- tests/specfilenewtests.py | 11 +++++++---- tests/specfiletests.py | 11 +++++++---- tests/testcase.py | 27 +++++++++++++++++++++++++++ tests/utiltests.py | 2 +- tests/xmlfiletests.py | 7 +++---- tools/fetchAll.py | 4 +--- 18 files changed, 97 insertions(+), 64 deletions(-) create mode 100644 tests/testcase.py diff --git a/pisi.e3p b/pisi.e3p index 80f9738d..91dbccae 100644 --- a/pisi.e3p +++ b/pisi.e3p @@ -1,7 +1,7 @@ - + Python @@ -364,6 +364,14 @@ tests specfilenewtests.py + + tests + ootests.py + + + tests + testcase.py + @@ -402,9 +410,9 @@ - - - + + + diff --git a/pisi/config.py b/pisi/config.py index 1e287100..1f49127e 100644 --- a/pisi/config.py +++ b/pisi/config.py @@ -30,10 +30,17 @@ from pisi.util import join_path as join class Error(pisi.Error): pass +class Options(object): + def __getattr__(self, name): + if not self.__dict__.has_key(name): + return None + else: + return self.__dict__[name] + class Config(object): """Config Singleton""" - def __init__(self, options = None): + def __init__(self, options = Options()): self.options = options self.values = ConfigurationFile("/etc/pisi/pisi.conf") @@ -56,7 +63,8 @@ class Config(object): dir = self.values.general.destinationdirectory import os.path if not os.path.exists(dir): - raise Error, _('Destination directory %s does not exist') % dir + ctx.ui.warning( _('Destination directory %s does not exist. Creating it.') % dir) + os.makedirs(dir) return dir def subdir(self, path): diff --git a/tests/actionsapitests.py b/tests/actionsapitests.py index dcb86358..c6536d1a 100644 --- a/tests/actionsapitests.py +++ b/tests/actionsapitests.py @@ -10,8 +10,11 @@ import unittest import zipfile -class ActionsAPITestCase(unittest.TestCase): +import testcase + +class ActionsAPITestCase(testcase.TestCase): def setUp(self): + testcase.TestCase.setUp(self) self.f = zipfile.ZipFile("helloworld-0.1-1.pisi", "r") self.filelist = [] diff --git a/tests/archivetests.py b/tests/archivetests.py index 362afc89..232503c2 100644 --- a/tests/archivetests.py +++ b/tests/archivetests.py @@ -20,12 +20,11 @@ from pisi import sourcearchive from pisi import fetcher from pisi import util from pisi.build import BuildContext -from pisi.config import config from pisi import uri -class ArchiveFileTestCase(unittest.TestCase): - def setUp(self): - pisi.api.init(comar = False) +import testcase + +class ArchiveFileTestCase(testcase.TestCase): def testUnpackTar(self): bctx = BuildContext("tests/popt/pspec.xml") @@ -52,8 +51,6 @@ class ArchiveFileTestCase(unittest.TestCase): self.assertEqual(util.sha1_file(testfile), "5af9dd7d754f788cf511c57ce0af3d555fed009d") - pisi.api.finalize() - def testUnpackZip(self): bctx = BuildContext("tests/pccts/pspec.xml") @@ -73,8 +70,6 @@ class ArchiveFileTestCase(unittest.TestCase): self.assertEqual(util.sha1_file(testfile), "f2be0f9783e84e98fe4e2b8201a8f506fcc07a4d") - pisi.api.finalize() - # TODO: no link file in pccts package. Need to find a ZIP file # containing a symlink # check for symbolic links @@ -97,8 +92,6 @@ class ArchiveFileTestCase(unittest.TestCase): zip.close() #TODO: do some more work to test the integrity of new zip file - - pisi.api.finalize() def testUnpackZipCond(self): bctx = BuildContext("tests/pccts/pspec.xml") @@ -118,6 +111,4 @@ class ArchiveFileTestCase(unittest.TestCase): testfile = targetDir + "/pccts/history.txt" assert pathexists(testfile) - pisi.api.finalize() - suite = unittest.makeSuite(ArchiveFileTestCase) diff --git a/tests/configfiletests.py b/tests/configfiletests.py index 1eace6b9..70722aa3 100644 --- a/tests/configfiletests.py +++ b/tests/configfiletests.py @@ -12,6 +12,7 @@ import unittest from pisi.configfile import ConfigurationFile +# NB: no need for pisi testcase in these things that do well without pisi init class ConfigFileTestCase(unittest.TestCase): def setUp(self): diff --git a/tests/fetchertests.py b/tests/fetchertests.py index d791b236..a3181269 100644 --- a/tests/fetchertests.py +++ b/tests/fetchertests.py @@ -18,9 +18,10 @@ from pisi import fetcher from pisi import util from pisi import uri -class FetcherTestCase(unittest.TestCase): +import testcase +class FetcherTestCase(testcase.TestCase): def setUp(self): - pisi.api.init(database = False, comar = False) + testcase.TestCase.setUp(self, database = False) self.spec = SpecFile() self.spec.read("tests/popt/pspec.xml") diff --git a/tests/graphtests.py b/tests/graphtests.py index 60eb564d..6249fd31 100644 --- a/tests/graphtests.py +++ b/tests/graphtests.py @@ -12,7 +12,6 @@ import unittest import os from pisi import graph -from pisi.config import config class GraphTestCase(unittest.TestCase): def setUp(self): diff --git a/tests/installdbtests.py b/tests/installdbtests.py index 8d888eed..7d4f9eee 100644 --- a/tests/installdbtests.py +++ b/tests/installdbtests.py @@ -16,21 +16,16 @@ import pisi.api import pisi.installdb from pisi import util -class InstallDBTestCase(unittest.TestCase): - - def setUp(self): - pisi.api.init(database = True, comar = False) - +import testcase +class InstallDBTestCase(testcase.TestCase): def testRemoveDummy(self): ctx.installdb.remove('installtest') self.assert_(not ctx.installdb.is_installed('installtest')) - pisi.api.finalize() def testInstall(self): ctx.installdb.purge('installtest') ctx.installdb.install('installtest', '0.1', '2', '3') - pisi.api.finalize() def testRemovePurge(self): ctx.installdb.install('installtest', '0.1', '2', '3') @@ -39,6 +34,5 @@ class InstallDBTestCase(unittest.TestCase): self.assert_(ctx.installdb.is_removed('installtest')) ctx.installdb.purge('installtest') self.assert_(not ctx.installdb.is_recorded('installtest')) - pisi.api.finalize() suite = unittest.makeSuite(InstallDBTestCase) diff --git a/tests/metadatatests.py b/tests/metadatatests.py index a37380c4..b2e03cf8 100644 --- a/tests/metadatatests.py +++ b/tests/metadatatests.py @@ -13,12 +13,11 @@ import os from pisi import metadata from pisi import util -from pisi.config import config +import pisi.context as ctx + +import testcase +class MetaDataTestCase(testcase.TestCase): -class MetaDataTestCase(unittest.TestCase): - def setUp(self): - pass - def testRead(self): md = metadata.MetaData() md.read('tests/popt/metadata.xml') @@ -32,7 +31,7 @@ class MetaDataTestCase(unittest.TestCase): def testWrite(self): md = self.testRead() - md.write(os.path.join(config.tmp_dir(),'metadata-test.xml' )) + md.write(os.path.join(ctx.config.tmp_dir(),'metadata-test.xml' )) def testVerify(self): md = self.testRead() diff --git a/tests/packagedbtests.py b/tests/packagedbtests.py index 175cd8e5..7d3acf54 100644 --- a/tests/packagedbtests.py +++ b/tests/packagedbtests.py @@ -17,11 +17,11 @@ from pisi.packagedb import PackageDB from pisi import util from pisi.specfile import SpecFile -class PackageDBTestCase(unittest.TestCase): +import testcase +class PackageDBTestCase(testcase.TestCase): def setUp(self): - pisi.api.init(comar = False) - + testcase.TestCase.setUp(self) self.spec = SpecFile() self.spec.read('tests/popt/pspec.xml') @@ -32,13 +32,10 @@ class PackageDBTestCase(unittest.TestCase): self.assert_(self.pdb.has_package('popt-libs')) # close the database and remove lock self.pdb.close() - pisi.api.finalize() def testRemove(self): self.pdb.remove_package('popt-libs') self.assert_(not self.pdb.has_package('popt-libs')) self.pdb.close() - pisi.api.finalize() suite = unittest.makeSuite(PackageDBTestCase) - diff --git a/tests/packagetests.py b/tests/packagetests.py index 701c88e7..6494c935 100644 --- a/tests/packagetests.py +++ b/tests/packagetests.py @@ -12,20 +12,21 @@ import unittest import os from pisi import util -from pisi.config import config +import pisi.context as ctx from pisi import package - -class PackageTestCase(unittest.TestCase): +import testcase +class PackageTestCase(testcase.TestCase): def setUp(self): + testcase.TestCase.setUp(self) self.pkgName = util.package_name("testing", "5.1", "2") def testAddExtract(self): cur = os.getcwd() - tmpdir = config.tmp_dir() + tmpdir = ctx.config.tmp_dir() testdir = os.path.join(cur, "tests/popt") pkg_path = os.path.join(tmpdir, self.pkgName) diff --git a/tests/sourcedbtests.py b/tests/sourcedbtests.py index 0a836c69..f11fac67 100644 --- a/tests/sourcedbtests.py +++ b/tests/sourcedbtests.py @@ -17,10 +17,11 @@ import pisi.sourcedb from pisi import util from pisi.specfile import SpecFile -class SourceDBTestCase(unittest.TestCase): +import testcase +class SourceDBTestCase(testcase.TestCase): def setUp(self): - pisi.api.init(comar = False) + testcase.TestCase.setUp(self) self.sourcedb = pisi.sourcedb.init() self.spec = SpecFile() diff --git a/tests/specfilenewtests.py b/tests/specfilenewtests.py index 488d397d..c29bd9c0 100644 --- a/tests/specfilenewtests.py +++ b/tests/specfilenewtests.py @@ -12,11 +12,15 @@ import unittest import os import pisi.specfilenew as specfile -from pisi.config import config +import pisi.context as ctx import pisi.util as util -class SpecFileNewTestCase(unittest.TestCase): +import testcase + +class SpecFileNewTestCase(testcase.TestCase): + def setUp(self): + testcase.TestCase.setUp(self) self.spec = specfile.SpecFile() self.spec.read("tests/popt/pspec.xml") @@ -72,9 +76,8 @@ class SpecFileNewTestCase(unittest.TestCase): self.fail("Failed to verify specfile") def testCopy(self): - util.check_dir(config.tmp_dir()) + util.check_dir(ctx.config.tmp_dir()) self.spec.read("tests/popt/pspec.xml") self.spec.write(os.path.join(config.tmp_dir(), 'popt-copy.pspec.xml')) - suite = unittest.makeSuite(SpecFileNewTestCase) diff --git a/tests/specfiletests.py b/tests/specfiletests.py index 58d644d3..9c3788bf 100644 --- a/tests/specfiletests.py +++ b/tests/specfiletests.py @@ -12,11 +12,14 @@ import unittest import os from pisi import specfile -from pisi.config import config +import pisi.context as ctx import pisi.util as util -class SpecFileTestCase(unittest.TestCase): +import testcase + +class SpecFileTestCase(testcase.TestCase): def setUp(self): + testcase.TestCase.setUp(self) self.spec = specfile.SpecFile() self.spec.read("tests/popt/pspec.xml") @@ -72,9 +75,9 @@ class SpecFileTestCase(unittest.TestCase): self.fail("Failed to verify specfile") def testCopy(self): - util.check_dir(config.tmp_dir()) self.spec.read("tests/popt/pspec.xml") - self.spec.write(os.path.join(config.tmp_dir(), 'popt-copy.pspec.xml')) + util.check_dir(ctx.config.tmp_dir()) + self.spec.write(os.path.join(ctx.config.tmp_dir(), 'popt-copy.pspec.xml')) suite = unittest.makeSuite(SpecFileTestCase) diff --git a/tests/testcase.py b/tests/testcase.py new file mode 100644 index 00000000..08d5b6ac --- /dev/null +++ b/tests/testcase.py @@ -0,0 +1,27 @@ +# Copyright (C) 2005, TUBITAK/UEKAE +# +# 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. +# +# Please read the COPYING file. +# + +import unittest +import os + +import pisi +import pisi.api +import pisi.util +import pisi.config + +class TestCase(unittest.TestCase): + + def setUp(self, comar = False, database = True): + options = pisi.config.Options() + options.destdir = 'tmp' + pisi.api.init(options = options, comar = comar, database = database) + + def tearDown(self, ): + pisi.api.finalize() diff --git a/tests/utiltests.py b/tests/utiltests.py index fca33bd1..6cd4927f 100644 --- a/tests/utiltests.py +++ b/tests/utiltests.py @@ -15,6 +15,7 @@ from pisi import version from pisi.util import * class UtilTestCase(unittest.TestCase): + def setUp(self): pass @@ -32,4 +33,3 @@ class UtilTestCase(unittest.TestCase): suite = unittest.makeSuite(UtilTestCase) - diff --git a/tests/xmlfiletests.py b/tests/xmlfiletests.py index 9bc857db..e67df762 100644 --- a/tests/xmlfiletests.py +++ b/tests/xmlfiletests.py @@ -17,14 +17,13 @@ import types import pisi import pisi.api from pisi import xmlfile -from pisi.config import config +import pisi.context as ctx import pisi.util as util from pisi.xmlext import * -class XmlFileTestCase(unittest.TestCase): +import testcase - def setUp(self): - pisi.api.init(database = False, comar = False) +class XmlFileTestCase(testcase.TestCase): def testMetaClass(self): diff --git a/tools/fetchAll.py b/tools/fetchAll.py index 4ce26c70..a522b01d 100755 --- a/tools/fetchAll.py +++ b/tools/fetchAll.py @@ -17,8 +17,6 @@ sys.path.append('.') import pisi.uri import pisi.specfile import pisi.util as util - -from pisi.config import config from pisi.fetcher import fetch_url def scanPSPEC(folder): @@ -50,7 +48,7 @@ if __name__ == "__main__": if not isCached(URI.filename(), spec.source.archiveSHA1): print URI, " -> " , os.path.join(config.archives_dir(), URI.filename()) try: - fetch_url(URI, config.archives_dir()) + fetch_url(URI, config.archives_dir()) except pisi.fetcher.FetchError, e: print e pass