Allow passing null parent directory to createTempFile/Directory

null signifies the default temp directory.

KT-19192
This commit is contained in:
Ilya Gorbunov
2020-11-20 05:29:04 +03:00
parent 64d85f259c
commit 84f5a294f7
3 changed files with 40 additions and 10 deletions
@@ -785,7 +785,7 @@ public inline fun Path.createFile(vararg attributes: FileAttribute<*>): Path =
* @param attributes an optional list of file attributes to set atomically when creating the file.
* @return the path to the newly created file that did not exist before.
*
* @throws UnsupportedOperationException if the array contains an attribute that cannot be set atomically
* @throws UnsupportedOperationException if the array contains an attribute that cannot be set atomically
* when creating the file.
*
* @see Files.createTempFile
@@ -800,19 +800,23 @@ public inline fun createTempFile(prefix: String? = null, suffix: String? = null,
* Creates an empty file in the specified [directory], using
* the given [prefix] and [suffix] to generate its name.
*
* @param directory the parent directory in which to create a new file.
* It can be `null`, in that case the new file is created in the default temp directory.
* @param attributes an optional list of file attributes to set atomically when creating the file.
* @return the path to the newly created file that did not exist before.
*
* @throws UnsupportedOperationException if the array contains an attribute that cannot be set atomically
* @throws UnsupportedOperationException if the array contains an attribute that cannot be set atomically
* when creating the file.
*
* @see Files.createTempFile
*/
@SinceKotlin("1.4")
@ExperimentalPathApi
@kotlin.internal.InlineOnly
public inline fun createTempFile(directory: Path, prefix: String? = null, suffix: String? = null, vararg attributes: FileAttribute<*>): Path =
Files.createTempFile(directory, prefix, suffix, *attributes)
public fun createTempFile(directory: Path?, prefix: String? = null, suffix: String? = null, vararg attributes: FileAttribute<*>): Path =
if (directory != null)
Files.createTempFile(directory, prefix, suffix, *attributes)
else
Files.createTempFile(prefix, suffix, *attributes)
/**
* Creates a new directory in the default temp directory, using the given [prefix] to generate its name.
@@ -820,7 +824,7 @@ public inline fun createTempFile(directory: Path, prefix: String? = null, suffix
* @param attributes an optional list of file attributes to set atomically when creating the directory.
* @return the path to the newly created directory that did not exist before.
*
* @throws UnsupportedOperationException if the array contains an attribute that cannot be set atomically
* @throws UnsupportedOperationException if the array contains an attribute that cannot be set atomically
* when creating the directory.
*
* @see Files.createTempDirectory
@@ -834,19 +838,23 @@ public inline fun createTempDirectory(prefix: String? = null, vararg attributes:
/**
* Creates a new directory in the specified [directory], using the given [prefix] to generate its name.
*
* @param directory the parent directory in which to create a new directory.
* It can be `null`, in that case the new directory is created in the default temp directory.
* @param attributes an optional list of file attributes to set atomically when creating the directory.
* @return the path to the newly created directory that did not exist before.
*
* @throws UnsupportedOperationException if the array contains an attribute that cannot be set atomically
* @throws UnsupportedOperationException if the array contains an attribute that cannot be set atomically
* when creating the directory.
*
* @see Files.createTempDirectory
*/
@SinceKotlin("1.4")
@ExperimentalPathApi
@kotlin.internal.InlineOnly
public inline fun createTempDirectory(directory: Path, prefix: String? = null, vararg attributes: FileAttribute<*>): Path =
Files.createTempDirectory(directory, prefix, *attributes)
public fun createTempDirectory(directory: Path?, prefix: String? = null, vararg attributes: FileAttribute<*>): Path =
if (directory != null)
Files.createTempDirectory(directory, prefix, *attributes)
else
Files.createTempDirectory(prefix, *attributes)
/**
* Resolves the given [other] path against this path.
@@ -59,6 +59,24 @@ class PathExtensionsTest : AbstractPathTest() {
assertFailsWith<FileAlreadyExistsException> { file.createFile() }
}
@Test
fun createTempFileDefaultDir() {
val file1 = createTempFile().cleanup()
val file2 = createTempFile(directory = null).cleanup()
assertEquals(file1.parent, file2.parent)
}
@Test
fun createTempDirectoryDefaultDir() {
val dir1 = createTempDirectory().cleanup()
val dir2 = createTempDirectory(directory = null).cleanupRecursively()
val dir3 = createTempDirectory(dir2)
assertEquals(dir1.parent, dir2.parent)
assertNotEquals(dir2.parent, dir3.parent)
}
@Test
fun copyTo() {
val root = createTempDirectory("copyTo-root").cleanupRecursively()
@@ -4,6 +4,10 @@ public abstract interface annotation class kotlin/io/path/ExperimentalPathApi :
public final class kotlin/io/path/PathsKt {
public static final fun appendText (Ljava/nio/file/Path;Ljava/lang/CharSequence;Ljava/nio/charset/Charset;)V
public static synthetic fun appendText$default (Ljava/nio/file/Path;Ljava/lang/CharSequence;Ljava/nio/charset/Charset;ILjava/lang/Object;)V
public static final fun createTempDirectory (Ljava/nio/file/Path;Ljava/lang/String;[Ljava/nio/file/attribute/FileAttribute;)Ljava/nio/file/Path;
public static synthetic fun createTempDirectory$default (Ljava/nio/file/Path;Ljava/lang/String;[Ljava/nio/file/attribute/FileAttribute;ILjava/lang/Object;)Ljava/nio/file/Path;
public static final fun createTempFile (Ljava/nio/file/Path;Ljava/lang/String;Ljava/lang/String;[Ljava/nio/file/attribute/FileAttribute;)Ljava/nio/file/Path;
public static synthetic fun createTempFile$default (Ljava/nio/file/Path;Ljava/lang/String;Ljava/lang/String;[Ljava/nio/file/attribute/FileAttribute;ILjava/lang/Object;)Ljava/nio/file/Path;
public static final fun fileAttributeViewNotAvailable (Ljava/nio/file/Path;Ljava/lang/Class;)Ljava/lang/Void;
public static final fun getExtension (Ljava/nio/file/Path;)Ljava/lang/String;
public static final fun getInvariantSeparatorsPath (Ljava/nio/file/Path;)Ljava/lang/String;