Skip to content

Commit

Permalink
Preserve file mode (permissions) on tidy.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
adherzog authored and autarch committed Oct 10, 2016
1 parent c8c2749 commit bfefe8a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/Code/TidyAll.pm
Expand Up @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions t/lib/Test/Code/TidyAll/Basic.pm
Expand Up @@ -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(
Expand Down

0 comments on commit bfefe8a

Please sign in to comment.