github.com/abdfnx/gh-api@v0.0.0-20210414084727-f5432eec23b8/git/ssh_config_test.go (about) 1 package git 2 3 import ( 4 "bytes" 5 "fmt" 6 "io" 7 "net/url" 8 "path/filepath" 9 "testing" 10 11 "github.com/MakeNowJust/heredoc" 12 ) 13 14 func Test_sshParser_read(t *testing.T) { 15 testFiles := map[string]string{ 16 "/etc/ssh/config": heredoc.Doc(` 17 Include sites/* 18 `), 19 "/etc/ssh/sites/cfg1": heredoc.Doc(` 20 Host s1 21 Hostname=site1.net 22 `), 23 "/etc/ssh/sites/cfg2": heredoc.Doc(` 24 Host s2 25 Hostname = site2.net 26 `), 27 "HOME/.ssh/config": heredoc.Doc(` 28 Host * 29 Host gh gittyhubby 30 Hostname github.com 31 #Hostname example.com 32 Host ex 33 Include ex_config/* 34 `), 35 "HOME/.ssh/ex_config/ex_cfg": heredoc.Doc(` 36 Hostname example.com 37 `), 38 } 39 globResults := map[string][]string{ 40 "/etc/ssh/sites/*": {"/etc/ssh/sites/cfg1", "/etc/ssh/sites/cfg2"}, 41 "HOME/.ssh/ex_config/*": {"HOME/.ssh/ex_config/ex_cfg"}, 42 } 43 44 p := &sshParser{ 45 homeDir: "HOME", 46 open: func(s string) (io.Reader, error) { 47 if contents, ok := testFiles[filepath.ToSlash(s)]; ok { 48 return bytes.NewBufferString(contents), nil 49 } else { 50 return nil, fmt.Errorf("no test file stub found: %q", s) 51 } 52 }, 53 glob: func(p string) ([]string, error) { 54 if results, ok := globResults[filepath.ToSlash(p)]; ok { 55 return results, nil 56 } else { 57 return nil, fmt.Errorf("no glob stubs found: %q", p) 58 } 59 }, 60 } 61 62 if err := p.read("/etc/ssh/config"); err != nil { 63 t.Fatalf("read(global config) = %v", err) 64 } 65 if err := p.read("HOME/.ssh/config"); err != nil { 66 t.Fatalf("read(user config) = %v", err) 67 } 68 69 if got := p.aliasMap["gh"]; got != "github.com" { 70 t.Errorf("expected alias %q to expand to %q, got %q", "gh", "github.com", got) 71 } 72 if got := p.aliasMap["gittyhubby"]; got != "github.com" { 73 t.Errorf("expected alias %q to expand to %q, got %q", "gittyhubby", "github.com", got) 74 } 75 if got := p.aliasMap["example.com"]; got != "" { 76 t.Errorf("expected alias %q to expand to %q, got %q", "example.com", "", got) 77 } 78 if got := p.aliasMap["ex"]; got != "example.com" { 79 t.Errorf("expected alias %q to expand to %q, got %q", "ex", "example.com", got) 80 } 81 if got := p.aliasMap["s1"]; got != "site1.net" { 82 t.Errorf("expected alias %q to expand to %q, got %q", "s1", "site1.net", got) 83 } 84 } 85 86 func Test_sshParser_absolutePath(t *testing.T) { 87 dir := "HOME" 88 p := &sshParser{homeDir: dir} 89 90 tests := map[string]struct { 91 parentFile string 92 arg string 93 want string 94 wantErr bool 95 }{ 96 "absolute path": { 97 parentFile: "/etc/ssh/ssh_config", 98 arg: "/etc/ssh/config", 99 want: "/etc/ssh/config", 100 }, 101 "system relative path": { 102 parentFile: "/etc/ssh/config", 103 arg: "configs/*.conf", 104 want: filepath.Join("/etc", "ssh", "configs", "*.conf"), 105 }, 106 "user relative path": { 107 parentFile: filepath.Join(dir, ".ssh", "ssh_config"), 108 arg: "configs/*.conf", 109 want: filepath.Join(dir, ".ssh", "configs/*.conf"), 110 }, 111 "shell-like ~ rerefence": { 112 parentFile: filepath.Join(dir, ".ssh", "ssh_config"), 113 arg: "~/.ssh/*.conf", 114 want: filepath.Join(dir, ".ssh", "*.conf"), 115 }, 116 } 117 118 for name, tt := range tests { 119 t.Run(name, func(t *testing.T) { 120 if got := p.absolutePath(tt.parentFile, tt.arg); got != tt.want { 121 t.Errorf("absolutePath(): %q, wants %q", got, tt.want) 122 } 123 }) 124 } 125 } 126 127 func Test_Translator(t *testing.T) { 128 m := SSHAliasMap{ 129 "gh": "github.com", 130 "github.com": "ssh.github.com", 131 } 132 tr := m.Translator() 133 134 cases := [][]string{ 135 {"ssh://gh/o/r", "ssh://github.com/o/r"}, 136 {"ssh://github.com/o/r", "ssh://github.com/o/r"}, 137 {"https://gh/o/r", "https://gh/o/r"}, 138 } 139 for _, c := range cases { 140 u, _ := url.Parse(c[0]) 141 got := tr(u) 142 if got.String() != c[1] { 143 t.Errorf("%q: expected %q, got %q", c[0], c[1], got) 144 } 145 } 146 }