Skip to content

Commit dad1ed1

Browse files
authored
Merge pull request #20 from ncode/ncode/coverage-to-90-percent
test: raise overall coverage from 71.7% to 90.1%
2 parents 8720193 + a60cb5c commit dad1ed1

16 files changed

Lines changed: 1681 additions & 5 deletions

cmd/hosts_test.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,134 @@ func TestValidatePort(t *testing.T) {
281281
t.Fatalf("expected out of range error")
282282
}
283283
}
284+
285+
func TestParseGroupSpecsMapInterfaceInterface(t *testing.T) {
286+
raw := map[interface{}]interface{}{
287+
"hosts": []interface{}{"host1"},
288+
}
289+
specs, err := parseGroupSpecs(raw, "alt")
290+
if err != nil {
291+
t.Fatalf("unexpected error: %v", err)
292+
}
293+
want := []HostSpec{
294+
{Host: "host1", Port: defaultPort},
295+
}
296+
if !reflect.DeepEqual(specs, want) {
297+
t.Fatalf("unexpected specs: %+v", specs)
298+
}
299+
}
300+
301+
func TestParseGroupSpecsInvalidKeyType(t *testing.T) {
302+
raw := map[interface{}]interface{}{
303+
42: "val",
304+
}
305+
_, err := parseGroupSpecs(raw, "badkey")
306+
if err == nil {
307+
t.Fatalf("expected error")
308+
}
309+
if !strings.Contains(err.Error(), "invalid key type") {
310+
t.Fatalf("expected invalid key type error, got %v", err)
311+
}
312+
}
313+
314+
func TestParseGroupSpecsEmptyHostsList(t *testing.T) {
315+
raw := map[string]interface{}{
316+
"hosts": []interface{}{},
317+
}
318+
specs, err := parseGroupSpecs(raw, "empty")
319+
if err != nil {
320+
t.Fatalf("unexpected error: %v", err)
321+
}
322+
if specs != nil {
323+
t.Fatalf("expected nil, got %+v", specs)
324+
}
325+
}
326+
327+
func TestParseGroupSpecsNonStringHostEntry(t *testing.T) {
328+
raw := map[string]interface{}{
329+
"hosts": []interface{}{42},
330+
}
331+
_, err := parseGroupSpecs(raw, "numhost")
332+
if err == nil {
333+
t.Fatalf("expected error")
334+
}
335+
if !strings.Contains(err.Error(), "must be a string") {
336+
t.Fatalf("expected 'must be a string' error, got %v", err)
337+
}
338+
}
339+
340+
func TestParseGroupSpecsHostsNotAList(t *testing.T) {
341+
raw := map[string]interface{}{
342+
"hosts": "not-a-list",
343+
}
344+
_, err := parseGroupSpecs(raw, "badlist")
345+
if err == nil {
346+
t.Fatalf("expected error")
347+
}
348+
if !strings.Contains(err.Error(), "hosts must be a list") {
349+
t.Fatalf("expected 'hosts must be a list' error, got %v", err)
350+
}
351+
}
352+
353+
func TestParseGroupSpecsNilReturnsNil(t *testing.T) {
354+
specs, err := parseGroupSpecs(nil, "nothing")
355+
if err != nil {
356+
t.Fatalf("unexpected error: %v", err)
357+
}
358+
if specs != nil {
359+
t.Fatalf("expected nil, got %+v", specs)
360+
}
361+
}
362+
363+
func TestParseHostSpecUserAtEmptyHost(t *testing.T) {
364+
_, err := parseHostSpec("user@")
365+
if err == nil {
366+
t.Fatalf("expected error")
367+
}
368+
if !strings.Contains(err.Error(), "invalid user@host") {
369+
t.Fatalf("expected 'invalid user@host' error, got %v", err)
370+
}
371+
}
372+
373+
func TestParseHostSpecIPv6WithPort(t *testing.T) {
374+
got, err := parseHostSpec("[::1]:22")
375+
if err != nil {
376+
t.Fatalf("unexpected error: %v", err)
377+
}
378+
want := HostSpec{Host: "::1", Port: 22, PortSet: true}
379+
if got != want {
380+
t.Fatalf("expected %+v, got %+v", want, got)
381+
}
382+
}
383+
384+
func TestParseHostSpecIPv6NoBrackets(t *testing.T) {
385+
got, err := parseHostSpec("2001:db8::1")
386+
if err != nil {
387+
t.Fatalf("unexpected error: %v", err)
388+
}
389+
want := HostSpec{Host: "2001:db8::1", Port: defaultPort}
390+
if got != want {
391+
t.Fatalf("expected %+v, got %+v", want, got)
392+
}
393+
}
394+
395+
func TestParseHostSpecIPv6BracketBadPort(t *testing.T) {
396+
_, err := parseHostSpec("[::1]:bad")
397+
if err == nil {
398+
t.Fatal("expected error for bad port in bracketed IPv6")
399+
}
400+
}
401+
402+
func TestParseHostSpecIPv6BracketBadFormat(t *testing.T) {
403+
_, err := parseHostSpec("[::1")
404+
if err == nil {
405+
t.Fatal("expected error for malformed bracketed IPv6")
406+
}
407+
}
408+
409+
func TestParseHostSpecColonEmptyHost(t *testing.T) {
410+
_, err := parseHostSpec(":22")
411+
if err == nil {
412+
t.Fatal("expected error for empty host with port")
413+
}
414+
}

cmd/root_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"errors"
55
"os"
6+
"path/filepath"
67
"strings"
78
"testing"
89

@@ -322,3 +323,50 @@ func TestExecuteAppliesGlobalUserToHostAndJumpSpecs(t *testing.T) {
322323
t.Fatalf("expected 2 resolve calls, got %d", call)
323324
}
324325
}
326+
327+
func TestInitConfigWithNonExistentFile(t *testing.T) {
328+
prevCfgFile := cfgFile
329+
t.Cleanup(func() {
330+
cfgFile = prevCfgFile
331+
viper.Reset()
332+
})
333+
334+
cfgFile = "/tmp/pretty-test-nonexistent-config-file.yaml"
335+
viper.Reset()
336+
337+
// Should not panic even though the file doesn't exist.
338+
initConfig()
339+
340+
// ReadInConfig silently fails, so viper should have no config file used.
341+
if used := viper.ConfigFileUsed(); used != "" {
342+
// When the file doesn't exist, ConfigFileUsed may still return the
343+
// path that was set, but ReadInConfig should have returned an error
344+
// (handled silently). Just verify no panic occurred.
345+
}
346+
}
347+
348+
func TestInitConfigWithExistingFile(t *testing.T) {
349+
prevCfgFile := cfgFile
350+
t.Cleanup(func() {
351+
cfgFile = prevCfgFile
352+
viper.Reset()
353+
})
354+
355+
dir := t.TempDir()
356+
configPath := filepath.Join(dir, "test-pretty.yaml")
357+
if err := os.WriteFile(configPath, []byte("username: testuser\nhistory_file: /tmp/test.history\n"), 0644); err != nil {
358+
t.Fatalf("unexpected write error: %v", err)
359+
}
360+
361+
viper.Reset()
362+
cfgFile = configPath
363+
364+
initConfig()
365+
366+
if got := viper.GetString("username"); got != "testuser" {
367+
t.Fatalf("expected username 'testuser', got %q", got)
368+
}
369+
if got := viper.GetString("history_file"); got != "/tmp/test.history" {
370+
t.Fatalf("expected history_file '/tmp/test.history', got %q", got)
371+
}
372+
}

0 commit comments

Comments
 (0)