changeset 26766:12fe42e16841

use temporary directory for testing dir function * dir.m: Use temporary directory for test to avoid the possibility of unexpected stray filenames.
author John W. Eaton <jwe@octave.org>
date Thu, 21 Feb 2019 19:21:57 +0000
parents c4e53014f248
children 178fd5e6c0c2
files scripts/miscellaneous/dir.m
diffstat 1 files changed, 28 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/miscellaneous/dir.m	Thu Feb 21 18:56:32 2019 +0000
+++ b/scripts/miscellaneous/dir.m	Thu Feb 21 19:21:57 2019 +0000
@@ -175,23 +175,36 @@
 
 
 %!test
-%! list = dir ();
-%! assert (isstruct (list) && ! isempty (list));
-%! assert (fieldnames (list),
-%!         {"name"; "folder"; "date"; "bytes"; "isdir"; "datenum"; "statinfo"});
+%! orig_dir = pwd ();
+%! tmp_dir = tempname ();
+%! unwind_protect
+%!   assert (mkdir (tmp_dir));
+%!   chdir (tmp_dir);
+%!   fclose (fopen ("f1", "w"));
+%!   list = dir ();
+%!   assert (isstruct (list) && ! isempty (list));
+%!   assert (fieldnames (list),
+%!           {"name"; "folder"; "date"; "bytes"; "isdir"; "datenum"; "statinfo"});
 %!
-%! if (isunix ())
-%!   idx = find (strcmp ({list.name}, "."), 1);
-%!   assert ({list(idx:idx+1).name}, {".", ".."});
-%!   assert ([list(idx:idx+1).isdir], [true true]);
-%! endif
+%!   if (isunix ())
+%!     idx = find (strcmp ({list.name}, "."), 1);
+%!     assert ({list(idx:idx+1).name}, {".", ".."});
+%!     assert ([list(idx:idx+1).isdir], [true true]);
+%!   endif
 %!
-%! ## test that specifying a filename works the same as using a directory.
-%! found = find (! [list.isdir], 1);
-%! if (! isempty (found))
-%!   list2 = dir (fullfile (list(found).folder, list(found).name));
-%!   assert (list(found), list2);
-%! endif
+%!   ## test that specifying a filename works the same as using a directory.
+%!   found = find (! [list.isdir], 1);
+%!   if (! isempty (found))
+%!     list2 = dir (fullfile (list(found).folder, list(found).name));
+%!     assert (list(found), list2);
+%!   endif
+%! unwind_protect_cleanup
+%!   chdir (orig_dir);
+%!   confirm_recursive_rmdir (false, "local");
+%!   if (exist (tmp_dir))
+%!     rmdir (tmp_dir, "s");
+%!   endif
+%! end_unwind_protect
 
 ## Test input validation
 %!error <DIRECTORY argument must be a string> dir (1)