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

print error when glob doesn't match any files #370

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
5 changes: 3 additions & 2 deletions internal/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,14 @@ func normalDiff(loader load.Loader, base, revision string, config *diff.Config)
func composedDiff(loader load.Loader, base, revision string, config *diff.Config) (*diff.Diff, *diff.OperationsSourcesMap, *ReturnError) {
s1, err := load.FromGlob(loader, base)
if err != nil {
return nil, nil, getErrFailedToLoadSpec("base", base, err)
return nil, nil, getErrFailedToLoadSpecs("base", base, err)
}

s2, err := load.FromGlob(loader, revision)
if err != nil {
return nil, nil, getErrFailedToLoadSpec("revision", revision, err)
return nil, nil, getErrFailedToLoadSpecs("revision", revision, err)
}

diffReport, operationsSources, err := diff.GetPathsDiff(config, s1, s2)
if err != nil {
return nil, nil, getErrDiffFailed(err)
Expand Down
7 changes: 7 additions & 0 deletions internal/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ func getErrFailedToLoadSpec(what string, path string, err error) *ReturnError {
}
}

func getErrFailedToLoadSpecs(what string, path string, err error) *ReturnError {
return &ReturnError{
error: fmt.Errorf("failed to load %s specs from glob %q with %v", what, path, err),
Code: 103,
}
}

func getErrDiffFailed(err error) *ReturnError {
return &ReturnError{
error: fmt.Errorf("diff failed with %v", err),
Expand Down
26 changes: 24 additions & 2 deletions internal/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"
"io"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -63,7 +62,30 @@ func Test_Summary(t *testing.T) {
}

func Test_InvalidFile(t *testing.T) {
require.Equal(t, 102, internal.Run(cmdToArgs("oasdiff diff no-file ../data/openapi-test3.yaml"), os.Stdout, io.Discard))
var stderr bytes.Buffer
require.Equal(t, 102, internal.Run(cmdToArgs("oasdiff diff no-file ../data/openapi-test3.yaml"), io.Discard, &stderr))
require.Condition(t, func() (success bool) {
return stderr.String() == "Error: failed to load base spec from \"no-file\" with open no-file: no such file or directory\n" ||
stderr.String() == "Error: failed to load base spec from \"no-file\" with open no-file: The system cannot find the file specified.\n" // windows
})
}

func Test_InvalidGlob(t *testing.T) {
var stderr bytes.Buffer
require.Equal(t, 103, internal.Run(cmdToArgs(`oasdiff diff -c "a[" ../data/openapi-test3.yaml`), io.Discard, &stderr))
require.Equal(t, "Error: failed to load base specs from glob \"\\\"a[\\\"\" with syntax error in pattern\n", stderr.String())
}

func Test_GlobNoFiles(t *testing.T) {
var stderr bytes.Buffer
require.Equal(t, 103, internal.Run(cmdToArgs("oasdiff diff -c no-file ../data/openapi-test3.yaml"), io.Discard, &stderr))
require.Equal(t, "Error: failed to load base specs from glob \"no-file\" with no matching files\n", stderr.String())
}

func Test_GlobWithUrl(t *testing.T) {
var stderr bytes.Buffer
require.Equal(t, 103, internal.Run(cmdToArgs("oasdiff diff -c ../data/openapi-test1.yaml https://"), io.Discard, &stderr))
require.Equal(t, "Error: failed to load revision specs from glob \"https://\" with no matching files (should be a glob, not a URL)\n", stderr.String())
}

func Test_DiffInvalidFormat(t *testing.T) {
Expand Down
19 changes: 18 additions & 1 deletion load/spec_info.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package load

import (
"errors"
"net/url"

"github.com/getkin/kin-openapi/openapi3"
"github.com/yargevad/filepathx"
)
Expand Down Expand Up @@ -37,5 +40,19 @@ func FromGlob(loader Loader, glob string) ([]SpecInfo, error) {
result = append(result, SpecInfo{Url: file, Spec: spec})
}

return result, nil
if len(result) > 0 {
return result, nil
}

if isUrl(glob) {
return nil, errors.New("no matching files (should be a glob, not a URL)")
}

return nil, errors.New("no matching files")

}

func isUrl(spec string) bool {
_, err := url.ParseRequestURI(spec)
return err == nil
}