Skip to content

Commit

Permalink
rewriting test to cover error scenario
Browse files Browse the repository at this point in the history
Signed-off-by: Akanksha Gahalot <akankshagahlot0307@gmail.com>
  • Loading branch information
enraiha0307 authored and qweeah committed Jul 12, 2023
1 parent fc4f3e5 commit 88ae475
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
4 changes: 1 addition & 3 deletions internal/credential/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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
}
Expand Down
36 changes: 22 additions & 14 deletions internal/credential/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ 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.
Expand All @@ -14,27 +16,33 @@ limitations under the License.
package credential

import (
"fmt"
"os"
"path"
"reflect"
"strings"
"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")
tmpDir := t.TempDir()
filename := path.Join(tmpDir, "testfile.txt")
_, err := os.Create(filename)
if err != nil {
t.Errorf("error: cannot create file : %v", err)
}
var config string = "testconfig"
credStore, err := NewStore(config)
if !reflect.DeepEqual(credStore, nil) {
err = os.Chmod(filename, 000)
if err != nil {
t.Errorf("error: cannot change file permissions: %v", err)
}
credStore, err := NewStore(filename)
if 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 ")
if err != nil {
ok := strings.Contains(err.Error(), "failed to open config file")
reflect.DeepEqual(ok, true)

} else {
t.Errorf("Expected err to be not nil")
}
}

0 comments on commit 88ae475

Please sign in to comment.