Skip to content

Commit

Permalink
fix(test): return network errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bashbunni committed Dec 4, 2024
1 parent d4a5910 commit d697cda
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
26 changes: 13 additions & 13 deletions glow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ func TestGlowSources(t *testing.T) {

for _, v := range tt {
t.Run(v, func(t *testing.T) {
// Start by checking for network issues.
_, nerr := readmeURL(v)
var dnsErr *net.DNSError
if nerr != nil && errors.As(nerr, &dnsErr) {
t.Logf("Error during execution (args: %s):\n%v", v, nerr)
t.Skip("Test uses network. Are you connected to the Internet?")
}

var urlErr *net.DNSError
buf := &bytes.Buffer{}
err := executeArg(rootCmd, v, buf)
_, err := sourceFromArg(v)
if err != nil {
t.Errorf("Error during execution (args: %s): %v", v, err)
}
if buf.Len() == 0 {
t.Errorf("Output buffer should not be empty (args: %s)", v)
// Check for network issues.
if errors.As(err, &urlErr) {
t.Logf("Error during execution (args: %s):\n%v", v, err)
t.Skip("Test uses network. Are you connected to the Internet?")
} else {
t.Errorf("Error during execution (args: %s): %v", v, err)
}

if buf.Len() == 0 {
t.Errorf("Output buffer should not be empty (args: %s)", v)
}
}
})
}
Expand Down
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"io/fs"
"net"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -75,10 +76,14 @@ func sourceFromArg(arg string) (*source, error) {
}

// a GitHub or GitLab URL (even without the protocol):
var dnsErr *net.DNSError
src, err := readmeURL(arg)
if src != nil && err == nil {
// if there's an error, try next methods...
return src, nil
} else if err != nil && errors.As(err, &dnsErr) {
// fail if there's a network error.
return src, err
}

// HTTP(S) URLs:
Expand Down

0 comments on commit d697cda

Please sign in to comment.