Fix another colorizer bug. (#3417)

If a maximum length was specified and the string did not end in a
colorization directive, the maximum length was not respected.

Fixes #3374.
This commit is contained in:
Pat Gavlin 2019-10-29 10:55:30 -07:00 committed by GitHub
parent 9f5f12c766
commit 3d35eebb58
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 8 deletions

View file

@ -6,6 +6,9 @@ CHANGELOG
- Adds a **preview** of .NET support for Pulumi. This code is an preview state and is subject
to change at any point.
- Fix another colorizer issue that could cause garbled output for messages that did not end in colorization tags.
[#3417](https://github.com/pulumi/pulumi/pull/3417)
## 1.4.0 (2019-10-24)
- `FileAsset` in the Python SDK now accepts anything implementing `os.PathLike` in addition to `str`.

View file

@ -132,7 +132,7 @@ func writeDirective(w io.StringWriter, c Colorization, directive string) {
func colorizeText(s string, c Colorization, maxLen int) string {
var buf bytes.Buffer
textLen := 0
textLen, reset := 0, false
for input := s; len(input) > 0; {
// Do we have another directive to process?
nextDirectiveStart := strings.Index(input, colorLeft)
@ -146,10 +146,8 @@ func colorizeText(s string, c Colorization, maxLen int) string {
return input
}
// Otherwise, write the remaining input into the buffer and terminate.
_, err := buf.WriteString(input)
contract.IgnoreError(err)
break
// Otherwise, set the start of the next directive to the end of the string and continue.
nextDirectiveStart = len(input)
}
if buf.Cap() < len(input) {
buf.Grow(len(input))
@ -160,12 +158,13 @@ func colorizeText(s string, c Colorization, maxLen int) string {
if maxLen >= 0 && textLen+len(text) > maxLen {
_, err := buf.WriteString(text[:maxLen-textLen])
contract.IgnoreError(err)
writeDirective(&buf, c, Reset)
if reset {
writeDirective(&buf, c, Reset)
}
break
}
_, err := buf.WriteString(text)
contract.IgnoreError(err)
input = input[nextDirectiveStart+len(colorLeft):]
textLen += len(text)
// If we have a start delimiter but no end delimiter, terminate. The partial command will not be present in the
@ -175,9 +174,11 @@ func colorizeText(s string, c Colorization, maxLen int) string {
break
}
directive := command(input[:nextDirectiveEnd])
directive := command(input[nextDirectiveStart+len(colorLeft) : nextDirectiveEnd])
writeDirective(&buf, c, directive)
input = input[nextDirectiveEnd+len(colorRight):]
reset = directive != Reset
}
return buf.String()

View file

@ -80,3 +80,27 @@ func TestColorizer(t *testing.T) {
})
}
}
// TestTrimColorizedString provides extra coverage for TrimColorizedString.
func TestTrimColorizedString(t *testing.T) {
str := "hello, " + Green + "world" + Reset + "!!"
actual := TrimColorizedString(str, len("hello"))
assert.Equal(t, "hello", actual)
actual = TrimColorizedString(str, len("hello, wo"))
assert.Equal(t, "hello, "+Green+"wo"+Reset, actual)
actual = TrimColorizedString(str, len("hello, world"))
assert.Equal(t, "hello, "+Green+"world"+Reset, actual)
actual = TrimColorizedString(str, len("hello, world!"))
assert.Equal(t, "hello, "+Green+"world"+Reset+"!", actual)
actual = TrimColorizedString(str, len("hello, world!!"))
assert.Equal(t, str, actual)
plain := "hello, world!!"
actual = TrimColorizedString(plain, len("hello"))
assert.Equal(t, "hello", actual)
}