Mon, 13 Nov 2006 23:06:30 -0500
[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 | 1 | #!/usr/bin/env python |
2 | # | |
3 | # compare.py -- High-level tests for x. | |
4 | # Copyright (c) 2006 Brett Smith <brettcsmith@brettcsmith.org>. | |
5 | # | |
6 | # This program is free software; you can redistribute it and/or modify it | |
7 | # under the terms of the GNU General Public License as published by the | |
8 | # Free Software Foundation; either version 2 of the License, or (at your | |
9 | # option) any later version. | |
10 | # | |
11 | # This program is distributed in the hope that it will be useful, but | |
12 | # WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | |
14 | # Public License for more details. | |
15 | # | |
16 | # You should have received a copy of the GNU General Public License along | |
17 | # with this program; if not, write to the Free Software Foundation, Inc., | |
18 | # 51 Franklin Street, 5th Floor, Boston, MA, 02111. | |
19 | ||
20 | import os | |
21 | import subprocess | |
22 | import sys | |
23 | ||
24 | from sets import Set as set | |
25 | ||
26 | TESTSCRIPT_NAME = 'testscript.sh' | |
27 | SCRIPT_PROLOGUE = """#!/bin/sh | |
28 | set -e | |
29 | """ | |
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 | 61 | |
62 | if os.path.exists('scripts/x') and os.path.exists('tests'): | |
63 | os.chdir('tests') | |
64 | elif os.path.exists('../scripts/x') and os.path.exists('../tests'): | |
65 | pass | |
66 | else: | |
67 | print "ERROR: Can't run tests in this directory!" | |
68 | sys.exit(2) | |
69 | ||
70 | class ExtractorTestError(Exception): | |
71 | pass | |
72 | ||
73 | ||
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 | 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 | 78 | |
79 | def get_results(self, commands): | |
80 | status = subprocess.call(commands) | |
81 | if status != 0: | |
82 | return None | |
83 | process = subprocess.Popen(['find'], stdout=subprocess.PIPE) | |
84 | process.wait() | |
85 | output = process.stdout.read(-1) | |
86 | process.stdout.close() | |
87 | return set(output.split('\n')) | |
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 | 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 | 92 | script.close() |
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 | 97 | return self.get_results(['sh', TESTSCRIPT_NAME]) |
98 | ||
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 | 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 | 109 | def clean(self): |
110 | status = subprocess.call(['find', '-mindepth', '1', '-maxdepth', '1', | |
111 | '-type', 'd', | |
112 | '!', '-name', 'CVS', '!', '-name', '.svn', | |
113 | '-exec', 'rm', '-rf', '{}', ';']) | |
114 | if status != 0: | |
115 | raise ExtractorTestError("cleanup exited with status code %s" % | |
116 | (status,)) | |
117 | ||
118 | def run(self): | |
119 | self.clean() | |
120 | expected = self.get_shell_results() | |
121 | self.clean() | |
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 | 124 | self.clean() |
125 | if expected is None: | |
126 | raise ExtractorTestError("could not get baseline results") | |
127 | elif actual is None: | |
128 | raise ExtractorTestError("could not get extractor results") | |
129 | elif expected != actual: | |
130 | print "FAILED:", self.archive_filename | |
131 | print "Only in baseline results:" | |
132 | print '\n'.join(expected.difference(actual)) | |
133 | print "Only in actual results:" | |
134 | print '\n'.join(actual.difference(expected)) | |
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 | 141 | else: |
142 | print "Passed:", self.archive_filename | |
143 | return True | |
144 | ||
145 | ||
146 | successes = 0 | |
147 | failures = 0 | |
148 | testnames = tests.keys() | |
149 | testnames.sort() | |
150 | for testname in testnames: | |
151 | test = ExtractorTest(testname, tests[testname]) | |
152 | if test.run(): | |
153 | successes += 1 | |
154 | else: | |
155 | failures += 1 | |
156 | print "Totals: %s successes, %s failures" % (successes, failures) | |
157 |