Closed thetroy closed 1 year ago
Hi,
I think you could use context.plSqlFile().toString()
to get the file path. It returns the path relative to the project base directory (for example: 'src/mydir/myfile.sql'
). So you could use new File(context.plSqlFile().toString())
to get a File object.
Hello Felipe,
Thanks for the tip. Unfortunately, it didn't work, not for TesetPlSqlFile anyway.
Here's the class for quick reference:
private class TestPlSqlFile private constructor(private val contents: String,
private val name: String) : PlSqlFile {
override fun contents(): String = contents
override fun fileName(): String = name
override fun type(): PlSqlFile.Type = PlSqlFile.Type.MAIN
companion object {
fun fromString(contents: String): PlSqlFile = TestPlSqlFile(contents.trim(), "unnamed")
fun fromFile(file: File): PlSqlFile =
try {
TestPlSqlFile(String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8), file.name)
} catch (e: IOException) {
throw IllegalStateException("Cannot read $file", e)
}
}
}
Thank you for the feedback.
You're correct, unfortunately, it doesn't work on the tests. It only works correctly on a "real" scan with SonarScanner.
As requested, I have added the path() method to the PlSqlFile interface. 😊
Awesome! we tried this out yesterday and to worked for us. Thank you!
I see that I can get the current file's name using
context.plSqlFile().fileName()
, but I require the full path.Why do I need the path? our migrates use a separate (non-sql) file that lists the sql files in the order they will be applied. That file is located relative to the sql files for each release. I want to write a rule to ensure the current sql file is included in the migrate file.
If there is a way to do this without changing the code, please let me know.
If a code change is needed I'm happy to submit a pull request. But will need some pointers on the approach. Adding
file()
toPlSqlFile
seems reasonable sinceInputFile
has aFile
andSonarQubePlSqlFile
has anInputFile
.TestPlSqlFile
also has aFile
if created withfromFile()
. Alternatively, we could addpath()
returning aString
(or aPath
), but this does not eliminate the question: what to do in theTestPlSqlFile.fromString()
case?Let me know if I can be of help.
Thanks for creating a great plugin!