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

Better error message, closes #12 #13

Merged
merged 4 commits into from
Feb 1, 2024
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
12 changes: 12 additions & 0 deletions .github/workflows/test.yml → .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,17 @@ jobs:
- name: Install dependencies
run: make init

- name: Lint
run: make lint-check

- name: check-is-dirty
run: |
if [[ -n $(git status --porcelain) ]]; then
echo "Detected uncommitted changes."
git status
git diff
exit 1
fi

- name: Run tests
run: make test
38 changes: 0 additions & 38 deletions .github/workflows/lint.yml

This file was deleted.

3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ linters:
enable:
- gofmt
- gofumpt
- goimports
- nolintlint
- lll
- staticcheck
- testifylint

issues:
max-issues-per-linter: 0
Expand Down
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ init:
lint:
@echo "Running lint"
@golines -w -m 92 cmd/* src/*
@gofumpt -w cmd/* src/*
@golangci-lint run --fix
@go mod tidy
@prettier --write .


Expand All @@ -39,5 +37,11 @@ bench:
.PHONY: clean
clean:
@echo "Cleaning up"
@go clean -x
@go clean
@go clean -testcache
@go clean -cache
@go clean -modcache
@go clean -i
@go clean -r
@rm -rf ./bin
18 changes: 10 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ go 1.21.6

require (
github.com/golangci/golangci-lint v1.55.2
github.com/segmentio/golines v0.12.2
github.com/stretchr/testify v1.8.4
github.com/urfave/cli/v2 v2.27.1
github.com/yuin/goldmark v1.6.0
mvdan.cc/gofumpt v0.5.0
github.com/urfave/cli/v2 v2.27.1 // app dep
github.com/yuin/goldmark v1.6.0 // app dep
honnef.co/go/tools v0.4.6 // indirect
mvdan.cc/gofumpt v0.5.0 // indirect
)

require golang.org/x/tools v0.17.0 // indirect

require github.com/segmentio/golines v0.12.2

require (
4d63.com/gocheckcompilerdirectives v1.2.1 // indirect
4d63.com/gochecknoglobals v0.2.1 // indirect
Expand Down Expand Up @@ -45,7 +49,7 @@ require (
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/charithe/durationcheck v0.0.10 // indirect
github.com/chavacava/garif v0.1.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
github.com/curioswitch/go-reassign v0.2.0 // indirect
github.com/daixiang0/gci v0.11.2 // indirect
github.com/dave/dst v0.27.3 // indirect
Expand Down Expand Up @@ -175,7 +179,7 @@ require (
github.com/uudashr/gocognit v1.1.2 // indirect
github.com/x-cray/logrus-prefixed-formatter v0.5.2 // indirect
github.com/xen0n/gosmopolitan v1.2.2 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e // indirect
github.com/yagipy/maintidx v1.0.0 // indirect
github.com/yeya24/promlinter v0.2.0 // indirect
github.com/ykadowak/zerologlint v0.1.3 // indirect
Expand All @@ -193,15 +197,13 @@ require (
golang.org/x/sys v0.16.0 // indirect
golang.org/x/term v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.17.0 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/alecthomas/kingpin.v2 v2.2.6 // indirect
gopkg.in/fsnotify.v1 v1.4.7 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
honnef.co/go/tools v0.4.6 // indirect
mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed // indirect
mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b // indirect
mvdan.cc/unparam v0.0.0-20221223090309-7455f1af531d // indirect
Expand Down
7 changes: 4 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.3 h1:qMCsGGgs+MAzDFyp9LpAe1Lqy/fY/qCovCm0qnXZOBM=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/curioswitch/go-reassign v0.2.0 h1:G9UZyOcpk/d7Gd6mqYgd8XYWFMw/znxwGDUstnC9DIo=
github.com/curioswitch/go-reassign v0.2.0/go.mod h1:x6OpXuWvgfQaMGks2BZybTngWjT84hqJfKoO8Tt/Roc=
github.com/daixiang0/gci v0.11.2 h1:Oji+oPsp3bQ6bNNgX30NBAVT18P4uBH4sRZnlOlTj7Y=
Expand Down Expand Up @@ -581,8 +582,8 @@ github.com/x-cray/logrus-prefixed-formatter v0.5.2 h1:00txxvfBM9muc0jiLIEAkAcIMJ
github.com/x-cray/logrus-prefixed-formatter v0.5.2/go.mod h1:2duySbKsL6M18s5GU7VPsoEPHyzalCE06qoARUCeBBE=
github.com/xen0n/gosmopolitan v1.2.2 h1:/p2KTnMzwRexIW8GlKawsTWOxn7UHA+jCMF/V8HHtvU=
github.com/xen0n/gosmopolitan v1.2.2/go.mod h1:7XX7Mj61uLYrj0qmeN0zi7XDon9JRAEhYQqAPLVNTeg=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e h1:+SOyEddqYF09QP7vr7CgJ1eti3pY9Fn3LHO1M1r/0sI=
github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
github.com/yagipy/maintidx v1.0.0 h1:h5NvIsCz+nRDapQ0exNv4aJ0yXSI0420omVANTv3GJM=
github.com/yagipy/maintidx v1.0.0/go.mod h1:0qNf/I/CCZXSMhsRsrEPDZ+DkekpKLXAJfsTACwgXLk=
github.com/yeya24/promlinter v0.2.0 h1:xFKDQ82orCU5jQujdaD8stOHiv8UN68BSdn2a8u8Y3o=
Expand Down
23 changes: 18 additions & 5 deletions src/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ func findLinks(markdown []byte) ([]string, error) {
type linkRecord struct {
Location string `json:"location"`
StatusCode int `json:"statusCode"`
ErrMsg string `json:"errMsg"`
OK bool `json:"ok"`
Message string `json:"message"`
}

// checkLink makes an HTTP request to url using the provided timeout.
Expand All @@ -94,23 +95,34 @@ func checkLink(url string, timeout time.Duration) linkRecord {
return linkRecord{
Location: url,
StatusCode: 0,
ErrMsg: fmt.Sprintf("Request timed out after %s", timeout),
OK: false,
Message: fmt.Sprintf("Request timed out after %s", timeout),
}
}

return linkRecord{
Location: url,
StatusCode: 0,
ErrMsg: err.Error(),
OK: false,
Message: err.Error(),
}
}

defer resp.Body.Close()

statusCode := resp.StatusCode
statusText := http.StatusText(statusCode)

OK := true
if statusCode >= 400 {
OK = false
}

return linkRecord{
Location: url,
StatusCode: resp.StatusCode,
ErrMsg: "",
OK: OK,
Message: statusText,
}
}

Expand All @@ -135,7 +147,8 @@ func printLinkRecordJSON(w io.Writer, lr linkRecord) error {
func printLinkRecordTab(w io.Writer, lr linkRecord) error {
tpl := `- Location : {{.Location}}
Status Code: {{if eq .StatusCode 0}}-{{else}}{{.StatusCode}}{{end}}
Error : {{if .ErrMsg}}{{.ErrMsg}}{{else}}-{{end}}
OK : {{.OK}}
Message : {{if .Message}}{{.Message}}{{else}}-{{end}}

`
t, err := template.New("record").Parse(tpl)
Expand Down
49 changes: 28 additions & 21 deletions src/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestReadMarkdown_NonMarkdownFile(t *testing.T) {
_, err = readMarkdown(filepath)

// Check if an error was returned
assert.Error(t, err, "Expected an error for non-markdown file")
require.Error(t, err, "Expected an error for non-markdown file")
}

func TestFindLinks(t *testing.T) {
Expand Down Expand Up @@ -195,10 +195,9 @@ func TestCheckLink_Success(t *testing.T) {
lr := checkLink(ts.URL, 1*time.Second)

assert.Equal(t, http.StatusOK, lr.StatusCode, "Status code should be 200")
assert.Empty(t, lr.ErrMsg, "Error message should be empty")
assert.Equal(t, "OK", lr.Message, "Error message should be 'OK'")
}


// TestCheckLink_ClientError tests the checkUrl function with a client error
func TestCheckLink_ClientError(t *testing.T) {
t.Parallel()
Expand All @@ -217,7 +216,7 @@ func TestCheckLink_ClientError(t *testing.T) {
lr.StatusCode,
"Status code should be 404",
)
assert.Empty(t, lr.ErrMsg, "Error message should be empty")
assert.Equal(t, "Not Found", lr.Message)
}

// TestCheckLink_ServerError tests the checkUrl function with a server error
Expand All @@ -238,7 +237,7 @@ func TestCheckLink_ServerError(t *testing.T) {
lr.StatusCode,
"Status code should be 500",
)
assert.Empty(t, lr.ErrMsg, "Error message should be empty")
assert.Equal(t, "Internal Server Error", lr.Message)
}

// TestCheckLink_ConnectionError tests the checkUrl function with a connection error
Expand All @@ -249,7 +248,7 @@ func TestCheckLink_ConnectionError(t *testing.T) {
assert.Equal(t, 0, lr.StatusCode, "Status code should be 0")
assert.Contains(
t,
lr.ErrMsg,
lr.Message,
"connection refused",
"Error message should contain 'connection refused'",
)
Expand All @@ -261,7 +260,7 @@ func TestCheckLink_InvalidLink(t *testing.T) {
lr := checkLink(":%", 1*time.Second)

assert.Equal(t, 0, lr.StatusCode, "Status code should be 0")
assert.Equal(t, lr.ErrMsg, "parse \":%\": missing protocol scheme")
assert.Equal(t, "parse \":%\": missing protocol scheme", lr.Message)
}

// Test for printFilepath function
Expand All @@ -285,10 +284,11 @@ func TestPrintFilepath(t *testing.T) {
// Test for printLinkRecordTab function
func TestPrintLinkRecordTab(t *testing.T) {
t.Parallel()
linkRecord := linkRecord{"http://example.com", 200, "OK"}
linkRecord := linkRecord{"http://example.com", 200, true, "OK"}
expectedOutput := "- Location : http://example.com\n" +
" Status Code: 200\n" +
" Error : OK\n\n"
" OK : true\n" +
" Message : OK\n\n"

var buf bytes.Buffer
w := tabwriter.NewWriter(&buf, 0, 0, 1, ' ', 0)
Expand All @@ -306,11 +306,12 @@ func TestPrintLinkRecordTab(t *testing.T) {
// Test for printLinkRecordJSON function
func TestPrintLinkRecordJSON(t *testing.T) {
t.Parallel()
linkRecord := linkRecord{"http://example.com", 200, "OK"}
linkRecord := linkRecord{"http://example.com", 200, true, "OK"}
expectedOutput := "{\n" +
" \"location\": \"http://example.com\",\n" +
" \"statusCode\": 200,\n" +
" \"errMsg\": \"OK\"\n" +
" \"ok\": true,\n" +
" \"message\": \"OK\"\n" +
"}\n"

var buf bytes.Buffer
Expand All @@ -329,10 +330,11 @@ func TestPrintLinkRecordJSON(t *testing.T) {
// Test for printLinkRecord function
func TestPrintLinkRecord(t *testing.T) {
t.Parallel()
linkRecord := linkRecord{"http://example.com", 200, "OK"}
linkRecord := linkRecord{"http://example.com", 200, true, "OK"}
expectedOutput := "- Location : http://example.com\n" +
" Status Code: 200\n" +
" Error : OK\n\n"
" OK : true\n" +
" Message : OK\n\n"

var buf bytes.Buffer
w := tabwriter.NewWriter(&buf, 0, 0, 1, ' ', 0)
Expand All @@ -350,7 +352,8 @@ func TestPrintLinkRecord(t *testing.T) {
expectedOutput = "{\n" +
" \"location\": \"http://example.com\",\n" +
" \"statusCode\": 200,\n" +
" \"errMsg\": \"OK\"\n" +
" \"ok\": true,\n" +
" \"message\": \"OK\"\n" +
"}\n"

var buf2 bytes.Buffer
Expand All @@ -371,6 +374,7 @@ func TestCheckLinks(t *testing.T) {
// Create a test tabwriter.Writer
buf := new(bytes.Buffer)
w := tabwriter.NewWriter(buf, 0, 0, 2, ' ', 0)
defer w.Flush()

// Create a test server that always returns a specific status code
ts := httptest.NewServer(
Expand All @@ -386,27 +390,30 @@ func TestCheckLinks(t *testing.T) {
// Set the timeout and error flag for testing
timeout := time.Second
errOK := false
asJSON := false

// Call the checkLinks function
_ = checkLinks(w, urls, timeout, errOK, false)
_ = checkLinks(w, urls, timeout, errOK, asJSON)

// Flush the tabwriter.Writer to get the output
w.Flush()
output := buf.String()

// Verify the output
expectedOutput1 := "- Location : " + ts.URL + "/ok\n" +
" Status Code: 200\n" +
" Error : -\n\n" +
" OK : true\n" +
" Message : OK\n\n" +
"- Location : " + ts.URL + "/invalid-url\n" +
" Status Code: 200\n" +
" Error : -\n\n"
" OK : true\n" +
" Message : OK\n\n"
expectedOutput2 := "- Location : " + ts.URL + "/invalid-url\n" +
" Status Code: 200\n" +
" Error : -\n\n" +
" OK : true\n" +
" Message : OK\n\n" +
"- Location : " + ts.URL + "/ok\n" +
" Status Code: 200\n" +
" Error : -\n\n"
" OK : true\n" +
" Message : OK\n\n"

assert.Contains(
t,
Expand Down
2 changes: 0 additions & 2 deletions tools.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
//go:build tools
// +build tools

package tools

import (
// Linters
_ "github.com/golangci/golangci-lint/cmd/golangci-lint"
_ "github.com/segmentio/golines"
_ "mvdan.cc/gofumpt"

// Testing
_ "github.com/stretchr/testify/mock"
Expand Down