From 380b8f30d8aaf45dd3679d8016696dc65ea13da7 Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Fri, 23 Dec 2022 17:11:49 +0800 Subject: [PATCH] enhance unit tests Signed-off-by: Billy Zha --- cmd/oras/internal/fileref/unix.go | 14 ++++---------- cmd/oras/internal/fileref/unix_test.go | 2 +- cmd/oras/internal/fileref/windows.go | 5 +++++ cmd/oras/internal/fileref/windows_test.go | 1 + 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/cmd/oras/internal/fileref/unix.go b/cmd/oras/internal/fileref/unix.go index b99e3583f..0b2bc51e3 100644 --- a/cmd/oras/internal/fileref/unix.go +++ b/cmd/oras/internal/fileref/unix.go @@ -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:] } diff --git a/cmd/oras/internal/fileref/unix_test.go b/cmd/oras/internal/fileref/unix_test.go index a9d670117..41b44c8e5 100644 --- a/cmd/oras/internal/fileref/unix_test.go +++ b/cmd/oras/internal/fileref/unix_test.go @@ -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}, } diff --git a/cmd/oras/internal/fileref/windows.go b/cmd/oras/internal/fileref/windows.go index 9ea05976f..38d1f859d 100644 --- a/cmd/oras/internal/fileref/windows.go +++ b/cmd/oras/internal/fileref/windows.go @@ -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) diff --git a/cmd/oras/internal/fileref/windows_test.go b/cmd/oras/internal/fileref/windows_test.go index 9782a630d..9990d47bb 100644 --- a/cmd/oras/internal/fileref/windows_test.go +++ b/cmd/oras/internal/fileref/windows_test.go @@ -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},