tests/compare.py

Sun, 29 Oct 2006 19:34:46 -0500

author
brett
date
Sun, 29 Oct 2006 19:34:46 -0500
branch
trunk
changeset 1
a86a0cb0dd57
child 7
1f3cb3845dfd
permissions
-rw-r--r--

[svn] Repository reorganization to make tags easy

1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
1 #!/usr/bin/env python
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
2 #
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
3 # compare.py -- High-level tests for x.
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
4 # Copyright (c) 2006 Brett Smith <brettcsmith@brettcsmith.org>.
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
5 #
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
6 # 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
7 # under the terms of the GNU General Public License as published by the
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
8 # Free Software Foundation; either version 2 of the License, or (at your
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
9 # option) any later version.
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
10 #
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
11 # 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
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
14 # Public License for more details.
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
15 #
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
16 # You should have received a copy of the GNU General Public License along
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
17 # with this program; if not, write to the Free Software Foundation, Inc.,
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
18 # 51 Franklin Street, 5th Floor, Boston, MA, 02111.
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
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
21 import subprocess
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
22 import sys
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
23
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
24 from sets import Set as set
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
25
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
26 TESTSCRIPT_NAME = 'testscript.sh'
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
27 SCRIPT_PROLOGUE = """#!/bin/sh
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
28 set -e
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
29 """
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
30
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
31 tests = {'test-1.23.tar': ['tar -xf test-1.23.tar'],
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
32 'test-1.23.tar.gz': ['tar -xzf test-1.23.tar.gz'],
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
33 'test-1.23.tar.bz2': ['mkdir test-1.23',
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
34 'cd test-1.23',
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
35 'tar -jxf ../test-1.23.tar.bz2'],
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
36 'test-1.23.zip': ['mkdir test-1.23',
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
37 'cd test-1.23',
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
38 'unzip -q ../test-1.23.zip'],
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
39 'test-1.23.cpio': ['cpio -i --make-directories \
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
40 <test-1.23.cpio 2>/dev/null'],
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
41 'test-1.23_all.deb': ['TD=$PWD',
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
42 'mkdir test-1.23',
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
43 'cd /tmp',
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
44 'ar x $TD/test-1.23_all.deb data.tar.gz',
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
45 'cd $TD/test-1.23',
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
46 'tar -zxf /tmp/data.tar.gz',
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
47 'rm /tmp/data.tar.gz']}
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
48
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
49 if os.path.exists('scripts/x') and os.path.exists('tests'):
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
50 os.chdir('tests')
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
51 elif os.path.exists('../scripts/x') and os.path.exists('../tests'):
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
52 pass
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
53 else:
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
54 print "ERROR: Can't run tests in this directory!"
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
55 sys.exit(2)
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
56
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
57 class ExtractorTestError(Exception):
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
58 pass
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
59
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
60
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
61 class ExtractorTest(object):
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
62 def __init__(self, archive_filename, commands):
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
63 self.archive_filename = archive_filename
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
64 self.shell_commands = commands
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
65
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
66 def get_results(self, commands):
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
67 status = subprocess.call(commands)
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
68 if status != 0:
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
69 return None
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
70 process = subprocess.Popen(['find'], stdout=subprocess.PIPE)
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
71 process.wait()
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
72 output = process.stdout.read(-1)
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
73 process.stdout.close()
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
74 return set(output.split('\n'))
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
75
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
76 def get_shell_results(self):
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
77 script = open(TESTSCRIPT_NAME, 'w')
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
78 script.write("%s%s\n" % (SCRIPT_PROLOGUE,
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
79 '\n'.join(self.shell_commands)))
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
80 script.close()
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
81 subprocess.call(['chmod', 'u+w', TESTSCRIPT_NAME])
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
82 return self.get_results(['sh', TESTSCRIPT_NAME])
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
83
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
84 def get_extractor_results(self):
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
85 return self.get_results(['../scripts/x', self.archive_filename])
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
86
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
87 def clean(self):
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
88 status = subprocess.call(['find', '-mindepth', '1', '-maxdepth', '1',
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
89 '-type', 'd',
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
90 '!', '-name', 'CVS', '!', '-name', '.svn',
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
91 '-exec', 'rm', '-rf', '{}', ';'])
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
92 if status != 0:
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
93 raise ExtractorTestError("cleanup exited with status code %s" %
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
94 (status,))
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
95
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
96 def run(self):
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
97 self.clean()
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
98 expected = self.get_shell_results()
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
99 self.clean()
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
100 actual = self.get_extractor_results()
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
101 self.clean()
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
102 if expected is None:
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
103 raise ExtractorTestError("could not get baseline results")
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
104 elif actual is None:
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
105 raise ExtractorTestError("could not get extractor results")
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
106 elif expected != actual:
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
107 print "FAILED:", self.archive_filename
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
108 print "Only in baseline results:"
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
109 print '\n'.join(expected.difference(actual))
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
110 print "Only in actual results:"
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
111 print '\n'.join(actual.difference(expected))
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
112 return False
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
113 else:
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
114 print "Passed:", self.archive_filename
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
115 return True
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
116
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
117
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
118 successes = 0
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
119 failures = 0
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
120 testnames = tests.keys()
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
121 testnames.sort()
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
122 for testname in testnames:
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
123 test = ExtractorTest(testname, tests[testname])
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
124 if test.run():
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
125 successes += 1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
126 else:
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
127 failures += 1
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
128 print "Totals: %s successes, %s failures" % (successes, failures)
a86a0cb0dd57 [svn] Repository reorganization to make tags easy
brett
parents:
diff changeset
129

mercurial