tests/compare.py

Sun, 30 Aug 2009 11:18:32 -0400

author
Brett Smith <brettcsmith@brettcsmith.org>
date
Sun, 30 Aug 2009 11:18:32 -0400
branch
trunk
changeset 122
2b676283366a
parent 121
957d19158b7e
permissions
-rw-r--r--

use the PyYAML library for tests, instead of Syck

1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
1 #!/usr/bin/env python
114
d2a28fe2a8ff use a real UTF-8 copyright symbol everywhere
Brett Smith <brettcsmith@brettcsmith.org>
parents: 107
diff changeset
2 # -*- coding: utf-8 -*-
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
3 #
42
4a4cab75d5e6 [svn] Update documentation.
brett
parents: 38
diff changeset
4 # compare.py -- High-level tests for dtrx.
121
957d19158b7e release dtrx 6.5
Brett Smith <brettcsmith@brettcsmith.org>
parents: 114
diff changeset
5 # Copyright © 2006-2009 Brett Smith <brettcsmith@brettcsmith.org>.
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
6 #
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
7 # 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
8 # under the terms of the GNU General Public License as published by the
28
4d88f2231d33 [svn] Change all the license notices from GPLv2 to GPLv3.
brett
parents: 20
diff changeset
9 # Free Software Foundation; either version 3 of the License, or (at your
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
10 # option) any later version.
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
11 #
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
12 # 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
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
15 # Public License for more details.
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
16 #
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
17 # You should have received a copy of the GNU General Public License along
42
4a4cab75d5e6 [svn] Update documentation.
brett
parents: 38
diff changeset
18 # with this program; if not, see <http://www.gnu.org/licenses/>.
1
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
17
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
21 import re
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
22 import subprocess
122
2b676283366a use the PyYAML library for tests, instead of Syck
Brett Smith <brettcsmith@brettcsmith.org>
parents: 121
diff changeset
23 import yaml
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
24 import sys
17
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
25 import tempfile
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
26
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
27 from sets import Set as set
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
28
19
bb6e9f4af1a5 [svn] Rename the program to dtrx.
brett
parents: 17
diff changeset
29 if os.path.exists('scripts/dtrx') and os.path.exists('tests'):
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
30 os.chdir('tests')
19
bb6e9f4af1a5 [svn] Rename the program to dtrx.
brett
parents: 17
diff changeset
31 elif os.path.exists('../scripts/dtrx') and os.path.exists('../tests'):
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
32 pass
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
33 else:
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
34 print "ERROR: Can't run tests in this directory!"
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
35 sys.exit(2)
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
36
19
bb6e9f4af1a5 [svn] Rename the program to dtrx.
brett
parents: 17
diff changeset
37 X_SCRIPT = os.path.realpath('../scripts/dtrx')
14
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
38 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
39 OUTCOMES = ['error', 'failed', 'passed']
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
40 TESTSCRIPT_NAME = 'testscript.sh'
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
41 SCRIPT_PROLOGUE = """#!/bin/sh
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
42 set -e
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
43 """
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
44
20
69c93c3e6972 [svn] If the archive contains one directory with the "wrong" name, ask the user
brett
parents: 19
diff changeset
45 input_buffer = tempfile.TemporaryFile()
17
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
46 output_buffer = tempfile.TemporaryFile()
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
47
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
48 class ExtractorTestError(Exception):
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
49 pass
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
50
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
51
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
52 class ExtractorTest(object):
14
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
53 def __init__(self, **kwargs):
20
69c93c3e6972 [svn] If the archive contains one directory with the "wrong" name, ask the user
brett
parents: 19
diff changeset
54 setattr(self, 'name', kwargs['name'])
69c93c3e6972 [svn] If the archive contains one directory with the "wrong" name, ask the user
brett
parents: 19
diff changeset
55 setattr(self, 'options', kwargs.get('options', '-n').split())
69c93c3e6972 [svn] If the archive contains one directory with the "wrong" name, ask the user
brett
parents: 19
diff changeset
56 setattr(self, 'filenames', kwargs.get('filenames', '').split())
17
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
57 for key in ('directory', 'prerun', 'posttest', 'baseline', 'error',
107
1b7450ae4c67 Support multiple greps and antigreps per test.
Brett Smith <brettcsmith@brettcsmith.org>
parents: 69
diff changeset
58 'input', 'output', 'cleanup'):
14
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
59 setattr(self, key, kwargs.get(key, None))
107
1b7450ae4c67 Support multiple greps and antigreps per test.
Brett Smith <brettcsmith@brettcsmith.org>
parents: 69
diff changeset
60 for key in ('grep', 'antigrep'):
1b7450ae4c67 Support multiple greps and antigreps per test.
Brett Smith <brettcsmith@brettcsmith.org>
parents: 69
diff changeset
61 value = kwargs.get(key, [])
1b7450ae4c67 Support multiple greps and antigreps per test.
Brett Smith <brettcsmith@brettcsmith.org>
parents: 69
diff changeset
62 if isinstance(value, str):
1b7450ae4c67 Support multiple greps and antigreps per test.
Brett Smith <brettcsmith@brettcsmith.org>
parents: 69
diff changeset
63 value = [value]
1b7450ae4c67 Support multiple greps and antigreps per test.
Brett Smith <brettcsmith@brettcsmith.org>
parents: 69
diff changeset
64 setattr(self, key, value)
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
65
20
69c93c3e6972 [svn] If the archive contains one directory with the "wrong" name, ask the user
brett
parents: 19
diff changeset
66 def get_results(self, commands, stdin=None):
17
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
67 print >>output_buffer, "Output from %s:" % (' '.join(commands),)
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
68 output_buffer.flush()
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
69 status = subprocess.call(commands, stdout=output_buffer,
20
69c93c3e6972 [svn] If the archive contains one directory with the "wrong" name, ask the user
brett
parents: 19
diff changeset
70 stderr=output_buffer, stdin=stdin)
17
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
71 process = subprocess.Popen(['find', '!', '-name', TESTSCRIPT_NAME],
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
72 stdout=subprocess.PIPE)
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
73 process.wait()
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
74 output = process.stdout.read(-1)
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
75 process.stdout.close()
17
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
76 return status, set(output.split('\n'))
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
77
7
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
78 def write_script(self, commands):
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
79 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
80 script.write("%s%s\n" % (SCRIPT_PROLOGUE, commands))
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
81 script.close()
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
82 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
83
30
1015bbd6dc5e [svn] If we can't figure out what the file is by mimetype, try using the file
brett
parents: 29
diff changeset
84 def run_script(self, key):
1015bbd6dc5e [svn] If we can't figure out what the file is by mimetype, try using the file
brett
parents: 29
diff changeset
85 commands = getattr(self, key)
1015bbd6dc5e [svn] If we can't figure out what the file is by mimetype, try using the file
brett
parents: 29
diff changeset
86 if commands is not None:
1015bbd6dc5e [svn] If we can't figure out what the file is by mimetype, try using the file
brett
parents: 29
diff changeset
87 if self.directory:
1015bbd6dc5e [svn] If we can't figure out what the file is by mimetype, try using the file
brett
parents: 29
diff changeset
88 directory_hint = '../'
1015bbd6dc5e [svn] If we can't figure out what the file is by mimetype, try using the file
brett
parents: 29
diff changeset
89 else:
1015bbd6dc5e [svn] If we can't figure out what the file is by mimetype, try using the file
brett
parents: 29
diff changeset
90 directory_hint = ''
1015bbd6dc5e [svn] If we can't figure out what the file is by mimetype, try using the file
brett
parents: 29
diff changeset
91 self.write_script(commands)
1015bbd6dc5e [svn] If we can't figure out what the file is by mimetype, try using the file
brett
parents: 29
diff changeset
92 subprocess.call(['sh', TESTSCRIPT_NAME, directory_hint])
1015bbd6dc5e [svn] If we can't figure out what the file is by mimetype, try using the file
brett
parents: 29
diff changeset
93
7
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
94 def get_shell_results(self):
30
1015bbd6dc5e [svn] If we can't figure out what the file is by mimetype, try using the file
brett
parents: 29
diff changeset
95 self.run_script('prerun')
14
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
96 self.write_script(self.baseline)
17
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
97 return self.get_results(['sh', TESTSCRIPT_NAME] + self.filenames)
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
98
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
99 def get_extractor_results(self):
30
1015bbd6dc5e [svn] If we can't figure out what the file is by mimetype, try using the file
brett
parents: 29
diff changeset
100 self.run_script('prerun')
20
69c93c3e6972 [svn] If the archive contains one directory with the "wrong" name, ask the user
brett
parents: 19
diff changeset
101 input_buffer.seek(0, 0)
69c93c3e6972 [svn] If the archive contains one directory with the "wrong" name, ask the user
brett
parents: 19
diff changeset
102 input_buffer.truncate()
69c93c3e6972 [svn] If the archive contains one directory with the "wrong" name, ask the user
brett
parents: 19
diff changeset
103 if self.input:
69c93c3e6972 [svn] If the archive contains one directory with the "wrong" name, ask the user
brett
parents: 19
diff changeset
104 input_buffer.write(self.input)
69c93c3e6972 [svn] If the archive contains one directory with the "wrong" name, ask the user
brett
parents: 19
diff changeset
105 if not self.input.endswith('\n'):
69c93c3e6972 [svn] If the archive contains one directory with the "wrong" name, ask the user
brett
parents: 19
diff changeset
106 input_buffer.write('\n')
69c93c3e6972 [svn] If the archive contains one directory with the "wrong" name, ask the user
brett
parents: 19
diff changeset
107 input_buffer.seek(0, 0)
69c93c3e6972 [svn] If the archive contains one directory with the "wrong" name, ask the user
brett
parents: 19
diff changeset
108 input_buffer.flush()
69c93c3e6972 [svn] If the archive contains one directory with the "wrong" name, ask the user
brett
parents: 19
diff changeset
109 return self.get_results([X_SCRIPT] + self.options + self.filenames,
69c93c3e6972 [svn] If the archive contains one directory with the "wrong" name, ask the user
brett
parents: 19
diff changeset
110 input_buffer)
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
111
7
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
112 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
113 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
114 return 0
14
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
115 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
116 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
117
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
118 def clean(self):
30
1015bbd6dc5e [svn] If we can't figure out what the file is by mimetype, try using the file
brett
parents: 29
diff changeset
119 self.run_script('cleanup')
14
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
120 if self.directory:
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
121 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
122 extra_options = ['!', '-name', TESTSCRIPT_NAME]
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
123 else:
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
124 target = ROOT_DIR
38
f637b9d24c21 [svn] Some more tests to make sure we always do the right thing in the
brett
parents: 30
diff changeset
125 extra_options = ['(', '(', '-type', 'd',
14
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
126 '!', '-name', 'CVS',
38
f637b9d24c21 [svn] Some more tests to make sure we always do the right thing in the
brett
parents: 30
diff changeset
127 '!', '-name', '.svn', ')',
f637b9d24c21 [svn] Some more tests to make sure we always do the right thing in the
brett
parents: 30
diff changeset
128 '-or', '-name', 'test-text',
f637b9d24c21 [svn] Some more tests to make sure we always do the right thing in the
brett
parents: 30
diff changeset
129 '-or', '-name', 'test-onefile', ')']
14
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
130 status = subprocess.call(['find', target,
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
131 '-mindepth', '1', '-maxdepth', '1'] +
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
132 extra_options +
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
133 ['-exec', 'rm', '-rf', '{}', ';'])
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
134 if status != 0:
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
135 raise ExtractorTestError("cleanup exited with status code %s" %
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
136 (status,))
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
137
14
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
138 def show_status(self, status, message=None):
17
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
139 raw_status = status.lower()
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
140 if raw_status != 'passed':
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
141 output_buffer.seek(0, 0)
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
142 sys.stdout.write(output_buffer.read(-1))
14
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
143 if message is None:
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
144 last_part = ''
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
145 else:
17
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
146 last_part = ': %s' % (message,)
14
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
147 print "%7s: %s%s" % (status, self.name, last_part)
17
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
148 return raw_status
14
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
149
17
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
150 def compare_results(self, actual):
7
1f3cb3845dfd [svn] Add a test for recursive extraction which also makes sure that we fix
brett
parents: 1
diff changeset
151 posttest_result = self.get_posttest_result()
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
152 self.clean()
17
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
153 status, expected = self.get_shell_results()
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
154 self.clean()
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
155 if expected != actual:
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
156 print >>output_buffer, "Only in baseline results:"
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
157 print >>output_buffer, '\n'.join(expected.difference(actual))
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
158 print >>output_buffer, "Only in actual results:"
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
159 print >>output_buffer, '\n'.join(actual.difference(expected))
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
160 return 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
161 elif posttest_result != 0:
17
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
162 print >>output_buffer, "Posttest gave status code", posttest_result
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
163 return self.show_status('FAILED')
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
164 return self.show_status('Passed')
14
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
165
17
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
166 def have_error_mismatch(self, status):
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
167 if self.error and (status == 0):
20
69c93c3e6972 [svn] If the archive contains one directory with the "wrong" name, ask the user
brett
parents: 19
diff changeset
168 return "dtrx did not return expected error"
17
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
169 elif (not self.error) and (status != 0):
20
69c93c3e6972 [svn] If the archive contains one directory with the "wrong" name, ask the user
brett
parents: 19
diff changeset
170 return "dtrx returned error code %s" % (status,)
17
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
171 return None
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
172
19
bb6e9f4af1a5 [svn] Rename the program to dtrx.
brett
parents: 17
diff changeset
173 def grep_output(self, output):
107
1b7450ae4c67 Support multiple greps and antigreps per test.
Brett Smith <brettcsmith@brettcsmith.org>
parents: 69
diff changeset
174 for pattern in self.grep:
1b7450ae4c67 Support multiple greps and antigreps per test.
Brett Smith <brettcsmith@brettcsmith.org>
parents: 69
diff changeset
175 if not re.search(pattern.replace(' ', '\\s+'), output,
1b7450ae4c67 Support multiple greps and antigreps per test.
Brett Smith <brettcsmith@brettcsmith.org>
parents: 69
diff changeset
176 re.MULTILINE):
1b7450ae4c67 Support multiple greps and antigreps per test.
Brett Smith <brettcsmith@brettcsmith.org>
parents: 69
diff changeset
177 return "output did not match %s" % (pattern)
1b7450ae4c67 Support multiple greps and antigreps per test.
Brett Smith <brettcsmith@brettcsmith.org>
parents: 69
diff changeset
178 for pattern in self.antigrep:
1b7450ae4c67 Support multiple greps and antigreps per test.
Brett Smith <brettcsmith@brettcsmith.org>
parents: 69
diff changeset
179 if re.search(pattern.replace(' ', '\\s+'), output, re.MULTILINE):
1b7450ae4c67 Support multiple greps and antigreps per test.
Brett Smith <brettcsmith@brettcsmith.org>
parents: 69
diff changeset
180 return "output matched antigrep %s" % (self.antigrep)
17
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
181 return None
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
182
19
bb6e9f4af1a5 [svn] Rename the program to dtrx.
brett
parents: 17
diff changeset
183 def check_output(self, output):
bb6e9f4af1a5 [svn] Rename the program to dtrx.
brett
parents: 17
diff changeset
184 if ((self.output is not None) and
bb6e9f4af1a5 [svn] Rename the program to dtrx.
brett
parents: 17
diff changeset
185 (self.output.strip() != output.strip())):
bb6e9f4af1a5 [svn] Rename the program to dtrx.
brett
parents: 17
diff changeset
186 return "output did not match provided text"
bb6e9f4af1a5 [svn] Rename the program to dtrx.
brett
parents: 17
diff changeset
187 return None
bb6e9f4af1a5 [svn] Rename the program to dtrx.
brett
parents: 17
diff changeset
188
17
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
189 def check_results(self):
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
190 output_buffer.seek(0, 0)
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
191 output_buffer.truncate()
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
192 self.clean()
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
193 status, actual = self.get_extractor_results()
19
bb6e9f4af1a5 [svn] Rename the program to dtrx.
brett
parents: 17
diff changeset
194 output_buffer.seek(0, 0)
bb6e9f4af1a5 [svn] Rename the program to dtrx.
brett
parents: 17
diff changeset
195 output_buffer.readline()
bb6e9f4af1a5 [svn] Rename the program to dtrx.
brett
parents: 17
diff changeset
196 output = output_buffer.read(-1)
bb6e9f4af1a5 [svn] Rename the program to dtrx.
brett
parents: 17
diff changeset
197 problem = (self.have_error_mismatch(status) or
bb6e9f4af1a5 [svn] Rename the program to dtrx.
brett
parents: 17
diff changeset
198 self.check_output(output) or self.grep_output(output))
17
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
199 if problem:
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
200 return self.show_status('FAILED', problem)
19
bb6e9f4af1a5 [svn] Rename the program to dtrx.
brett
parents: 17
diff changeset
201 if self.baseline:
bb6e9f4af1a5 [svn] Rename the program to dtrx.
brett
parents: 17
diff changeset
202 return self.compare_results(actual)
bb6e9f4af1a5 [svn] Rename the program to dtrx.
brett
parents: 17
diff changeset
203 else:
50
71bdbc1148af [svn] Run the cleanup script after the test even if there's no more comparing to do.
brett
parents: 42
diff changeset
204 self.clean()
19
bb6e9f4af1a5 [svn] Rename the program to dtrx.
brett
parents: 17
diff changeset
205 return self.show_status('Passed')
17
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
206
14
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
207 def run(self):
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
208 if self.directory:
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
209 os.mkdir(self.directory)
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
210 os.chdir(self.directory)
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
211 try:
17
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
212 result = self.check_results()
14
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
213 except ExtractorTestError, error:
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
214 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
215 if self.directory:
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
216 os.chdir(ROOT_DIR)
17
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
217 subprocess.call(['chmod', '-R', '700', self.directory])
14
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
218 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
219 return result
1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
220
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
221
14
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
222 test_db = open('tests.yml')
122
2b676283366a use the PyYAML library for tests, instead of Syck
Brett Smith <brettcsmith@brettcsmith.org>
parents: 121
diff changeset
223 test_data = yaml.load(test_db.read(-1))
14
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
224 test_db.close()
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
225 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
226 for original_data in test_data:
17
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
227 if (original_data.has_key('directory') or
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
228 (not original_data.has_key('baseline'))):
14
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
229 continue
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
230 data = original_data.copy()
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
231 data['name'] += ' in ..'
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
232 data['directory'] = 'inside-dir'
17
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
233 data['filenames'] = ' '.join(['../%s' % filename for filename in
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
234 data.get('filenames', '').split()])
14
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
235 tests.append(ExtractorTest(**data))
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
236 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
237 counts = {}
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
238 for outcome in OUTCOMES:
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
239 counts[outcome] = 0
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
240 for result in results:
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
241 counts[result] += 1
6f9e1bb59719 [svn] Add support for just decompressing files that are compressed. So, if you
brett
parents: 10
diff changeset
242 print " Totals:", ', '.join(["%s %s" % (counts[key], key) for key in OUTCOMES])
20
69c93c3e6972 [svn] If the archive contains one directory with the "wrong" name, ask the user
brett
parents: 19
diff changeset
243 input_buffer.close()
17
481a2b4be471 [svn] Lots of tests for various boundary cases, and slightly better handling for
brett
parents: 15
diff changeset
244 output_buffer.close()

mercurial