This is an automated email from the git hooks/post-receive script. guillem pushed a commit to branch main in repository dpkg.
View the commit online: https://git.dpkg.org/cgit/dpkg/dpkg.git/commit/?id=a3341d7db833b1e25f09b9cca6630301856c0aa8 commit a3341d7db833b1e25f09b9cca6630301856c0aa8 Author: Guillem Jover <[email protected]> AuthorDate: Wed Oct 26 19:55:58 2022 +0200 Dpkg::File: Add new file_dump() function --- scripts/Dpkg/File.pm | 20 ++++++++++++++++++++ scripts/Makefile.am | 2 ++ scripts/t/Dpkg_File.t | 22 +++++++++++++++++++++- scripts/t/Dpkg_File/append-me | 3 +++ scripts/t/Dpkg_File/{slurp-me => dump-partial} | 1 - 5 files changed, 46 insertions(+), 2 deletions(-) diff --git a/scripts/Dpkg/File.pm b/scripts/Dpkg/File.pm index 6ba49a6e6..316198989 100644 --- a/scripts/Dpkg/File.pm +++ b/scripts/Dpkg/File.pm @@ -22,6 +22,7 @@ use warnings; our $VERSION = '0.01'; our @EXPORT = qw( file_slurp + file_dump ); use Exporter qw(import); @@ -48,4 +49,23 @@ sub file_slurp { return $data; } +sub file_dump { + my ($file, $data) = @_; + my $fh; + my $doclose = 0; + + if (openhandle($file)) { + $fh = $file; + } else { + open $fh, '>', $file or syserr(g_('cannot create file %s'), $file); + $doclose = 1; + } + print { $fh } $data; + if ($doclose) { + close $fh or syserr(g_('cannot write %s'), $file); + } + + return; +} + 1; diff --git a/scripts/Makefile.am b/scripts/Makefile.am index 862af6cb9..3e7053617 100644 --- a/scripts/Makefile.am +++ b/scripts/Makefile.am @@ -279,6 +279,8 @@ test_data = \ t/Dpkg_Dist_Files/files-byhand \ t/Dpkg_Dist_Files/files-fragment-a \ t/Dpkg_Dist_Files/files-fragment-b \ + t/Dpkg_File/append-me \ + t/Dpkg_File/dump-partial \ t/Dpkg_File/slurp-me \ t/Dpkg_OpenPGP/package_1.0.orig.tar \ t/Dpkg_OpenPGP/package_1.0.orig.tar.asc \ diff --git a/scripts/t/Dpkg_File.t b/scripts/t/Dpkg_File.t index b36356105..5ed187acd 100644 --- a/scripts/t/Dpkg_File.t +++ b/scripts/t/Dpkg_File.t @@ -16,14 +16,18 @@ use strict; use warnings; -use Test::More tests => 4; +use Test::More tests => 7; use Test::Dpkg qw(:paths); +use File::Compare; +use File::Path qw(rmtree); + BEGIN { use_ok('Dpkg::File'); } my $datadir = test_get_data_path(); +my $tempdir = test_get_temp_path(); my ($data, $data_ref, $data_fh); @@ -35,6 +39,10 @@ final line DATA is($data, $data_ref, 'slurped data'); +file_dump("$tempdir/slurp-me", $data); +ok(compare("$tempdir/slurp-me", "$datadir/slurp-me") == 0, + 'dumped slurped data'); + open $data_fh, '<', "$datadir/slurp-me" or die "cannot open $datadir/slurp-me for reading: $!"; my $discard = <$data_fh>; @@ -46,6 +54,18 @@ final line DATA is($data, $data_ref, 'slurped partial data'); +file_dump("$tempdir/dump-partial", $data); +ok(compare("$tempdir/dump-partial", "$datadir/dump-partial") == 0, + 'dumped slurped partial data'); + +open $data_fh, '>', "$tempdir/append-me" + or die "cannot create $tempdir/append-me: $!"; +print { $data_fh } "append line\n"; +file_dump($data_fh, "new line\nend line\n"); +close $data_fh; +ok(compare("$tempdir/append-me", "$datadir/append-me") == 0, + 'dumped appended data'); + $data = undef; eval { $data = file_slurp("$datadir/non-existent"); diff --git a/scripts/t/Dpkg_File/append-me b/scripts/t/Dpkg_File/append-me new file mode 100644 index 000000000..d332d6409 --- /dev/null +++ b/scripts/t/Dpkg_File/append-me @@ -0,0 +1,3 @@ +append line +new line +end line diff --git a/scripts/t/Dpkg_File/slurp-me b/scripts/t/Dpkg_File/dump-partial similarity index 65% copy from scripts/t/Dpkg_File/slurp-me copy to scripts/t/Dpkg_File/dump-partial index 8840ed422..4a00dc04b 100644 --- a/scripts/t/Dpkg_File/slurp-me +++ b/scripts/t/Dpkg_File/dump-partial @@ -1,3 +1,2 @@ -first line next line final line -- Dpkg.Org's dpkg

