Skip to content

Commit

Permalink
Stop passing basePath where it's no longer needed
Browse files Browse the repository at this point in the history
  • Loading branch information
3flex committed May 5, 2024
1 parent 307051e commit adc6d0c
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ typealias ParsingStrategy = (settings: ProcessingSettings) -> List<KtFile>
fun contentToKtFile(content: String, path: Path): ParsingStrategy = { settings ->
require(path.isRegularFile()) { "Given sub path ($path) should be a regular file!" }
listOf(
KtCompiler(settings.environment).createKtFile(content, settings.spec.projectSpec.basePath, path)
KtCompiler(settings.environment).createKtFile(content, path)

Check warning on line 14 in detekt-core/src/main/kotlin/io/gitlab/arturbosch/detekt/core/tooling/ParsingStrategy.kt

View check run for this annotation

Codecov / codecov/patch

detekt-core/src/main/kotlin/io/gitlab/arturbosch/detekt/core/tooling/ParsingStrategy.kt#L14

Added line #L14 was not covered by tests
)
}

val inputPathsToKtFiles: ParsingStrategy = { settings ->
val compiler = KtCompiler(settings.environment)
val basePath = settings.spec.projectSpec.basePath
settings.spec.projectSpec.inputPaths.map { path ->
compiler.compile(basePath, path)
compiler.compile(path)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ private fun parseAll(parser: KtCompiler, root: Path): Collection<KtFile> =
Files.walk(root)
.asSequence()
.filter { it.extension == "kt" }
.map { parser.compile(root, it) }
.map { parser.compile(it) }
.toList()
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ open class KtCompiler(

protected val psiFileFactory = KtPsiFactory(environment.project, markGenerated = false)

fun compile(basePath: Path, path: Path): KtFile {
fun compile(path: Path): KtFile {
require(path.isRegularFile()) { "Given path '$path' should be a regular file!" }
return createKtFile(path.readText(), basePath, path)
return createKtFile(path.readText(), path)
}

fun createKtFile(@Language("kotlin") content: String, basePath: Path, path: Path): KtFile {
fun createKtFile(@Language("kotlin") content: String, path: Path): KtFile {
val psiFile = psiFileFactory.createPhysicalFile(path.name, StringUtilRt.convertLineSeparators(content))

return psiFile.apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,36 @@ class KtCompilerSpec {

@Test
fun `Kotlin file with LF line separators has extra user data`() {
val ktFile = ktCompiler.compile(path, path.resolve("DefaultLf.kt"))
val ktFile = ktCompiler.compile(path.resolve("DefaultLf.kt"))

assertThat(ktFile.lineSeparator).isEqualTo("\n")
}

@Test
fun `Kotlin file with CRLF line separators has extra user data`() {
val ktFile = ktCompiler.compile(path, path.resolve("DefaultCrLf.kt"))
val ktFile = ktCompiler.compile(path.resolve("DefaultCrLf.kt"))

assertThat(ktFile.lineSeparator).isEqualTo("\r\n")
}

@Test
fun `throws an exception for an invalid path`() {
assertThatIllegalArgumentException()
.isThrownBy { ktCompiler.compile(path, path) }
.isThrownBy { ktCompiler.compile(path) }
.withMessage("Given path '$path' should be a regular file!")
}

@Test
fun `throws an exception for an non existent path`() {
assertThatIllegalArgumentException()
.isThrownBy { ktCompiler.compile(Path(""), Path("nonExistent")) }
.isThrownBy { ktCompiler.compile(Path("nonExistent")) }
.withMessage("Given path 'nonExistent' should be a regular file!")
}

@Test
fun `parses with errors for non kotlin files`() {
val cssPath = resourceAsPath("css")
val ktFile = ktCompiler.compile(cssPath, cssPath.resolve("test.css"))
val ktFile = ktCompiler.compile(cssPath.resolve("test.css"))

val errors = mutableListOf<PsiErrorElement>()
ktFile.accept(object : KtTreeVisitorVoid() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ fun compileContentForTest(
basePath: Path = Path("/").absolute(),
path: Path = basePath.resolve("Test.kt"),
): KtFile {
return KtTestCompiler.createKtFile(content, basePath, path)
return KtTestCompiler.createKtFile(content, path)
}

/**
* Use this method if you test a kt file/class in the test resources.
*/
fun compileForTest(path: Path) = KtTestCompiler.compile(resourceAsPath("/"), path)
fun compileForTest(path: Path) = KtTestCompiler.compile(path)

0 comments on commit adc6d0c

Please sign in to comment.