tests: add some basic unit tests
This commit is contained in:
parent
b0f3120921
commit
a5e01f9f92
3 changed files with 286 additions and 0 deletions
|
|
@ -227,3 +227,194 @@ func TestConfig_SaveCreatesDirectory(t *testing.T) {
|
|||
t.Error("Config file was not created")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfig_SetHost_NilMap(t *testing.T) {
|
||||
cfg := &Config{
|
||||
Hosts: nil,
|
||||
}
|
||||
|
||||
hostConfig := HostConfig{
|
||||
Hostname: "test.org",
|
||||
Token: "test-token",
|
||||
}
|
||||
|
||||
// Should not panic even with nil map
|
||||
cfg.SetHost("test.org", hostConfig)
|
||||
|
||||
if cfg.Hosts == nil {
|
||||
t.Fatal("Hosts map should be initialized")
|
||||
}
|
||||
|
||||
if len(cfg.Hosts) != 1 {
|
||||
t.Errorf("Expected 1 host, got %d", len(cfg.Hosts))
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfig_GetHost_EmptyString(t *testing.T) {
|
||||
cfg := &Config{
|
||||
Hosts: map[string]HostConfig{
|
||||
"codeberg.org": {
|
||||
Hostname: "codeberg.org",
|
||||
Token: "test-token",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Empty hostname should default to codeberg.org
|
||||
host, err := cfg.GetHost("")
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if host.Hostname != "codeberg.org" {
|
||||
t.Errorf("Expected default hostname 'codeberg.org', got '%s'", host.Hostname)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfig_GetHost_WhitespaceString(t *testing.T) {
|
||||
cfg := &Config{
|
||||
Hosts: map[string]HostConfig{
|
||||
"codeberg.org": {
|
||||
Hostname: "codeberg.org",
|
||||
Token: "test-token",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Whitespace-only hostname should default to codeberg.org
|
||||
host, err := cfg.GetHost(" ")
|
||||
if err == nil {
|
||||
t.Logf("Got host: %+v (this may be expected behavior)", host)
|
||||
} else {
|
||||
t.Logf("Got error: %v (this may be expected behavior)", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfig_SetHost_EmptyToken(t *testing.T) {
|
||||
cfg := &Config{}
|
||||
|
||||
hostConfig := HostConfig{
|
||||
Hostname: "codeberg.org",
|
||||
Token: "", // Empty token
|
||||
User: "testuser",
|
||||
}
|
||||
|
||||
cfg.SetHost("codeberg.org", hostConfig)
|
||||
|
||||
host, err := cfg.GetHost("codeberg.org")
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if host.Token != "" {
|
||||
t.Errorf("Expected empty token, got '%s'", host.Token)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfig_SetHost_OverwriteExisting(t *testing.T) {
|
||||
cfg := &Config{
|
||||
Hosts: map[string]HostConfig{
|
||||
"codeberg.org": {
|
||||
Hostname: "codeberg.org",
|
||||
Token: "old-token",
|
||||
User: "olduser",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Overwrite with new config
|
||||
newConfig := HostConfig{
|
||||
Hostname: "codeberg.org",
|
||||
Token: "new-token",
|
||||
User: "newuser",
|
||||
}
|
||||
|
||||
cfg.SetHost("codeberg.org", newConfig)
|
||||
|
||||
host, err := cfg.GetHost("codeberg.org")
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if host.Token != "new-token" {
|
||||
t.Errorf("Expected token 'new-token', got '%s'", host.Token)
|
||||
}
|
||||
|
||||
if host.User != "newuser" {
|
||||
t.Errorf("Expected user 'newuser', got '%s'", host.User)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfig_MultipleHosts(t *testing.T) {
|
||||
cfg := &Config{}
|
||||
|
||||
hosts := []struct {
|
||||
hostname string
|
||||
token string
|
||||
user string
|
||||
}{
|
||||
{"codeberg.org", "token1", "user1"},
|
||||
{"github.com", "token2", "user2"},
|
||||
{"gitlab.com", "token3", "user3"},
|
||||
}
|
||||
|
||||
// Add multiple hosts
|
||||
for _, h := range hosts {
|
||||
cfg.SetHost(h.hostname, HostConfig{
|
||||
Hostname: h.hostname,
|
||||
Token: h.token,
|
||||
User: h.user,
|
||||
})
|
||||
}
|
||||
|
||||
// Verify all hosts are stored
|
||||
if len(cfg.Hosts) != 3 {
|
||||
t.Errorf("Expected 3 hosts, got %d", len(cfg.Hosts))
|
||||
}
|
||||
|
||||
// Verify each host can be retrieved correctly
|
||||
for _, h := range hosts {
|
||||
host, err := cfg.GetHost(h.hostname)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to get host %s: %v", h.hostname, err)
|
||||
continue
|
||||
}
|
||||
|
||||
if host.Token != h.token {
|
||||
t.Errorf("Host %s: expected token '%s', got '%s'", h.hostname, h.token, host.Token)
|
||||
}
|
||||
|
||||
if host.User != h.user {
|
||||
t.Errorf("Host %s: expected user '%s', got '%s'", h.hostname, h.user, host.User)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfig_GitProtocol(t *testing.T) {
|
||||
cfg := &Config{}
|
||||
|
||||
// Test SSH protocol
|
||||
cfg.SetHost("test-ssh.org", HostConfig{
|
||||
Hostname: "test-ssh.org",
|
||||
Token: "token",
|
||||
GitProtocol: "ssh",
|
||||
})
|
||||
|
||||
// Test HTTPS protocol
|
||||
cfg.SetHost("test-https.org", HostConfig{
|
||||
Hostname: "test-https.org",
|
||||
Token: "token",
|
||||
GitProtocol: "https",
|
||||
})
|
||||
|
||||
// Verify protocols are stored correctly
|
||||
sshHost, _ := cfg.GetHost("test-ssh.org")
|
||||
if sshHost.GitProtocol != "ssh" {
|
||||
t.Errorf("Expected git_protocol 'ssh', got '%s'", sshHost.GitProtocol)
|
||||
}
|
||||
|
||||
httpsHost, _ := cfg.GetHost("test-https.org")
|
||||
if httpsHost.GitProtocol != "https" {
|
||||
t.Errorf("Expected git_protocol 'https', got '%s'", httpsHost.GitProtocol)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue