Package translate :: Package convert :: Module test_convert
[hide private]
[frames] | no frames]

Source Code for Module translate.convert.test_convert

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3   
  4  from translate.convert import convert 
  5  import os 
  6  import sys 
  7  from py import test 
  8   
9 -class TestConvertCommand:
10 """Tests running actual commands on files""" 11 convertmodule = convert 12 defaultoptions = {"progress": "none", "psyco": "none"} 13
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__) 17 self.cleardir() 18 os.mkdir(self.testdir) 19 self.rundir = os.path.abspath(os.getcwd())
20
21 - def teardown_method(self, method):
22 """removes the test directory for the given method""" 23 os.chdir(self.rundir) 24 self.cleardir()
25
26 - def cleardir(self):
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)) 32 for name in subdirs: 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)
36
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) 40 argv = list(argv) 41 kwoptions = getattr(self, "defaultoptions", {}).copy() 42 kwoptions.update(kwargs) 43 for key, value in kwoptions.iteritems(): 44 if value is True: 45 argv.append("--%s" % key) 46 else: 47 argv.append("--%s=%s" % (key, value)) 48 try: 49 self.convertmodule.main(argv) 50 finally: 51 os.chdir(self.rundir)
52
53 - def get_testfilename(self, filename):
54 """gets the path to the test file""" 55 return os.path.join(self.testdir, filename)
56
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) 62 currentpath = "" 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): 67 os.mkdir(currentpath) 68 return open(filename, mode)
69
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)
74
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()
79
80 - def help_check(self, options, option, last=False):
81 """check that a help string occurs and remove it""" 82 assert option in options 83 newoptions = [] 84 for line in options.splitlines(): 85 if option in line or not line.lstrip().startswith("-"): 86 continue 87 newoptions.append(line) 88 if last: 89 assert newoptions == [] 90 return "\n".join(newoptions)
91
92 - def test_help(self):
93 """tests getting help (returning the help_string so further tests can be done)""" 94 stdout = sys.stdout 95 helpfile = self.open_testfile("help.txt", "w") 96 sys.stdout = helpfile 97 try: 98 test.raises(SystemExit, self.run_command, help=True) 99 finally: 100 sys.stdout = stdout 101 helpfile.close() 102 help_string = self.read_testfile("help.txt") 103 print help_string 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 110 # that into account 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") 124 return options
125