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

Fix broken colorized verbose output #668

Merged
merged 1 commit into from
Oct 28, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 7 additions & 3 deletions github/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type verboseTransport struct {
Transport *http.Transport
Verbose bool
OverrideURL *url.URL
Out io.Writer
Colorized bool
}

func (t *verboseTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
Expand Down Expand Up @@ -107,11 +109,11 @@ func (t *verboseTransport) dumpBody(body io.ReadCloser) io.ReadCloser {
}

func (t *verboseTransport) verbosePrintln(msg string) {
if isTerminal(os.Stderr.Fd()) {
msg = fmt.Sprintf("\\e[36m%s\\e[m", msg)
if t.Colorized {
msg = fmt.Sprintf("\033[36m%s\033[0m", msg)
}

fmt.Fprintln(os.Stderr, msg)
fmt.Fprintln(t.Out, msg)
}

func newHttpClient(testHost string, verbose bool) *http.Client {
Expand All @@ -123,6 +125,8 @@ func newHttpClient(testHost string, verbose bool) *http.Client {
Transport: &http.Transport{Proxy: proxyFromEnvironment},
Verbose: verbose,
OverrideURL: testURL,
Out: os.Stderr,
Colorized: isTerminal(os.Stderr.Fd()),
}
return &http.Client{Transport: tr}
}
Expand Down
12 changes: 12 additions & 0 deletions github/http_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package github

import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -52,3 +53,14 @@ func TestNewHttpClient_OverrideURL(t *testing.T) {
c = newHttpClient("", false)
c.Get(fmt.Sprintf("%s/not-override", s.URL.String()))
}

func TestVerboseTransport_VerbosePrintln(t *testing.T) {
var b bytes.Buffer
tr := &verboseTransport{
Out: &b,
Colorized: true,
}

tr.verbosePrintln("foo")
assert.Equal(t, "\033[36mfoo\033[0m\n", b.String())
}