diff --git a/utils/common/src/funTest/kotlin/SafeDeleteRecursivelyFunTest.kt b/utils/common/src/funTest/kotlin/SafeDeleteRecursivelyFunTest.kt new file mode 100644 index 0000000000000..32c40d09bc369 --- /dev/null +++ b/utils/common/src/funTest/kotlin/SafeDeleteRecursivelyFunTest.kt @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2024 The ORT Project Authors (see ) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * License-Filename: LICENSE + */ + +package org.ossreviewtoolkit.utils.common + +import io.kotest.assertions.throwables.shouldNotThrow +import io.kotest.core.spec.style.WordSpec +import io.kotest.engine.spec.tempdir +import io.kotest.matchers.shouldBe + +import java.io.IOException + +class SafeDeleteRecursivelyFunTest : WordSpec({ + "File.safeDeleteRecursively()" should { + "be able to delete files that are not writable" { + val dir = tempdir().apply { + resolve("read-only.txt").apply { + writeText("Hello!") + check(setWritable(false)) + } + } + + shouldNotThrow { + dir.safeDeleteRecursively(force = true) + } + + dir.exists() shouldBe false + } + + "delete empty parent directories below a base directory" { + val tempDir = tempdir() + val baseDir = tempDir.resolve("a/basedir") + val deleteDir = baseDir.resolve("c/delete").apply { safeMkdirs() } + + shouldNotThrow { + deleteDir.safeDeleteRecursively(force = true, baseDir) + } + + deleteDir.exists() shouldBe false + deleteDir.parentFile.exists() shouldBe false + baseDir.exists() shouldBe true + } + } +}) diff --git a/utils/common/src/test/kotlin/ExtensionsTest.kt b/utils/common/src/test/kotlin/ExtensionsTest.kt index dee75f1f431c9..6a3f5d7385365 100644 --- a/utils/common/src/test/kotlin/ExtensionsTest.kt +++ b/utils/common/src/test/kotlin/ExtensionsTest.kt @@ -99,37 +99,6 @@ class ExtensionsTest : WordSpec({ } } - "File.safeDeleteRecursively()" should { - "be able to delete files that are not writable" { - val dir = tempdir().apply { - resolve("read-only.txt").apply { - writeText("Hello!") - check(setWritable(false)) - } - } - - shouldNotThrow { - dir.safeDeleteRecursively(force = true) - } - - dir.exists() shouldBe false - } - - "delete empty parent directories below a base directory" { - val tempDir = tempdir() - val baseDir = tempDir.resolve("a/basedir") - val deleteDir = baseDir.resolve("c/delete").apply { safeMkdirs() } - - shouldNotThrow { - deleteDir.safeDeleteRecursively(force = true, baseDir) - } - - deleteDir.exists() shouldBe false - deleteDir.parentFile.exists() shouldBe false - baseDir.exists() shouldBe true - } - } - "File.expandTilde()" should { "expand the path if the SHELL environment variable is set".config(enabled = Os.env["SHELL"] != null) { File("~/Desktop").expandTilde() shouldBe Os.userHomeDirectory.resolve("Desktop")