Skip to content

Commit

Permalink
test: added test for concurrent flag
Browse files Browse the repository at this point in the history
  • Loading branch information
lindell committed Dec 19, 2020
1 parent 4b3205c commit c5d2400
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 25 deletions.
10 changes: 10 additions & 0 deletions tests/scripts/changer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@ package main

import (
"bytes"
"flag"
"io/ioutil"
"time"
)

const fileName = "test.txt"

func main() {
duration := flag.String("sleep", "", "Time to sleep before running the script")
flag.Parse()

if *duration != "" {
d, _ := time.ParseDuration(*duration)
time.Sleep(d)
}

data, err := ioutil.ReadFile(fileName)
if err != nil {
panic(err)
Expand Down
21 changes: 21 additions & 0 deletions tests/setup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package tests

import (
"os"
"os/exec"
"testing"
)

func TestMain(m *testing.M) {
command := exec.Command("go", "build", "-o", "scripts/changer/main", "scripts/changer/main.go")
if err := command.Run(); err != nil {
panic(err)
}

command = exec.Command("go", "build", "-o", "scripts/printer/main", "scripts/printer/main.go")
if err := command.Run(); err != nil {
panic(err)
}

os.Exit(m.Run())
}
6 changes: 1 addition & 5 deletions tests/story_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tests

import (
"fmt"
"io/ioutil"
"os"
"path"
Expand All @@ -24,9 +23,6 @@ func TestStory(t *testing.T) {
defer os.RemoveAll(tmpDir)
assert.NoError(t, err)

workingDir, err := os.Getwd()
assert.NoError(t, err)

changeRepo := createRepo(t, "should-change", "i like apples")
changeRepo2 := createRepo(t, "should-change-2", "i like my apple")
noChangeRepo := createRepo(t, "should-not-change", "i like oranges")
Expand All @@ -43,7 +39,7 @@ func TestStory(t *testing.T) {
"--author-email", "test@example.com",
"-B", "custom-branch-name",
"-m", "test",
fmt.Sprintf(`go run %s`, path.Join(workingDir, "scripts/changer/main.go")),
"scripts/changer/main",
})
err = command.Execute()
assert.NoError(t, err)
Expand Down
102 changes: 82 additions & 20 deletions tests/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ import (
"os"
"path"
"testing"
"time"

"github.com/lindell/multi-gitter/cmd"
"github.com/lindell/multi-gitter/tests/vcmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type outputs struct {
type runData struct {
out string
logOut string
took time.Duration
}

func TestTable(t *testing.T) {
Expand All @@ -28,7 +30,7 @@ func TestTable(t *testing.T) {
vcCreate func(t *testing.T) *vcmock.VersionController // Can be used if advanced setup is needed for the vc

args []string
verify func(t *testing.T, vcMock *vcmock.VersionController, outputs outputs)
verify func(t *testing.T, vcMock *vcmock.VersionController, runData runData)

expectErr bool
}{
Expand All @@ -45,20 +47,50 @@ func TestTable(t *testing.T) {
"--author-email", "test@example.com",
"-B", "custom-branch-name",
"-m", "custom message",
fmt.Sprintf(`go run %s`, path.Join(workingDir, "scripts/changer/main.go")),
path.Join(workingDir, "scripts/changer/main"),
},
verify: func(t *testing.T, vcMock *vcmock.VersionController, outputs outputs) {
verify: func(t *testing.T, vcMock *vcmock.VersionController, runData runData) {
require.Len(t, vcMock.PullRequests, 1)
assert.Equal(t, "custom-branch-name", vcMock.PullRequests[0].Head)
assert.Equal(t, "custom message", vcMock.PullRequests[0].Title)

assert.Contains(t, outputs.logOut, "Running on 1 repositories")
assert.Contains(t, outputs.logOut, "Cloning and running script")
assert.Contains(t, outputs.logOut, "Change done, creating pull request")
assert.Contains(t, runData.logOut, "Running on 1 repositories")
assert.Contains(t, runData.logOut, "Cloning and running script")
assert.Contains(t, runData.logOut, "Change done, creating pull request")

assert.Equal(t, `Repositories with a successful run:
should-change
`, outputs.out)
`, runData.out)
},
},

{
name: "with go run",
vc: &vcmock.VersionController{
Repositories: []vcmock.Repository{
createRepo(t, "should-change", "i like apples"),
},
},
args: []string{
"run",
"--author-name", "Test Author",
"--author-email", "test@example.com",
"-B", "custom-branch-name",
"-m", "custom message",
fmt.Sprintf("go run %s", path.Join(workingDir, "scripts/changer/main.go")),
},
verify: func(t *testing.T, vcMock *vcmock.VersionController, runData runData) {
require.Len(t, vcMock.PullRequests, 1)
assert.Equal(t, "custom-branch-name", vcMock.PullRequests[0].Head)
assert.Equal(t, "custom message", vcMock.PullRequests[0].Title)

assert.Contains(t, runData.logOut, "Running on 1 repositories")
assert.Contains(t, runData.logOut, "Cloning and running script")
assert.Contains(t, runData.logOut, "Change done, creating pull request")

assert.Equal(t, `Repositories with a successful run:
should-change
`, runData.out)
},
},

Expand All @@ -76,11 +108,11 @@ func TestTable(t *testing.T) {
"-B", "custom-branch-name",
"--base-branch", "custom-base-branch",
"-m", "custom message",
fmt.Sprintf(`go run %s`, path.Join(workingDir, "scripts/changer/main.go")),
path.Join(workingDir, "scripts/changer/main"),
},
verify: func(t *testing.T, vcMock *vcmock.VersionController, outputs outputs) {
verify: func(t *testing.T, vcMock *vcmock.VersionController, runData runData) {
require.Len(t, vcMock.PullRequests, 0)
assert.Contains(t, outputs.logOut, `msg="couldn't find remote ref \"refs/heads/custom-base-branch\""`)
assert.Contains(t, runData.logOut, `msg="couldn't find remote ref \"refs/heads/custom-base-branch\""`)
},
},

Expand All @@ -104,9 +136,9 @@ func TestTable(t *testing.T) {
"-B", "custom-branch-name",
"--base-branch", "custom-base-branch",
"-m", "custom message",
fmt.Sprintf(`go run %s`, path.Join(workingDir, "scripts/changer/main.go")),
path.Join(workingDir, "scripts/changer/main"),
},
verify: func(t *testing.T, vcMock *vcmock.VersionController, outputs outputs) {
verify: func(t *testing.T, vcMock *vcmock.VersionController, runData runData) {
require.Len(t, vcMock.PullRequests, 1)
assert.Equal(t, "custom-base-branch", vcMock.PullRequests[0].Base)
assert.Equal(t, "custom-branch-name", vcMock.PullRequests[0].Head)
Expand All @@ -130,9 +162,9 @@ func TestTable(t *testing.T) {
"--author-email", "test@example.com",
"-m", "custom message",
"-r", "reviewer1,reviewer2",
fmt.Sprintf(`go run %s`, path.Join(workingDir, "scripts/changer/main.go")),
path.Join(workingDir, "scripts/changer/main"),
},
verify: func(t *testing.T, vcMock *vcmock.VersionController, outputs outputs) {
verify: func(t *testing.T, vcMock *vcmock.VersionController, runData runData) {
require.Len(t, vcMock.PullRequests, 1)
assert.Len(t, vcMock.PullRequests[0].Reviewers, 2)
assert.Contains(t, vcMock.PullRequests[0].Reviewers, "reviewer1")
Expand All @@ -154,9 +186,9 @@ func TestTable(t *testing.T) {
"-m", "custom message",
"-r", "reviewer1,reviewer2,reviewer3",
"--max-reviewers", "2",
fmt.Sprintf(`go run %s`, path.Join(workingDir, "scripts/changer/main.go")),
path.Join(workingDir, "scripts/changer/main"),
},
verify: func(t *testing.T, vcMock *vcmock.VersionController, outputs outputs) {
verify: func(t *testing.T, vcMock *vcmock.VersionController, runData runData) {
require.Len(t, vcMock.PullRequests, 1)
assert.Len(t, vcMock.PullRequests[0].Reviewers, 2)
},
Expand All @@ -176,14 +208,41 @@ func TestTable(t *testing.T) {
"-m", "custom message",
"-B", "custom-branch-name",
"--dry-run",
fmt.Sprintf(`go run %s`, path.Join(workingDir, "scripts/changer/main.go")),
path.Join(workingDir, "scripts/changer/main"),
},
verify: func(t *testing.T, vcMock *vcmock.VersionController, outputs outputs) {
verify: func(t *testing.T, vcMock *vcmock.VersionController, runData runData) {
require.Len(t, vcMock.PullRequests, 0)
assert.True(t, branchExist(t, vcMock.Repositories[0].Path, "master"))
assert.False(t, branchExist(t, vcMock.Repositories[0].Path, "custom-branch-name"))
},
},

{
name: "parallel",
vc: &vcmock.VersionController{
Repositories: []vcmock.Repository{
createRepo(t, "should-change-1", "i like apples"),
createRepo(t, "should-change-2", "i like apples"),
createRepo(t, "should-change-3", "i like apples"),
createRepo(t, "should-change-4", "i like apples"),
createRepo(t, "should-change-5", "i like apples"),
createRepo(t, "should-change-6", "i like apples"),
},
},
args: []string{
"run",
"--author-name", "Test Author",
"--author-email", "test@example.com",
"-m", "custom message",
"-B", "custom-branch-name",
"-C", "3",
fmt.Sprintf("%s -sleep 100ms", path.Join(workingDir, "scripts/changer/main")),
},
verify: func(t *testing.T, vcMock *vcmock.VersionController, runData runData) {
require.Len(t, vcMock.PullRequests, 6)
require.Less(t, runData.took.Milliseconds(), int64(600))
},
},
}

for _, test := range tests {
Expand All @@ -210,7 +269,9 @@ func TestTable(t *testing.T) {
"--log-file", logFile.Name(),
"--output", outFile.Name(),
))
before := time.Now()
err = command.Execute()
took := time.Since(before)
if test.expectErr {
assert.Error(t, err)
} else {
Expand All @@ -223,9 +284,10 @@ func TestTable(t *testing.T) {
outData, err := ioutil.ReadAll(outFile)
assert.NoError(t, err)

test.verify(t, vc, outputs{
test.verify(t, vc, runData{
logOut: string(logData),
out: string(outData),
took: took,
})
})
}
Expand Down

0 comments on commit c5d2400

Please sign in to comment.