Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dustin Blackman committed Feb 1, 2020
1 parent 2c5d183 commit 8f52f4a
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 6 deletions.
11 changes: 6 additions & 5 deletions pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ func GetPkgRoot() (string, error) {
}

for {
if currentDir == "/" {
return "", errors.New("go.mod not found")
}

if _, err := os.Stat(path.Join(currentDir, "go.mod")); !os.IsNotExist(err) {
absPath, err := filepath.Abs(currentDir)
if absPath == "/" {
return "", errors.New("go.mod not found")
}
if err != nil {
return "", err
}
Expand Down Expand Up @@ -66,7 +67,7 @@ func GetCommandVersionedPkgPath(pkgRoot, binName string) (string, error) {
}

if binModulePath == "" {
return "", errors.New("can not find bin in tools file")
return "", errors.New("cant find bin in tools file")
}

gomodPath := path.Join(pkgRoot, "go.mod")
Expand All @@ -88,7 +89,7 @@ func GetCommandVersionedPkgPath(pkgRoot, binName string) (string, error) {
}

if cmdPath == "" {
return "", fmt.Errorf("can not find require for module %s in go.mod", binModulePath)
return "", fmt.Errorf("cant find require for module %s in go.mod", binModulePath)
}

return cmdPath, nil
Expand Down
50 changes: 49 additions & 1 deletion pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,27 @@ var _ = Describe("pkg", func() {
goVersion := strings.Split(string(goVersionOutput), " ")[2]

Context("GetPkgRoot", func() {
It("it should return the current working directory that contains a go.mod", func() {
It("should return the current working directory that contains a go.mod", func() {
dir, err := gomodrun.GetPkgRoot()
Expect(err).To(BeNil())
Expect(dir).To(Equal(cwd))
})

It("should return an error when it can not find a go.mod", func() {
err := os.Chdir("../")
if err != nil {
panic(err)
}

dir, err := gomodrun.GetPkgRoot()
Expect(err).ToNot(BeNil())
Expect(dir).To(Equal(""))

err = os.Chdir(cwd)
if err != nil {
panic(err)
}
})
})

Context("GetCommandVersionedPkgPath", func() {
Expand All @@ -34,6 +50,38 @@ var _ = Describe("pkg", func() {
Expect(err).To(BeNil())
Expect(cmdPath).To(Equal(testPackage))
})

It("should throw an error when it cant find tools imports", func() {
cmdPath, err := gomodrun.GetCommandVersionedPkgPath(path.Join(cwd, "../"), "hello-world")
Expect(err).ToNot(BeNil())
Expect(cmdPath).To(Equal(""))
})

It("should throw an error when it cant find specified bin in imports", func() {
cmdPath, err := gomodrun.GetCommandVersionedPkgPath(cwd, "not-real")
Expect(err).ToNot(BeNil())
Expect(strings.Contains(err.Error(), "cant find bin in tools file")).To(BeTrue())
Expect(cmdPath).To(Equal(""))
})

It("should throw an error when go.mod cant be found", func() {
cmdPath, err := gomodrun.GetCommandVersionedPkgPath(path.Join(cwd, "./tests/missing-go-mod"), "hello-world")
Expect(err).ToNot(BeNil())
Expect(cmdPath).To(Equal(""))
})

It("should throw an error when go.mod is corrupted", func() {
cmdPath, err := gomodrun.GetCommandVersionedPkgPath(path.Join(cwd, "./tests/corrupted-go-mod"), "hello-world")
Expect(err).ToNot(BeNil())
Expect(cmdPath).To(Equal(""))
})

It("should throw an error when go.mod is missing the requested dependency", func() {
cmdPath, err := gomodrun.GetCommandVersionedPkgPath(path.Join(cwd, "./tests/incomplete-go-mod"), "hello-world")
Expect(err).ToNot(BeNil())
Expect(strings.Contains(err.Error(), "cant find require")).To(BeTrue())
Expect(cmdPath).To(Equal(""))
})
})

Context("GetCachedBin", func() {
Expand Down
1 change: 1 addition & 0 deletions tests/corrupted-go-mod/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
not-a-real-go-mod
7 changes: 7 additions & 0 deletions tests/corrupted-go-mod/go.tools.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build tools

package gomodrun

import (
_ "github.com/dustinblackman/go-hello-world-test/hello-world"
)
3 changes: 3 additions & 0 deletions tests/incomplete-go-mod/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/dustinblackman/gomodrun-test

go 1.13
7 changes: 7 additions & 0 deletions tests/incomplete-go-mod/go.tools.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build tools

package gomodrun

import (
_ "github.com/dustinblackman/go-hello-world-test/hello-world"
)
7 changes: 7 additions & 0 deletions tests/missing-go-mod/go.tools.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build tools

package gomodrun

import (
_ "github.com/dustinblackman/go-hello-world-test/hello-world"
)

0 comments on commit 8f52f4a

Please sign in to comment.