MCPcopy
hub / github.com/cli/cli / TestGenerateAutomaticSSHKeys

Function TestGenerateAutomaticSSHKeys

pkg/cmd/codespace/ssh_test.go:30–124  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

28}
29
30func TestGenerateAutomaticSSHKeys(t *testing.T) {
31 tests := []struct {
32 // These files exist when calling generateAutomaticSSHKeys
33 existingFiles []string
34 // These files should exist after generateAutomaticSSHKeys finishes
35 wantFinalFiles []string
36 }{
37 // Basic case: no existing keys, they should be created
38 {
39 nil,
40 []string{automaticPrivateKeyName, automaticPrivateKeyName + ".pub"},
41 },
42 // Basic case: keys already exist
43 {
44 []string{automaticPrivateKeyName, automaticPrivateKeyName + ".pub"},
45 []string{automaticPrivateKeyName, automaticPrivateKeyName + ".pub"},
46 },
47 // Backward compatibility: both old keys exist, they should be renamed
48 {
49 []string{automaticPrivateKeyNameOld, automaticPrivateKeyNameOld + ".pub"},
50 []string{automaticPrivateKeyName, automaticPrivateKeyName + ".pub"},
51 },
52 // Backward compatibility: old private key exists but not the public key, the new keys should be created
53 {
54 []string{automaticPrivateKeyNameOld},
55 []string{automaticPrivateKeyNameOld, automaticPrivateKeyName, automaticPrivateKeyName + ".pub"},
56 },
57 // Backward compatibility: old public key exists but not the private key, the new keys should be created
58 {
59 []string{automaticPrivateKeyNameOld + ".pub"},
60 []string{automaticPrivateKeyNameOld + ".pub", automaticPrivateKeyName, automaticPrivateKeyName + ".pub"},
61 },
62 // Backward compatibility (edge case): files exist which contains old key name as a substring, the new keys should be created
63 {
64 []string{"foo" + automaticPrivateKeyNameOld + ".pub", "foo" + automaticPrivateKeyNameOld},
65 []string{"foo" + automaticPrivateKeyNameOld + ".pub", "foo" + automaticPrivateKeyNameOld, automaticPrivateKeyName, automaticPrivateKeyName + ".pub"},
66 },
67 }
68
69 for _, tt := range tests {
70 dir := t.TempDir()
71
72 sshContext := ssh.NewContextForTests(dir, "")
73
74 for _, file := range tt.existingFiles {
75 f, err := os.Create(filepath.Join(dir, file))
76 if err != nil {
77 t.Errorf("Failed to setup test files: %v", err)
78 }
79 // If the file isn't closed here windows will have errors about file already in use
80 f.Close()
81 }
82
83 keyPair, err := generateAutomaticSSHKeys(sshContext)
84 if err != nil {
85 t.Errorf("Unexpected error from generateAutomaticSSHKeys: %v", err)
86 }
87 if keyPair == nil {

Callers

nothing calls this directly

Calls 7

NewContextForTestsFunction · 0.92
generateAutomaticSSHKeysFunction · 0.85
JoinMethod · 0.80
CreateMethod · 0.65
ErrorfMethod · 0.65
CloseMethod · 0.65
NameMethod · 0.65

Tested by

no test coverage detected