Skip to content
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
3 changes: 2 additions & 1 deletion view/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/base64"
"fmt"
"io"
"log"
"mime/quotedprintable"
"os"
"regexp"
Expand Down Expand Up @@ -273,7 +274,7 @@ func debugImageProtocol(format string, args ...interface{}) {
return
}
msg := fmt.Sprintf("[img-protocol] "+format+"\n", args...)
fmt.Print(msg)
log.Print(msg)
if path := os.Getenv("DEBUG_IMAGE_PROTOCOL_LOG"); path != "" {
if f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err == nil {
_, _ = f.WriteString(msg)
Expand Down
53 changes: 53 additions & 0 deletions view/html_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package view

import (
"bytes"
"fmt"
"io"
"log"
"os"
"regexp"
"strings"
Expand Down Expand Up @@ -68,6 +71,56 @@ func TestDecodeQuotedPrintable(t *testing.T) {
}
}

func TestDebugImageProtocolUsesLogger(t *testing.T) {
Comment thread
FromSi marked this conversation as resolved.
t.Setenv("DEBUG_IMAGE_PROTOCOL", "1")
t.Setenv("DEBUG_IMAGE_PROTOCOL_LOG", "")
t.Setenv("DEBUG_KITTY_IMAGES", "")
t.Setenv("DEBUG_KITTY_LOG", "")

var logBuf bytes.Buffer
originalLogOutput := log.Writer()
originalLogFlags := log.Flags()
log.SetOutput(&logBuf)
log.SetFlags(0)
t.Cleanup(func() {
log.SetOutput(originalLogOutput)
log.SetFlags(originalLogFlags)
})

originalStdout := os.Stdout
readPipe, writePipe, err := os.Pipe()
if err != nil {
t.Fatalf("os.Pipe() failed: %v", err)
}
t.Cleanup(func() {
_ = readPipe.Close()
_ = writePipe.Close()
})
os.Stdout = writePipe
t.Cleanup(func() {
os.Stdout = originalStdout
})

debugImageProtocol("hello %s", "world")

if err := writePipe.Close(); err != nil {
t.Fatalf("closing stdout pipe failed: %v", err)
}
os.Stdout = originalStdout
stdout, err := io.ReadAll(readPipe)
if err != nil {
t.Fatalf("reading stdout pipe failed: %v", err)
}
if got := string(stdout); got != "" {
t.Fatalf("debugImageProtocol wrote to stdout: %q", got)
}

want := "[img-protocol] hello world\n"
if got := logBuf.String(); got != want {
t.Fatalf("debugImageProtocol log output = %q, want %q", got, want)
}
}

func TestMarkdownToHTML(t *testing.T) {
testCases := []struct {
name string
Expand Down
Loading