@@ -2,6 +2,7 @@ package sicher
22
33import (
44 "encoding/hex"
5+ "fmt"
56 "os"
67 "testing"
78)
@@ -70,6 +71,96 @@ URI=localhost
7071 t .Errorf ("Expected dotenv style env not be be parseable with yaml envType" )
7172 }
7273}
74+ func TestParseConfig (t * testing.T ) {
75+
76+ tests := []struct {
77+ text string
78+ expected map [string ]string
79+ envType string
80+ }{
81+ {
82+ text : `
83+ PORT:8080
84+ URI:localhost
85+ #OLD_PORT:5000
86+ ` ,
87+ expected : map [string ]string {
88+ "PORT" : "8080" ,
89+ "URI" : "localhost" ,
90+ },
91+ envType : "yaml" ,
92+ },
93+ {
94+ text : `
95+ PORT=8080
96+ URI=localhost
97+ #OLD_PORT=5000
98+ ` ,
99+ expected : map [string ]string {
100+ "PORT" : "8080" ,
101+ "URI" : "localhost" ,
102+ },
103+ envType : "dotenv" ,
104+ },
105+ {
106+ text : `
107+ PORT=8080
108+ URI=localhost
109+ #OLD_PORT=5000
110+ KEY=value=ndsjhjdghdhg
111+ ` ,
112+ expected : map [string ]string {
113+ "PORT" : "8080" ,
114+ "URI" : "localhost" ,
115+ "KEY" : "value=ndsjhjdghdhg" ,
116+ },
117+ envType : "dotenv" ,
118+ },
119+ {
120+ text : `
121+ PORT:8080
122+ URI=localhost
123+ ` ,
124+ expected : map [string ]string {
125+ "PORT" : "8080" ,
126+ },
127+ envType : "yaml" ,
128+ },
129+ {
130+ text : `
131+ PORT:8080
132+ URI=localhost
133+ SOME_KEY:somevalue=jsfhjdghdhg
134+ ` ,
135+ expected : map [string ]string {
136+ "URI" : "localhost" ,
137+ },
138+ envType : "dotenv" ,
139+ },
140+ }
141+
142+ for _ , val := range tests {
143+ enMap := make (map [string ]string )
144+ if err := parseConfig ([]byte (val .text ), enMap , EnvStyle (val .envType )); err != nil {
145+ t .Errorf ("Unable to parse config; %v" , err )
146+ }
147+
148+ t .Run (fmt .Sprintf ("Envtype %s" , val .envType ), func (t * testing.T ) {
149+ for key , value := range val .expected {
150+ if enMap [key ] != value {
151+ t .Errorf ("Expected value to be %s, got %s" , value , enMap [key ])
152+ }
153+ }
154+ for key , value := range enMap {
155+ if val .expected [key ] != value {
156+ t .Errorf ("Expected value to be %s, got %s" , value , val .expected [key ])
157+ }
158+ }
159+ })
160+ }
161+
162+ }
163+
73164func TestYamlParseConfig (t * testing.T ) {
74165 enMap := make (map [string ]string )
75166 cfg := []byte (`
0 commit comments