test_start_command_functions.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # -*- coding: utf-8 -*-
  2. # the utf-8 is important because we do use the characters
  3. """Tests of start_command function family (location independent)"""
  4. import sys
  5. from grass.gunittest.case import TestCase
  6. from grass.gunittest.main import test
  7. from grass.script.core import start_command, PIPE, run_command, write_command
  8. from grass.script.core import read_command, find_program
  9. from grass.script.utils import encode
  10. class TestPythonKeywordsInParameters(TestCase):
  11. """Tests additional underscore syntax which helps to avoid Python keywords
  12. It works the same for keywords, buildins and any names.
  13. """
  14. raster = b'does_not_exist'
  15. def test_prefixed_underscore(self):
  16. proc = start_command(
  17. 'g.region', _raster=self.raster, stderr=PIPE)
  18. stderr = proc.communicate()[1]
  19. self.assertNotIn(b'_raster', stderr)
  20. self.assertIn(self.raster, stderr,
  21. msg="Raster map name should appear in the error output")
  22. def test_suffixed_underscore(self):
  23. proc = start_command(
  24. 'g.region', raster_=self.raster, stderr=PIPE)
  25. stderr = proc.communicate()[1]
  26. self.assertNotIn(b'raster_', stderr)
  27. self.assertIn(self.raster, stderr,
  28. msg="Raster map name should appear in the error output")
  29. def test_multiple_underscores(self):
  30. proc = start_command(
  31. 'g.region', _raster_=self.raster, stderr=PIPE)
  32. stderr = proc.communicate()[1]
  33. returncode = proc.poll()
  34. self.assertEquals(returncode, 1)
  35. self.assertIn(b'raster', stderr)
  36. class TestPythonModuleWithUnicodeParameters(TestCase):
  37. """Tests if unicode works in parameters of Python modules
  38. This in fact tests also the `parser()` function (original motivation
  39. for this tests).
  40. Using g.search.module because it takes any option values.
  41. """
  42. def test_python_module_ascii(self):
  43. """This tests if Python module works"""
  44. run_command('g.search.modules', keyword='Priserny kun', encoding=None)
  45. def test_python_module_ascii_with_encoding(self):
  46. """This tests if Python module works with user-specified encoding"""
  47. run_command('g.search.modules', keyword='Priserny kun',
  48. encoding='utf-16')
  49. def test_python_module_czech_nonascii(self):
  50. """This likely fails on non-UTF-8 systems (i.e. MS Win)"""
  51. run_command('g.search.modules', keyword='Příšerný kůň')
  52. def test_python_module_czech_unicode(self):
  53. """This likely fails on non-UTF-8 systems (i.e. MS Win)"""
  54. run_command('g.search.modules', keyword=u'Příšerný kůň')
  55. class TestPythonModuleWithStdinStdout(TestCase):
  56. raster = 'rand_raster'
  57. @classmethod
  58. def setUpClass(cls):
  59. cls.runModule('g.region', s=0, n=10, w=0, e=10, res=1)
  60. cls.runModule("r.mapcalc", expression="{} = rand(1, 5)".
  61. format(cls.raster), seed=1, overwrite=True)
  62. @classmethod
  63. def tearDownClass(cls):
  64. cls.runModule("g.remove", type='raster', name=cls.raster,
  65. flags='f')
  66. def test_write_labels_unicode(self):
  67. """This tests if Python module works"""
  68. find_program('ls', '--version')
  69. write_command('r.category', map=self.raster, rules='-',
  70. stdin='1:kůň\n2:kráva\n3:ovečka\n4:býk', separator=':')
  71. res = read_command('r.category', map=self.raster,
  72. separator=':').strip()
  73. self.assertEquals(res, u'1:kůň\n2:kráva\n3:ovečka\n4:býk')
  74. if sys.version_info.major >= 3:
  75. self.assertIsInstance(res, str)
  76. else:
  77. self.assertIsInstance(res, unicode)
  78. def test_write_labels_bytes(self):
  79. """This tests if Python module works"""
  80. write_command('r.category', map=self.raster, rules='-',
  81. stdin='1:kůň\n2:kráva\n3:ovečka\n4:býk',
  82. separator=':', encoding=None)
  83. res = read_command('r.category', map=self.raster,
  84. separator=':', encoding=None).strip()
  85. self.assertEquals(res, encode('1:kůň\n2:kráva\n3:ovečka\n4:býk'))
  86. self.assertIsInstance(res, bytes)
  87. if __name__ == '__main__':
  88. test()