Sun, 31 Dec 2006 19:20:26 -0500
[svn] Add support for just decompressing files that are compressed. So, if you
have foobar.txt.gz, running x on it will give you foobar.txt.
I also moved the test definitions to a YAML file, which makes them easier
to write and understand. Hopefully.
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 | |
14
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
22 | import syck |
1 | 23 | import sys |
24 | ||
25 | from sets import Set as set | |
26 | ||
27 | if os.path.exists('scripts/x') and os.path.exists('tests'): | |
28 | os.chdir('tests') | |
29 | elif os.path.exists('../scripts/x') and os.path.exists('../tests'): | |
30 | pass | |
31 | else: | |
32 | print "ERROR: Can't run tests in this directory!" | |
33 | sys.exit(2) | |
34 | ||
14
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
35 | X_SCRIPT = os.path.realpath('../scripts/x') |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
36 | ROOT_DIR = os.path.realpath(os.curdir) |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
37 | OUTCOMES = ['error', 'failed', 'passed'] |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
38 | TESTSCRIPT_NAME = 'testscript.sh' |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
39 | SCRIPT_PROLOGUE = """#!/bin/sh |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
40 | set -e |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
41 | """ |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
42 | |
1 | 43 | class ExtractorTestError(Exception): |
44 | pass | |
45 | ||
46 | ||
47 | class ExtractorTest(object): | |
14
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
48 | def __init__(self, **kwargs): |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
49 | for key in ('name', 'filename', 'baseline'): |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
50 | setattr(self, key, kwargs[key]) |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
51 | for key in ('directory', 'prerun', 'posttest'): |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
52 | setattr(self, key, kwargs.get(key, None)) |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
53 | for key in ('options',): |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
54 | setattr(self, key, kwargs.get(key, '').split()) |
1 | 55 | |
56 | def get_results(self, commands): | |
57 | status = subprocess.call(commands) | |
58 | if status != 0: | |
59 | return None | |
60 | process = subprocess.Popen(['find'], stdout=subprocess.PIPE) | |
61 | process.wait() | |
62 | output = process.stdout.read(-1) | |
63 | process.stdout.close() | |
64 | return set(output.split('\n')) | |
65 | ||
7
1f3cb3845dfd
[svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents:
1
diff
changeset
|
66 | def write_script(self, commands): |
1 | 67 | script = open(TESTSCRIPT_NAME, 'w') |
14
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
68 | script.write("%s%s\n" % (SCRIPT_PROLOGUE, commands)) |
1 | 69 | script.close() |
70 | 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
|
71 | |
1f3cb3845dfd
[svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents:
1
diff
changeset
|
72 | def get_shell_results(self): |
14
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
73 | self.write_script(self.baseline) |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
74 | return self.get_results(['sh', TESTSCRIPT_NAME, self.filename]) |
1 | 75 | |
76 | def get_extractor_results(self): | |
14
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
77 | if self.prerun: |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
78 | self.write_script(self.prerun) |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
79 | subprocess.call(['sh', TESTSCRIPT_NAME]) |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
80 | return self.get_results([X_SCRIPT] + self.options + [self.filename]) |
1 | 81 | |
7
1f3cb3845dfd
[svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents:
1
diff
changeset
|
82 | def get_posttest_result(self): |
14
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
83 | if not self.posttest: |
7
1f3cb3845dfd
[svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents:
1
diff
changeset
|
84 | return 0 |
14
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
85 | self.write_script(self.posttest) |
7
1f3cb3845dfd
[svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents:
1
diff
changeset
|
86 | 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
|
87 | |
1 | 88 | def clean(self): |
14
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
89 | if self.directory: |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
90 | target = os.path.join(ROOT_DIR, self.directory) |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
91 | extra_options = ['!', '-name', TESTSCRIPT_NAME] |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
92 | else: |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
93 | target = ROOT_DIR |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
94 | extra_options = ['-type', 'd', |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
95 | '!', '-name', 'CVS', |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
96 | '!', '-name', '.svn'] |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
97 | status = subprocess.call(['find', target, |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
98 | '-mindepth', '1', '-maxdepth', '1'] + |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
99 | extra_options + |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
100 | ['-exec', 'rm', '-rf', '{}', ';']) |
1 | 101 | if status != 0: |
102 | raise ExtractorTestError("cleanup exited with status code %s" % | |
103 | (status,)) | |
104 | ||
14
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
105 | def show_status(self, status, message=None): |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
106 | if message is None: |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
107 | last_part = '' |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
108 | else: |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
109 | last_part = ': ' + message |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
110 | print "%7s: %s%s" % (status, self.name, last_part) |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
111 | return status.lower() |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
112 | |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
113 | def compare_results(self): |
1 | 114 | self.clean() |
115 | expected = self.get_shell_results() | |
116 | self.clean() | |
117 | 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
|
118 | posttest_result = self.get_posttest_result() |
1 | 119 | self.clean() |
120 | if expected is None: | |
121 | raise ExtractorTestError("could not get baseline results") | |
122 | elif actual is None: | |
123 | raise ExtractorTestError("could not get extractor results") | |
124 | elif expected != actual: | |
14
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
125 | result = self.show_status('FAILED') |
1 | 126 | print "Only in baseline results:" |
127 | print '\n'.join(expected.difference(actual)) | |
128 | print "Only in actual results:" | |
129 | print '\n'.join(actual.difference(expected)) | |
7
1f3cb3845dfd
[svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents:
1
diff
changeset
|
130 | elif posttest_result != 0: |
14
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
131 | result = self.show_status('FAILED') |
7
1f3cb3845dfd
[svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents:
1
diff
changeset
|
132 | print "Posttest returned status code", posttest_result |
1 | 133 | else: |
14
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
134 | result = self.show_status('Passed') |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
135 | return result |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
136 | |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
137 | def run(self): |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
138 | if self.directory: |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
139 | os.mkdir(self.directory) |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
140 | os.chdir(self.directory) |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
141 | try: |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
142 | result = self.compare_results() |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
143 | except ExtractorTestError, error: |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
144 | result = self.show_status('ERROR', error) |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
145 | if self.directory: |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
146 | os.chdir(ROOT_DIR) |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
147 | subprocess.call(['rm', '-rf', self.directory]) |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
148 | return result |
1 | 149 | |
150 | ||
14
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
151 | test_db = open('tests.yml') |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
152 | test_data = syck.load(test_db.read(-1)) |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
153 | test_db.close() |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
154 | tests = [ExtractorTest(**data) for data in test_data] |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
155 | for original_data in test_data: |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
156 | if original_data.has_key('directory'): |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
157 | continue |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
158 | data = original_data.copy() |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
159 | data['name'] += ' in ..' |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
160 | data['directory'] = 'inside-dir' |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
161 | data['filename'] = os.path.join('..', data['filename']) |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
162 | tests.append(ExtractorTest(**data)) |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
163 | results = [test.run() for test in tests] |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
164 | counts = {} |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
165 | for outcome in OUTCOMES: |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
166 | counts[outcome] = 0 |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
167 | for result in results: |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
168 | counts[result] += 1 |
6f9e1bb59719
[svn] Add support for just decompressing files that are compressed. So, if you
brett
parents:
10
diff
changeset
|
169 | print " Totals:", ', '.join(["%s %s" % (counts[key], key) for key in OUTCOMES]) |