diff --git a/lib/Code/TidyAll.pm b/lib/Code/TidyAll.pm index 4283ef2f..af3783a0 100644 --- a/lib/Code/TidyAll.pm +++ b/lib/Code/TidyAll.pm @@ -350,7 +350,10 @@ sub process_file { # write new contents out to disk $contents = $result->new_contents; - path( $full_path . $self->output_suffix )->spew($contents); + + # We don't use ->spew because that creates a new file and renames it, + # losing the existing mode setting in the process. + path( $full_path . $self->output_suffix )->append( { truncate => 1 }, $contents ); # change the in memory contents of the cache (but don't update yet) $cache_model->file_contents($contents) unless $self->output_suffix; diff --git a/t/lib/Test/Code/TidyAll/Basic.pm b/t/lib/Test/Code/TidyAll/Basic.pm index 6efc4a06..fb1800d1 100644 --- a/t/lib/Test/Code/TidyAll/Basic.pm +++ b/t/lib/Test/Code/TidyAll/Basic.pm @@ -78,6 +78,20 @@ sub test_basic : Tests { ); } +sub test_filemode : Tests { + my $self = shift; + my $root_dir = $self->create_dir( { "foo.txt" => "abc" } ); + my $file = $root_dir->child('foo.txt'); + $file->chmod('0755'); + my $ct = Code::TidyAll->new( + plugins => { test_plugin('UpperText') => { select => '**/foo*' } }, + root_dir => $root_dir, + ); + $ct->process_paths($file); + is( $file->slurp, "ABC" ); + is( $file->stat->mode, 0100755 ); +} + sub test_multiple_plugin_instances : Tests { my $self = shift; $self->tidy(