test_grass_tmp_mapset.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env python3
  2. """
  3. TEST: Test of grass --tmp-mapset
  4. AUTHOR(S): Vaclav Petras <wenzeslaus gmail com>
  5. PURPOSE: Test that --tmp-mapset option of grass command works
  6. COPYRIGHT: (C) 2020 Vaclav Petras and the GRASS Development Team
  7. This program is free software under the GNU General Public
  8. License (>=v2). Read the file COPYING that comes with GRASS
  9. for details.
  10. """
  11. import unittest
  12. import os
  13. import shutil
  14. import subprocess
  15. # Note that unlike rest of GRASS GIS, here we are using unittest package
  16. # directly. The grass.gunittest machinery for mapsets is not needed here.
  17. # How this plays out together with the rest of testing framework is yet to be
  18. # determined.
  19. class TestTmpMapset(unittest.TestCase):
  20. """Tests --tmp-mapset option of grass command"""
  21. # TODO: here we need a name of or path to the main GRASS GIS executable
  22. executable = "grass"
  23. # an arbitrary, but identifiable and fairly unique name
  24. location = "test_tmp_mapset_xy"
  25. def setUp(self):
  26. """Creates a location used in the tests"""
  27. subprocess.check_call([self.executable, "-c", "XY", self.location, "-e"])
  28. self.subdirs = os.listdir(self.location)
  29. def tearDown(self):
  30. """Deletes the location"""
  31. shutil.rmtree(self.location, ignore_errors=True)
  32. def test_command_runs(self):
  33. """Check that correct parameters are accepted"""
  34. return_code = subprocess.call(
  35. [self.executable, "--tmp-mapset", self.location, "--exec", "g.proj", "-g"]
  36. )
  37. self.assertEqual(
  38. return_code,
  39. 0,
  40. msg=(
  41. "Non-zero return code from {self.executable}"
  42. " when creating mapset".format(**locals())
  43. ),
  44. )
  45. def test_command_fails_without_location(self):
  46. """Check that the command fails with a nonexistent location"""
  47. return_code = subprocess.call(
  48. [
  49. self.executable,
  50. "--tmp-mapset",
  51. "does_not_exist",
  52. "--exec",
  53. "g.proj",
  54. "-g",
  55. ]
  56. )
  57. self.assertNotEqual(
  58. return_code,
  59. 0,
  60. msg=(
  61. "Zero return code from {self.executable},"
  62. " but the location directory does not exist".format(**locals())
  63. ),
  64. )
  65. def test_mapset_metadata_correct(self):
  66. """Check that metadata is readable and have expected value (XY CRS)"""
  67. output = subprocess.check_output(
  68. [self.executable, "--tmp-mapset", self.location, "--exec", "g.proj", "-g"]
  69. )
  70. self.assertEqual(
  71. output.strip(),
  72. "name=xy_location_unprojected".encode("ascii"),
  73. msg="Mapset metadata are not what was expected, but: {output}".format(
  74. **locals()
  75. ),
  76. )
  77. def test_mapset_deleted(self):
  78. """Check that mapset is deleted at the end of execution"""
  79. subprocess.check_call(
  80. [self.executable, "--tmp-mapset", self.location, "--exec", "g.proj", "-p"]
  81. )
  82. for directory in os.listdir(self.location):
  83. self.assertTrue(
  84. directory in self.subdirs,
  85. msg="Directory {directory} should have been deleted".format(**locals()),
  86. )
  87. if __name__ == "__main__":
  88. unittest.main()