Skip to content

Commit

Permalink
ref #13 update unit tests for increasing the coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
tamada committed Mar 24, 2019
1 parent 03e02c2 commit 56a1a23
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 40 deletions.
27 changes: 14 additions & 13 deletions common/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,21 @@ func (db *Database) FindGroup(groupID string) *Group {
}

func sortIfNeeded(db *Database) {
if db.Config.GetValue(RrhSortOnUpdating) == "yes" {
sort.Slice(db.Repositories, func(i, j int) bool {
return db.Repositories[i].ID < db.Repositories[j].ID
})
sort.Slice(db.Groups, func(i, j int) bool {
return db.Groups[i].Name < db.Groups[j].Name
})
sort.Slice(db.Relations, func(i, j int) bool {
if db.Relations[i].GroupName == db.Relations[j].GroupName {
return db.Relations[i].RepositoryID < db.Relations[j].RepositoryID
}
return db.Relations[i].GroupName < db.Relations[j].GroupName
})
if db.Config.GetValue(RrhSortOnUpdating) != "true" {
return
}
sort.Slice(db.Repositories, func(i, j int) bool {
return db.Repositories[i].ID < db.Repositories[j].ID
})
sort.Slice(db.Groups, func(i, j int) bool {
return db.Groups[i].Name < db.Groups[j].Name
})
sort.Slice(db.Relations, func(i, j int) bool {
if db.Relations[i].GroupName == db.Relations[j].GroupName {
return db.Relations[i].RepositoryID < db.Relations[j].RepositoryID
}
return db.Relations[i].GroupName < db.Relations[j].GroupName
})
}

/*
Expand Down
17 changes: 15 additions & 2 deletions common/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

func openDatabase() *Database {
os.Setenv(RrhDatabasePath, "../testdata/database.json")
os.Setenv(RrhConfigPath, "../testdata/config.json")
var config = OpenConfig()
var db, _ = Open(config)
return db
Expand Down Expand Up @@ -45,6 +46,9 @@ func TestOpenNullDatabase(t *testing.T) {
if len(db.Groups) != 0 {
t.Error("null db have no group entries")
}
if len(db.Relations) != 0 {
t.Error("null db have no group entries")
}
}

func TestStore(t *testing.T) {
Expand Down Expand Up @@ -72,7 +76,10 @@ func TestStore(t *testing.T) {
if !db2.HasRepository("repo2") {
t.Error("repo2 not found!")
}
os.Remove("./testdata/tmp.json")
if !db2.HasRelation("group1", "repo1") {
t.Error("group1 does not relate with repo1")
}

}

func TestPrune(t *testing.T) {
Expand All @@ -90,6 +97,9 @@ func TestPrune(t *testing.T) {
if db.HasRepository("repo2") {
t.Error("repo2 was not pruned")
}
if !db.HasRelation("group1", "repo1") {
t.Error("relation is removed?")
}
}

func TestDeleteGroup(t *testing.T) {
Expand Down Expand Up @@ -131,7 +141,6 @@ func TestDeleteRepository(t *testing.T) {
}

func TestUnrelate(t *testing.T) {
t.Skip("db.Unrelate never failed.")
var db = openDatabase()

db.CreateRepository("somerepo", "unknown", []Remote{})
Expand All @@ -141,6 +150,10 @@ func TestUnrelate(t *testing.T) {

db.Unrelate("group2", "Rrh")
db.Unrelate("no-group", "rrh")

if !db.HasRelation("group2", "somerepo") {
t.Error("group2 does not relate with somerepo")
}
}

func TestCreateRepository(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion remove/remove_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (rm *RemoveCommand) Run(args []string) int {
}
}
if result == 0 {
if config.GetValue(common.RrhAutoDeleteGroup) == "yes" {
if config.GetValue(common.RrhAutoDeleteGroup) == "true" {
db.Prune()
}
db.StoreAndClose()
Expand Down
35 changes: 16 additions & 19 deletions status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ func isTarget(options *statusOptions, ref *plumbing.Reference) bool {
return refName.String() == "HEAD" || options.isRemoteTarget(refName) || options.isBranchTarget(refName)
}

func (status *StatusCommand) findLocalBranches(name repo, r *git.Repository, options *statusOptions) ([]StatusResult, error) {
func (status *StatusCommand) findLocalBranches(name repo, r *git.Repository) ([]StatusResult, error) {
var results = []StatusResult{}
var iter, err2 = r.References()
if err2 != nil {
return results, err2
}

iter.ForEach(func(ref *plumbing.Reference) error {
if isTarget(options, ref) {
if isTarget(status.Options, ref) {
var result, err = status.lastCommitOnLocalBranch(name, r, ref)
if err != nil {
return err
Expand All @@ -82,7 +82,7 @@ func (status *StatusCommand) findLocalBranches(name repo, r *git.Repository, opt
return results, nil
}

func (status *StatusCommand) findTime(path string, repoID string, db *common.Database) time.Time {
func (status *StatusCommand) findTime(db *common.Database, path string, repoID string) time.Time {
var repo = db.FindRepository(repoID)
var absPath = common.ToAbsolutePath(repo.Path, db.Config)
var target = filepath.Join(absPath, path)
Expand All @@ -102,7 +102,7 @@ func (status *StatusCommand) findTime(path string, repoID string, db *common.Dat
}

func (status *StatusCommand) flagChecker(db *common.Database, rname string, key string, lastModified *time.Time) *time.Time {
var time = status.findTime(key, rname, db)
var time = status.findTime(db, key, rname)
if lastModified == nil || time.After(*lastModified) {
return &time
}
Expand Down Expand Up @@ -141,7 +141,7 @@ func (status *StatusCommand) findWorktree(name repo, r *git.Repository, db *comm
return &StatusResult{name.gname, name.rname, "WORKTREE", lastModified, status.generateMessage(staging, changesNotAdded)}, nil
}

func (status *StatusCommand) executeStatusOnRepository(db *common.Database, name repo, options *statusOptions) ([]StatusResult, error) {
func (status *StatusCommand) executeStatusOnRepository(db *common.Database, name repo) ([]StatusResult, error) {
var r, err = status.openRepository(db, name.rname)
if err != nil {
return nil, fmt.Errorf("%s: %s", name.rname, err.Error())
Expand All @@ -151,7 +151,7 @@ func (status *StatusCommand) executeStatusOnRepository(db *common.Database, name
if err2 != nil {
return nil, err2
}
var localBranches, err3 = status.findLocalBranches(name, r, options)
var localBranches, err3 = status.findLocalBranches(name, r)
if err3 != nil {
return nil, err3
}
Expand All @@ -161,36 +161,33 @@ func (status *StatusCommand) executeStatusOnRepository(db *common.Database, name
return results, nil
}

func (status *StatusCommand) executeStatus(db *common.Database, name string, options *statusOptions) ([]StatusResult, []error) {
func (status *StatusCommand) executeStatus(db *common.Database, name string) ([]StatusResult, []error) {
if db.HasRepository(name) {
var results, err = status.executeStatusOnRepository(db, repo{"unknown-group", name}, options)
var results, err = status.executeStatusOnRepository(db, repo{"unknown-group", name})
if err != nil {
return results, []error{err}
}
return results, nil
} else if db.HasGroup(name) {
return status.executeStatusOnGroup(db, name, options)
return status.executeStatusOnGroup(db, name)
}
return nil, []error{fmt.Errorf("%s: group and repository not found", name)}
}

func (status *StatusCommand) executeStatusOnGroup(db *common.Database, groupName string, options *statusOptions) ([]StatusResult, []error) {
func (status *StatusCommand) executeStatusOnGroup(db *common.Database, groupName string) ([]StatusResult, []error) {
var group = db.FindGroup(groupName)
if group == nil {
return nil, []error{fmt.Errorf("%s: group not found", groupName)}
}
var errors = []error{}
var results = []StatusResult{}
for _, relation := range db.Relations {
if relation.GroupName == groupName {
var sr, err = status.executeStatusOnRepository(db, repo{groupName, relation.RepositoryID}, options)
if err != nil {
errors = append(errors, err)
} else {
results = append(results, sr...)
}
for _, repoID := range db.FindRelationsOfGroup(groupName) {
var sr, err = status.executeStatusOnRepository(db, repo{groupName, repoID})
if err != nil {
errors = append(errors, err)
} else {
results = append(results, sr...)
}
}

return results, errors
}
12 changes: 7 additions & 5 deletions status/status_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
StatusCommand represents a command.
*/
type StatusCommand struct {
Options *statusOptions
}

type statusOptions struct {
Expand All @@ -34,7 +35,7 @@ func (options *statusOptions) isBranchTarget(name plumbing.ReferenceName) bool {
StatusCommandFactory returns an instance of the StatusCommand.
*/
func StatusCommandFactory() (cli.Command, error) {
return &StatusCommand{}, nil
return &StatusCommand{&statusOptions{false, false, false, []string{}}}, nil
}

/*
Expand Down Expand Up @@ -92,16 +93,16 @@ func (status *StatusCommand) printResult(results []StatusResult, config *common.
}
}

func (status *StatusCommand) runStatus(db *common.Database, arg string, options *statusOptions) int {
func (status *StatusCommand) runStatus(db *common.Database, arg string) int {
var errorFlag = 0
var result, err = status.executeStatus(db, arg, options)
var result, err = status.executeStatus(db, arg)
if len(err) != 0 {
for _, item := range err {
fmt.Println(item.Error())
errorFlag = 1
}
} else {
if options.csv {
if status.Options.csv {
status.printResultInCsv(result, db.Config)
} else {
status.printResult(result, db.Config)
Expand All @@ -127,7 +128,7 @@ func (status *StatusCommand) Run(args []string) int {
}
var errorFlag = 0
for _, arg := range options.args {
errorFlag += status.runStatus(db, arg, options)
errorFlag += status.runStatus(db, arg)
}

return errorFlag
Expand All @@ -150,6 +151,7 @@ func (status *StatusCommand) parse(args []string, config *common.Config) (*statu
if len(options.args) == 0 {
options.args = []string{config.GetValue(common.RrhDefaultGroupName)}
}
status.Options = &options
return &options, nil
}

Expand Down

0 comments on commit 56a1a23

Please sign in to comment.