diff --git a/registry/remote/credentials/internal/config/config.go b/registry/remote/credentials/internal/config/config.go index 43cb8c0c..bd00a010 100644 --- a/registry/remote/credentials/internal/config/config.go +++ b/registry/remote/credentials/internal/config/config.go @@ -158,7 +158,7 @@ func Load(configPath string) (*Config, error) { // Path returns the path to the config file. func (cfg *Config) Path() (string, error) { if cfg == nil { - errors.New("config file is nil") + return "", errors.New("config file is nil") } return cfg.path, nil } diff --git a/registry/remote/credentials/internal/config/config_test.go b/registry/remote/credentials/internal/config/config_test.go index 6622108b..c673f1a9 100644 --- a/registry/remote/credentials/internal/config/config_test.go +++ b/registry/remote/credentials/internal/config/config_test.go @@ -925,6 +925,28 @@ func TestConfig_GetCredentialHelper(t *testing.T) { } } +func TestConfig_Path(t *testing.T) { + cfg := Config{ + path: "/path/to/config.json", + } + got, err := cfg.Path() + + if err != nil { + t.Errorf("Config.GetPath() error = %v", err) + } + if got != cfg.path { + t.Errorf("Config.GetPath() = %v, want %v", got, cfg.path) + } +} + +func TestConfig_Path_nil(t *testing.T) { + var cfg *Config + _, err := cfg.Path() + if err == nil { + t.Error("expecting error, got nil") + } +} + func TestConfig_CredentialsStore(t *testing.T) { tests := []struct { name string diff --git a/registry/remote/credentials/store_test.go b/registry/remote/credentials/store_test.go index 285e2796..cf9f30cc 100644 --- a/registry/remote/credentials/store_test.go +++ b/registry/remote/credentials/store_test.go @@ -25,6 +25,7 @@ import ( "testing" "oras.land/oras-go/v2/registry/remote/auth" + "oras.land/oras-go/v2/registry/remote/credentials/internal/config" "oras.land/oras-go/v2/registry/remote/credentials/internal/config/configtest" ) @@ -611,6 +612,30 @@ func Test_DynamicStore_getHelperSuffix(t *testing.T) { }) } } +func Test_DynamicStore_GetConfigPath(t *testing.T) { + var store DynamicStore + var err error + path := "../../testdata/credsStore_config.json" + store.config, err = config.Load(path) + if err != nil { + t.Fatal("config.Load() error =", err) + } + got, err := store.GetConfigPath() + if err != nil { + t.Errorf("Config.GetPath() error = %v", err) + } + if got != path { + t.Errorf("Config.GetPath() = %v, want %v", got, path) + } +} + +func Test_DynamicStore_Path_nil(t *testing.T) { + var store DynamicStore + _, err := store.GetConfigPath() + if err == nil { + t.Error("expecting error, got nil") + } +} func Test_DynamicStore_getStore_nativeStore(t *testing.T) { tests := []struct {