grim/govcs

11a3dcfb973b
Parents 044d89c67b9a
Children 77caf60cb0bf
Return an error if a repository could not be found
  • +7 -3
    govcs.go
  • +13 -2
    govcs_test.go
  • --- a/govcs.go Tue Dec 05 21:19:23 2017 -0600
    +++ b/govcs.go Tue Dec 05 21:55:16 2017 -0600
    @@ -18,6 +18,8 @@
    package govcs
    import (
    + "errors"
    +
    "bitbucket.org/rw_grim/govcs/git"
    "bitbucket.org/rw_grim/govcs/hg"
    "bitbucket.org/rw_grim/govcs/vcs"
    @@ -30,17 +32,19 @@
    git.Detect,
    hg.Detect,
    }
    +
    + ErrNoRepositoryFound = errors.New("no repository found")
    )
    // Detect will try to determine what version control system is in use in the
    // given working directory and return a vcs.VCS instance for it.
    -func Detect(wd string) vcs.VCS {
    +func Detect(wd string) (vcs.VCS, error) {
    for _, detector := range detectors {
    inst := detector(wd)
    if inst != nil {
    - return inst
    + return inst, nil
    }
    }
    - return nil
    + return nil, ErrNoRepositoryFound
    }
    --- a/govcs_test.go Tue Dec 05 21:19:23 2017 -0600
    +++ b/govcs_test.go Tue Dec 05 21:55:16 2017 -0600
    @@ -62,8 +62,9 @@
    backend := s.setup("git", exec.NewMockCommand("found", nil))
    defer exec.SetBackend(backend)
    - vcs := Detect("working-directory")
    + vcs, err := Detect("working-directory")
    Expect(vcs).ToNot(BeNil())
    + Expect(err).To(BeNil())
    g, ok := vcs.(*git.Git)
    Expect(ok).To(BeTrue())
    @@ -74,10 +75,20 @@
    backend := s.setup("hg", exec.NewMockCommand("found", nil))
    defer exec.SetBackend(backend)
    - vcs := Detect("working-directory")
    + vcs, err := Detect("working-directory")
    Expect(vcs).ToNot(BeNil())
    + Expect(err).To(BeNil())
    m, ok := vcs.(*hg.Mercurial)
    Expect(ok).To(BeTrue())
    Expect(m).To(BeAssignableToTypeOf(&hg.Mercurial{}))
    }
    +
    +func (s *vcsSuite) TestFindNoRepository(t sweet.T) {
    + backend := s.setup("", exec.NewMockCommand("", errors.New("not a repo")))
    + defer exec.SetBackend(backend)
    +
    + vcs, err := Detect("working-directory")
    + Expect(vcs).To(BeNil())
    + Expect(err).To(MatchError(ErrNoRepositoryFound))
    +}