Skip to content

Commit

Permalink
Merge branch 'releases/v0.3' of github.com:tamada/rrh into releases/v0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
tamada committed Apr 3, 2019
2 parents 60794c4 + 945638e commit 57b3757
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 86 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ before_script:
- export TZ=Asia/Tokyo

script:
- make update test
- make test
- $GOPATH/bin/goveralls -coverprofile=coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN
4 changes: 2 additions & 2 deletions add/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestAdd(t *testing.T) {

os.Setenv(common.RrhConfigPath, "../testdata/config.json")
for _, testcase := range testcases {
common.Rollback("../testdata/tmp.json", func() {
common.Rollback("../testdata/tmp.json", "../testdata/config.json", func() {
var command, _ = AddCommandFactory()
var status = command.Run(testcase.args)

Expand Down Expand Up @@ -102,7 +102,7 @@ func TestAdd(t *testing.T) {

func TestAddToDifferentGroup(t *testing.T) {
os.Setenv(common.RrhConfigPath, "../testdata/config.json")
common.Rollback("../testdata/tmp.json", func() {
common.Rollback("../testdata/tmp.json", "../testdata/config.json", func() {
var command, _ = AddCommandFactory()
command.Run([]string{"../testdata/fibonacci"})
command.Run([]string{"-g", "group1", "../testdata/fibonacci"})
Expand Down
14 changes: 3 additions & 11 deletions clone/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@ func cleanup(dirs []string) {
}
}

func rollback(f func()) {
var config = common.OpenConfig()
var db, _ = common.Open(config)
defer db.StoreAndClose()

f()
}

func validate(repo common.Repository, repoID string, repoPath string) string {
var dir, _ = filepath.Abs(repoPath)
if repo.ID != repoID || repo.Path != dir {
Expand All @@ -49,7 +41,7 @@ func TestCloneCommand_MultipleProjects(t *testing.T) {
os.Setenv(common.RrhConfigPath, "../testdata/config.json")
os.Setenv(common.RrhDatabasePath, "../testdata/tmp.json")

rollback(func() {
common.Rollback("../testdata/tmp.json", "../testdata/config.json", func() {
var clone, _ = CloneCommandFactory()
clone.Run([]string{"-d", "../testdata/hoge", "-g", "not-exist-group",
"../testdata/helloworld",
Expand Down Expand Up @@ -82,7 +74,7 @@ func TestCloneCommand_MultipleProjects(t *testing.T) {
func TestCloneCommand_Run(t *testing.T) {
os.Setenv(common.RrhConfigPath, "../testdata/config.json")
os.Setenv(common.RrhDatabasePath, "../testdata/tmp.json")
rollback(func() {
common.Rollback("../testdata/tmp.json", "../testdata/config.json", func() {
var clone, _ = CloneCommandFactory()
clone.Run([]string{"https://htamada@bitbucket.org/htamada/helloworld.git"})
defer cleanup([]string{"./helloworld"})
Expand All @@ -105,7 +97,7 @@ func TestCloneCommand_Run(t *testing.T) {
func TestCloneCommand_SpecifyingId(t *testing.T) {
os.Setenv(common.RrhConfigPath, "../testdata/config.json")
os.Setenv(common.RrhDatabasePath, "../testdata/tmp.json")
rollback(func() {
common.Rollback("../testdata/tmp.json", "../testdata/config.json", func() {
var clone, _ = CloneCommandFactory()
clone.Run([]string{"-d", "../testdata/newid", "../testdata/helloworld"})
defer cleanup([]string{"../testdata/newid"})
Expand Down
18 changes: 15 additions & 3 deletions common/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,31 @@ import (
"bytes"
"io"
"os"
"strings"
)

/*
Rollback rollback database after executing f.
Rollback rollbacks database after executing function f.
*/
func Rollback(dbpath string, f func()) {
func Rollback(dbpath, configPath string, f func()) {
os.Setenv(RrhConfigPath, configPath)
os.Setenv(RrhDatabasePath, dbpath)
var config = OpenConfig()
var db, _ = Open(config)
defer db.StoreAndClose()

f()
}

db.StoreAndClose()
/*
ReplaceNewline trims spaces and converts the return codes in `originalString` to `replaceTo` string.
*/
func ReplaceNewline(originalString, replaceTo string) string {
return strings.NewReplacer(
"\r\n", replaceTo,
"\r", replaceTo,
"\n", replaceTo,
).Replace(strings.TrimSpace(originalString))
}

/*
Expand Down
2 changes: 1 addition & 1 deletion export/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestImport(t *testing.T) {

for _, testcase := range testcases {
os.Setenv(common.RrhConfigPath, "../testdata/config.json")
common.Rollback("../testdata/tmp.json", func() {
common.Rollback("../testdata/tmp.json", "../testdata/config.json", func() {
var command, _ = ImportCommandFactory()
var statusCode = command.Run(testcase.args)
if statusCode != testcase.statusCode {
Expand Down
33 changes: 12 additions & 21 deletions group/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,6 @@ import (
"github.com/tamada/rrh/common"
)

func rollback(dbpath string, f func()) {
os.Setenv(common.RrhConfigPath, "../testdata/config.json")
os.Setenv(common.RrhDatabasePath, dbpath)
var config = common.OpenConfig()
var db, _ = common.Open(config)

f()

db.StoreAndClose()
}

func Example() {
os.Setenv(common.RrhDatabasePath, "../testdata/tmp.json")
var gc, _ = GroupCommandFactory()
Expand Down Expand Up @@ -114,7 +103,7 @@ func TestAddGroup(t *testing.T) {
{[]string{"add"}, 3, []groupChecker{}},
}
for _, testcase := range testcases {
rollback("../testdata/tmp.json", func() {
common.Rollback("../testdata/tmp.json", "../testdata/config.json", func() {
var gac, _ = GroupCommandFactory()
if val := gac.Run(testcase.args); val != testcase.statusCode {
t.Errorf("%v: test failed, wont: %d, got: %d", testcase.args, testcase.statusCode, val)
Expand Down Expand Up @@ -150,13 +139,15 @@ func TestUpdateGroupFailed(t *testing.T) {
{updateOptions{"newName", "desc", "omitList", "target"}, true},
}
for _, testcase := range testcases {
var guc = groupUpdateCommand{}
var config = common.OpenConfig()
var db, _ = common.Open(config)
var err = guc.updateGroup(db, &testcase.opt)
if (err != nil) != testcase.errFlag {
t.Errorf("%v: test failed: err wont: %v, got: %v: err (%v)", testcase.opt, testcase.errFlag, !testcase.errFlag, err)
}
common.Rollback("../testdata/tmp.json", "../testdata/config.json", func() {
var guc = groupUpdateCommand{}
var config = common.OpenConfig()
var db, _ = common.Open(config)
var err = guc.updateGroup(db, &testcase.opt)
if (err != nil) != testcase.errFlag {
t.Errorf("%v: test failed: err wont: %v, got: %v: err (%v)", testcase.opt, testcase.errFlag, !testcase.errFlag, err)
}
})
}
}

Expand Down Expand Up @@ -188,7 +179,7 @@ func TestUpdateGroup(t *testing.T) {
{[]string{"update", "group1", "group4"}, 1, []groupChecker{}, []relation{}},
}
for _, testcase := range testcases {
rollback("../testdata/tmp.json", func() {
common.Rollback("../testdata/tmp.json", "../testdata/config.json", func() {
var guc, _ = GroupCommandFactory()
if val := guc.Run(testcase.args); val != testcase.statusCode {
t.Errorf("%v: group update failed status code wont: %d, got: %d", testcase.args, testcase.statusCode, val)
Expand Down Expand Up @@ -231,7 +222,7 @@ func TestRemoveGroup(t *testing.T) {
{[]string{"rm"}, 1, []groupChecker{}},
}
for _, testcase := range testcases {
rollback("../testdata/tmp.json", func() {
common.Rollback("../testdata/tmp.json", "../testdata/config.json", func() {
var grc, _ = GroupCommandFactory()
if val := grc.Run(testcase.args); val != testcase.statusCode {
t.Errorf("%v: group remove failed: wont: %d, got: %d", testcase.args, testcase.statusCode, val)
Expand Down
10 changes: 4 additions & 6 deletions list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package list
import (
"fmt"
"os"
"strings"
"testing"

"github.com/tamada/rrh/common"
Expand Down Expand Up @@ -47,10 +46,10 @@ func TestRunByCsvOutput(t *testing.T) {
var list, _ = ListCommandFactory()
list.Run([]string{"--all-entries", "--csv"})
})
result = strings.TrimSpace(result)
var want = "group1,desc1,repo1,path1\ngroup3,desc3,repo2,path2"
result = common.ReplaceNewline(result, "&")
var want = "group1,desc1,repo1,path1&group3,desc3,repo2,path2"
if result != want {
t.Errorf("result did not match\ngot: %s\nwont: %s", result, want)
t.Errorf("result did not match, wont: %s, got: %s", want, result)
}
}

Expand All @@ -71,8 +70,7 @@ func TestSimpleResults(t *testing.T) {
t.Errorf("%v: status code did not match: wont: %d, got: %d", tc.args, tc.status, status)
}
})
result = strings.TrimSpace(result)
result = strings.Replace(result, "\n", ",", -1)
result = common.ReplaceNewline(result, ",")
if result != tc.result {
t.Errorf("%v: result did not match: wont: %s, got: %s", tc.args, tc.result, result)
}
Expand Down
10 changes: 1 addition & 9 deletions move/move_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ import (
"github.com/tamada/rrh/common"
)

func rollback(dbpath string, f func()) {
var db = open(dbpath)

f()

db.StoreAndClose()
}

func open(jsonName string) *common.Database {
os.Setenv(common.RrhConfigPath, "../testdata/config.json")
os.Setenv(common.RrhDatabasePath, fmt.Sprintf("../testdata/%s", jsonName))
Expand Down Expand Up @@ -83,7 +75,7 @@ func TestMoveCommand(t *testing.T) {
{"group1", "repo1", false}}},
}
for _, item := range cases {
rollback("tmp.json", func() {
common.Rollback("../testdata/tmp.json", "../testdata/config.json", func() {
var mv, _ = MoveCommandFactory()
mv.Run(item.args)
var db = open("tmp.json")
Expand Down
4 changes: 1 addition & 3 deletions path/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package path

import (
"os"
"strings"
"testing"

"github.com/tamada/rrh/common"
Expand Down Expand Up @@ -54,8 +53,7 @@ func TestPathCommand(t *testing.T) {
}
})
if tc.status == 0 {
output = strings.TrimSpace(output)
output = strings.Replace(output, "\n", ",", -1)
output = common.ReplaceNewline(output, ",")
if output != tc.results {
t.Errorf("%v: output did not match: wont: %s, got: %s", tc.args, tc.results, output)
}
Expand Down
65 changes: 36 additions & 29 deletions remove/remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,43 +16,50 @@ func open(jsonName string) *common.Database {
}

func ExampleRemoveCommand_Run() {
var db = open("tmp.json")
os.Setenv(common.RrhDatabasePath, "../testdata/tmp.json")
var rm, _ = RemoveCommandFactory()
rm.Run([]string{"-v", "group2", "repo1"})
common.Rollback("../testdata/tmp.json", "../testdata/config.json", func() {
var rm, _ = RemoveCommandFactory()
rm.Run([]string{"-v", "group2", "repo1"})
})
// Output: group2: group removed
// repo1: repository removed

db.StoreAndClose()
}

func TestRemoveCommandUnknownGroupAndRepository(t *testing.T) {
var db = open("tmp.json")

var rm = RemoveCommand{}
var err = rm.executeRemove(db, "not_exist_group_and_repository")
if err.Error() != "not_exist_group_and_repository: not found in repositories and groups" {
t.Error("not exist group and repository found!?")
}

db.StoreAndClose()
common.Rollback("../testdata/tmp.json", "../testdata/config.json", func() {
var db = open("tmp.json")
var rm = RemoveCommand{}
var err = rm.executeRemove(db, "not_exist_group_and_repository")
if err.Error() != "not_exist_group_and_repository: not found in repositories and groups" {
t.Error("not exist group and repository found!?")
}
})
}

func TestRemoveRepository(t *testing.T) {
var db = open("tmp.json")
var rm = RemoveCommand{&removeOptions{}}
if err := rm.executeRemoveRepository(db, "unknown-repo"); err == nil {
t.Error("unknown-repo: found")
}
var err = rm.executeRemoveRepository(db, "repo1")
if err != nil || len(db.Repositories) != 1 {
t.Errorf("repo1 did not remove?: %s", err.Error())
}
if len(db.Groups) != 3 {
t.Error("the number of groups changed")
}
if db.ContainsCount("group1") != 0 || db.ContainsCount("group2") != 0 {
t.Error("Unrelate repo was failed?")
var testcases = []struct {
repositoryName string
removeSuccess bool
belongedGroup string
repoCountInGroup int
}{
{"unknown-repo", false, "", 0},
{"repo1", true, "group1", 0},
}
for _, tc := range testcases {
common.Rollback("../testdata/tmp.json", "../testdata/config.json", func() {
var db = open("tmp.json")
var rm = RemoveCommand{&removeOptions{}}
var err = rm.executeRemoveRepository(db, tc.repositoryName)
if (err == nil) != tc.removeSuccess {
t.Errorf("%v: remove result not match: wont: %v, got: %v", tc.repositoryName, tc.removeSuccess, !tc.removeSuccess)
}
if tc.belongedGroup != "" {
var count = db.ContainsCount(tc.belongedGroup)
if count != tc.repoCountInGroup {
t.Errorf("%v: repo count in group %s did not match: wont: %d, got: %d", tc.repositoryName, tc.belongedGroup, tc.repoCountInGroup, count)
}
}
})
}
}

Expand Down

0 comments on commit 57b3757

Please sign in to comment.