Files
Phoenix/bin/make-new-unittest-file.py
Per A. Brodtkorb e4e8bf8317 Fixes issue 1571:
Adding missing close for open.
If the "close()" call is missing after a "open(filename)" call, the filename isn't guaranteed to be closed before the interpreter exits.
This is generally a bad practice as explained here: https://stackoverflow.com/questions/7395542/is-explicitly-closing-files-important

Also replaced "fid=open(filename) fid.close()" statements for files with the safer
"with open(filename) as fid:" blocks. See https://www.python.org/dev/peps/pep-0343/
2020-03-23 17:16:44 +01:00

65 lines
1.5 KiB
Python
Executable File

#!/usr/bin/env python
#---------------------------------------------------------------------------
# Name: bin/make-new-unittest-file.py
# Author: Robin Dunn
#
# Created: 12-July-2012
# Copyright: (c) 2013-2020 by Robin Dunn
# License: wxWindows License
#---------------------------------------------------------------------------
import os
import sys
script_dir = os.path.dirname(__file__)
root_dir = os.path.abspath(os.path.join(script_dir, ".."))
usage = "usage: %prog [options] name module"
unitteststub = """\
import unittest
from unittests import wtc
import wx
#---------------------------------------------------------------------------
class %(name)s_Tests(wtc.WidgetTestCase):
# TODO: Remove this test and add real ones.
def test_%(name)s1(self):
self.fail("Unit tests for %(name)s not implemented yet.")
#---------------------------------------------------------------------------
if __name__ == '__main__':
unittest.main()
"""
def main(args):
if not args:
print("usage: %s names" % __file__)
return
for name in args:
writeFile(
os.path.join(root_dir, "unittests", "test_%s.py" % name),
unitteststub, dict(name=name))
def writeFile(filename, stub, values):
if os.path.exists(filename):
print("'%s' already exists. Exiting." % filename)
sys.exit(1)
with open(filename, 'w') as output:
output.write(stub % values)
print("Wrote %s" % filename)
if __name__ == '__main__':
main(sys.argv[1:])