lib_gis_env_test.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. """Test environment and GIS environment functions"""
  2. import multiprocessing
  3. import grass.script as gs
  4. import grass.script.setup as grass_setup
  5. def run_in_subprocess(function):
  6. """Run function in a separate process
  7. The function must take a Queue and put its result there.
  8. The result is then returned from this function.
  9. """
  10. queue = multiprocessing.Queue()
  11. process = multiprocessing.Process(target=function, args=(queue,))
  12. process.start()
  13. result = queue.get()
  14. process.join()
  15. return result
  16. def test_reading_respects_change_of_session(tmp_path):
  17. """Check new session file path is retrieved and the file is read"""
  18. # The switching must happen in one process to test that the reading functions
  19. # are not using the cached values. However, the test itself needs to be
  20. # process-separated otherwise other tests will be influenced by the loaded
  21. # libraries and initialized data structures.
  22. def switch_through_locations(queue):
  23. """Switches through a list of locations"""
  24. # Just to be sure we don't influence other tests.
  25. # pylint: disable=import-outside-toplevel
  26. import grass.pygrass.utils as pygrass_utils
  27. import grass.lib.gis as libgis
  28. names = []
  29. for location_name in ["test1", "test2", "abc"]:
  30. # pylint: disable=protected-access
  31. gs.core._create_location_xy(tmp_path, location_name)
  32. with grass_setup.init(tmp_path / location_name):
  33. libgis.G__read_gisrc_path()
  34. libgis.G__read_gisrc_env()
  35. names.append((pygrass_utils.getenv("LOCATION_NAME"), location_name))
  36. queue.put(names)
  37. names = run_in_subprocess(switch_through_locations)
  38. for getenv_name, expected_name in names:
  39. assert getenv_name == expected_name, f"All recorded names: {names}"