makepkg: allow specifying alternative build directory
[pacman-ng.git] / test / pacman / pactest.py
blob77f87da6ff8455657be29cbbc500dd4215f5e376
1 #! /usr/bin/python
3 # pactest : run automated testing on the pacman binary
5 # Copyright (c) 2006 by Aurelien Foret <orelien@chez.com>
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 import glob
21 from optparse import OptionParser
22 import os
23 import shutil
24 import sys
25 import tempfile
27 import pmenv
28 import util
30 __author__ = "Aurelien FORET"
31 __version__ = "0.4"
33 def resolveBinPath(option, opt_str, value, parser):
34 setattr(parser.values, option.dest, os.path.abspath(value))
36 def globTests(option, opt_str, value, parser):
37 idx = 0
38 globlist = []
40 # maintain the idx so we can modify rargs
41 while idx < len(parser.rargs) and \
42 not parser.rargs[idx].startswith('-'):
43 globlist += glob.glob(parser.rargs[idx])
44 idx += 1
46 parser.rargs = parser.rargs[idx:]
47 setattr(parser.values, option.dest, globlist)
49 def createOptParser():
50 testcases = []
51 usage = "usage: %prog [options] [[--test <path/to/testfile.py>] ...]"
52 description = "Runs automated tests on the pacman binary. Tests are " \
53 "described using an easy python syntax, and several can be " \
54 "ran at once."
55 parser = OptionParser(usage = usage, description = description)
57 parser.add_option("-v", "--verbose", action = "count",
58 dest = "verbose", default = 0,
59 help = "print verbose output")
60 parser.add_option("-d", "--debug", type = "int",
61 dest = "debug", default = 0,
62 help = "set debug level for pacman")
63 parser.add_option("-p", "--pacman", action = "callback",
64 callback = resolveBinPath, type = "string",
65 dest = "bin", default = "pacman",
66 help = "specify location of the pacman binary")
67 parser.add_option("-t", "--test", action = "callback",
68 callback = globTests, dest = "testcases",
69 help = "specify test case(s)")
70 parser.add_option("--keep-root", action = "store_true",
71 dest = "keeproot", default = False,
72 help = "don't remove the generated pacman root filesystem")
73 parser.add_option("--nolog", action = "store_true",
74 dest = "nolog", default = False,
75 help = "do not log pacman messages")
76 parser.add_option("--gdb", action = "store_true",
77 dest = "gdb", default = False,
78 help = "use gdb while calling pacman")
79 parser.add_option("--valgrind", action = "store_true",
80 dest = "valgrind", default = False,
81 help = "use valgrind while calling pacman")
82 parser.add_option("--manual-confirm", action = "store_true",
83 dest = "manualconfirm", default = False,
84 help = "do not use --noconfirm for pacman calls")
85 return parser
88 if __name__ == "__main__":
89 # instantiate env and parser objects
90 root_path = tempfile.mkdtemp()
91 env = pmenv.pmenv(root=root_path)
92 opt_parser = createOptParser()
93 (opts, args) = opt_parser.parse_args()
95 # add parsed options to env object
96 util.verbose = opts.verbose
97 env.pacman["debug"] = opts.debug
98 env.pacman["bin"] = opts.bin
99 env.pacman["nolog"] = opts.nolog
100 env.pacman["gdb"] = opts.gdb
101 env.pacman["valgrind"] = opts.valgrind
102 env.pacman["manual-confirm"] = opts.manualconfirm
104 if opts.testcases is None or len(opts.testcases) == 0:
105 print "no tests defined, nothing to do"
106 os.rmdir(root_path)
107 sys.exit(2)
109 for i in opts.testcases:
110 env.addtest(i)
112 # run tests and print overall results
113 env.run()
114 env.results()
116 if env.failed > 0:
117 print "pacman testing root saved: %s" % root_path
118 sys.exit(1)
120 if not opts.keeproot:
121 shutil.rmtree(root_path)
122 else:
123 print "pacman testing root saved: %s" % root_path
125 # vim: set ts=4 sw=4 et: