1313import textwrap
1414import unittest
1515from pathlib import Path
16+ from typing import Any , TypedDict , Unpack
1617
1718REPO_ROOT = Path (__file__ ).resolve ().parents [3 ]
1819CMAKE_DIR = REPO_ROOT / "vortex-ffi/cmake"
1920
2021
21- def rust_toolchain_environment ():
22+ class CommandOptions (TypedDict , total = False ):
23+ env : dict [str , str ] | None
24+ cwd : str | Path | None
25+ success : bool
26+ timeout : int
27+
28+
29+ def rust_toolchain_environment () -> dict [str , str ]:
2230 """Keep the repository's Rust selection when fixtures run outside its tree."""
2331 env = os .environ .copy ()
2432 if not env .get ("RUSTUP_TOOLCHAIN" ) and shutil .which ("rustup" ):
@@ -36,25 +44,32 @@ def rust_toolchain_environment():
3644
3745
3846class CMakeTest (unittest .TestCase ):
39- def setUp (self ):
47+ def setUp (self ) -> None :
4048 temporary = tempfile .TemporaryDirectory (prefix = "vortex-cmake-" )
4149 self .addCleanup (temporary .cleanup )
4250 self .work = Path (temporary .name ).resolve ()
4351 self .repo = REPO_ROOT
4452 self .env = os .environ .copy ()
4553
46- def write (self , path , contents ) :
54+ def write (self , path : str | Path , contents : str ) -> Path :
4755 path = self .work / path
4856 path .parent .mkdir (parents = True , exist_ok = True )
4957 path .write_text (textwrap .dedent (contents ), encoding = "utf-8" )
5058 return path
5159
52- def executable (self , name , body ) :
60+ def executable (self , name : str , body : str ) -> str :
5361 path = self .write (name , f"#!{ sys .executable } \n " + textwrap .dedent (body ))
5462 path .chmod (0o755 )
5563 return str (path )
5664
57- def command (self , * args , env = None , cwd = None , success = True , timeout = 120 ):
65+ def command (
66+ self ,
67+ * args : str | Path ,
68+ env : dict [str , str ] | None = None ,
69+ cwd : str | Path | None = None ,
70+ success : bool = True ,
71+ timeout : int = 120 ,
72+ ) -> subprocess .CompletedProcess [str ]:
5873 result = subprocess .run (
5974 list (map (str , args )),
6075 cwd = self .work if cwd is None else cwd ,
@@ -71,13 +86,17 @@ def command(self, *args, env=None, cwd=None, success=True, timeout=120):
7186 self .assertNotEqual (result .returncode , 0 , output )
7287 return result
7388
74- def cmake_configure (self , source , build , * options , generator = "Ninja" , ** kwargs ):
89+ def cmake_configure (
90+ self , source : Path , build : Path , * options : str , generator : str = "Ninja" , ** kwargs : Unpack [CommandOptions ]
91+ ) -> subprocess .CompletedProcess [str ]:
7592 return self .command ("cmake" , "-G" , generator , "-S" , source , "-B" , build , * options , ** kwargs )
7693
77- def cmake_build (self , build , * options , ** kwargs ):
94+ def cmake_build (
95+ self , build : Path , * options : str , ** kwargs : Unpack [CommandOptions ]
96+ ) -> subprocess .CompletedProcess [str ]:
7897 return self .command ("cmake" , "--build" , build , * options , ** kwargs )
7998
80- def recording_cargo (self ):
99+ def recording_cargo (self ) -> str :
81100 """Record the handoff and emit an archive without compiling Vortex."""
82101 return self .executable (
83102 "cargo" ,
@@ -96,7 +115,7 @@ def option(name):
96115 """ ,
97116 )
98117
99- def fake_rustc (self , release = "1.95.0" , host = None ):
118+ def fake_rustc (self , release : str = "1.95.0" , host : str | None = None ) -> str :
100119 arch = {"arm64" : "aarch64" , "AMD64" : "x86_64" }.get (platform .machine (), platform .machine ())
101120 host = host or f"{ arch } -{ 'apple-darwin' if sys .platform == 'darwin' else 'unknown-linux-gnu' } "
102121 return self .executable (
@@ -109,5 +128,5 @@ def fake_rustc(self, release="1.95.0", host=None):
109128 """ ,
110129 )
111130
112- def cargo_recording (self , target_dir ) :
131+ def cargo_recording (self , target_dir : Path ) -> dict [ str , Any ] :
113132 return json .loads ((target_dir / "environment.json" ).read_text (encoding = "utf-8" ))
0 commit comments