Still there's the possibility of race conditions (multiple test runs happen at the same time, e.g. for multiple perls) or the possibility of some crash between creation of the test contents and running the END{} block.
One possibility would be to fake the $HOME environment variable and use an exclusive version of the config file. See the attached patch.
The original test could maybe still be used as an author or travis-ci only test.
On 2015-04-18 22:32:57, KIMRYAN wrote:
Show quoted text> Sorry, code should be
>
> my $xdg_user_dir_installed = 0;
> if (which 'xdg-user-dir') {
> plan tests => 8;
> $xdg_user_dir_installed = 1;
> } else {
> plan skip_all => '"xdg-user-dir" executable not found. Install
> package "xdg-user-dirs".';
>
> }
>
> On Sat Apr 18 22:12:20 2015, KIMRYAN wrote:
> > Thanks for pointing out this bug. I think this is the change required
> > to get it working.
> >
> > my $xdg_user_dir_installed = 0;
> > if (which 'xdg-user-dir') {
> > plan tests => 8;
> > $xdg_user_dir_installed = 0;
> > } else {
> > plan skip_all => '"xdg-user-dir" executable not found. Install
> > package "xdg-user-dirs".';
> > $xdg_user_dir_installed = 1;
> >
> > }
> >
> > Agree it is not good practice to be altering config file contents
> > during test phase. I adopted this module as it had been abandoned,
> > and
> > applied several old patches. This includes the 03_userdirs.t test.
> > It
> > relies on running the xdg-user-dir binary, which doesn't seem like a
> > good thing to put into a CPAN test suite. But I don't really have the
> > time or knowledge to try and unravel all this code. So if you think
> > above fix is enough, I can apply that.
diff --git i/t/03_userdirs.t w/t/03_userdirs.t
index d28f344..0302c7b 100644
--- i/t/03_userdirs.t
+++ w/t/03_userdirs.t
@@ -6,6 +6,7 @@ use File::UserDirs qw(:all);
use File::BaseDir qw(config_home);
use File::Spec::Functions qw(catfile);
use File::Which qw(which);
+use File::Temp qw(tempdir);
my $xdg_user_dir_installed = 0;
if (which 'xdg-user-dir') {
@@ -16,10 +17,10 @@ if (which 'xdg-user-dir') {
}
-my $udd = config_home('user-dirs.dirs');
-if (-e $udd) {
- rename $udd, "$udd~" or die "could not make backup of $udd: $!";
-}
+my $temphomedir = tempdir(CLEANUP => 1);
+local $ENV{HOME} = $temphomedir;
+mkdir "$temphomedir/.config";
+my $udd = "$temphomedir/.config/user-dirs.dirs";
open my $fh, '>', $udd or die "could not open $udd for writing: $!";
print $fh <<'UDD';
@@ -43,12 +44,3 @@ is xdg_publicshare_dir, catfile($ENV{HOME}, 'public_html');
is xdg_templates_dir, catfile($ENV{HOME}, 'Files/Document templates');
is xdg_videos_dir, catfile($ENV{HOME}, 'Files/Video');
-END {
- if ($xdg_user_dir_installed) {
- if (-e "$udd~") {
- rename "$udd~", $udd or die "could not restore backup of $udd: $!";
- } else {
- unlink $udd or die "could not delete $udd: $!";
- }
- }
-}
\ No newline at end of file