Merge pull request #101217 from xuhdev/dir-test

TestGetPatchSetsForPathMustBeDirectory: Ensure the error type is os.PathError
This commit is contained in:
Kubernetes Prow Robot 2021-04-19 22:12:32 -07:00 committed by GitHub
commit f948c75ef2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 4 deletions

View File

@ -283,7 +283,11 @@ func getPatchSetsFromPath(targetPath string, knownTargets []string, output io.Wr
goto return_path_error goto return_path_error
} }
if !info.IsDir() { if !info.IsDir() {
err = errors.New("not a directory") err = &os.PathError{
Op: "getPatchSetsFromPath",
Path: info.Name(),
Err: errors.New("not a directory"),
}
goto return_path_error goto return_path_error
} }

View File

@ -24,6 +24,7 @@ import (
"reflect" "reflect"
"testing" "testing"
"github.com/pkg/errors"
"k8s.io/api/core/v1" "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/types"
) )
@ -165,15 +166,16 @@ func TestCreatePatchSet(t *testing.T) {
} }
func TestGetPatchSetsForPathMustBeDirectory(t *testing.T) { func TestGetPatchSetsForPathMustBeDirectory(t *testing.T) {
tempFile, err := ioutil.TempFile("", "test-file") tempFile, err := os.CreateTemp("", "test-file")
if err != nil { if err != nil {
t.Errorf("error creating temporary file: %v", err) t.Errorf("error creating temporary file: %v", err)
} }
defer os.Remove(tempFile.Name()) defer os.Remove(tempFile.Name())
_, _, _, err = getPatchSetsFromPath(tempFile.Name(), testKnownTargets, ioutil.Discard) _, _, _, err = getPatchSetsFromPath(tempFile.Name(), testKnownTargets, ioutil.Discard)
if err == nil { var pathErr *os.PathError
t.Fatalf("expected error for non-directory path %q", tempFile.Name()) if !errors.As(err, &pathErr) {
t.Fatalf("expected os.PathError for non-directory path %q, but got %v", tempFile.Name(), err)
} }
} }