tests/compare.py

Sat, 11 Nov 2006 18:42:19 -0500

author
brett
date
Sat, 11 Nov 2006 18:42:19 -0500
branch
trunk
changeset 7
1f3cb3845dfd
parent 1
a86a0cb0dd57
child 8
97388f5ff770
permissions
-rw-r--r--

[svn] Add a test for recursive extraction which also makes sure that we fix
permissions after we extract the archive, and DTRT when an archive contains
one file. Add code to handle the latter two cases.

ExtractorApplication is a total mess at this point. I already am having a
hard time following how the pieces fit together. Cleaning it up is my next
task; that'll be easier now that I test most of the functionality again.

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',
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
53 'tar -xf test-badperms.tar',
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
54 'mv testdir test-badperms',
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
55 'chmod 755 test-badperms'],
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
56 ['if [ "x`cat test-recursive-badperms/test-badperms/testfile`" = "xhey" ]',
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
57 '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
58 )}
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
59
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
60 if os.path.exists('scripts/x') and os.path.exists('tests'):
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
61 os.chdir('tests')
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
62 elif os.path.exists('../scripts/x') and os.path.exists('../tests'):
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
63 pass
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
64 else:
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
65 print "ERROR: Can't run tests in this directory!"
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
66 sys.exit(2)
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
67
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
68 class ExtractorTestError(Exception):
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
69 pass
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
70
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
71
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
72 class ExtractorTest(object):
7
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
73 def __init__(self, archive_filename, info):
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
74 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
75 self.arguments, self.shell_commands, self.shell_test = info
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
76
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
77 def get_results(self, commands):
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
78 status = subprocess.call(commands)
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
79 if status != 0:
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
80 return None
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
81 process = subprocess.Popen(['find'], stdout=subprocess.PIPE)
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
82 process.wait()
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
83 output = process.stdout.read(-1)
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
84 process.stdout.close()
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
85 return set(output.split('\n'))
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
86
7
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
87 def write_script(self, commands):
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
88 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
89 script.write("%s%s\n" % (SCRIPT_PROLOGUE, '\n'.join(commands)))
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
90 script.close()
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
91 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
92
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
93 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
94 self.write_script(self.shell_commands)
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
95 return self.get_results(['sh', TESTSCRIPT_NAME])
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
96
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
97 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
98 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
99 [self.archive_filename])
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
100
7
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
101 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
102 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
103 return 0
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
104 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
105 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
106
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
107 def clean(self):
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
108 status = subprocess.call(['find', '-mindepth', '1', '-maxdepth', '1',
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
109 '-type', 'd',
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
110 '!', '-name', 'CVS', '!', '-name', '.svn',
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
111 '-exec', 'rm', '-rf', '{}', ';'])
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
112 if status != 0:
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
113 raise ExtractorTestError("cleanup exited with status code %s" %
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
114 (status,))
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
115
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
116 def run(self):
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
117 self.clean()
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
118 expected = self.get_shell_results()
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 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
121 posttest_result = self.get_posttest_result()
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
122 self.clean()
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
123 if expected is None:
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
124 raise ExtractorTestError("could not get baseline results")
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
125 elif actual is None:
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
126 raise ExtractorTestError("could not get extractor results")
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
127 elif expected != actual:
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
128 print "FAILED:", self.archive_filename
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
129 print "Only in baseline results:"
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
130 print '\n'.join(expected.difference(actual))
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
131 print "Only in actual results:"
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
132 print '\n'.join(actual.difference(expected))
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
133 return False
7
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
134 elif posttest_result != 0:
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
135 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
136 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
137 print actual
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
138 return False
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
139 else:
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
140 print "Passed:", self.archive_filename
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
141 return True
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
142
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
143
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
144 successes = 0
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
145 failures = 0
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
146 testnames = tests.keys()
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
147 testnames.sort()
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
148 for testname in testnames:
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
149 test = ExtractorTest(testname, tests[testname])
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
150 if test.run():
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
151 successes += 1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
152 else:
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
153 failures += 1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
154 print "Totals: %s successes, %s failures" % (successes, failures)
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
155

mercurial