Make Scapegoat sensor work with absolute file paths in the Scapegoat report #116
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Sonar Scala Plugin | ||
* Copyright (C) 2018 All contributors | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package com.mwz.sonar.scala | ||
|
||
import java.io.File | ||
import java.nio.file.{Files, Path} | ||
|
||
trait WithFile { | ||
def withFile(path: Path)(test: File => Any): Unit = { | ||
val file = Files.createFile(path).toFile | ||
try test(file) | ||
finally Files.deleteIfExists(path) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,25 +16,46 @@ | |
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package com.mwz.sonar.scala.scapegoat | ||
|
||
import org.scalatest.{FlatSpec, Matchers} | ||
package com.mwz.sonar.scala | ||
package scapegoat | ||
|
||
import java.nio.file.Paths | ||
|
||
import com.mwz.sonar.scala.util.PathUtils.cwd | ||
import org.scalatest.{FlatSpec, Matchers} | ||
|
||
/** Tests the correct behavior of the Scapegoat XML reports parser */ | ||
class ScapegoatReportParserSpec extends FlatSpec with Matchers { | ||
class ScapegoatReportParserSpec extends FlatSpec with Matchers with WithFile { | ||
val scapegoatReportParser = new ScapegoatReportParser() | ||
|
||
behavior of "the Scapegoat XML Report Parser" | ||
|
||
it should "replace all dots (.) except the last one in a scaegoat path with slashes (/)" in { | ||
"replaceAllDotsButLastWithSlashes" should "work with relative paths" in { | ||
val scapegoatPath = "com.mwz.sonar.scala.scapegoat.TestFile.scala" | ||
val linuxPath = scapegoatReportParser.replaceAllDotsButLastWithSlashes(scapegoatPath) | ||
|
||
linuxPath shouldBe "com/mwz/sonar/scala/scapegoat/TestFile.scala" | ||
} | ||
|
||
it should "work with absolute paths" in { | ||
val scapegoatPath = cwd.resolve("ScapegoatReportParserSpec.scala").toString.replace("/", ".") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would rather use a literal value. Both for the input and for the expected result. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is possible because the new implementation tests for the existence of the path on the file system so the input and output are both dynamic values based on where this project is located in the file system. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, yeah yeah my fault... |
||
val linuxPath = scapegoatReportParser.replaceAllDotsButLastWithSlashes(scapegoatPath) | ||
|
||
linuxPath shouldBe cwd.resolve("ScapegoatReportParserSpec.scala").toString | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would also add a test with filenames that have dots (.) inside them. Like: |
||
it should "handle correctly dots in the path" in withFile(cwd.resolve("example.file.scala")) { file => | ||
val scapegoatPath = file.toString.replace("/", ".") | ||
val linuxPath = scapegoatReportParser.replaceAllDotsButLastWithSlashes(scapegoatPath) | ||
|
||
linuxPath shouldBe file.toString | ||
} | ||
|
||
it should "handle correctly multiple dots in path" in withFile(cwd.resolve("example.file.scala")) { file => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aren't those two tests exactly the same?, or what am I missing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, it was meant to be a different test - I fixed it in 9c8fdba. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
val scapegoatPath = file.toString.replace("/", ".") | ||
val linuxPath = scapegoatReportParser.replaceAllDotsButLastWithSlashes(scapegoatPath) | ||
|
||
linuxPath shouldBe file.toString | ||
} | ||
|
||
it should "be able to parse an empty report" in { | ||
val scapegoatReportPath = Paths.get("src", "test", "resources", "scapegoat", "no-warnings.xml") | ||
val scapegoatWarnings = scapegoatReportParser.parse(scapegoatReportPath) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't confirm it, neither test it (cause I'm on my cell phone right now).
But, I think this isn't working the way you think.
For example, I think this file will make it fail:
target.scala.12..file.Scala
Ok, so I just noticed that all files in #115 scapegoat report start with
'.'
- that's probably because they are full paths. But, that wasn't the problem you tried to solve here, but instead paths with dots in their files - which could be relative.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll add some tests with extra dots in the middle of the path, but I think it's rather uncommon to have Scala source files hidden, i.e. starting with a
.
, so I think we can safely ignore this case.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hidden files probably don't, but what about paths like
scala-2.12
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, so your example would work since the implementation checks first if
scala-2
exists in the file system and if it doesn't instead of resolving12
as a subdirectory it concatenates it with a.
and the previous path before checking again for existence in the file system - I'll add a unit test to cover that.