From 84f5a294f770b9332b7aa860b961867dd30bf45d Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Fri, 20 Nov 2020 05:29:04 +0300 Subject: [PATCH] Allow passing null parent directory to createTempFile/Directory null signifies the default temp directory. KT-19192 --- .../jdk7/src/kotlin/io/path/PathUtils.kt | 28 ++++++++++++------- .../stdlib/jdk7/test/PathExtensionsTest.kt | 18 ++++++++++++ .../kotlin-stdlib-jdk7.txt | 4 +++ 3 files changed, 40 insertions(+), 10 deletions(-) diff --git a/libraries/stdlib/jdk7/src/kotlin/io/path/PathUtils.kt b/libraries/stdlib/jdk7/src/kotlin/io/path/PathUtils.kt index 4316fb8ee1b..17536f48ebc 100644 --- a/libraries/stdlib/jdk7/src/kotlin/io/path/PathUtils.kt +++ b/libraries/stdlib/jdk7/src/kotlin/io/path/PathUtils.kt @@ -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. diff --git a/libraries/stdlib/jdk7/test/PathExtensionsTest.kt b/libraries/stdlib/jdk7/test/PathExtensionsTest.kt index e7fda78b02b..7c14e214b03 100644 --- a/libraries/stdlib/jdk7/test/PathExtensionsTest.kt +++ b/libraries/stdlib/jdk7/test/PathExtensionsTest.kt @@ -59,6 +59,24 @@ class PathExtensionsTest : AbstractPathTest() { assertFailsWith { 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() diff --git a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-jdk7.txt b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-jdk7.txt index 059f0aad4f3..e05528a7486 100644 --- a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-jdk7.txt +++ b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-jdk7.txt @@ -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;