Skip to content

Commit

Permalink
add extra checks (campoy#16)
Browse files Browse the repository at this point in the history
* add extra checks

* fixed a bunch of errors

* vendored dependencies for a better life
  • Loading branch information
campoy authored Sep 1, 2017
1 parent 521dac5 commit e43704c
Show file tree
Hide file tree
Showing 23 changed files with 2,019 additions and 22 deletions.
8 changes: 8 additions & 0 deletions .gofmt.travis.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#! /bin/bash

OUTPUT="$(go list ./... | grep -v vendor | xargs go fmt)"
if [ -n "$OUTPUT" ]; then
echo "Go code is not formatted, run gofmt on:" >&2
echo "$OUTPUT" >&2
false
fi
15 changes: 14 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,17 @@ language: go
go:
- 1.7
- 1.8.3
- 1.9
- 1.9

install:
- go get -u github.com/campoy/embedmd
- go get -u github.com/golang/lint/golint
- go get -u github.com/kisielk/errcheck

script:
- embedmd -d **/*.md
- bash .gofmt.travis.sh
- go list ./... | grep -v vendor | xargs go test
- go list ./... | grep -v vendor | xargs golint
- go list ./... | grep -v "vendor\|errcheck" | xargs errcheck
- go list ./... | grep -v vendor | xargs go vet
15 changes: 15 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"


[[constraint]]
name = "github.com/pkg/errors"
version = "0.8.0"
12 changes: 6 additions & 6 deletions httplog/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// httplog provides an implementation of http.RoundTripper that logs every
// single request and response using a given logging function.
// Package httplog provides an implementation of http.RoundTripper that logs
// every single request and response using a given logging function.
package httplog

import (
Expand Down Expand Up @@ -48,14 +48,14 @@ func NewTransport(rt http.RoundTripper, logBody bool, logf func(string, ...inter
// Client returns a new http.Client using the given transport.
func (t Transport) Client() *http.Client { return &http.Client{Transport: t} }

// Transport satifies http.RoundTripper
// RoundTrip so Transport satifies http.RoundTripper
func (t Transport) RoundTrip(req *http.Request) (*http.Response, error) {
if b, err := httputil.DumpRequest(req, t.logBody); err != nil {
b, err := httputil.DumpRequest(req, t.logBody)
if err != nil {
t.logf("httplog: dump request: %v", err)
return nil, err
} else {
t.logf("httplog: %s", b)
}
t.logf("httplog: %s", b)

res, err := t.transport.RoundTrip(req)
if err != nil {
Expand Down
15 changes: 12 additions & 3 deletions httplog/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,19 @@ func TestMessagesAreLogged(t *testing.T) {
err := func() error {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if test.resBody != nil {
io.Copy(w, test.resBody)
_, err := io.Copy(w, test.resBody)
checkError(t, err)
}
}))
defer ts.Close()

c := httplog.NewTransport(nil, test.logBody, log).Client()
if test.reqBody == nil {
c.Get(ts.URL)
_, err := c.Get(ts.URL)
checkError(t, err)
} else {
c.Post(ts.URL, "text/plain", test.reqBody)
_, err := c.Post(ts.URL, "text/plain", test.reqBody)
checkError(t, err)
}

if len(logs) != 2 {
Expand All @@ -184,3 +187,9 @@ func TestMessagesAreLogged(t *testing.T) {
}
}
}

func checkError(t *testing.T, err error) {
if err != nil {
t.Errorf("unexpected error: %v", err)
}
}
7 changes: 5 additions & 2 deletions imgcat/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ func ExampleNewEncoder() {
if err != nil {
log.Fatal(err)
}
defer f.Close()
defer func() { _ = f.Close() }()

enc.Encode(f) // this will display the image in the terminal
// Display the image in the terminal.
if err := enc.Encode(f); err != nil {
log.Fatal(err)
}
}
18 changes: 14 additions & 4 deletions imgcat/imgcat.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"encoding/base64"
"fmt"
"io"
"log"
"os"
)

Expand All @@ -45,7 +46,9 @@ func Name(name string) Option {
buf := new(bytes.Buffer)
enc := base64.NewEncoder(base64.StdEncoding, buf)
fmt.Fprint(enc, name)
enc.Close()
if err := enc.Close(); err != nil {
log.Fatalf("could not encode to buffer: %v", err)
}
return Option(fmt.Sprintf("name=%s", buf))
}

Expand Down Expand Up @@ -127,13 +130,20 @@ func (enc *Encoder) Encode(r io.Reader) error {
pr, pw := io.Pipe()
go func() {
enc := base64.NewEncoder(base64.StdEncoding, pw)
defer enc.Close()
defer func() {
if err := enc.Close(); err != nil {
// always returns nil according to specs.
_ = pw.CloseWithError(err)
}
}()

_, err := io.Copy(enc, r)
if err != nil {
pw.CloseWithError(err)
// always returns nil according to specs.
_ = pw.CloseWithError(err)
} else {
pw.CloseWithError(enc.Close())
// always returns nil according to specs.
_ = pw.CloseWithError(enc.Close())
}
}()

Expand Down
7 changes: 4 additions & 3 deletions imgcat/imgcat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ func cat(enc *imgcat.Encoder, path string) error {
if err != nil {
return errors.Wrapf(err, "could not open %s", path)
}
defer f.Close()

return enc.Encode(f)
if err := enc.Encode(f); err != nil {
return err
}
return f.Close()
}
10 changes: 8 additions & 2 deletions imgcat/imgcat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

func TestIsSupported(t *testing.T) {
defer func(old string) { os.Setenv("TERM_PROGRAM", old) }(os.Getenv("TERM_PROGRAM"))
os.Setenv("TERM_PROGRAM", "foo")
defer func(old string) { check(t, os.Setenv("TERM_PROGRAM", old)) }(os.Getenv("TERM_PROGRAM"))
check(t, os.Setenv("TERM_PROGRAM", "foo"))
if _, err := NewEncoder(nil); err == nil {
t.Fatal("imgcat should not be supported now")
}
Expand Down Expand Up @@ -87,3 +87,9 @@ func TestWriter(t *testing.T) {
t.Fatalf("expected error bad writer; got %v", err)
}
}

func check(t *testing.T, err error) {
if err != nil {
t.Errorf("unexpected error: %v", err)
}
}
2 changes: 1 addition & 1 deletion tree/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func visit(path, indent string) (dirs, files int, err error) {
return 1, 0, fmt.Errorf("open %s: %v", path, err)
}
names, err := dir.Readdirnames(-1)
dir.Close()
_ = dir.Close() // safe to ignore this error.
if err != nil {
return 1, 0, fmt.Errorf("read dir names %s: %v", path, err)
}
Expand Down
24 changes: 24 additions & 0 deletions vendor/github.com/pkg/errors/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions vendor/github.com/pkg/errors/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions vendor/github.com/pkg/errors/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions vendor/github.com/pkg/errors/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions vendor/github.com/pkg/errors/appveyor.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e43704c

Please sign in to comment.