Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support escaping commas in extra flags #443

Merged
merged 2 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class ExtraFlagsExtractor(private val generalConfig: GeneralConfig,
* @return [ExtraFlags] or null if no match occurred
*/
internal fun extractExtraFlagsFrom(line: String) = line
.split(",", ", ")
.splitByNonEscaped(',')
.map { it.replace("\\,", ",") }
Copy link
Member

@orchestr7 orchestr7 Aug 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

case with escaping of a single slash won't work here:
aaa\\,bbb\\, -> usually it is converted to aaa\,bbb\,

.associate { part ->
val pair = part.split("=", limit = 2).map {
it.replace("\\=", "=")
Expand Down Expand Up @@ -95,3 +96,22 @@ fun resolvePlaceholdersFrom(
}
}
}

/**
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
* Split [this] string by [delimiter] unless it's prepended by `\`.
*/
internal fun String.splitByNonEscaped(delimiter: Char): List<String> {
val indicesToSplit = mapIndexed { index, c -> index to c }
Fixed Show fixed Hide fixed
.filter { (index, c) ->
Fixed Show fixed Hide fixed
c == delimiter && (index == 0 || get(index - 1) != '\\')
}
.map { (index, _) -> index }
val result = mutableListOf<String>()
Fixed Show fixed Hide fixed
var currentOffset = 0
indicesToSplit.forEach {
result.add(substring(currentOffset, it))
currentOffset = it + 1
}
result.add(substring(currentOffset, length))
return result
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package com.saveourtool.save.core

import com.saveourtool.save.core.plugin.ExtraFlags
import com.saveourtool.save.core.plugin.ExtraFlagsExtractor
import com.saveourtool.save.core.plugin.GeneralConfig
import com.saveourtool.save.core.plugin.filterAndJoinBy
import com.saveourtool.save.core.plugin.resolvePlaceholdersFrom
import com.saveourtool.save.core.plugin.*
Fixed Show fixed Hide fixed

import okio.fakefilesystem.FakeFileSystem

Expand All @@ -26,6 +22,9 @@ class ExtraFlagsExtractorTest {
"Unparseable nonsense" to ExtraFlags.empty,
"args1=--flag --opt,args2=-debug --flag2" to ExtraFlags("--flag --opt", "-debug --flag2"),
"args1=--flag\\=value,args2=--foo=bar" to ExtraFlags("--flag=value", "--foo=bar"),
"args1=option1\\,option2,args2=option3\\,option4" to ExtraFlags("option1,option2", "option3,option4"),
"args1=option1\\,option2" to ExtraFlags("option1,option2", ""),
"args2=option3\\,option4" to ExtraFlags("", "option3,option4"),
)
.forEach { (line, extraFlags) ->
assertEquals(extraFlags, extraFlagsExtractor.extractExtraFlagsFrom(line))
Expand Down Expand Up @@ -105,6 +104,52 @@ class ExtraFlagsExtractorTest {
"another-cmd --flag"
)
)

checkMultilineDirectives(
Regex("""// RUN: (.*([^\\]=)?.*)\\?"""),
listOf(
"// RUN: command --flag=option\\,\\",
"// RUN: another-option --another-flag",
"// RUN: another-cmd\\=\\",
"// RUN: --flag=option\\,another-option",
),
listOf(
"command --flag=option\\,another-option --another-flag",
"another-cmd\\=--flag=option\\,another-option"
)
)
}

@Test
fun `test for splitByNonEscaped`() {
assertEquals(
listOf("this string\\, not split"),
"this string\\, not split".splitByNonEscaped(','),
Fixed Show fixed Hide fixed
)
assertEquals(
listOf("this string", " but split"),
"this string, but split".splitByNonEscaped(','),
)
assertEquals(
listOf("this string\\, not split", " but here - it's split"),
"this string\\, not split, but here - it's split".splitByNonEscaped(','),
Fixed Show fixed Hide fixed
)
assertEquals(
listOf("", ""),
",".splitByNonEscaped(','),
)
assertEquals(
listOf("", "text"),
",text".splitByNonEscaped(','),
)
assertEquals(
listOf("\\,"),
"\\,".splitByNonEscaped(','),
)
assertEquals(
listOf("\\,text"),
"\\,text".splitByNonEscaped(','),
)
}

private fun checkMultilineDirectives(
Expand Down