-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/np: add -f flag to read config file (#21)
Fixes #20
- Loading branch information
Showing
7 changed files
with
183 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package config | ||
|
||
import ( | ||
"io" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestLoad(t *testing.T) { | ||
emptyAPIKeyCfg := `{"server":"http://some.tld","login":"mylogin","password":"mypass"}` | ||
emptyLoginCfg := `{"server":"http://some.tld","apiKey":"key","password":"mypass"}` | ||
emptyPasswordCfg := `{"server":"http://some.tld","login":"mylogin","apiKey":"key"}` | ||
emptyServerCfg := `{"apiKey":"key","login":"mylogin","password":"mypass"}` | ||
tests := []struct { | ||
name string | ||
r io.Reader | ||
wantErr bool | ||
}{ | ||
{"nil reader", nil, true}, | ||
{"bad config", strings.NewReader("nojson"), true}, | ||
{"valid config", strings.NewReader(validCfg), false}, | ||
{"empty API key", strings.NewReader(emptyAPIKeyCfg), true}, | ||
{"empty login", strings.NewReader(emptyLoginCfg), true}, | ||
{"empty password", strings.NewReader(emptyPasswordCfg), true}, | ||
{"empty server", strings.NewReader(emptyServerCfg), true}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if err := Load(tt.r); (err != nil) != tt.wantErr { | ||
t.Errorf("Load() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
var validCfg = ` | ||
{ | ||
"server": "http://some.tld", | ||
"login": "mylogin", | ||
"password": "mypass", | ||
"apiKey": "key" | ||
} | ||
` | ||
|
||
func TestLogin(t *testing.T) { | ||
Load(strings.NewReader(validCfg)) | ||
tests := []struct { | ||
name string | ||
want string | ||
}{ | ||
{"login value", "mylogin"}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := Login(); got != tt.want { | ||
t.Errorf("Login() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestPassword(t *testing.T) { | ||
Load(strings.NewReader(validCfg)) | ||
tests := []struct { | ||
name string | ||
want string | ||
}{ | ||
{"password value", "mypass"}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := Password(); got != tt.want { | ||
t.Errorf("Password() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestAPIKey(t *testing.T) { | ||
Load(strings.NewReader(validCfg)) | ||
tests := []struct { | ||
name string | ||
want string | ||
}{ | ||
{"key value", "key"}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := APIKey(); got != tt.want { | ||
t.Errorf("APIKey() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestServer(t *testing.T) { | ||
Load(strings.NewReader(validCfg)) | ||
tests := []struct { | ||
name string | ||
want string | ||
}{ | ||
{"server url", "http://some.tld"}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := Server(); got != tt.want { | ||
t.Errorf("Server() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters