Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Billy Zha <[email protected]>
  • Loading branch information
qweeah committed Jan 11, 2024
1 parent e197c00 commit df42924
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion registry/remote/credentials/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
22 changes: 22 additions & 0 deletions registry/remote/credentials/internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions registry/remote/credentials/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit df42924

Please sign in to comment.