@@ -74,7 +74,7 @@ impl Config {
7474 let config: ConfigWrapper = Figment :: new ( )
7575 . merge ( Toml :: string ( config_defaults) )
7676 . merge ( Toml :: file ( filename) )
77- . merge ( Env :: prefixed ( prefix. get_prefix ( ) ) )
77+ . merge ( Env :: prefixed ( prefix. get_prefix ( ) ) . split ( "__" ) )
7878 . extract ( )
7979 . map_err ( |e| e. to_string ( ) ) ?;
8080 config. 0 . validate ( ) ?;
@@ -330,4 +330,72 @@ mod tests {
330330 "Ignoring unknown configuration field: plumbus"
331331 ) ) ;
332332 }
333+
334+ // Test that we can fill in mandatory config fields missing from the config file with
335+ // environment variables
336+ #[ sealed_test( files = [ "minimal-config-example.toml" ] ) ]
337+ fn test_fill_in_missing_with_env ( ) {
338+ let mut minimal_config: toml:: Value = toml:: from_str (
339+ fs:: read_to_string ( "minimal-config-example.toml" )
340+ . unwrap ( )
341+ . as_str ( ) ,
342+ )
343+ . unwrap ( ) ;
344+ // Remove the subgraphs.network.query_url field from minimal config
345+ minimal_config
346+ . get_mut ( "subgraphs" )
347+ . unwrap ( )
348+ . get_mut ( "network" )
349+ . unwrap ( )
350+ . as_table_mut ( )
351+ . unwrap ( )
352+ . remove ( "query_url" ) ;
353+
354+ // Save the modified minimal config to a named temporary file using tempfile
355+ let temp_minimal_config_path = tempfile:: NamedTempFile :: new ( ) . unwrap ( ) ;
356+ fs:: write (
357+ temp_minimal_config_path. path ( ) ,
358+ toml:: to_string ( & minimal_config) . unwrap ( ) ,
359+ )
360+ . unwrap ( ) ;
361+
362+ // This should fail because the subgraphs.network.query_url field is missing
363+ Config :: parse (
364+ ConfigPrefix :: Service ,
365+ & PathBuf :: from ( temp_minimal_config_path. path ( ) ) ,
366+ )
367+ . unwrap_err ( ) ;
368+
369+ let test_value = "http://localhost:8000/testvalue" ;
370+ std:: env:: set_var ( "INDEXER_SERVICE_SUBGRAPHS__NETWORK__QUERY_URL" , test_value) ;
371+
372+ let config = Config :: parse (
373+ ConfigPrefix :: Service ,
374+ & PathBuf :: from ( temp_minimal_config_path. path ( ) ) ,
375+ )
376+ . unwrap ( ) ;
377+
378+ assert_eq ! (
379+ config. subgraphs. network. config. query_url. as_str( ) ,
380+ test_value
381+ ) ;
382+ }
383+
384+ // Test that we can override nested config values with environment variables
385+ #[ sealed_test( files = [ "minimal-config-example.toml" ] ) ]
386+ fn test_override_with_env ( ) {
387+ let test_value = "http://localhost:8000/testvalue" ;
388+ std:: env:: set_var ( "INDEXER_SERVICE_SUBGRAPHS__NETWORK__QUERY_URL" , test_value) ;
389+
390+ let config = Config :: parse (
391+ ConfigPrefix :: Service ,
392+ & PathBuf :: from ( "minimal-config-example.toml" ) ,
393+ )
394+ . unwrap ( ) ;
395+
396+ assert_eq ! (
397+ config. subgraphs. network. config. query_url. as_str( ) ,
398+ test_value
399+ ) ;
400+ }
333401}
0 commit comments