test_start_command_functions.py 4.1 KB

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