From bfefe8a6bc5c5913a89889bad821afc5520b42d4 Mon Sep 17 00:00:00 2001 From: Adam Herzog Date: Sat, 8 Oct 2016 12:59:42 -0700 Subject: [PATCH] Preserve file mode (permissions) on tidy. This changes the method of writing out the tidied file from ->spew() to ->append({truncate => 1}). spew() copies the new file into place, which ends up resetting the file mode based on the current umask. Using append() instead preserves the file permissions and ownership, without us having to keep track of the mode and resetting it after writing the file. --- lib/Code/TidyAll.pm | 5 ++++- t/lib/Test/Code/TidyAll/Basic.pm | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) 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(