Sat, 11 Nov 2006 18:42:19 -0500
[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 | 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', |
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 | 59 | |
60 | if os.path.exists('scripts/x') and os.path.exists('tests'): | |
61 | os.chdir('tests') | |
62 | elif os.path.exists('../scripts/x') and os.path.exists('../tests'): | |
63 | pass | |
64 | else: | |
65 | print "ERROR: Can't run tests in this directory!" | |
66 | sys.exit(2) | |
67 | ||
68 | class ExtractorTestError(Exception): | |
69 | pass | |
70 | ||
71 | ||
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 | 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 | 76 | |
77 | def get_results(self, commands): | |
78 | status = subprocess.call(commands) | |
79 | if status != 0: | |
80 | return None | |
81 | process = subprocess.Popen(['find'], stdout=subprocess.PIPE) | |
82 | process.wait() | |
83 | output = process.stdout.read(-1) | |
84 | process.stdout.close() | |
85 | return set(output.split('\n')) | |
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 | 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 | 90 | script.close() |
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 | 95 | return self.get_results(['sh', TESTSCRIPT_NAME]) |
96 | ||
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 | 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 | 107 | def clean(self): |
108 | status = subprocess.call(['find', '-mindepth', '1', '-maxdepth', '1', | |
109 | '-type', 'd', | |
110 | '!', '-name', 'CVS', '!', '-name', '.svn', | |
111 | '-exec', 'rm', '-rf', '{}', ';']) | |
112 | if status != 0: | |
113 | raise ExtractorTestError("cleanup exited with status code %s" % | |
114 | (status,)) | |
115 | ||
116 | def run(self): | |
117 | self.clean() | |
118 | expected = self.get_shell_results() | |
119 | self.clean() | |
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 | 122 | self.clean() |
123 | if expected is None: | |
124 | raise ExtractorTestError("could not get baseline results") | |
125 | elif actual is None: | |
126 | raise ExtractorTestError("could not get extractor results") | |
127 | elif expected != actual: | |
128 | print "FAILED:", self.archive_filename | |
129 | print "Only in baseline results:" | |
130 | print '\n'.join(expected.difference(actual)) | |
131 | print "Only in actual results:" | |
132 | print '\n'.join(actual.difference(expected)) | |
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 | 139 | else: |
140 | print "Passed:", self.archive_filename | |
141 | return True | |
142 | ||
143 | ||
144 | successes = 0 | |
145 | failures = 0 | |
146 | testnames = tests.keys() | |
147 | testnames.sort() | |
148 | for testname in testnames: | |
149 | test = ExtractorTest(testname, tests[testname]) | |
150 | if test.run(): | |
151 | successes += 1 | |
152 | else: | |
153 | failures += 1 | |
154 | print "Totals: %s successes, %s failures" % (successes, failures) | |
155 |