Fix Issue 17488 - Add trailing slash to POSIX tempDir() to match Windows behaviour

This commit is contained in:
Ryan Frame 2017-06-11 22:29:09 -04:00 committed by RazvanN7
parent e1d6e6f116
commit 02f4f148cf

View file

@ -5275,9 +5275,20 @@ slurp(Types...)(string filename, scope const(char)[] format)
assert(slurp!(int)(deleteme, "%d") == [10, 20]);
}
/**
Returns the path to a directory for temporary files.
On POSIX platforms, it searches through the following list of directories
and returns the first one which is found to exist:
$(OL
$(LI The directory given by the `TMPDIR` environment variable.)
$(LI The directory given by the `TEMP` environment variable.)
$(LI The directory given by the `TMP` environment variable.)
$(LI `/tmp/`)
$(LI `/var/tmp/`)
$(LI `/usr/tmp/`)
)
On all platforms, `tempDir` returns the current working directory on failure.
The return value of the function is cached, so the procedures described
below will only be performed the first time the function is called. All
@ -5321,13 +5332,14 @@ string tempDir() @trusted
}
else version (Posix)
{
import std.path : dirSeparator;
import std.process : environment;
// This function looks through the list of alternative directories
// and returns the first one which exists and is a directory.
static string findExistingDir(T...)(lazy T alternatives)
{
foreach (dir; alternatives)
if (!dir.empty && exists(dir)) return dir;
if (!dir.empty && exists(dir)) return dir ~ dirSeparator;
return null;
}
@ -5340,7 +5352,10 @@ string tempDir() @trusted
}
else static assert(false, "Unsupported platform");
if (cache is null) cache = getcwd();
if (cache is null)
{
cache = getcwd() ~ dirSeparator;
}
}
return cache;
}
@ -5363,6 +5378,13 @@ string tempDir() @trusted
assert(myFile.readText == "hello");
}
@safe unittest
{
import std.algorithm.searching : endsWith;
import std.path : dirSeparator;
assert(tempDir.endsWith(dirSeparator));
}
/**
Returns the available disk space based on a given path.
On Windows, `path` must be a directory; on POSIX systems, it can be a file or directory.