From 6a491519b3c265cd4ffaac151e3755c5e33d45c0 Mon Sep 17 00:00:00 2001 From: Florian De Temmerman Date: Thu, 19 Oct 2023 14:17:23 +0200 Subject: [PATCH] add a test case for tempdir fallback --- libpysal/examples/tests/test_available.py | 34 ++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/libpysal/examples/tests/test_available.py b/libpysal/examples/tests/test_available.py index f1bcf2ce2..920184888 100644 --- a/libpysal/examples/tests/test_available.py +++ b/libpysal/examples/tests/test_available.py @@ -1,9 +1,13 @@ #!/usr/bin/env python3 - +import errno import os import platform +import tempfile import unittest import pandas +from unittest.mock import MagicMock, patch + +from platformdirs import user_data_dir from .. import available, get_url, load_example @@ -11,6 +15,10 @@ os_name = platform.system() +original_path_exists = os.path.exists +original_makedirs = os.makedirs + + class Testexamples(unittest.TestCase): def test_available(self): @@ -40,6 +48,30 @@ def test_data_home(self): self.assertEqual(heads[-2], "Local") self.assertEqual(heads[-3], "AppData") + @patch("os.makedirs") + @patch("os.path.exists") + def test_data_home_fallback(self, path_exists_mock, makedirs_mock): + data_home = user_data_dir("pysal", "pysal") + + def makedirs_side_effect(path, exist_ok=False): + if path == data_home: + raise OSError(errno.EROFS) + + def path_exists_side_effect(path): + if path == data_home: + return False + return original_path_exists(path) + + makedirs_mock.side_effect = makedirs_side_effect + path_exists_mock.side_effect = path_exists_side_effect + + pth = get_data_home() + head, tail = os.path.split(pth) + + self.assertEqual(tail, "pysal") + self.assertEqual(head, tempfile.gettempdir()) + + def test_get_url(self): self.assertEqual(get_url("10740"), None) url = "https://geodacenter.github.io/data-and-lab//data/baltimore.zip"