Skip to content

Commit

Permalink
Use cleaner Path functions (#7236)
Browse files Browse the repository at this point in the history
* Use createParentDirectories instead of createDirectories

* Use absolute() instead of toAbsolutePath()
  • Loading branch information
3flex committed Apr 27, 2024
1 parent a06562d commit 8e7364e
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 17 deletions.
@@ -1,7 +1,7 @@
package io.gitlab.arturbosch.detekt.api

import java.nio.file.Path
import kotlin.io.path.createDirectories
import kotlin.io.path.createParentDirectories
import kotlin.io.path.extension
import kotlin.io.path.writeText

Expand All @@ -25,8 +25,7 @@ abstract class OutputReport : Extension {
assert(filePath.extension == ending) {
"The $id needs to have a file ending of type .$ending, but was ${filePath.fileName}."
}
filePath.parent?.createDirectories()
filePath.writeText(reportData)
filePath.createParentDirectories().writeText(reportData)
}
}

Expand Down
Expand Up @@ -9,12 +9,13 @@ import org.jetbrains.kotlin.psi.psiUtil.findDescendantOfType
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import kotlin.io.path.Path
import kotlin.io.path.absolute
import kotlin.io.path.relativeToOrSelf
import kotlin.io.path.toPath

class EntitySpec {

private val path = Path("src/test/resources/EntitySpecFixture.kt").toAbsolutePath()
private val path = Path("src/test/resources/EntitySpecFixture.kt").absolute()
private val basePath = EntitySpec::class.java.getResource("/")!!.toURI().toPath()
private val relativePath = path.relativeToOrSelf(basePath)
private val code = compileForTest(path)
Expand Down
Expand Up @@ -34,9 +34,9 @@ internal class CliArgsSpec {
@Test
fun `the current working directory is used if parameter is not set`() {
val spec = parseArguments(emptyArray()).toSpec()
val workingDir = Path("").toAbsolutePath()
val workingDir = Path("").absolute()

assertThat(spec.projectSpec.inputPaths).allSatisfy { it.toAbsolutePath().startsWith(workingDir) }
assertThat(spec.projectSpec.inputPaths).allSatisfy { it.absolute().startsWith(workingDir) }
assertThat(spec.projectSpec.inputPaths).contains(pathBuildGradle)
assertThat(spec.projectSpec.inputPaths).contains(pathCliArgs)
assertThat(spec.projectSpec.inputPaths).contains(pathCliArgsSpec)
Expand Down
Expand Up @@ -5,7 +5,7 @@ import io.gitlab.arturbosch.detekt.api.ReportingExtension
import io.gitlab.arturbosch.detekt.api.SetupContext
import io.gitlab.arturbosch.detekt.api.getOrNull
import java.nio.file.Path
import kotlin.io.path.createDirectories
import kotlin.io.path.createParentDirectories
import kotlin.io.path.exists
import kotlin.io.path.isRegularFile

Expand Down Expand Up @@ -59,7 +59,7 @@ class BaselineResultMapping : ReportingExtension {
val baselineFormat = BaselineFormat()
val baseline = baselineFormat.of(oldBaseline.manuallySuppressedIssues, ids)
if (oldBaseline != baseline) {
baselineFile.parent?.createDirectories()
baselineFile.createParentDirectories()
baselineFormat.write(baselineFile, baseline)
}
}
Expand Down
Expand Up @@ -11,6 +11,7 @@ import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import kotlin.io.path.Path
import kotlin.io.path.absolute

class FormattingRuleSpec {

Expand Down Expand Up @@ -69,7 +70,7 @@ class FormattingRuleSpec {

@Test
fun `#3063_ formatting issues have an absolute path`() {
val expectedPath = Path("src/test/resources/configTests/chain-wrapping-before.kt").toAbsolutePath()
val expectedPath = Path("src/test/resources/configTests/chain-wrapping-before.kt").absolute()

val rule = ChainWrapping(Config.empty)
val findings = rule.visitFile(compileForTest(expectedPath))
Expand Down
Expand Up @@ -2,8 +2,7 @@ package io.gitlab.arturbosch.detekt.generator.out

import java.io.PrintStream
import java.nio.file.Path
import kotlin.io.path.createDirectories
import kotlin.io.path.exists
import kotlin.io.path.createParentDirectories
import kotlin.io.path.writeText

internal abstract class AbstractWriter(
Expand All @@ -14,12 +13,7 @@ internal abstract class AbstractWriter(

fun write(path: Path, fileName: String, content: () -> String) {
val filePath = path.resolve("$fileName.$ending")
filePath.parent?.let { parentPath ->
if (!parentPath.exists()) {
parentPath.createDirectories()
}
}
filePath.writeText(content())
filePath.createParentDirectories().writeText(content())
outputPrinter.println("Wrote: $filePath")
}
}
Expand Down

0 comments on commit 8e7364e

Please sign in to comment.