From 423e43dd51615da3a6eef553e4fba5cb05194c13 Mon Sep 17 00:00:00 2001 From: Akanksha Gahalot Date: Tue, 20 Jun 2023 19:02:37 +0530 Subject: [PATCH] adding unit test for error scenario in internal/credential/store.go file Signed-off-by: Akanksha Gahalot --- internal/credential/store.go | 4 +++- internal/credential/store_test.go | 40 +++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 internal/credential/store_test.go diff --git a/internal/credential/store.go b/internal/credential/store.go index 5c61d8726..62764ba8c 100644 --- a/internal/credential/store.go +++ b/internal/credential/store.go @@ -19,6 +19,8 @@ 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} @@ -29,7 +31,7 @@ func NewStore(configPaths ...string) (credentials.Store, error) { var stores []credentials.Store for _, config := range configPaths { - store, err := credentials.NewStore(config, opts) + store, err := CreateNewStore(config, opts) if err != nil { return nil, err } diff --git a/internal/credential/store_test.go b/internal/credential/store_test.go new file mode 100644 index 000000000..86b5427d4 --- /dev/null +++ b/internal/credential/store_test.go @@ -0,0 +1,40 @@ +/* +Copyright The ORAS Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package credential + +import ( + "fmt" + "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") + } + var config string = "testconfig" + 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 ") + } +}