tests/compare.py

Mon, 13 Nov 2006 23:06:30 -0500

author
brett
date
Mon, 13 Nov 2006 23:06:30 -0500
branch
trunk
changeset 8
97388f5ff770
parent 7
1f3cb3845dfd
child 10
f0acfe12a0e2
permissions
-rw-r--r--

[svn] Make ExtractorApplication suck less. Now the strategies for handling
different archive types are out in their own classes, and polymorphism
takes care of everything for us. This is way cleaner.

While I was at it I changed the behavior in the case where an archive
contains one directory that doesn't match the basename. I now treat that
the same as a bomb. This can lead to silly directory structures but
ensures that there's no "data" loss nor unexpected results.

1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
1 #!/usr/bin/env python
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
2 #
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
3 # compare.py -- High-level tests for x.
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
4 # Copyright (c) 2006 Brett Smith <brettcsmith@brettcsmith.org>.
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
5 #
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
6 # This program is free software; you can redistribute it and/or modify it
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
7 # under the terms of the GNU General Public License as published by the
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
8 # Free Software Foundation; either version 2 of the License, or (at your
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
9 # option) any later version.
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
10 #
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful, but
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
14 # Public License for more details.
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
15 #
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
16 # You should have received a copy of the GNU General Public License along
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
17 # with this program; if not, write to the Free Software Foundation, Inc.,
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
18 # 51 Franklin Street, 5th Floor, Boston, MA, 02111.
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
19
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
20 import os
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
21 import subprocess
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
22 import sys
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
23
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
24 from sets import Set as set
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
25
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
26 TESTSCRIPT_NAME = 'testscript.sh'
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
27 SCRIPT_PROLOGUE = """#!/bin/sh
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
28 set -e
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
29 """
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
30
7
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
31 tests = {'test-1.23.tar': ([], ['tar -xf test-1.23.tar'], []),
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
32 'test-1.23.tar.gz': ([], ['tar -xzf test-1.23.tar.gz'], []),
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
33 'test-1.23.tar.bz2': ([], ['mkdir test-1.23',
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
34 'cd test-1.23',
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
35 'tar -jxf ../test-1.23.tar.bz2'], []),
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
36 'test-1.23.zip': ([], ['mkdir test-1.23',
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
37 'cd test-1.23',
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
38 'unzip -q ../test-1.23.zip'], []),
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
39 'test-1.23.cpio': ([], ['cpio -i --make-directories \
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
40 <test-1.23.cpio 2>/dev/null'], []),
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
41 'test-1.23_all.deb': ([], ['TD=$PWD',
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
42 'mkdir test-1.23',
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
43 'cd /tmp',
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
44 'ar x $TD/test-1.23_all.deb data.tar.gz',
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
45 'cd $TD/test-1.23',
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
46 'tar -zxf /tmp/data.tar.gz',
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
47 'rm /tmp/data.tar.gz'], []),
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
48 'test-recursive-badperms.tar.bz2': (
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
49 ['-r'],
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
50 ['mkdir test-recursive-badperms',
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
51 'cd test-recursive-badperms',
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
52 'tar -jxf ../test-recursive-badperms.tar.bz2',
8
97388f5ff770 [svn] Make ExtractorApplication suck less. Now the strategies for handling
brett
parents: 7
diff changeset
53 'mkdir test-badperms',
97388f5ff770 [svn] Make ExtractorApplication suck less. Now the strategies for handling
brett
parents: 7
diff changeset
54 'cd test-badperms',
97388f5ff770 [svn] Make ExtractorApplication suck less. Now the strategies for handling
brett
parents: 7
diff changeset
55 'tar -xf ../test-badperms.tar',
97388f5ff770 [svn] Make ExtractorApplication suck less. Now the strategies for handling
brett
parents: 7
diff changeset
56 'chmod 755 testdir'],
97388f5ff770 [svn] Make ExtractorApplication suck less. Now the strategies for handling
brett
parents: 7
diff changeset
57 ['if [ "x`cat test-recursive-badperms/test-badperms/testdir/testfile`" = \
97388f5ff770 [svn] Make ExtractorApplication suck less. Now the strategies for handling
brett
parents: 7
diff changeset
58 "xhey" ]',
7
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
59 'then exit 0; else exit 1; fi']
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
60 )}
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
61
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
62 if os.path.exists('scripts/x') and os.path.exists('tests'):
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
63 os.chdir('tests')
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
64 elif os.path.exists('../scripts/x') and os.path.exists('../tests'):
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
65 pass
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
66 else:
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
67 print "ERROR: Can't run tests in this directory!"
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
68 sys.exit(2)
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
69
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
70 class ExtractorTestError(Exception):
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
71 pass
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
72
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
73
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
74 class ExtractorTest(object):
7
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
75 def __init__(self, archive_filename, info):
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
76 self.archive_filename = archive_filename
7
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
77 self.arguments, self.shell_commands, self.shell_test = info
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
78
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
79 def get_results(self, commands):
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
80 status = subprocess.call(commands)
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
81 if status != 0:
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
82 return None
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
83 process = subprocess.Popen(['find'], stdout=subprocess.PIPE)
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
84 process.wait()
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
85 output = process.stdout.read(-1)
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
86 process.stdout.close()
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
87 return set(output.split('\n'))
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
88
7
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
89 def write_script(self, commands):
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
90 script = open(TESTSCRIPT_NAME, 'w')
7
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
91 script.write("%s%s\n" % (SCRIPT_PROLOGUE, '\n'.join(commands)))
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
92 script.close()
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
93 subprocess.call(['chmod', 'u+w', TESTSCRIPT_NAME])
7
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
94
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
95 def get_shell_results(self):
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
96 self.write_script(self.shell_commands)
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
97 return self.get_results(['sh', TESTSCRIPT_NAME])
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
98
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
99 def get_extractor_results(self):
7
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
100 return self.get_results(['../scripts/x'] + self.arguments +
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
101 [self.archive_filename])
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
102
7
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
103 def get_posttest_result(self):
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
104 if not self.shell_test:
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
105 return 0
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
106 self.write_script(self.shell_test)
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
107 return subprocess.call(['sh', TESTSCRIPT_NAME])
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
108
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
109 def clean(self):
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
110 status = subprocess.call(['find', '-mindepth', '1', '-maxdepth', '1',
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
111 '-type', 'd',
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
112 '!', '-name', 'CVS', '!', '-name', '.svn',
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
113 '-exec', 'rm', '-rf', '{}', ';'])
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
114 if status != 0:
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
115 raise ExtractorTestError("cleanup exited with status code %s" %
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
116 (status,))
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
117
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
118 def run(self):
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
119 self.clean()
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
120 expected = self.get_shell_results()
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
121 self.clean()
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
122 actual = self.get_extractor_results()
7
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
123 posttest_result = self.get_posttest_result()
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
124 self.clean()
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
125 if expected is None:
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
126 raise ExtractorTestError("could not get baseline results")
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
127 elif actual is None:
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
128 raise ExtractorTestError("could not get extractor results")
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
129 elif expected != actual:
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
130 print "FAILED:", self.archive_filename
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
131 print "Only in baseline results:"
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
132 print '\n'.join(expected.difference(actual))
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
133 print "Only in actual results:"
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
134 print '\n'.join(actual.difference(expected))
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
135 return False
7
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
136 elif posttest_result != 0:
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
137 print "FAILED:", self.archive_filename
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
138 print "Posttest returned status code", posttest_result
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
139 print actual
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
140 return False
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
141 else:
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
142 print "Passed:", self.archive_filename
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
143 return True
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
144
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
145
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
146 successes = 0
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
147 failures = 0
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
148 testnames = tests.keys()
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
149 testnames.sort()
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
150 for testname in testnames:
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
151 test = ExtractorTest(testname, tests[testname])
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
152 if test.run():
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
153 successes += 1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
154 else:
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
155 failures += 1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
156 print "Totals: %s successes, %s failures" % (successes, failures)
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
157

mercurial