[pacman-dev] [PATCH 2/3] Simplify pmtest.generate and .run API (removed pacman parameter).
Andrew Gregory
andrew.gregory.8 at gmail.com
Fri Oct 4 11:21:19 EDT 2013
On 10/02/13 at 11:48am, Jeremy Heiner wrote:
> Previously pmtest.__init__ received and cached the pmenv.root, and
> generate and run received the pmenv.pacman. Now __init__ receives and
> caches the pmenv, allowing any method to fetch root, pacman, or other
> pmenv goodies directly instead of needing the extra parameter.
>
> Moved the "which fakeroot" and "which fakechroot" logic from pmtest up
> into pmenv. Repeating the probes and warnings for each test was a bit
> redundant.
>
> Signed-off-by: Jeremy Heiner <ScalaProtractor at gmail.com>
> ---
> test/pacman/pmenv.py | 28 +++++++++++++++++++++++---
> test/pacman/pmrule.py | 10 +++++-----
> test/pacman/pmtest.py | 55 ++++++++++++++++++++++-----------------------------
> 3 files changed, 54 insertions(+), 39 deletions(-)
>
> diff --git a/test/pacman/pmenv.py b/test/pacman/pmenv.py
> index f358285..3cfcfa5 100644
> --- a/test/pacman/pmenv.py
> +++ b/test/pacman/pmenv.py
> @@ -21,6 +21,7 @@
>
> import pmtest
> import tap
> +import util
>
>
> class pmenv(object):
> @@ -43,6 +44,27 @@ def __init__(self, root = "root"):
> "nolog": 0
> }
>
> + if os.geteuid() != 0:
> + self.fakeroot = util.which("fakeroot")
> + if not self.fakeroot:
> + tap.diag("WARNING: fakeroot not found!")
> + else:
> + self.fakeroot = "fakeroot"
> +
> + self.fakechroot = util.which("fakechroot")
> + if not self.fakechroot:
> + tap.diag("WARNING: fakechroot not found!")
> + else:
> + self.fakechroot = "fakechroot"
I know this was a copy-paste, but let's improve it while we're at it.
Saving the result of util.which only to overwrite it is kind of
confusing. Let's change these to:
if util.which("fakeroot"):
self.fakeroot = "fakeroot"
else:
...
> +
> + def cmdroot(self):
> + cmd = []
> + if self.fakeroot:
> + cmd.append(self.fakeroot)
> + if self.fakechroot:
> + cmd.append(self.fakechroot)
> + return cmd
> +
> def __str__(self):
> return "root = %s\n" \
> "pacman = %s" \
> @@ -53,7 +75,7 @@ def addtest(self, testcase):
> """
> if not os.path.isfile(testcase):
> raise IOError("test file %s not found" % testcase)
> - test = pmtest.pmtest(testcase, self.root)
> + test = pmtest.pmtest(testcase, self)
> self.testcases.append(test)
>
> def run(self):
> @@ -64,8 +86,8 @@ def run(self):
> tap.diag("Running '%s'" % t.testname)
>
> t.load()
> - t.generate(self.pacman)
> - t.run(self.pacman)
> + t.generate()
> + t.run()
>
> tap.diag("==> Checking rules")
> tap.todo = t.expectfailure
> diff --git a/test/pacman/pmrule.py b/test/pacman/pmrule.py
> index c97a158..718c87c 100644
> --- a/test/pacman/pmrule.py
> +++ b/test/pacman/pmrule.py
> @@ -56,7 +56,7 @@ def check(self, test):
> if test.retcode != int(key):
> success = 0
> elif case == "OUTPUT":
> - logfile = os.path.join(test.root, util.LOGFILE)
> + logfile = os.path.join(test.env.root, util.LOGFILE)
> if not os.access(logfile, os.F_OK):
> tap.diag("LOGFILE not found, cannot validate 'OUTPUT' rule")
> success = 0
> @@ -112,7 +112,7 @@ def check(self, test):
> tap.diag("PKG rule '%s' not found" % case)
> success = -1
> elif kind == "FILE":
> - filename = os.path.join(test.root, key)
> + filename = os.path.join(test.env.root, key)
> if case == "EXIST":
> if not os.path.isfile(filename):
> success = 0
> @@ -152,7 +152,7 @@ def check(self, test):
> tap.diag("FILE rule '%s' not found" % case)
> success = -1
> elif kind == "DIR":
> - filename = os.path.join(test.root, key)
> + filename = os.path.join(test.env.root, key)
> if case == "EXIST":
> if not os.path.isdir(filename):
> success = 0
> @@ -160,7 +160,7 @@ def check(self, test):
> tap.diag("DIR rule '%s' not found" % case)
> success = -1
> elif kind == "LINK":
> - filename = os.path.join(test.root, key)
> + filename = os.path.join(test.env.root, key)
> if case == "EXIST":
> if not os.path.islink(filename):
> success = 0
> @@ -168,7 +168,7 @@ def check(self, test):
> tap.diag("LINK rule '%s' not found" % case)
> success = -1
> elif kind == "CACHE":
> - cachedir = os.path.join(test.root, util.PM_CACHEDIR)
> + cachedir = os.path.join(test.env.root, util.PM_CACHEDIR)
> if case == "EXISTS":
> pkg = test.findpkg(key, value, allow_local=True)
> if not pkg or not os.path.isfile(
> diff --git a/test/pacman/pmtest.py b/test/pacman/pmtest.py
> index 4101546..a0a1455 100644
> --- a/test/pacman/pmtest.py
> +++ b/test/pacman/pmtest.py
> @@ -35,20 +35,20 @@ class pmtest(object):
> """Test object
> """
>
> - def __init__(self, name, root):
> + def __init__(self, name, env):
> self.name = name
> self.testname = os.path.basename(name).replace('.py', '')
> - self.root = root
> + self.env = env
> self.cachepkgs = True
>
> def __str__(self):
> return "name = %s\n" \
> "testname = %s\n" \
> - "root = %s" % (self.name, self.testname, self.root)
> + "root = %s" % (self.name, self.testname, self.env.root)
>
> def addpkg2db(self, treename, pkg):
> if not treename in self.db:
> - self.db[treename] = pmdb.pmdb(treename, self.root)
> + self.db[treename] = pmdb.pmdb(treename, self.env.root)
> self.db[treename].pkgs.append(pkg)
>
> def addpkg(self, pkg):
> @@ -75,13 +75,13 @@ def addrule(self, rulename):
> self.rules.append(rule)
>
> def rootjoin(self, path):
> - return os.path.join(self.root, path)
> + return os.path.join(self.env.root, path)
>
> def rootremove(self, path):
> - end = len(self.root)
> + end = len(self.env.root)
> if ( end < len(path)
> and os.sep == path[end]
> - and path.startswith(self.root) ):
> + and path.startswith(self.env.root) ):
> return path[end+1:]
> raise ValueError("path not under root: %s" % path)
>
> @@ -94,7 +94,7 @@ def load(self):
> self.args = ""
> self.retcode = 0
> self.db = {
> - "local": pmdb.pmdb("local", self.root)
> + "local": pmdb.pmdb("local", self.env.root)
> }
> self.localpkgs = []
> self.createlocalpkgs = False
> @@ -115,13 +115,16 @@ def load(self):
> else:
> raise IOError("file %s does not exist!" % self.name)
>
> - def generate(self, pacman):
> + def generate(self):
> tap.diag("==> Generating test environment")
>
> + root = self.env.root
> + pacman = self.env.pacman
> +
> # Cleanup leftover files from a previous test session
> - if os.path.isdir(self.root):
> - shutil.rmtree(self.root)
> - vprint("\t%s" % self.root)
> + if os.path.isdir(root):
> + shutil.rmtree(root)
> + vprint("\t%s" % root)
>
> # Create directory structure
> vprint(" Creating directory structure:")
> @@ -153,7 +156,7 @@ def generate(self, pacman):
>
> # Configuration file
> vprint(" Creating configuration file")
> - util.mkcfgfile(util.PACCONF, self.root, self.option, self.db)
> + util.mkcfgfile(util.PACCONF, root, self.option, self.db)
>
> # Creating packages
> vprint(" Creating package archives")
> @@ -191,18 +194,18 @@ def generate(self, pacman):
> os.utime(path, (355, 355))
> for pkg in self.db["local"].pkgs:
> vprint("\tinstalling %s" % pkg.fullname())
> - pkg.install_package(self.root)
> + pkg.install_package(root)
>
> # Done.
> vprint(" Taking a snapshot of the file system")
> - for roots, dirs, files in os.walk(self.root):
> + for roots, dirs, files in os.walk(root):
> for i in files:
> filename = os.path.join(roots, i)
> - f = pmfile.PacmanFile(self.root, self.rootremove(filename))
> + f = pmfile.PacmanFile(root, self.rootremove(filename))
> self.files.append(f)
> vprint("\t%s" % f.name)
>
> - def run(self, pacman):
> + def run(self):
> if os.path.isfile(util.PM_LOCK):
> tap.bail("\tERROR: another pacman session is on-going -- skipping")
> return
> @@ -210,19 +213,9 @@ def run(self, pacman):
> tap.diag("==> Running test")
> vprint("\tpacman %s" % self.args)
>
> - cmd = []
> - if os.geteuid() != 0:
> - fakeroot = util.which("fakeroot")
> - if not fakeroot:
> - tap.diag("WARNING: fakeroot not found!")
> - else:
> - cmd.append("fakeroot")
> -
> - fakechroot = util.which("fakechroot")
> - if not fakechroot:
> - tap.diag("WARNING: fakechroot not found!")
> - else:
> - cmd.append("fakechroot")
> + root = self.env.root
> + pacman = self.env.pacman
> + cmd = self.env.cmdroot()
>
> if pacman["gdb"]:
> cmd.extend(["libtool", "execute", "gdb", "--args"])
> @@ -235,7 +228,7 @@ def run(self, pacman):
> "--suppressions=%s" % suppfile])
> cmd.extend([pacman["bin"],
> "--config", self.rootjoin(util.PACCONF),
> - "--root", self.root,
> + "--root", root,
> "--dbpath", self.rootjoin(util.PM_DBPATH),
> "--cachedir", self.rootjoin(util.PM_CACHEDIR)])
> if not pacman["manual-confirm"]:
> --
> 1.8.4
More information about the pacman-dev
mailing list