grass_script_setup_test.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. """Test functions in grass.script.setup"""
  2. import multiprocessing
  3. import os
  4. import pytest
  5. import grass.script as gs
  6. import grass.script.setup as grass_setup
  7. # All init tests change the global environment, but when it really matters,
  8. # we use a separate process.
  9. # Ideally, the functions would support env parameter and the test
  10. # would mostly use that.
  11. def run_in_subprocess(function):
  12. """Run function in a separate process
  13. The function must take a Queue and put its result there.
  14. The result is then returned from this function.
  15. """
  16. queue = multiprocessing.Queue()
  17. process = multiprocessing.Process(target=function, args=(queue,))
  18. process.start()
  19. result = queue.get()
  20. process.join()
  21. return result
  22. def test_init_as_context_manager(tmp_path):
  23. """Check that init function return value works as a context manager"""
  24. location = "test"
  25. gs.core._create_location_xy(tmp_path, location) # pylint: disable=protected-access
  26. with grass_setup.init(tmp_path / location):
  27. gs.run_command("g.region", flags="p")
  28. session_file = os.environ["GISRC"]
  29. assert not os.path.exists(session_file)
  30. def test_init_session_finish(tmp_path):
  31. """Check that init works with finish on the returned session object"""
  32. location = "test"
  33. gs.core._create_location_xy(tmp_path, location) # pylint: disable=protected-access
  34. session = grass_setup.init(tmp_path / location)
  35. gs.run_command("g.region", flags="p")
  36. session_file = os.environ["GISRC"]
  37. session.finish()
  38. with pytest.raises(ValueError):
  39. session.finish()
  40. assert not session.active
  41. assert not os.path.exists(session_file)
  42. def test_init_finish_global_functions(tmp_path):
  43. """Check that init and finish global functions work"""
  44. location = "test"
  45. gs.core._create_location_xy(tmp_path, location) # pylint: disable=protected-access
  46. grass_setup.init(tmp_path / location)
  47. gs.run_command("g.region", flags="p")
  48. session_file = os.environ["GISRC"]
  49. grass_setup.finish()
  50. assert not os.path.exists(session_file)
  51. def test_init_finish_global_functions_capture_strerr(tmp_path):
  52. """Check that init and finish global functions work"""
  53. def init_finish(queue):
  54. gs.set_capture_stderr(True)
  55. location = "test"
  56. gs.core._create_location_xy( # pylint: disable=protected-access
  57. tmp_path, location
  58. )
  59. grass_setup.init(tmp_path / location)
  60. gs.run_command("g.region", flags="p")
  61. queue.put(os.environ["GISRC"])
  62. grass_setup.finish()
  63. session_file = run_in_subprocess(init_finish)
  64. assert session_file, "Expected file name from the subprocess"
  65. assert not os.path.exists(session_file), "Session file not deleted"