Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed test_version to ensure it always has _version.py #574

Merged
merged 3 commits into from
Oct 29, 2024
Merged
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
67 changes: 36 additions & 31 deletions tests/test_version.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,43 @@
import importlib
import os
import subprocess
import sys
from unittest import TestCase
from pathlib import Path


class TestVersion(TestCase):
def test_version(self):
self.assertTrue(os.path.exists(os.path.join("montepy", "_version.py")))
# Test without version.py
try:
# try without setuptools_scm
old_file = os.path.join("montepy", "_version.py")
new_file = os.path.join("montepy", "_version.bak")
os.rename(old_file, new_file)
# clear out previous imports
to_delete = set()
for mod in sys.modules:
for bad_mod in ["setuptools_scm", "montepy"]:
if bad_mod in mod:
to_delete.add(mod)
for mod in to_delete:
del sys.modules[mod]
sys.modules["setuptools_scm"] = None
import montepy
def test_version():
version_file = Path("montepy") / "_version.py"
if not version_file.exists():
subprocess.run(
["python", "-m", "setuptools_scm", "--force-write-version-files"]
)
assert os.path.exists(os.path.join("montepy", "_version.py"))
# Test without version.py
try:
# try without setuptools_scm
old_file = os.path.join("montepy", "_version.py")
new_file = os.path.join("montepy", "_version.bak")
os.rename(old_file, new_file)
# clear out previous imports
to_delete = set()
for mod in sys.modules:
for bad_mod in ["setuptools_scm", "montepy"]:
if bad_mod in mod:
to_delete.add(mod)
for mod in to_delete:
del sys.modules[mod]
sys.modules["setuptools_scm"] = None
import montepy

self.assertEqual(montepy.__version__, "Undefined")
# try with setuptools_scm
del sys.modules["setuptools_scm"]
importlib.reload(montepy)
print(f"From setuptools_scm: {montepy.__version__}")
self.assertTrue(len(montepy.__version__.split(".")) >= 3)
finally:
os.rename(new_file, old_file)
# do base with _version
assert montepy.__version__ == "Undefined"
# try with setuptools_scm
del sys.modules["setuptools_scm"]
importlib.reload(montepy)
print(f"From _version.py: {montepy.__version__}")
self.assertTrue(len(montepy.__version__.split(".")) >= 3)
print(f"From setuptools_scm: {montepy.__version__}")
assert len(montepy.__version__.split(".")) >= 3
finally:
os.rename(new_file, old_file)
# do base with _version
importlib.reload(montepy)
print(f"From _version.py: {montepy.__version__}")
assert len(montepy.__version__.split(".")) >= 3