Skip to content

Commit

Permalink
refactor(common-utils): Move recursive deletion tests to funTest
Browse files Browse the repository at this point in the history
Prepare for adding a more heavy-weight test.

Signed-off-by: Sebastian Schuberth <sebastian@doubleopen.org>
  • Loading branch information
sschuberth committed Sep 5, 2024
1 parent ab12481 commit 78154d8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 31 deletions.
60 changes: 60 additions & 0 deletions utils/common/src/funTest/kotlin/SafeDeleteRecursivelyFunTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (C) 2024 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>)
*
* 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<IOException> {
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<IOException> {
deleteDir.safeDeleteRecursively(force = true, baseDir)
}

deleteDir.exists() shouldBe false
deleteDir.parentFile.exists() shouldBe false
baseDir.exists() shouldBe true
}
}
})
31 changes: 0 additions & 31 deletions utils/common/src/test/kotlin/ExtensionsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<IOException> {
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<IOException> {
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")
Expand Down

0 comments on commit 78154d8

Please sign in to comment.