From 08cfc6feaf7e8ee632dda61da46731ab590500bd Mon Sep 17 00:00:00 2001 From: Akanksha Gahalot Date: Wed, 21 Jun 2023 13:59:29 +0530 Subject: [PATCH] rewriting test to cover error scenario Signed-off-by: Akanksha Gahalot --- internal/credential/store.go | 4 +--- internal/credential/store_test.go | 30 ++++++++++++++++++++---------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/internal/credential/store.go b/internal/credential/store.go index 62764ba8c..5c61d8726 100644 --- a/internal/credential/store.go +++ b/internal/credential/store.go @@ -19,8 +19,6 @@ import ( credentials "github.com/oras-project/oras-credentials-go" ) -var CreateNewStore = credentials.NewStore - // NewStore generates a store based on the passed-in config file paths. func NewStore(configPaths ...string) (credentials.Store, error) { opts := credentials.StoreOptions{AllowPlaintextPut: true} @@ -31,7 +29,7 @@ func NewStore(configPaths ...string) (credentials.Store, error) { var stores []credentials.Store for _, config := range configPaths { - store, err := CreateNewStore(config, opts) + store, err := credentials.NewStore(config, opts) if err != nil { return nil, err } diff --git a/internal/credential/store_test.go b/internal/credential/store_test.go index 86b5427d4..26d37b79f 100644 --- a/internal/credential/store_test.go +++ b/internal/credential/store_test.go @@ -14,27 +14,37 @@ limitations under the License. package credential import ( - "fmt" + "os" "reflect" "testing" - - credentials "github.com/oras-project/oras-credentials-go" ) func TestNewStoreError(t *testing.T) { - // save current function - oldFunction := CreateNewStore - // restoring changes - defer func() { CreateNewStore = oldFunction }() - CreateNewStore = func(configPath string, opts credentials.StoreOptions) (credentials.Store, error) { - return nil, fmt.Errorf("New Error") + filename := "testfile.txt" + _, err := os.Create(filename) + if err != nil { + t.Errorf("error while creating test file %v", err) + } + defer func() { + err := os.Remove(filename) + if err != nil { + t.Errorf("error while removing test file %v", err) + } + }() + + err = os.Chmod(filename, 000) + if err != nil { + t.Errorf("error: cannot change file permissions: %v", err) } - var config string = "testconfig" + + var config string = filename credStore, err := NewStore(config) + if !reflect.DeepEqual(credStore, nil) { t.Errorf("Expected NewStore to return nil but actually returned %v ", credStore) } if reflect.DeepEqual(err, nil) { t.Error("Expected Error to be not nil but actually returned nil ") } + }