Skip to content

Commit

Permalink
Merge pull request #24 from gopherguides/better-errors
Browse files Browse the repository at this point in the history
better errors and go 1.22
  • Loading branch information
markbates authored Feb 21, 2024
2 parents e097409 + 5d6c9da commit 8833481
Show file tree
Hide file tree
Showing 232 changed files with 4,568 additions and 20,711 deletions.
37 changes: 18 additions & 19 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,29 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
go-version: [1.21.x]
go-version: [1.22.x]
os: [macos-latest, ubuntu-latest]
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: "1.21.x"
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: "1.22.x"

- name: Go Environment
run: go env
- name: Go Environment
run: go env

- name: Verify Go Modules
run: go mod verify
- name: Verify Go Modules
run: go mod verify

- name: Build
run: go build -v ./...
- name: Build
run: go build -v ./...

- name: Run tests with Race Detector
run: go test -race -vet=off ./...
- name: Run tests with Race Detector
run: go test -race -vet=off ./...

- name: Install staticcheck
run: go install honnef.co/go/tools/cmd/staticcheck@latest

- name: Run staticcheck
run: staticcheck ./...
- name: Install staticcheck
run: go install honnef.co/go/tools/cmd/staticcheck@latest

- name: Run staticcheck
run: staticcheck ./...
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ tmp
*.pid
coverage
coverage.data
coverage.out
build/*
*.pbxuser
*.mode1v3
Expand Down
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ default: test install hype
test:
go test -timeout 10s -race -cover ./...

cov:
go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out

hypecov:
go test -coverprofile=coverage.out .
go tool cover -html=coverage.out

install:
go install -v ./cmd/hype

Expand Down
2 changes: 1 addition & 1 deletion binding/testdata/toc/01-one/simple/src/greet/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module demo

go 1.18
go 1.22
2 changes: 1 addition & 1 deletion binding/testdata/toc/01-one/src/greet/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module demo

go 1.18
go 1.22
2 changes: 1 addition & 1 deletion binding/testdata/toc/02-two/simple/src/greet/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module demo

go 1.18
go 1.22
2 changes: 1 addition & 1 deletion binding/testdata/toc/02-two/src/greet/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module demo

go 1.18
go 1.22
2 changes: 1 addition & 1 deletion binding/testdata/toc/03-three/simple/src/greet/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module demo

go 1.18
go 1.22
2 changes: 1 addition & 1 deletion binding/testdata/toc/03-three/src/greet/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module demo

go 1.18
go 1.22
5 changes: 2 additions & 3 deletions body.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package hype

import (
"encoding/json"
"fmt"
)

// Body is a container for all the elements in a document.
Expand All @@ -23,9 +22,9 @@ func (b *Body) MarshalJSON() ([]byte, error) {
return nil, err
}

m["type"] = fmt.Sprintf("%T", b)
m["type"] = toType(b)

return json.Marshal(m)
return json.MarshalIndent(m, "", " ")
}

// AsPage returns the body as a Page.
Expand Down
11 changes: 11 additions & 0 deletions body_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,16 @@ func Test_Body_AsPage(t *testing.T) {
p := body.AsPage()
r.NotNil(p)
r.Equal(body.Element, p.Element)
}

func Test_Body_MarshalJSON(t *testing.T) {
t.Parallel()

body := &Body{
Element: NewEl("body", nil),
}

body.Nodes = append(body.Nodes, Text("hello"))

testJSON(t, "body", body)
}
39 changes: 30 additions & 9 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (
type Cmd struct {
*Element

Args []string `json:"args,omitempty"`
Env []string `json:"env,omitempty"`
ExpectedExit int `json:"expected_exit,omitempty"`
Timeout time.Duration `json:"timeout,omitempty"`
Args []string
Env []string
ExpectedExit int
Timeout time.Duration

res *CmdResult
}
Expand All @@ -38,7 +38,7 @@ func (c *Cmd) MarshalJSON() ([]byte, error) {
return nil, err
}

m["type"] = fmt.Sprintf("%T", c)
m["type"] = toType(c)
m["expected_exit"] = c.ExpectedExit
m["timeout"] = c.Timeout.String()

Expand All @@ -54,7 +54,7 @@ func (c *Cmd) MarshalJSON() ([]byte, error) {
m["result"] = c.res
}

return json.Marshal(m)
return json.MarshalIndent(m, "", " ")
}

func (c *Cmd) MD() string {
Expand Down Expand Up @@ -109,18 +109,18 @@ func (c *Cmd) Execute(ctx context.Context, doc *Document) error {
switch c.ExpectedExit {
case -1:
if res.Exit == 0 {
return fmt.Errorf("unexpected exit code: %d: %w", res.Exit, err)
return c.newError(err)
}
default:
if res.Exit != c.ExpectedExit {
return fmt.Errorf("unexpected exit code: %d: %w", res.Exit, err)
return c.newError(err)
}
}
}

cres, err := NewCmdResult(doc.Parser, c, res)
if err != nil {
return err
return c.newError(err)
}

c.Lock()
Expand All @@ -131,6 +131,27 @@ func (c *Cmd) Execute(ctx context.Context, doc *Document) error {
return nil
}

func (c *Cmd) newError(err error) error {
if c == nil {
return err
}

re, ok := err.(clam.RunError)
if !ok {
return CmdError{
RunError: clam.RunError{
Err: err,
},
Filename: c.Filename,
}
}

return CmdError{
RunError: re,
Filename: c.Filename,
}
}

func NewCmd(el *Element) (*Cmd, error) {
if el == nil {
return nil, ErrIsNil("element")
Expand Down
6 changes: 5 additions & 1 deletion cmd/hype/cli/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ func Test_Encode_JSON(t *testing.T) {
act := bb.Out.String()
act = strings.TrimSpace(act)

// fmt.Println(act)
// fp := filepath.Join(root, tc.exp)
// f, err := os.Create(fp)
// r.NoError(err)
// f.Write([]byte(act))
// r.NoError(f.Close())

r.Equal(exp, act)

Expand Down
Loading

0 comments on commit 8833481

Please sign in to comment.