3 # Copyright (c) 2004 Trent Mick
5 """Test relicensing of inputs/... with relic.py."""
19 gInputsDir
= "relicense_inputs"
20 gOutputsDir
= "relicense_outputs"
21 gTmpDir
= "relicense_tmp"
26 class RelicInputsTestCase(unittest
.TestCase
):
28 if not os
.path
.exists(gTmpDir
):
32 testsupport
.rmtree(gTmpDir
)
35 def _testOneInputFile(self
, fname
):
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:
46 # Whitespace is stripped off the value.
47 optsfile
= os
.path
.join(gInputsDir
, fname
+'.options') # input options
51 print "*"*50, "relic '%s'" % fname
53 # Determine input options to use, if any.
55 if os
.path
.exists(optsfile
):
56 for line
in open(optsfile
, 'r').read().splitlines(0):
57 name
, value
= line
.split('=', 1)
59 try: # allow value to be a type other than string
65 print "*"*50, "options"
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
73 oldStdout
= sys
.stdout
74 oldStderr
= sys
.stderr
75 sys
.stdout
= StringIO
.StringIO()
76 sys
.stderr
= StringIO
.StringIO()
79 relic
.relicense([tmpfile
], **opts
)
80 except relic
.RelicError
, ex
:
85 stdout
= sys
.stdout
.getvalue()
86 stderr
= sys
.stderr
.getvalue()
87 sys
.stdout
= oldStdout
88 sys
.stderr
= oldStderr
90 print "*"*50, "stdout"
92 print "*"*50, "stderr"
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())
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
)
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())