Skip to content

Commit

Permalink
Store relative path of file in baseline file (#2147)
Browse files Browse the repository at this point in the history
Closes #2146
  • Loading branch information
paul-dingemans committed Jul 23, 2023
1 parent 065deba commit 7694110
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
* Fix indent of IS_EXPRESSION, PREFIX_EXPRESSION and POSTFIX_EXPRESSION in case it contains a linebreak `indent` [#2094](https://github.com/pinterest/ktlint/issues/2094)
* Fix false positive regarding property that potentially can contain data which is mutable. `property-naming` [#2140](https://github.com/pinterest/ktlint/issues/2140)
* Add new experimental rule `function-literal`. This rule enforces the parameter list of a function literal to be formatted consistently. `function-literal` [#2121](https://github.com/pinterest/ktlint/issues/2121)
* Store relative path of file in baseline file [#2146](https://github.com/pinterest/ktlint/issues/2146)
* Fix null pointer exception for if-else statement with empty THEN block `if-else-bracing` [#2135](https://github.com/pinterest/ktlint/issues/2135)

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ package com.pinterest.ktlint.cli.reporter.baseline
import com.pinterest.ktlint.cli.reporter.core.api.KtlintCliError
import com.pinterest.ktlint.cli.reporter.core.api.KtlintCliError.Status.FORMAT_IS_AUTOCORRECTED
import com.pinterest.ktlint.cli.reporter.core.api.ReporterV2
import java.io.File
import java.io.PrintStream
import java.nio.file.Path
import java.nio.file.Paths
import java.util.concurrent.ConcurrentHashMap
import kotlin.io.path.pathString
import kotlin.io.path.relativeToOrSelf

public class BaselineReporter(
private val out: PrintStream,
Expand All @@ -24,7 +29,10 @@ public class BaselineReporter(
out.println("""<?xml version="1.0" encoding="utf-8"?>""")
out.println("""<baseline version="1.0">""")
for ((file, errList) in acc.entries.sortedBy { it.key }) {
out.println(""" <file name="${file.escapeXMLAttrValue()}">""")
// Store error in baseline always a relative path. This allows a baseline file to be stored inside a repository and after
// checking out this repository on a different path, the baseline will still be respected.
val relativeFile = Paths.get(file).relativeLocation()
out.println(""" <file name="${relativeFile.escapeXMLAttrValue()}">""")
for ((line, col, ruleId, _) in errList) {
out.println(""" <error line="$line" column="$col" source="$ruleId" />""")
}
Expand All @@ -39,4 +47,13 @@ public class BaselineReporter(
.replace("'", "&apos;")
.replace("<", "&lt;")
.replace(">", "&gt;")

private fun Path.relativeLocation() =
relativeToOrSelf(ROOT_DIR_PATH)
.pathString
.replace(File.separatorChar, '/')

private companion object {
val ROOT_DIR_PATH: Path = Paths.get("").toAbsolutePath()
}
}

0 comments on commit 7694110

Please sign in to comment.