Release 0.9
[0test.git] / test_spec.py
blob0e218906a0b17a4e309124d3dc2c702f8b279bec
1 # Copyright (C) 2010, Thomas Leonard
2 # Visit http://0install.net for details.
4 from zeroinstall import SafeException
5 from zeroinstall.injector import model
6 import logging
8 class TestSpec:
9 def __init__(self):
10 self.test_iface = None # The URI of the program being tested
12 self.test_ifaces = [] # [Interface] - list of interfaces with specified versions
13 self.test_matrix = {} # {URI: [version]} - versions of each interface to test
14 # (test_ifaces is needed because we care about the order)
16 self.command = None
17 self.test_wrapper = None # Command to execute to perform tests
18 self.test_args = [] # Arguments to pass to test script
20 # Yield a sequence of set((uri, version)), one for each combination to test
21 def get_combos(self, ifaces):
22 if not ifaces:
23 yield {}
24 return
25 for version in self.test_matrix[ifaces[0]]:
26 for combo in self.get_combos(ifaces[1:]):
27 combo[ifaces[0]] = version
28 yield combo.copy()
30 def parse_arguments(options, args):
31 spec = TestSpec()
33 if '--' in args:
34 i = args.index('--')
35 spec.test_args = args[i + 1:]
36 args = args[:i]
38 if options.test_command:
39 spec.test_wrapper = options.test_command + ' #'
40 if spec.test_args:
41 raise SafeException("Can't specify arguments with --test-command")
43 spec.command = 'run'
44 else:
45 spec.command = 'test'
47 if options.command == '':
48 spec.command = None
49 elif options.command:
50 spec.command = options.command
52 if spec.command:
53 logging.info("Test command is '{command}'".format(command = spec.command))
54 else:
55 logging.info("Test command is None")
57 iface = None
58 for x in args:
59 if (x[0].isdigit() or x[0] in ',%.!') and iface and '/' not in x:
60 spec.test_matrix[iface].append(x)
61 else:
62 assert x not in spec.test_matrix, "Interface %s given twice!" % x
63 iface = model.canonical_iface_uri(x)
64 spec.test_matrix[iface] = []
65 spec.test_ifaces.append(iface)
67 # We don't need to specify the version of the interface under test.
68 spec.test_iface = spec.test_ifaces[0]
69 if not spec.test_matrix[spec.test_iface]:
70 del spec.test_matrix[spec.test_iface]
71 del spec.test_ifaces[0]
73 # We do need a version for all the others (else what was the point of listing them?)
74 for iface in spec.test_ifaces:
75 if not spec.test_matrix[iface]:
76 raise SafeException("No versions given for interface %s" % iface)
78 return spec