tests/compare.py

branch
trunk
changeset 7
1f3cb3845dfd
parent 1
a86a0cb0dd57
child 8
97388f5ff770
equal deleted inserted replaced
6:77043f4e6a9f 7:1f3cb3845dfd
26 TESTSCRIPT_NAME = 'testscript.sh' 26 TESTSCRIPT_NAME = 'testscript.sh'
27 SCRIPT_PROLOGUE = """#!/bin/sh 27 SCRIPT_PROLOGUE = """#!/bin/sh
28 set -e 28 set -e
29 """ 29 """
30 30
31 tests = {'test-1.23.tar': ['tar -xf test-1.23.tar'], 31 tests = {'test-1.23.tar': ([], ['tar -xf test-1.23.tar'], []),
32 'test-1.23.tar.gz': ['tar -xzf test-1.23.tar.gz'], 32 'test-1.23.tar.gz': ([], ['tar -xzf test-1.23.tar.gz'], []),
33 'test-1.23.tar.bz2': ['mkdir test-1.23', 33 'test-1.23.tar.bz2': ([], ['mkdir test-1.23',
34 'cd test-1.23', 34 'cd test-1.23',
35 'tar -jxf ../test-1.23.tar.bz2'], 35 'tar -jxf ../test-1.23.tar.bz2'], []),
36 'test-1.23.zip': ['mkdir test-1.23', 36 'test-1.23.zip': ([], ['mkdir test-1.23',
37 'cd test-1.23', 37 'cd test-1.23',
38 'unzip -q ../test-1.23.zip'], 38 'unzip -q ../test-1.23.zip'], []),
39 'test-1.23.cpio': ['cpio -i --make-directories \ 39 'test-1.23.cpio': ([], ['cpio -i --make-directories \
40 <test-1.23.cpio 2>/dev/null'], 40 <test-1.23.cpio 2>/dev/null'], []),
41 'test-1.23_all.deb': ['TD=$PWD', 41 'test-1.23_all.deb': ([], ['TD=$PWD',
42 'mkdir test-1.23', 42 'mkdir test-1.23',
43 'cd /tmp', 43 'cd /tmp',
44 'ar x $TD/test-1.23_all.deb data.tar.gz', 44 'ar x $TD/test-1.23_all.deb data.tar.gz',
45 'cd $TD/test-1.23', 45 'cd $TD/test-1.23',
46 'tar -zxf /tmp/data.tar.gz', 46 'tar -zxf /tmp/data.tar.gz',
47 'rm /tmp/data.tar.gz']} 47 'rm /tmp/data.tar.gz'], []),
48 'test-recursive-badperms.tar.bz2': (
49 ['-r'],
50 ['mkdir test-recursive-badperms',
51 'cd test-recursive-badperms',
52 'tar -jxf ../test-recursive-badperms.tar.bz2',
53 'tar -xf test-badperms.tar',
54 'mv testdir test-badperms',
55 'chmod 755 test-badperms'],
56 ['if [ "x`cat test-recursive-badperms/test-badperms/testfile`" = "xhey" ]',
57 'then exit 0; else exit 1; fi']
58 )}
48 59
49 if os.path.exists('scripts/x') and os.path.exists('tests'): 60 if os.path.exists('scripts/x') and os.path.exists('tests'):
50 os.chdir('tests') 61 os.chdir('tests')
51 elif os.path.exists('../scripts/x') and os.path.exists('../tests'): 62 elif os.path.exists('../scripts/x') and os.path.exists('../tests'):
52 pass 63 pass
57 class ExtractorTestError(Exception): 68 class ExtractorTestError(Exception):
58 pass 69 pass
59 70
60 71
61 class ExtractorTest(object): 72 class ExtractorTest(object):
62 def __init__(self, archive_filename, commands): 73 def __init__(self, archive_filename, info):
63 self.archive_filename = archive_filename 74 self.archive_filename = archive_filename
64 self.shell_commands = commands 75 self.arguments, self.shell_commands, self.shell_test = info
65 76
66 def get_results(self, commands): 77 def get_results(self, commands):
67 status = subprocess.call(commands) 78 status = subprocess.call(commands)
68 if status != 0: 79 if status != 0:
69 return None 80 return None
71 process.wait() 82 process.wait()
72 output = process.stdout.read(-1) 83 output = process.stdout.read(-1)
73 process.stdout.close() 84 process.stdout.close()
74 return set(output.split('\n')) 85 return set(output.split('\n'))
75 86
76 def get_shell_results(self): 87 def write_script(self, commands):
77 script = open(TESTSCRIPT_NAME, 'w') 88 script = open(TESTSCRIPT_NAME, 'w')
78 script.write("%s%s\n" % (SCRIPT_PROLOGUE, 89 script.write("%s%s\n" % (SCRIPT_PROLOGUE, '\n'.join(commands)))
79 '\n'.join(self.shell_commands)))
80 script.close() 90 script.close()
81 subprocess.call(['chmod', 'u+w', TESTSCRIPT_NAME]) 91 subprocess.call(['chmod', 'u+w', TESTSCRIPT_NAME])
92
93 def get_shell_results(self):
94 self.write_script(self.shell_commands)
82 return self.get_results(['sh', TESTSCRIPT_NAME]) 95 return self.get_results(['sh', TESTSCRIPT_NAME])
83 96
84 def get_extractor_results(self): 97 def get_extractor_results(self):
85 return self.get_results(['../scripts/x', self.archive_filename]) 98 return self.get_results(['../scripts/x'] + self.arguments +
99 [self.archive_filename])
86 100
101 def get_posttest_result(self):
102 if not self.shell_test:
103 return 0
104 self.write_script(self.shell_test)
105 return subprocess.call(['sh', TESTSCRIPT_NAME])
106
87 def clean(self): 107 def clean(self):
88 status = subprocess.call(['find', '-mindepth', '1', '-maxdepth', '1', 108 status = subprocess.call(['find', '-mindepth', '1', '-maxdepth', '1',
89 '-type', 'd', 109 '-type', 'd',
90 '!', '-name', 'CVS', '!', '-name', '.svn', 110 '!', '-name', 'CVS', '!', '-name', '.svn',
91 '-exec', 'rm', '-rf', '{}', ';']) 111 '-exec', 'rm', '-rf', '{}', ';'])
96 def run(self): 116 def run(self):
97 self.clean() 117 self.clean()
98 expected = self.get_shell_results() 118 expected = self.get_shell_results()
99 self.clean() 119 self.clean()
100 actual = self.get_extractor_results() 120 actual = self.get_extractor_results()
121 posttest_result = self.get_posttest_result()
101 self.clean() 122 self.clean()
102 if expected is None: 123 if expected is None:
103 raise ExtractorTestError("could not get baseline results") 124 raise ExtractorTestError("could not get baseline results")
104 elif actual is None: 125 elif actual is None:
105 raise ExtractorTestError("could not get extractor results") 126 raise ExtractorTestError("could not get extractor results")
107 print "FAILED:", self.archive_filename 128 print "FAILED:", self.archive_filename
108 print "Only in baseline results:" 129 print "Only in baseline results:"
109 print '\n'.join(expected.difference(actual)) 130 print '\n'.join(expected.difference(actual))
110 print "Only in actual results:" 131 print "Only in actual results:"
111 print '\n'.join(actual.difference(expected)) 132 print '\n'.join(actual.difference(expected))
133 return False
134 elif posttest_result != 0:
135 print "FAILED:", self.archive_filename
136 print "Posttest returned status code", posttest_result
137 print actual
112 return False 138 return False
113 else: 139 else:
114 print "Passed:", self.archive_filename 140 print "Passed:", self.archive_filename
115 return True 141 return True
116 142

mercurial