Sun, 17 Dec 2006 09:20:02 -0500
[svn] Add tests for the case where we do recursive extraction of an archive
that's not in the current working directory, and fix the associated bug.
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 | ||
10
f0acfe12a0e2
[svn] Add tests for the case where we do recursive extraction of an archive
brett
parents:
8
diff
changeset
|
31 | tests = {'test-1.23.tar': ([], ['tar -xf $1'], []), |
f0acfe12a0e2
[svn] Add tests for the case where we do recursive extraction of an archive
brett
parents:
8
diff
changeset
|
32 | 'test-1.23.tar.gz': ([], ['tar -xzf $1'], []), |
7
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', |
10
f0acfe12a0e2
[svn] Add tests for the case where we do recursive extraction of an archive
brett
parents:
8
diff
changeset
|
35 | 'tar -jxf ../$1'], []), |
7
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', |
10
f0acfe12a0e2
[svn] Add tests for the case where we do recursive extraction of an archive
brett
parents:
8
diff
changeset
|
38 | 'unzip -q ../$1'], []), |
7
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 \ |
10
f0acfe12a0e2
[svn] Add tests for the case where we do recursive extraction of an archive
brett
parents:
8
diff
changeset
|
40 | <$1 2>/dev/null'], []), |
7
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', |
10
f0acfe12a0e2
[svn] Add tests for the case where we do recursive extraction of an archive
brett
parents:
8
diff
changeset
|
44 | 'ar x $TD/$1 data.tar.gz', |
7
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', |
10
f0acfe12a0e2
[svn] Add tests for the case where we do recursive extraction of an archive
brett
parents:
8
diff
changeset
|
52 | 'tar -jxf ../$1', |
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): | |
10
f0acfe12a0e2
[svn] Add tests for the case where we do recursive extraction of an archive
brett
parents:
8
diff
changeset
|
75 | def __init__(self, directory, archive_filename, info): |
f0acfe12a0e2
[svn] Add tests for the case where we do recursive extraction of an archive
brett
parents:
8
diff
changeset
|
76 | self.directory = directory |
f0acfe12a0e2
[svn] Add tests for the case where we do recursive extraction of an archive
brett
parents:
8
diff
changeset
|
77 | self.archive_filename = os.path.join(directory, archive_filename) |
7
1f3cb3845dfd
[svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents:
1
diff
changeset
|
78 | self.arguments, self.shell_commands, self.shell_test = info |
1 | 79 | |
80 | def get_results(self, commands): | |
81 | status = subprocess.call(commands) | |
82 | if status != 0: | |
83 | return None | |
84 | process = subprocess.Popen(['find'], stdout=subprocess.PIPE) | |
85 | process.wait() | |
86 | output = process.stdout.read(-1) | |
87 | process.stdout.close() | |
88 | return set(output.split('\n')) | |
89 | ||
7
1f3cb3845dfd
[svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents:
1
diff
changeset
|
90 | def write_script(self, commands): |
1 | 91 | 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
|
92 | script.write("%s%s\n" % (SCRIPT_PROLOGUE, '\n'.join(commands))) |
1 | 93 | script.close() |
94 | 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
|
95 | |
1f3cb3845dfd
[svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents:
1
diff
changeset
|
96 | 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
|
97 | self.write_script(self.shell_commands) |
10
f0acfe12a0e2
[svn] Add tests for the case where we do recursive extraction of an archive
brett
parents:
8
diff
changeset
|
98 | return self.get_results(['sh', TESTSCRIPT_NAME, self.archive_filename]) |
1 | 99 | |
100 | def get_extractor_results(self): | |
10
f0acfe12a0e2
[svn] Add tests for the case where we do recursive extraction of an archive
brett
parents:
8
diff
changeset
|
101 | script = os.path.join(self.directory, '../scripts/x') |
f0acfe12a0e2
[svn] Add tests for the case where we do recursive extraction of an archive
brett
parents:
8
diff
changeset
|
102 | return self.get_results([script] + self.arguments + |
7
1f3cb3845dfd
[svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents:
1
diff
changeset
|
103 | [self.archive_filename]) |
1 | 104 | |
7
1f3cb3845dfd
[svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents:
1
diff
changeset
|
105 | 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
|
106 | 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
|
107 | return 0 |
1f3cb3845dfd
[svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents:
1
diff
changeset
|
108 | 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
|
109 | 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
|
110 | |
1 | 111 | def clean(self): |
112 | status = subprocess.call(['find', '-mindepth', '1', '-maxdepth', '1', | |
113 | '-type', 'd', | |
114 | '!', '-name', 'CVS', '!', '-name', '.svn', | |
115 | '-exec', 'rm', '-rf', '{}', ';']) | |
116 | if status != 0: | |
117 | raise ExtractorTestError("cleanup exited with status code %s" % | |
118 | (status,)) | |
119 | ||
120 | def run(self): | |
121 | self.clean() | |
122 | expected = self.get_shell_results() | |
123 | self.clean() | |
124 | 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
|
125 | posttest_result = self.get_posttest_result() |
1 | 126 | self.clean() |
127 | if expected is None: | |
128 | raise ExtractorTestError("could not get baseline results") | |
129 | elif actual is None: | |
130 | raise ExtractorTestError("could not get extractor results") | |
131 | elif expected != actual: | |
132 | print "FAILED:", self.archive_filename | |
133 | print "Only in baseline results:" | |
134 | print '\n'.join(expected.difference(actual)) | |
135 | print "Only in actual results:" | |
136 | print '\n'.join(actual.difference(expected)) | |
137 | return False | |
7
1f3cb3845dfd
[svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents:
1
diff
changeset
|
138 | elif posttest_result != 0: |
1f3cb3845dfd
[svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents:
1
diff
changeset
|
139 | 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
|
140 | 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
|
141 | print actual |
1f3cb3845dfd
[svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents:
1
diff
changeset
|
142 | return False |
1 | 143 | else: |
144 | print "Passed:", self.archive_filename | |
145 | return True | |
146 | ||
147 | ||
10
f0acfe12a0e2
[svn] Add tests for the case where we do recursive extraction of an archive
brett
parents:
8
diff
changeset
|
148 | def run_tests(directory, testnames): |
f0acfe12a0e2
[svn] Add tests for the case where we do recursive extraction of an archive
brett
parents:
8
diff
changeset
|
149 | successes = 0 |
f0acfe12a0e2
[svn] Add tests for the case where we do recursive extraction of an archive
brett
parents:
8
diff
changeset
|
150 | failures = 0 |
f0acfe12a0e2
[svn] Add tests for the case where we do recursive extraction of an archive
brett
parents:
8
diff
changeset
|
151 | for testname in testnames: |
f0acfe12a0e2
[svn] Add tests for the case where we do recursive extraction of an archive
brett
parents:
8
diff
changeset
|
152 | test = ExtractorTest(directory, testname, tests[testname]) |
f0acfe12a0e2
[svn] Add tests for the case where we do recursive extraction of an archive
brett
parents:
8
diff
changeset
|
153 | if test.run(): |
f0acfe12a0e2
[svn] Add tests for the case where we do recursive extraction of an archive
brett
parents:
8
diff
changeset
|
154 | successes += 1 |
f0acfe12a0e2
[svn] Add tests for the case where we do recursive extraction of an archive
brett
parents:
8
diff
changeset
|
155 | else: |
f0acfe12a0e2
[svn] Add tests for the case where we do recursive extraction of an archive
brett
parents:
8
diff
changeset
|
156 | failures += 1 |
f0acfe12a0e2
[svn] Add tests for the case where we do recursive extraction of an archive
brett
parents:
8
diff
changeset
|
157 | return successes, failures |
f0acfe12a0e2
[svn] Add tests for the case where we do recursive extraction of an archive
brett
parents:
8
diff
changeset
|
158 | |
f0acfe12a0e2
[svn] Add tests for the case where we do recursive extraction of an archive
brett
parents:
8
diff
changeset
|
159 | results = [] |
1 | 160 | testnames = tests.keys() |
161 | testnames.sort() | |
10
f0acfe12a0e2
[svn] Add tests for the case where we do recursive extraction of an archive
brett
parents:
8
diff
changeset
|
162 | results.append(run_tests('.', testnames)) |
f0acfe12a0e2
[svn] Add tests for the case where we do recursive extraction of an archive
brett
parents:
8
diff
changeset
|
163 | os.mkdir('inside-dir') |
f0acfe12a0e2
[svn] Add tests for the case where we do recursive extraction of an archive
brett
parents:
8
diff
changeset
|
164 | os.chdir('inside-dir') |
f0acfe12a0e2
[svn] Add tests for the case where we do recursive extraction of an archive
brett
parents:
8
diff
changeset
|
165 | results.append(run_tests('..', testnames)) |
f0acfe12a0e2
[svn] Add tests for the case where we do recursive extraction of an archive
brett
parents:
8
diff
changeset
|
166 | os.chdir('..') |
f0acfe12a0e2
[svn] Add tests for the case where we do recursive extraction of an archive
brett
parents:
8
diff
changeset
|
167 | subprocess.call(['rm', '-rf', 'inside-dir']) |
f0acfe12a0e2
[svn] Add tests for the case where we do recursive extraction of an archive
brett
parents:
8
diff
changeset
|
168 | print "Totals: %s successes, %s failures" % \ |
f0acfe12a0e2
[svn] Add tests for the case where we do recursive extraction of an archive
brett
parents:
8
diff
changeset
|
169 | tuple([sum(total) for total in zip(*results)]) |