Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pytm/tm.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ def _stale(self, days: int) -> str:
"""Report source files whose age diverges from the model script."""
base_path = os.path.dirname(sys.argv[0])
try:
tm_path = os.path.join(base_path, sys.argv[0])
tm_path = sys.argv[0]
tm_mtime = datetime.fromtimestamp(os.stat(tm_path).st_mtime)
except OSError as err:
sys.stderr.write(f"{sys.argv[0]} - {err}\n")
Expand Down
47 changes: 47 additions & 0 deletions tests/test_stale_paths.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""CLI staleness checks use the path of the invoked model once."""

import os
import subprocess
import sys

import pytest


@pytest.mark.parametrize(
"model_name,absolute",
[
("models/model.py", False),
("./models/model.py", False),
("nested/models/model.py", False),
("model directory/model.py", False),
("model.py", False),
("models/model.py", True),
],
)
def test_stale_days_reads_invoked_model(tmp_path, model_name, absolute):
model = tmp_path / model_name
model.parent.mkdir(parents=True, exist_ok=True)
model.write_text(
"from pytm import TM, Server\n"
"tm = TM('Staleness')\n"
"Server('API', sourceFiles=['source.py'])\n"
"tm.process()\n"
)
source = model.parent / "source.py"
source.write_text("# Source newer than its model\n")
os.utime(model, (1_700_000_000, 1_700_000_000))
os.utime(source, (1_700_259_200, 1_700_259_200))
invocation = str(model) if absolute else model_name

result = subprocess.run(
[sys.executable, invocation, "--stale_days", "2"],
cwd=tmp_path,
capture_output=True,
text=True,
check=True,
)

assert result.stderr == ""
assert "[ERROR]" not in result.stdout
assert "This model is 3 days older than" in result.stdout
assert "source.py" in result.stdout