github.com/emmahsax/go-git-helper@v0.0.8-0.20240519163017-907b9de0fa52/cmd/changeRemote/changeRemote_test.go (about) 1 package changeRemote 2 3 import ( 4 "os" 5 "path/filepath" 6 "reflect" 7 "testing" 8 9 "github.com/emmahsax/go-git-helper/internal/commandline" 10 ) 11 12 type MockExecutor struct { 13 Args []string 14 Command string 15 Debug bool 16 Output []byte 17 } 18 19 func (me *MockExecutor) Exec(execType string, command string, args ...string) ([]byte, error) { 20 me.Command = command 21 me.Args = args 22 return me.Output, nil 23 } 24 25 func Test_execute(t *testing.T) { 26 tmpDir := t.TempDir() 27 err := os.Chdir(tmpDir) 28 29 if err != nil { 30 t.Errorf("error was found: %s", err.Error()) 31 } 32 33 cr := newChangeRemote("oldOwner", "newOwner", true, &MockExecutor{Debug: true}) 34 cr.execute() 35 } 36 37 func Test_processDir(t *testing.T) { 38 tempDir, err := os.MkdirTemp("", "test") 39 if err != nil { 40 t.Fatal(err) 41 } 42 defer os.RemoveAll(tempDir) 43 44 gitDir := filepath.Join(tempDir, ".git") 45 err = os.Mkdir(gitDir, 0755) 46 if err != nil { 47 t.Fatal(err) 48 } 49 50 originalAskYesNoQuestion := commandline.AskYesNoQuestion 51 t.Cleanup(func() { 52 commandline.AskYesNoQuestion = originalAskYesNoQuestion 53 }) 54 commandline.AskYesNoQuestion = func(question string) bool { 55 return true 56 } 57 58 executor := &MockExecutor{Debug: true} 59 cr := newChangeRemote("oldOwner", "newOwner", true, executor) 60 cr.processDir(tempDir, "") 61 args := []string{"remote", "-v"} 62 63 if cr.Executor.(*MockExecutor).Command != "git" { 64 t.Errorf("unexpected command received: expected %s, but got %s", "git", cr.Executor.(*MockExecutor).Command) 65 } 66 67 if len(executor.Args) != len(args) { 68 t.Errorf("unexpected args received: expected %v, but got %v", args, executor.Args) 69 } 70 71 for i, v := range executor.Args { 72 if v != args[i] { 73 t.Errorf("unexpected args received: expected %v, but got %v", args, executor.Args) 74 } 75 } 76 } 77 78 func Test_processGitRepository(t *testing.T) { 79 tests := []struct { 80 name string 81 executorOutput []byte 82 expected map[string]map[string]string 83 }{ 84 { 85 name: "SSH remote", 86 executorOutput: []byte(`origin git@github.com:oldOwner/repository.git (fetch) 87 origin git@github.com:oldOwner/repository.git (push) 88 `), 89 expected: map[string]map[string]string{ 90 "repository.git": { 91 "plainRemote": "git@github.com:oldOwner/repository.git", 92 "remoteName": "origin", 93 "host": "git@github.com", 94 }, 95 }, 96 }, 97 { 98 name: "HTTP remote", 99 executorOutput: []byte(`origin https://github.com/oldOwner/repository.git (fetch) 100 origin https://github.com/oldOwner/repository.git (push) 101 `), 102 expected: map[string]map[string]string{ 103 "repository.git": { 104 "plainRemote": "https://github.com/oldOwner/repository.git", 105 "remoteName": "origin", 106 "host": "https://github.com", 107 }, 108 }, 109 }, 110 { 111 "wrong owner", 112 []byte(`origin https://github.com/randomOwner/repository.git (fetch) 113 origin https://github.com/randomOwner/repository.git (push) 114 `), 115 map[string]map[string]string{}, 116 }, 117 } 118 119 for _, test := range tests { 120 executor := &MockExecutor{ 121 Debug: true, 122 Output: test.executorOutput, 123 } 124 125 cr := newChangeRemote("oldOwner", "newOwner", true, executor) 126 fullRemoteInfo := cr.processGitRepository() 127 128 if !reflect.DeepEqual(fullRemoteInfo, test.expected) { 129 t.Errorf("expected %v, but got %v", test.expected, fullRemoteInfo) 130 } 131 } 132 } 133 134 func Test_processRemote(t *testing.T) { 135 tests := []struct { 136 name string 137 url string 138 host string 139 repo string 140 remoteName string 141 expectedArgs []string 142 }{ 143 { 144 name: "SSH remote", 145 url: "git@github.com:oldOwner/repo.git", 146 host: "git@github.com", 147 repo: "repo.git", 148 remoteName: "origin", 149 expectedArgs: []string{"remote", "set-url", "origin", "git@github.com:newOwner/repo.git"}, 150 }, 151 { 152 name: "HTTP remote", 153 url: "https://github.com/oldOwner/repo.git", 154 host: "https://github.com", 155 repo: "repo.git", 156 remoteName: "origin", 157 expectedArgs: []string{"remote", "set-url", "origin", "https://github.com/newOwner/repo.git"}, 158 }, 159 } 160 161 executor := &MockExecutor{Debug: true} 162 cr := newChangeRemote("oldOwner", "newOwner", true, executor) 163 164 for _, test := range tests { 165 cr.processRemote(test.url, test.host, test.repo, test.remoteName) 166 167 if executor.Command != "git" { 168 t.Errorf("unexpected command received: expected %s, but got %s", "git", executor.Command) 169 } 170 171 if len(executor.Args) != len(test.expectedArgs) { 172 t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args) 173 } 174 175 for i, v := range executor.Args { 176 if v != test.expectedArgs[i] { 177 t.Errorf("unexpected args received: expected %v, but got %v", test.expectedArgs, executor.Args) 178 } 179 } 180 } 181 } 182 183 func Test_remoteInfo(t *testing.T) { 184 tests := []struct { 185 name string 186 remote string 187 expected []string 188 }{ 189 { 190 name: "SSH remote", 191 remote: "git@github.com:oldOwner/repo.git", 192 expected: []string{"git@github.com", "oldOwner", "repo.git"}, 193 }, 194 { 195 name: "HTTPS remote", 196 remote: "https://github.com/oldOwner/repo.git", 197 expected: []string{"https://github.com", "oldOwner", "repo.git"}, 198 }, 199 } 200 201 cr := newChangeRemote("oldOwner", "newOwner", false, &MockExecutor{Debug: false}) 202 203 for _, test := range tests { 204 t.Run(test.name, func(t *testing.T) { 205 host, owner, repo := cr.remoteInfo(test.remote) 206 if host != test.expected[0] || owner != test.expected[1] || repo != test.expected[2] { 207 t.Errorf("expected %v, but got %v, %v, %v", test.expected, host, owner, repo) 208 } 209 }) 210 } 211 }