@@ -5,6 +5,7 @@ mod python_environment;
55mod rule_selection;
66
77use anyhow:: Context as _;
8+ use insta:: Settings ;
89use insta:: internals:: SettingsBindDropGuard ;
910use insta_cmd:: { assert_cmd_snapshot, get_cargo_bin} ;
1011use std:: {
@@ -760,7 +761,8 @@ fn can_handle_large_binop_expressions() -> anyhow::Result<()> {
760761
761762pub ( crate ) struct CliTest {
762763 _temp_dir : TempDir ,
763- _settings_scope : SettingsBindDropGuard ,
764+ settings : Settings ,
765+ settings_scope : Option < SettingsBindDropGuard > ,
764766 project_dir : PathBuf ,
765767 ty_binary_path : PathBuf ,
766768}
@@ -795,7 +797,8 @@ impl CliTest {
795797 Ok ( Self {
796798 project_dir,
797799 _temp_dir : temp_dir,
798- _settings_scope : settings_scope,
800+ settings,
801+ settings_scope : Some ( settings_scope) ,
799802 ty_binary_path : get_cargo_bin ( "ty" ) ,
800803 } )
801804 }
@@ -838,6 +841,17 @@ impl CliTest {
838841 Ok ( self )
839842 }
840843
844+ /// Add a filter to the settings and rebind them.
845+ pub ( crate ) fn with_filter ( mut self , pattern : & str , replacement : & str ) -> Self {
846+ self . settings . add_filter ( pattern, replacement) ;
847+ // Drop the old scope before binding a new one, otherwise the old scope is dropped _after_
848+ // binding and assigning the new one, restoring the settings to their state before the old
849+ // scope was bound.
850+ drop ( self . settings_scope . take ( ) ) ;
851+ self . settings_scope = Some ( self . settings . bind_to_scope ( ) ) ;
852+ self
853+ }
854+
841855 fn ensure_parent_directory ( path : & Path ) -> anyhow:: Result < ( ) > {
842856 if let Some ( parent) = path. parent ( ) {
843857 std:: fs:: create_dir_all ( parent)
@@ -896,3 +910,11 @@ impl CliTest {
896910fn tempdir_filter ( path : & Path ) -> String {
897911 format ! ( r"{}\\?/?" , regex:: escape( path. to_str( ) . unwrap( ) ) )
898912}
913+
914+ fn site_packages_filter ( python_version : & str ) -> String {
915+ if cfg ! ( windows) {
916+ "Lib/site-packages" . to_string ( )
917+ } else {
918+ format ! ( "lib/python{}/site-packages" , regex:: escape( python_version) )
919+ }
920+ }
0 commit comments