Skip to content

Commit

Permalink
Merge pull request #120 from tonycoz/107-dont-modify-source-files
Browse files Browse the repository at this point in the history
chmod/chown/utime: don't modify ourselves
  • Loading branch information
toddr authored Dec 28, 2023
2 parents 9d4ac6f + 68f311d commit d2da12f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
9 changes: 6 additions & 3 deletions t/chmod.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use Test::More tests => 7;
use constant NO_SUCH_FILE => "this_file_had_better_not_exist";
use constant ERROR_REGEXP => qr{Can't chmod\(0755, '${\(NO_SUCH_FILE)}'\):};
use constant SINGLE_DIGIT_ERROR_REGEXP => qr{Can't chmod\(0010, '${\(NO_SUCH_FILE)}'\):};
use File::Temp qw(tempfile);
use autodie;

# This tests RT #50423, Debian #550462
Expand All @@ -16,9 +17,11 @@ eval { chmod(8, NO_SUCH_FILE); };
isa_ok($@, 'autodie::exception', 'exception thrown for chmod');
like($@, SINGLE_DIGIT_ERROR_REGEXP, "Message should include numeric mode in octal form");

eval { chmod(0755, $0); };
ok(! $@, "We can chmod ourselves just fine.");
my ($fh, $filename) = tempfile;

eval { chmod(0755, $0, NO_SUCH_FILE) };
eval { chmod(0755, $filename); };
ok(! $@, "We can chmod a file we own just fine.");

eval { chmod(0755, $filename, NO_SUCH_FILE) };
isa_ok($@, 'autodie::exception', 'chmod exception on any file failure.');
is($@->return,1,"Confirm autodie on a 'true' chown failure.");
9 changes: 6 additions & 3 deletions t/chown.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use strict;
use Test::More;
use constant NO_SUCH_FILE => "this_file_had_better_not_exist";
use autodie;
use File::Temp qw(tempfile);

if ($^O eq 'MSWin32') {
plan skip_all => 'chown() seems to always succeed on Windows';
Expand All @@ -20,9 +21,11 @@ isa_ok($@, 'autodie::exception', 'exception thrown for chown');
# should die if the return value is not equal to the number of arguments
# minus two.

eval { chown($<, -1, $0); };
ok(! $@, "Can chown ourselves just fine.");
my ($fh, $filename) = tempfile;

eval { chown($<, -1, $0, NO_SUCH_FILE); };
eval { chown($<, -1, $filename); };
ok(! $@, "Can chown a file we own just fine.");

eval { chown($<, -1, $filename, NO_SUCH_FILE); };
isa_ok($@, 'autodie::exception', "Exception if ANY file changemode fails");
is($@->return, 1, "Confirm we're dying on a 'true' chown failure.");
2 changes: 0 additions & 2 deletions t/touch_me

This file was deleted.

14 changes: 6 additions & 8 deletions t/utime.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@ use Test::More tests => 4;
use constant NO_SUCH_FILE => "this_file_had_better_not_exist";
use FindBin qw($Bin);
use File::Spec;
use constant TOUCH_ME => File::Spec->catfile($Bin, 'touch_me');
use autodie;
use File::Temp qw(tempfile);

my ($fh, $filename) = tempfile;

eval { utime(undef, undef, NO_SUCH_FILE); };
isa_ok($@, 'autodie::exception', 'exception thrown for utime');

my($atime, $mtime) = (stat TOUCH_ME)[8, 9];
my($atime, $mtime) = (stat $filename)[8, 9];

eval { utime(undef, undef, TOUCH_ME); };
eval { utime(undef, undef, $filename); };
ok(! $@, "We can utime a file just fine.") or diag $@;

eval { utime(undef, undef, NO_SUCH_FILE, TOUCH_ME); };
eval { utime(undef, undef, NO_SUCH_FILE, $filename); };
isa_ok($@, 'autodie::exception', 'utime exception on single failure.');
is($@->return, 1, "utime fails correctly on a 'true' failure.");

# Reset timestamps so that Git doesn't think the file has changed when
# running the test in the core perl distribution.
utime($atime, $mtime, TOUCH_ME);

0 comments on commit d2da12f

Please sign in to comment.