Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #9584 - Using dirEntries and chdir() can have unwanted results. #6125

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions std/file.d
Original file line number Diff line number Diff line change
Expand Up @@ -4038,7 +4038,8 @@ foreach (d; dFiles)
+/
auto dirEntries(string path, SpanMode mode, bool followSymlink = true)
{
return DirIterator(path, mode, followSymlink);
import std.path : absolutePath;
return DirIterator(absolutePath(path), mode, followSymlink);
}

/// Duplicate functionality of D1's $(D std.file.listdir()):
Expand Down Expand Up @@ -4109,7 +4110,7 @@ auto dirEntries(string path, SpanMode mode, bool followSymlink = true)
foreach (string name; dirEntries(testdir, SpanMode.breadth))
{
//writeln(name);
assert(name.startsWith(testdir));
assert(name.startsWith(absolutePath(testdir)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not break existing tests!

Changing existing unit tests to conform to changes in the implementation is almost always a mistake.

}
foreach (DirEntry e; dirEntries(absolutePath(testdir), SpanMode.breadth))
{
Expand All @@ -4135,6 +4136,17 @@ auto dirEntries(string path, SpanMode mode, bool followSymlink = true)

// issue 15146
dirEntries("", SpanMode.shallow).walkLength();

// issue 6138
string cwd = getcwd();
foreach (string entry; dirEntries(testdir, SpanMode.shallow))
{
if (entry.isDir)
{
chdir(entry);
}
}
chdir (cwd); // needed for the directories to be removed
}

/// Ditto
Expand Down