6
6
#[ path = "import_lib.rs" ]
7
7
mod import_lib;
8
8
9
+ #[ cfg( test) ]
10
+ use std:: cell:: RefCell ;
9
11
use std:: {
10
12
collections:: { HashMap , HashSet } ,
11
13
env,
@@ -15,8 +17,7 @@ use std::{
15
17
io:: { BufRead , BufReader , Read , Write } ,
16
18
path:: { Path , PathBuf } ,
17
19
process:: { Command , Stdio } ,
18
- str,
19
- str:: FromStr ,
20
+ str:: { self , FromStr } ,
20
21
} ;
21
22
22
23
pub use target_lexicon:: Triple ;
@@ -41,6 +42,11 @@ const MINIMUM_SUPPORTED_VERSION_GRAALPY: PythonVersion = PythonVersion {
41
42
/// Maximum Python version that can be used as minimum required Python version with abi3.
42
43
pub ( crate ) const ABI3_MAX_MINOR : u8 = 12 ;
43
44
45
+ #[ cfg( test) ]
46
+ thread_local ! {
47
+ static READ_ENV_VARS : RefCell <Vec <String >> = const { RefCell :: new( Vec :: new( ) ) } ;
48
+ }
49
+
44
50
/// Gets an environment variable owned by cargo.
45
51
///
46
52
/// Environment variables set by cargo are expected to be valid UTF8.
@@ -54,6 +60,12 @@ pub fn env_var(var: &str) -> Option<OsString> {
54
60
if cfg ! ( feature = "resolve-config" ) {
55
61
println ! ( "cargo:rerun-if-env-changed={}" , var) ;
56
62
}
63
+ #[ cfg( test) ]
64
+ {
65
+ READ_ENV_VARS . with ( |env_vars| {
66
+ env_vars. borrow_mut ( ) . push ( var. to_owned ( ) ) ;
67
+ } ) ;
68
+ }
57
69
env:: var_os ( var)
58
70
}
59
71
@@ -420,7 +432,7 @@ print("gil_disabled", get_config_var("Py_GIL_DISABLED"))
420
432
/// The `abi3` features, if set, may apply an `abi3` constraint to the Python version.
421
433
#[ allow( dead_code) ] // only used in build.rs
422
434
pub ( super ) fn from_pyo3_config_file_env ( ) -> Option < Result < Self > > {
423
- cargo_env_var ( "PYO3_CONFIG_FILE" ) . map ( |path| {
435
+ env_var ( "PYO3_CONFIG_FILE" ) . map ( |path| {
424
436
let path = Path :: new ( & path) ;
425
437
println ! ( "cargo:rerun-if-changed={}" , path. display( ) ) ;
426
438
// Absolute path is necessary because this build script is run with a cwd different to the
@@ -3070,4 +3082,12 @@ mod tests {
3070
3082
"
3071
3083
) ) ;
3072
3084
}
3085
+
3086
+ #[ test]
3087
+ fn test_from_pyo3_config_file_env_rebuild ( ) {
3088
+ READ_ENV_VARS . with ( |vars| vars. borrow_mut ( ) . clear ( ) ) ;
3089
+ let _ = InterpreterConfig :: from_pyo3_config_file_env ( ) ;
3090
+ // it's possible that other env vars were also read, hence just checking for contains
3091
+ READ_ENV_VARS . with ( |vars| assert ! ( vars. borrow( ) . contains( & "PYO3_CONFIG_FILE" . to_string( ) ) ) ) ;
3092
+ }
3073
3093
}
0 commit comments