Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / tools / relic / test / test_relicense_inputs.py
blob2158fc9552f10d7abe04913da80d019f601c39be
1 #!/usr/bin/env python
3 # Copyright (c) 2004 Trent Mick
5 """Test relicensing of inputs/... with relic.py."""
7 import sys
8 import os
9 import unittest
10 import difflib
11 import pprint
12 import shutil
13 import StringIO
15 import testsupport
17 #---- globals
19 gInputsDir = "relicense_inputs"
20 gOutputsDir = "relicense_outputs"
21 gTmpDir = "relicense_tmp"
24 #----- test cases
26 class RelicInputsTestCase(unittest.TestCase):
27 def setUp(self):
28 if not os.path.exists(gTmpDir):
29 os.mkdir(gTmpDir)
31 def tearDown(self):
32 testsupport.rmtree(gTmpDir)
35 def _testOneInputFile(self, fname):
36 import relic
37 _debug = 0 # Set to true to dump status info for each test run.
39 infile = os.path.join(gInputsDir, fname) # input
40 outfile = os.path.join(gOutputsDir, fname) # expected output
41 tmpfile = os.path.join(gTmpDir, fname) # actual output
42 errfile = os.path.join(gOutputsDir, fname+'.error') # expected error
43 # An options file is a set of kwargs for the relic.relicense()
44 # method call. One key-value pair per-line like this:
45 # key=value
46 # Whitespace is stripped off the value.
47 optsfile = os.path.join(gInputsDir, fname+'.options') # input options
49 if _debug:
50 print
51 print "*"*50, "relic '%s'" % fname
53 # Determine input options to use, if any.
54 opts = {}
55 if os.path.exists(optsfile):
56 for line in open(optsfile, 'r').read().splitlines(0):
57 name, value = line.split('=', 1)
58 value = value.strip()
59 try: # allow value to be a type other than string
60 value = eval(value)
61 except Exception:
62 pass
63 opts[name] = value
64 if _debug:
65 print "*"*50, "options"
66 pprint.pprint(opts)
68 # Copy the input file to the tmp location where relicensing is done.
69 shutil.copy(infile, tmpfile)
71 # Relicense the file, capturing stdout and stderr and any possible
72 # error.
73 oldStdout = sys.stdout
74 oldStderr = sys.stderr
75 sys.stdout = StringIO.StringIO()
76 sys.stderr = StringIO.StringIO()
77 try:
78 try:
79 relic.relicense([tmpfile], **opts)
80 except relic.RelicError, ex:
81 error = ex
82 else:
83 error = None
84 finally:
85 stdout = sys.stdout.getvalue()
86 stderr = sys.stderr.getvalue()
87 sys.stdout = oldStdout
88 sys.stderr = oldStderr
89 if _debug:
90 print "*"*50, "stdout"
91 print stdout
92 print "*"*50, "stderr"
93 print stderr
94 print "*"*50, "error"
95 print str(error)
96 print "*" * 50
98 # Verify that the results are as expected.
99 if os.path.exists(outfile) and error:
100 self.fail("relicensing '%s' raised an error but success was "
101 "expected: error='%s'" % (fname, str(error)))
102 elif os.path.exists(outfile):
103 expected = open(outfile, 'r').readlines()
104 actual = open(tmpfile, 'r').readlines()
105 if expected != actual:
106 diff = list(difflib.ndiff(expected, actual))
107 self.fail("%r != %r:\n%s"\
108 % (outfile, tmpfile, pprint.pformat(diff)))
109 elif os.path.exists(errfile):
110 # There is no reference output file. This means that processing
111 # this file is expected to fail.
112 expectedError = open(errfile, 'r').read()
113 actualError = str(error)
114 self.failUnlessEqual(actualError.strip(), expectedError.strip())
115 else:
116 self.fail("No reference ouput file or error file for '%s'." % infile)
118 # Ensure next test file gets a clean relic.
119 del sys.modules['relic']
122 #for fname in ["separated_license_comment_blocks.pl"]:
123 for fname in os.listdir(gInputsDir):
124 if fname.endswith(".options"): continue # skip input option files
125 testFunction = lambda self, fname=fname: _testOneInputFile(self, fname)
126 name = 'test_relicense_'+fname
127 setattr(RelicInputsTestCase, name, testFunction)
130 #---- mainline
132 def suite():
133 """Return a unittest.TestSuite to be used by test.py."""
134 return unittest.makeSuite(RelicInputsTestCase)
136 if __name__ == "__main__":
137 runner = unittest.TextTestRunner(sys.stdout, verbosity=2)
138 result = runner.run(suite())