Skip to content

Commit

Permalink
enhance unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Billy Zha <[email protected]>
  • Loading branch information
qweeah committed Dec 23, 2022
1 parent 219e3cc commit 380b8f3
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
14 changes: 4 additions & 10 deletions cmd/oras/internal/fileref/unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,10 @@ import (
)

// Parse parses file reference on unix.
func Parse(reference string, defaultMediaType string) (filePath, mediaType string, err error) {
filePath = reference
mediaType = defaultMediaType

func Parse(reference string, mediaType string) (filePath, mediatype string) {
i := strings.LastIndex(reference, ":")
if i >= 0 {
filePath, mediaType = reference[:i], reference[i+1:]
}
if filePath == "" {
return "", "", fmt.Errorf("found empty file path in %q", reference)
if i < 0 {
return reference, mediaType
}
return filePath, mediaType, nil
return reference[:i], reference[i+1:]
}
2 changes: 1 addition & 1 deletion cmd/oras/internal/fileref/unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestParse(t *testing.T) {
wantErr bool
}{

{"empty file name", args{"", ""}, "", "", true},
{"no input", args{"", ""}, "", "", false},
{"empty file name and media type", args{":", ""}, "", "", true},
{"empty file name with media type", args{":a", "b"}, "", "", true},
}
Expand Down
5 changes: 5 additions & 0 deletions cmd/oras/internal/fileref/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ import (

// Parse parses file reference on windows.
func Parse(reference string, defaultMediaType string) (filePath, mediaType string, err error) {
if reference == "" {
// no input
return "", "", nil
}

filePath, mediaType = doParse(reference, defaultMediaType)
if filePath == "" {
return "", "", fmt.Errorf("found empty file path in %q", reference)
Expand Down
1 change: 1 addition & 0 deletions cmd/oras/internal/fileref/windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func TestParse(t *testing.T) {
wantMediatype string
wantErr bool
}{
{"no input", args{"", ""}, "", "", false},
{"empty file name", args{":", ""}, "", "", true},
{"reserved character1 in file name", args{"<", "a"}, "", "", true},
{"reserved character2 in file name", args{">", "a"}, "", "", true},
Expand Down

0 comments on commit 380b8f3

Please sign in to comment.