Skip to content
This repository has been archived by the owner on Apr 26, 2021. It is now read-only.

Commit

Permalink
repository: improve error reporting on validation failures
Browse files Browse the repository at this point in the history
Related to #183.
  • Loading branch information
Francisco Souza committed Feb 19, 2015
1 parent c6574ef commit ee6ea9e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
14 changes: 11 additions & 3 deletions repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,14 @@ func (r *Repository) isValid() (bool, error) {
panic(e)
}
if !m {
return false, errors.New("Validation Error: repository name is not valid")
return false, &InvalidRepositoryError{message: "repository name is not valid"}
}
absPath, err := filepath.Abs(barePath(r.Name))
if err != nil || !strings.HasPrefix(absPath, bare) {
return false, errors.New("Validation Error: repository name is not valid")
return false, &InvalidRepositoryError{message: "repository name is not valid"}
}
if len(r.Users) == 0 {
return false, errors.New("Validation Error: repository should have at least one user")
return false, &InvalidRepositoryError{message: "repository should have at least one user"}
}
return true, nil
}
Expand Down Expand Up @@ -910,3 +910,11 @@ func CommitZip(repo string, z *multipart.FileHeader, c GitCommit) (*Ref, error)
func GetLogs(repo, hash string, total int, path string) (*GitHistory, error) {
return retriever().GetLogs(repo, hash, total, path)
}

type InvalidRepositoryError struct {
message string
}

func (err *InvalidRepositoryError) Error() string {
return err.message
}
34 changes: 18 additions & 16 deletions repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,9 @@ func (s *S) TestNewPublicRepository(c *check.C) {
func (s *S) TestNewBreaksOnValidationError(c *check.C) {
_, err := New("", []string{"smeagol"}, []string{""}, false)
c.Check(err, check.NotNil)
expected := "Validation Error: repository name is not valid"
got := err.Error()
c.Assert(got, check.Equals, expected)
e, ok := err.(*InvalidRepositoryError)
c.Assert(ok, check.Equals, true)
c.Assert(e.message, check.Equals, "repository name is not valid")
}

func (s *S) TestNewDuplicate(c *check.C) {
Expand Down Expand Up @@ -284,44 +284,46 @@ func (s *S) TestRepositoryIsNotValidWithoutAName(c *check.C) {
v, err := r.isValid()
c.Assert(v, check.Equals, false)
c.Check(err, check.NotNil)
got := err.Error()
expected := "Validation Error: repository name is not valid"
c.Assert(got, check.Equals, expected)
e, ok := err.(*InvalidRepositoryError)
c.Assert(ok, check.Equals, true)
c.Assert(e.message, check.Equals, "repository name is not valid")
}

func (s *S) TestRepositoryIsNotValidWithInvalidName(c *check.C) {
r := Repository{Name: "foo bar", Users: []string{"gollum"}, IsPublic: true}
v, err := r.isValid()
c.Assert(v, check.Equals, false)
c.Check(err, check.NotNil)
got := err.Error()
expected := "Validation Error: repository name is not valid"
c.Assert(got, check.Equals, expected)
e, ok := err.(*InvalidRepositoryError)
c.Assert(ok, check.Equals, true)
c.Assert(e.message, check.Equals, "repository name is not valid")
}

func (s *S) TestRepositoryShoudBeInvalidWIthoutAnyUsers(c *check.C) {
r := Repository{Name: "foo_bar", Users: []string{}, IsPublic: true}
v, err := r.isValid()
c.Assert(v, check.Equals, false)
c.Assert(err, check.NotNil)
got := err.Error()
expected := "Validation Error: repository should have at least one user"
c.Assert(got, check.Equals, expected)
e, ok := err.(*InvalidRepositoryError)
c.Assert(ok, check.Equals, true)
c.Assert(e.message, check.Equals, "repository should have at least one user")
}

func (s *S) TestRepositoryShoudBeInvalidWIthInvalidNamespace(c *check.C) {
r := Repository{Name: "../repositories", Users: []string{}}
v, err := r.isValid()
c.Assert(v, check.Equals, false)
c.Assert(err, check.NotNil)
expected := "^Validation Error: repository name is not valid$"
c.Assert(err, check.ErrorMatches, expected)
e, ok := err.(*InvalidRepositoryError)
c.Assert(ok, check.Equals, true)
c.Assert(e.message, check.Equals, "repository name is not valid")
r = Repository{Name: "../../repositories", Users: []string{}}
v, err = r.isValid()
c.Assert(v, check.Equals, false)
c.Assert(err, check.NotNil)
expected = "^Validation Error: repository name is not valid$"
c.Assert(err, check.ErrorMatches, expected)
e, ok = err.(*InvalidRepositoryError)
c.Assert(ok, check.Equals, true)
c.Assert(e.message, check.Equals, "repository name is not valid")
}

func (s *S) TestRepositoryAcceptsValidNamespaces(c *check.C) {
Expand Down

0 comments on commit ee6ea9e

Please sign in to comment.