Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Fix race condition and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
2opremio committed May 20, 2019
1 parent 5051b8d commit d90ec58
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
21 changes: 19 additions & 2 deletions git/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"os/exec"
"strings"
"sync"

"github.com/pkg/errors"
)
Expand Down Expand Up @@ -327,6 +328,23 @@ func traceGitCommand(args []string, config gitCmdConfig, stdOutAndStdErr string)
)
}

type threadSafeBuffer struct {
bytes.Buffer
sync.Mutex
}

func (b *threadSafeBuffer) Write(p []byte) (n int, err error) {
b.Lock()
defer b.Unlock()
return b.Buffer.Write(p)
}

func (b *threadSafeBuffer) ReadFrom(r io.Reader) (n int64, err error) {
b.Lock()
defer b.Unlock()
return b.Buffer.ReadFrom(r)
}

// execGitCmd runs a `git` command with the supplied arguments.
func execGitCmd(ctx context.Context, args []string, config gitCmdConfig) error {
c := exec.CommandContext(ctx, "git", args...)
Expand All @@ -335,8 +353,7 @@ func execGitCmd(ctx context.Context, args []string, config gitCmdConfig) error {
c.Dir = config.dir
}
c.Env = append(env(), config.env...)

stdOutAndStdErr := &bytes.Buffer{}
stdOutAndStdErr := &threadSafeBuffer{}
c.Stdout = stdOutAndStdErr
c.Stderr = stdOutAndStdErr
if config.out != nil {
Expand Down
9 changes: 4 additions & 5 deletions git/operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ func TestTraceGitCommand(t *testing.T) {
dir: "/tmp/flux-working628880789",
},
},
expected: `TRACE: command="git clone --branch master /tmp/flux-gitclone239583443 /tmp/flux-working628880789" out="" err="" dir="/tmp/flux-working628880789" env=""`,
expected: `TRACE: command="git clone --branch master /tmp/flux-gitclone239583443 /tmp/flux-working628880789" out="" dir="/tmp/flux-working628880789" env=""`,
},
{
name: "git rev-list",
Expand All @@ -333,7 +333,7 @@ func TestTraceGitCommand(t *testing.T) {
dir: "/tmp/flux-gitclone239583443",
},
},
expected: `TRACE: command="git rev-list --max-count 1 flux-sync --" out="b9d6a543acf8085ff6bed23fac17f8dc71bfcb66" err="" dir="/tmp/flux-gitclone239583443" env=""`,
expected: `TRACE: command="git rev-list --max-count 1 flux-sync --" out="b9d6a543acf8085ff6bed23fac17f8dc71bfcb66" dir="/tmp/flux-gitclone239583443" env=""`,
},
{
name: "git config email",
Expand All @@ -347,7 +347,7 @@ func TestTraceGitCommand(t *testing.T) {
dir: "/tmp/flux-working056923691",
},
},
expected: `TRACE: command="git config user.email [email protected]" out="" err="" dir="/tmp/flux-working056923691" env=""`,
expected: `TRACE: command="git config user.email [email protected]" out="" dir="/tmp/flux-working056923691" env=""`,
},
{
name: "git notes",
Expand All @@ -363,15 +363,14 @@ func TestTraceGitCommand(t *testing.T) {
},
out: "refs/notes/flux",
},
expected: `TRACE: command="git notes --ref flux get-ref" out="refs/notes/flux" err="" dir="/tmp/flux-working647148942" env=""`,
expected: `TRACE: command="git notes --ref flux get-ref" out="refs/notes/flux" dir="/tmp/flux-working647148942" env=""`,
},
}
for _, example := range examples {
actual := traceGitCommand(
example.input.args,
example.input.config,
example.input.out,
example.input.err,
)
assert.Equal(t, example.expected, actual)
}
Expand Down

0 comments on commit d90ec58

Please sign in to comment.