Skip to content

Commit

Permalink
Add streaming renderer tests
Browse files Browse the repository at this point in the history
This checks that the streaming approach produces the same output as the classical buffer-everything approach, for all existing cases.
  • Loading branch information
DrJosh9000 committed Jan 17, 2024
1 parent 0ced955 commit eb5f762
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
7 changes: 7 additions & 0 deletions 4streaming.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
goos: darwin
goarch: arm64
pkg: github.com/buildkite/terminal-to-html/v3
BenchmarkRendererNpm-10 99 11804480 ns/op 12303395 B/op 163908 allocs/op
BenchmarkStreamingNpm-10 93 12547077 ns/op 10829185 B/op 163919 allocs/op
PASS
ok github.com/buildkite/terminal-to-html/v3 3.674s
5 changes: 4 additions & 1 deletion screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ func (s *Screen) getCurrentLineForWriting() *screenLine {
// If MaxLines is not in use, or adding a new line would not make it
// larger than MaxLines, then just allocate a new line.
if s.MaxLines <= 0 || len(s.screen)+1 <= s.MaxLines {
s.screen = append(s.screen, screenLine{nodes: make([]node, 0, 80)})
// nodes is preallocated with space for 80 columns, which is
// arbitrary, but also the traditional terminal width.
newLine := screenLine{nodes: make([]node, 0, 80)}
s.screen = append(s.screen, newLine)
continue
}

Expand Down
45 changes: 43 additions & 2 deletions terminal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ func TestRendererAgainstCases(t *testing.T) {
want := c.expected

if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("%s Render(%q) diff (-got +want):\n%s", c.name, c.input, diff)
t.Errorf("Render(%q) diff (-got +want):\n%s", c.input, diff)
}
})
}
Expand All @@ -356,7 +356,48 @@ func TestRendererAgainstFixtures(t *testing.T) {
got := Render(raw)

if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("%s diff (-got +want):\n%s", base, diff)
t.Errorf("Render diff (-got +want):\n%s", diff)
}
})
}
}

func streamingRender(raw []byte) string {
var buf strings.Builder
s := &Screen{
MaxLines: 300,
ScrollOutFunc: func(line string) {
fmt.Fprintln(&buf, line)
},
}
s.Write(raw)
buf.WriteString(s.AsHTML())
return buf.String()
}

func TestStreamingRendererAgainstCases(t *testing.T) {
for _, c := range rendererTestCases {
t.Run(c.name, func(t *testing.T) {
got := streamingRender([]byte(c.input))
want := c.expected

if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("streamingRender(%q) diff (-got +want):\n%s", c.input, diff)
}
})
}
}

func TestStreamingRendererAgainstFixtures(t *testing.T) {
for _, base := range TestFiles {
t.Run(fmt.Sprintf("for fixture %q", base), func(t *testing.T) {
raw := loadFixture(t, base, "raw")
want := string(loadFixture(t, base, "rendered"))

got := streamingRender(raw)

if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("streamingRender diff (-got +want):\n%s", diff)
}
})
}
Expand Down

0 comments on commit eb5f762

Please sign in to comment.