From 102da6b5bed8cf8c64be5600199afef5b1e5bd0e Mon Sep 17 00:00:00 2001
From: Em Zhan <emzhan@ssec.wisc.edu>
Date: Fri, 7 Feb 2025 11:19:28 -0600
Subject: [PATCH] Add SIPSANC_DATA_DIR and SIPSANC_LOG_DIR env vars

Allows changing where `sipsanc init` saves its files and where any
errors in all `sipsanc` subcommands are saved, respectively

Uses these variables in tests to separate the test instance of `sipsanc`
from the local install
---
 .gitignore               |  2 ++
 README.md                | 11 +++++++++++
 src/sipsanc/cli.py       |  8 ++++++--
 tests/conftest.py        | 18 ++++++++++++++++++
 tests/test_cli_output.py |  1 -
 tests/test_output.py     |  5 -----
 6 files changed, 37 insertions(+), 8 deletions(-)
 create mode 100644 tests/conftest.py

diff --git a/.gitignore b/.gitignore
index 89af34b..bb4e3f1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -10,6 +10,8 @@ wheels/
 .venv
 
 # Test data
+/tests/data/
+/tests/log/
 /tests/tmp/
 /tests/fails/
 /plots*/
diff --git a/README.md b/README.md
index 6397faa..cd5fc7e 100644
--- a/README.md
+++ b/README.md
@@ -47,6 +47,17 @@ To upgrade an existing install, run:
 uv tool upgrade sipsanc
 ```
 
+## Environment Variables
+
+By default, `sipsanc` will save files in [standard user data directories](https://github.com/tox-dev/platformdirs). These locations can be configured by setting the following environment variables:
+
+```sh
+export SIPSANC_DATA_DIR=my/custom/path/
+export SIPSANC_LOG_DIR=another/custom/path  # can be the same as SIPSANC_DATA_DIR
+```
+
+This is useful for testing or making self-contained code deliveries.
+
 ## Contributing
 
 See [CONTRIBUTING.md](https://gitlab.ssec.wisc.edu/emzhan/sipsanc/-/blob/main/CONTRIBUTING.md) for how to set up a development environment.
diff --git a/src/sipsanc/cli.py b/src/sipsanc/cli.py
index bff7b6e..cd7d566 100644
--- a/src/sipsanc/cli.py
+++ b/src/sipsanc/cli.py
@@ -1,4 +1,5 @@
 import logging
+import os
 import shutil
 import sys
 import traceback
@@ -17,8 +18,11 @@ Source = Literal["release", "sipssci1", "dev"]
 
 DIRS = PlatformDirs("sipsanc", "SSEC")
 INIT_DIR_VERSION = 1
-INIT_DIR = DIRS.user_data_path / f"v{INIT_DIR_VERSION}"
-LOG_DIR = DIRS.user_log_path
+
+DATA_DIR = Path(os.environ.get("SIPSANC_DATA_DIR") or DIRS.user_data_dir)
+LOG_DIR = Path(os.environ.get("SIPSANC_LOG_DIR") or DIRS.user_log_dir)
+
+INIT_DIR = DATA_DIR / f"v{INIT_DIR_VERSION}"
 LOG_DIR.mkdir(parents=True, exist_ok=True)
 
 LOG_FILE = LOG_DIR / "errors.txt"
diff --git a/tests/conftest.py b/tests/conftest.py
new file mode 100644
index 0000000..c8a3dab
--- /dev/null
+++ b/tests/conftest.py
@@ -0,0 +1,18 @@
+import os
+from subprocess import run
+
+
+def pytest_sessionstart(session):
+    print("Setting environment variables and running `sipsanc init all`...")
+
+    os.environ["SIPSANC_DATA_DIR"] = "tests/data/"
+    os.environ["SIPSANC_LOG_DIR"] = "tests/log/"
+
+    print()
+    print(f"SIPSANC_DATA_DIR={os.environ['SIPSANC_DATA_DIR']}")
+    print(f"SIPSANC_LOG_DIR={os.environ['SIPSANC_LOG_DIR']}")
+    print()
+
+    run(["sipsanc", "init", "all"], check=True)
+
+    print("\nPre-test setup complete!")
diff --git a/tests/test_cli_output.py b/tests/test_cli_output.py
index cfad174..73718ae 100644
--- a/tests/test_cli_output.py
+++ b/tests/test_cli_output.py
@@ -35,7 +35,6 @@ def test_truth(product: str, version: str, date: str, tmp_path: Path):
     output = str(tmp_path / "out.nc")
     plots = str(tmp_path / "plots")
 
-    run(["sipsanc", "init", "all"], check=True)
     cmd = ["sipsanc", "regrid", "all", l1b_file, "--output", output, "--plot", plots]
     run(cmd, check=True)
 
diff --git a/tests/test_output.py b/tests/test_output.py
index 023e840..7c0b63d 100644
--- a/tests/test_output.py
+++ b/tests/test_output.py
@@ -16,11 +16,6 @@ pytestmark = pytest.mark.skipif(
 )
 
 
-@pytest.fixture(scope="session", autouse=True)
-def ensure_init():
-    run(["sipsanc", "init", "all"], check=True)
-
-
 @pytest.fixture(
     scope="module",
     params=[
-- 
GitLab