@@ -8,11 +8,65 @@ import (
88
99 "github.com/stretchr/testify/assert"
1010 "github.com/stretchr/testify/require"
11+ "gopkg.in/yaml.v3"
1112
1213 "github.com/launchdarkly/ldcli/internal/config"
1314 "github.com/launchdarkly/ldcli/internal/resources"
1415)
1516
17+ type mockReadFile struct {
18+ contents []byte
19+ successful bool
20+ }
21+
22+ func (m mockReadFile ) readFile (name string ) ([]byte , error ) {
23+ if ! m .successful {
24+ return nil , errors .New ("an error" )
25+ }
26+
27+ if m .contents != nil {
28+ return yaml .Marshal (m .contents )
29+ }
30+
31+ return yaml .Marshal (map [string ]interface {}{
32+ "access-token" : "test-access-token" ,
33+ })
34+ }
35+
36+ func TestNewConfigFromFile (t * testing.T ) {
37+ t .Run ("with valid input is a valid config" , func (t * testing.T ) {
38+ mock := mockReadFile {
39+ successful : true ,
40+ }
41+
42+ c , err := config .NewConfigFromFile ("test-config-file" , mock .readFile )
43+
44+ require .NoError (t , err )
45+ assert .Equal (t , "test-access-token" , c .AccessToken )
46+ })
47+
48+ t .Run ("with invalid file is an error" , func (t * testing.T ) {
49+ mock := mockReadFile {
50+ successful : false ,
51+ }
52+
53+ _ , err := config .NewConfigFromFile ("test-config-file" , mock .readFile )
54+
55+ assert .EqualError (t , err , "could not read config file" )
56+ })
57+
58+ t .Run ("with invalid formatting in the file contents is an error" , func (t * testing.T ) {
59+ mock := mockReadFile {
60+ contents : []byte (`invalid` ),
61+ successful : true ,
62+ }
63+
64+ _ , err := config .NewConfigFromFile ("test-config-file" , mock .readFile )
65+
66+ assert .EqualError (t , err , "config file is invalid yaml" )
67+ })
68+ }
69+
1670func TestNewConfig (t * testing.T ) {
1771 t .Run ("analytics-opt-out" , func (t * testing.T ) {
1872 tests := map [string ]struct {
0 commit comments