77 lines
3.0 KiB
Python
77 lines
3.0 KiB
Python
import importlib.util
|
|
import os
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
|
|
def load_fortran_toolchain():
|
|
module_path = Path(__file__).resolve().parent / "fortran_toolchain.py"
|
|
spec = importlib.util.spec_from_file_location("fortran_toolchain", module_path)
|
|
module = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(module)
|
|
return module
|
|
|
|
|
|
class FortranToolchainTests(unittest.TestCase):
|
|
def test_auto_prefers_ifx_over_ifort_when_both_are_on_path(self):
|
|
fortran_toolchain = load_fortran_toolchain()
|
|
|
|
def fake_which(name):
|
|
return f"C:\\oneapi\\{name}.exe" if name in {"ifx", "ifort"} else None
|
|
|
|
with patch.object(fortran_toolchain.shutil, "which", side_effect=fake_which):
|
|
with patch.dict(os.environ, {}, clear=True):
|
|
toolchain = fortran_toolchain.resolve_toolchain()
|
|
|
|
self.assertEqual(toolchain.name, "ifx")
|
|
self.assertEqual(toolchain.executable, "C:\\oneapi\\ifx.exe")
|
|
self.assertIsNone(toolchain.env_script)
|
|
|
|
def test_auto_uses_oneapi_env_script_when_compiler_is_not_on_path(self):
|
|
fortran_toolchain = load_fortran_toolchain()
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
vars_bat = Path(tmp) / "setvars.bat"
|
|
vars_bat.write_text("@echo off\n", encoding="utf-8")
|
|
|
|
with patch.object(fortran_toolchain.shutil, "which", return_value=None):
|
|
with patch.object(fortran_toolchain, "ONEAPI_VARS_CANDIDATES", [vars_bat]):
|
|
with patch.dict(os.environ, {}, clear=True):
|
|
toolchain = fortran_toolchain.resolve_toolchain()
|
|
|
|
self.assertEqual(toolchain.name, "ifx")
|
|
self.assertEqual(toolchain.executable, "ifx")
|
|
self.assertEqual(toolchain.env_script, vars_bat)
|
|
|
|
def test_explicit_compiler_preference_is_honored(self):
|
|
fortran_toolchain = load_fortran_toolchain()
|
|
|
|
def fake_which(name):
|
|
return f"C:\\oneapi\\{name}.exe"
|
|
|
|
with patch.object(fortran_toolchain.shutil, "which", side_effect=fake_which):
|
|
with patch.dict(os.environ, {"HARNESS_FORTRAN_COMPILER": "ifort"}, clear=True):
|
|
toolchain = fortran_toolchain.resolve_toolchain()
|
|
|
|
self.assertEqual(toolchain.name, "ifort")
|
|
self.assertEqual(toolchain.executable, "C:\\oneapi\\ifort.exe")
|
|
|
|
def test_wrap_command_calls_oneapi_env_before_compiler(self):
|
|
fortran_toolchain = load_fortran_toolchain()
|
|
toolchain = fortran_toolchain.FortranToolchain(
|
|
name="ifx",
|
|
executable="ifx",
|
|
env_script=Path(r"C:\Program Files (x86)\Intel\oneAPI\setvars.bat"),
|
|
)
|
|
|
|
command = fortran_toolchain.wrap_command(toolchain, ["ifx", "/nologo", "test.f90"])
|
|
|
|
self.assertIn("cmd /d /s /c", command)
|
|
self.assertIn('call "C:\\Program Files (x86)\\Intel\\oneAPI\\setvars.bat" intel64', command)
|
|
self.assertIn("ifx /nologo test.f90", command)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|