Skip to content

Commit

Permalink
Fix double escaping of brackets
Browse files Browse the repository at this point in the history
  • Loading branch information
radarsh committed Feb 7, 2019
1 parent c6f6b62 commit ceac524
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import groovy.transform.CompileStatic
import groovy.transform.InheritConstructors
import org.gradle.api.tasks.testing.TestResult.ResultType

import static com.adarshr.gradle.testlogger.util.RendererUtils.preserveAnsi
import static java.lang.System.lineSeparator
import static org.gradle.api.tasks.testing.TestResult.ResultType.*

Expand Down Expand Up @@ -116,7 +117,7 @@ class MochaTheme extends AbstractTheme {
return ''
}

lines = lines.replace('[', '\\[')
lines = preserveAnsi(lines)

def indentation = ' ' * indent
def line = new StringBuilder("[grey]${lineSeparator()}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.adarshr.gradle.testlogger.TestResultWrapper
import groovy.transform.CompileStatic
import groovy.transform.InheritConstructors

import static com.adarshr.gradle.testlogger.util.RendererUtils.preserveAnsi
import static java.lang.System.lineSeparator
import static org.gradle.api.tasks.testing.TestResult.ResultType.*

Expand Down Expand Up @@ -91,7 +92,7 @@ class PlainTheme extends AbstractTheme {
return ''
}

lines = lines.replace('[', '\\[')
lines = preserveAnsi(lines)

def indentation = ' ' * indent
def line = new StringBuilder(lineSeparator())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.adarshr.gradle.testlogger.TestResultWrapper
import groovy.transform.CompileStatic
import groovy.transform.InheritConstructors

import static com.adarshr.gradle.testlogger.util.RendererUtils.preserveAnsi
import static java.lang.System.lineSeparator
import static org.gradle.api.tasks.testing.TestResult.ResultType.*

Expand Down Expand Up @@ -108,7 +109,7 @@ class StandardTheme extends AbstractTheme {
return ''
}

lines = lines.replace('[', '\\[')
lines = preserveAnsi(lines)

def indentation = ' ' * indent
def line = new StringBuilder("[default]${lineSeparator()}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ class RendererUtils {
static String escape(String text) {
text?.replace('\u001B', '')?.replace('[', '\\[')?.replace(']', '\\]')
}

static String preserveAnsi(String text) {
text?.replace('\u001B[', '\u001B\\[')
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ abstract class BaseThemeSpec extends Specification {
protected def testLoggerExtensionMock = Mock(TestLoggerExtension)
protected def testDescriptorMock = Mock(TestDescriptorWrapper)
protected def testResultMock = Mock(TestResultWrapper)
protected def streamLines = "Hello${lineSeparator()}World"
protected def streamLines = "Hello${lineSeparator()}World [brackets] \u001B[0mANSI"

def setup() {
testResultMock.loggable >> true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class MochaParallelThemeSpec extends BaseThemeSpec {
theme.testStandardStreamText(streamLines, testResultMock) ==
'''|[grey]
| Hello
| World[/]
| World [brackets] \u001B\\[0mANSI[/]
|'''.stripMargin().replace('\n', lineSeparator())
}

Expand All @@ -207,7 +207,7 @@ class MochaParallelThemeSpec extends BaseThemeSpec {
theme.suiteStandardStreamText(streamLines, testResultMock) ==
'''|[grey]
| Hello
| World[/]
| World [brackets] \u001B\\[0mANSI[/]
|'''.stripMargin().replace('\n', lineSeparator())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class MochaThemeSpec extends BaseThemeSpec {
theme.testStandardStreamText(streamLines, testResultMock) ==
'''|[grey]
| Hello
| World[/]
| World [brackets] \u001B\\[0mANSI[/]
|'''.stripMargin().replace('\n', lineSeparator())
}

Expand All @@ -208,7 +208,7 @@ class MochaThemeSpec extends BaseThemeSpec {
theme.suiteStandardStreamText(streamLines, testResultMock) ==
'''|[grey]
| Hello
| World[/]
| World [brackets] \u001B\\[0mANSI[/]
|'''.stripMargin().replace('\n', lineSeparator())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class PlainParallelThemeSpec extends BaseThemeSpec {
theme.testStandardStreamText(streamLines, testResultMock) ==
'''|
| Hello
| World
| World [brackets] \u001B\\[0mANSI
|'''.stripMargin().replace('\n', lineSeparator())
}

Expand All @@ -163,7 +163,7 @@ class PlainParallelThemeSpec extends BaseThemeSpec {
theme.suiteStandardStreamText(streamLines, testResultMock) ==
'''|
| Hello
| World
| World [brackets] \u001B\\[0mANSI
|'''.stripMargin().replace('\n', lineSeparator())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class PlainThemeSpec extends BaseThemeSpec {
theme.testStandardStreamText(streamLines, testResultMock) ==
'''|
| Hello
| World
| World [brackets] \u001B\\[0mANSI
|'''.stripMargin().replace('\n', lineSeparator())
}

Expand All @@ -162,7 +162,7 @@ class PlainThemeSpec extends BaseThemeSpec {
theme.suiteStandardStreamText(streamLines, testResultMock) ==
'''|
| Hello
| World
| World [brackets] \u001B\\[0mANSI
|'''.stripMargin().replace('\n', lineSeparator())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class StandardParallelThemeSpec extends BaseThemeSpec {
theme.testStandardStreamText(streamLines, testResultMock) ==
'''|[default]
| Hello
| World[/]
| World [brackets] \u001B\\[0mANSI[/]
|'''.stripMargin().replace('\n', lineSeparator())
}

Expand All @@ -187,7 +187,7 @@ class StandardParallelThemeSpec extends BaseThemeSpec {
theme.suiteStandardStreamText(streamLines, testResultMock) ==
'''|[default]
| Hello
| World[/]
| World [brackets] \u001B\\[0mANSI[/]
|'''.stripMargin().replace('\n', lineSeparator())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class StandardThemeSpec extends BaseThemeSpec {
theme.testStandardStreamText(streamLines, testResultMock) ==
'''|[default]
| Hello
| World[/]
| World [brackets] \u001B\\[0mANSI[/]
|'''.stripMargin().replace('\n', lineSeparator())
}

Expand All @@ -184,7 +184,7 @@ class StandardThemeSpec extends BaseThemeSpec {
theme.suiteStandardStreamText(streamLines, testResultMock) ==
'''|[default]
| Hello
| World[/]
| World [brackets] \u001B\\[0mANSI[/]
|'''.stripMargin().replace('\n', lineSeparator())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,16 @@ class RendererUtilsSpec extends Specification {
'[escape]' | '\\[escape\\]'
'\u001Btext' | 'text'
}

@Unroll
def "preserve ansi #text"() {
expect:
RendererUtils.preserveAnsi(text) == expected
where:
text | expected
null | null
'' | ''
'[do not escape]' | '[do not escape]'
'\u001B[0mANSI' | '\u001B\\[0mANSI'
}
}

0 comments on commit ceac524

Please sign in to comment.