2 # -*- coding: utf-8 -*-
4 from translate
.convert
import convert
9 class TestConvertCommand
:
10 """Tests running actual commands on files"""
11 convertmodule
= convert
12 defaultoptions
= {"progress": "none", "psyco": "none"}
14 def setup_method(self
, method
):
15 """creates a clean test directory for the given method"""
16 self
.testdir
= "%s_%s" % (self
.__class
__.__name
__, method
.__name
__)
18 os
.mkdir(self
.testdir
)
19 self
.rundir
= os
.path
.abspath(os
.getcwd())
21 def teardown_method(self
, method
):
22 """removes the test directory for the given method"""
27 """removes the test directory"""
28 if os
.path
.exists(self
.testdir
):
29 for dirpath
, subdirs
, filenames
in os
.walk(self
.testdir
, topdown
=False):
30 for name
in filenames
:
31 os
.remove(os
.path
.join(dirpath
, name
))
33 os
.rmdir(os
.path
.join(dirpath
, name
))
34 if os
.path
.exists(self
.testdir
): os
.rmdir(self
.testdir
)
35 assert not os
.path
.exists(self
.testdir
)
37 def run_command(self
, *argv
, **kwargs
):
38 """runs the command via the main function, passing self.defaultoptions and keyword arguments as --long options and argv arguments straight"""
39 os
.chdir(self
.testdir
)
41 kwoptions
= getattr(self
, "defaultoptions", {}).copy()
42 kwoptions
.update(kwargs
)
43 for key
, value
in kwoptions
.iteritems():
45 argv
.append("--%s" % key
)
47 argv
.append("--%s=%s" % (key
, value
))
49 self
.convertmodule
.main(argv
)
53 def get_testfilename(self
, filename
):
54 """gets the path to the test file"""
55 return os
.path
.join(self
.testdir
, filename
)
57 def open_testfile(self
, filename
, mode
="r"):
58 """opens the given filename in the testdirectory in the given mode"""
59 filename
= self
.get_testfilename(filename
)
60 if not mode
.startswith("r"):
61 subdir
= os
.path
.dirname(filename
)
63 if not os
.path
.isdir(subdir
):
64 for part
in subdir
.split(os
.sep
):
65 currentpath
= os
.path
.join(currentpath
, part
)
66 if not os
.path
.isdir(currentpath
):
68 return open(filename
, mode
)
70 def create_testfile(self
, filename
, contents
):
71 """creates the given file in the testdirectory with the given contents"""
72 testfile
= self
.open_testfile(filename
, "w")
73 testfile
.write(contents
)
75 def read_testfile(self
, filename
):
76 """reads the given file in the testdirectory and returns the contents"""
77 testfile
= open(self
.get_testfilename(filename
))
78 return testfile
.read()
80 def help_check(self
, options
, option
, last
=False):
81 """check that a help string occurs and remove it"""
82 assert option
in options
84 for line
in options
.splitlines():
85 if option
in line
or not line
.lstrip().startswith("-"):
87 newoptions
.append(line
)
89 assert newoptions
== []
90 return "\n".join(newoptions
)
93 """tests getting help (returning the help_string so further tests can be done)"""
95 helpfile
= self
.open_testfile("help.txt", "w")
98 test
.raises(SystemExit, self
.run_command
, help=True)
102 help_string
= self
.read_testfile("help.txt")
104 convertsummary
= self
.convertmodule
.__doc
__.split("\n")[0]
105 # the convertsummary might be wrapped. this will probably unwrap it
106 assert convertsummary
in help_string
.replace("\n", " ")
107 usageline
= help_string
[:help_string
.find("\n")]
108 # Different versions of optparse might contain either upper or
109 # lowercase versions of 'Usage:' and 'Options:', so we need to take
111 assert (usageline
.startswith("Usage: ") or usageline
.startswith("usage: ")) \
112 and "[--version] [-h|--help]" in usageline
113 options
= help_string
[help_string
.find("ptions:\n"):]
114 options
= options
[options
.find("\n")+1:]
115 options
= self
.help_check(options
, "--progress=PROGRESS")
116 options
= self
.help_check(options
, "--version")
117 options
= self
.help_check(options
, "-h, --help")
118 options
= self
.help_check(options
, "--manpage")
119 options
= self
.help_check(options
, "--errorlevel=ERRORLEVEL")
120 options
= self
.help_check(options
, "--psyco=MODE")
121 options
= self
.help_check(options
, "-i INPUT, --input=INPUT")
122 options
= self
.help_check(options
, "-x EXCLUDE, --exclude=EXCLUDE")
123 options
= self
.help_check(options
, "-o OUTPUT, --output=OUTPUT")