Use git client in extension manager#6547
Conversation
| return g.client.Clone(context.Background(), cloneURL, cloneArgs, mods...) | ||
| } | ||
|
|
||
| func (g *gitExecuter) CommandOutput(args []string, mods ...git.CommandModifier) ([]byte, error) { |
There was a problem hiding this comment.
Combine Command() and Output() methods for easier testing.
8c3028b to
0d2a30e
Compare
| // git init should create the directory named by argument | ||
| if len(args) > 2 && strings.HasPrefix(strings.Join(args, " "), "git init") { | ||
| dir := args[len(args)-1] | ||
| if !strings.HasPrefix(dir, "-") { | ||
| if err := os.MkdirAll(dir, 0755); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
git init calls are now mocked out so never reach here.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
0d2a30e to
797ef46
Compare
| defer reg.Verify(t) | ||
| client := http.Client{Transport: ®} | ||
| m := newTestManager(tempDir, &client, ios) | ||
| gc := &gitExecuter{client: &git.Client{}} |
There was a problem hiding this comment.
This test uses run.Stub() which git.Client abides by, so there was no need to use the mockGitClient here.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
|
||
| func TestManager_Create(t *testing.T) { | ||
| chdirTemp(t) | ||
| err := os.MkdirAll("gh-test", 0755) |
There was a problem hiding this comment.
Manually create the extension directory now, but we have assertions that git init is executed which will do this folder creation in production.
|
|
||
| localSha, err := cmd.Output() | ||
| dir := filepath.Join(m.installDir(), extension) | ||
| localSha, err := m.gitClient.CommandOutput([]string{"rev-parse", "HEAD"}, git.WithRepoDir(dir)) |
There was a problem hiding this comment.
Wild idea: instead of passing repo dir to each individual invocation, and thus having to modify underlying GitClient methods to accept ...mods as argument, how about adding the ability to create an instance of GitClient for a specific repo? Something like:
scopedClient := gitClient.ForRepo(dir)
scopedClient.Config("remote.origin.url")
scopedClient.CheckoutBranch("branch")There was a problem hiding this comment.
@mislav While I think that would work, it seems more difficult to test.
func (m *Manager) getCurrentVersion(extension string) string {
dir := filepath.Join(m.installDir(), extension)
scopedClient := m.gitClient.ForRepo(dir)
localSha, err := scopedClient.CommandOutput([]string{"rev-parse", "HEAD"})
if err != nil {
return ""
}
return string(bytes.TrimSpace(localSha))
}
func TestGetCurrentVersion(t *testing.T) {
tempDir := t.TempDir()
dir := filepath.Join(tempDir, "extensions", "gh-hello")
gc1 := &mockGitClient{}
gc2 := &mockGitClient{}
// We would require two mock git clients...
gc1.On("ForRepo", dir).Return(gc2).Once()
gc2.On("CommandOutput", []string{"rev-parse", "HEAD"}).Return("", nil).Once()
m := newTestManager(tempDir, nil, gc1, nil)
version := m.getCurrentVersion("gh-hello")
assert.Equal(t, 1, version)
}There was a problem hiding this comment.
I went ahead and implemented this, let me know what you think of the changes.
cc7d27e to
6eff8a9
Compare
b7a73b2 to
347180e
Compare
347180e to
0dac256
Compare
This PR changes the
ExtensionManagerto utilize the newgit.Client. It required a bit of refactoring, mostly creating agitClientinterface for just theExtensionManagerto use, in order to make everything testable. Overall functionality ofExtensionManagershould not change at all.