Skip to content

Commit

Permalink
Use more powerful package name determination algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
petergtz committed Feb 13, 2019
1 parent 5d0e5bd commit b23876c
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 3 deletions.
5 changes: 3 additions & 2 deletions pegomock/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"strings"
"time"

"gopkg.in/alecthomas/kingpin.v2"
kingpin "gopkg.in/alecthomas/kingpin.v2"

"github.com/petergtz/pegomock/pegomock/filehandling"
"github.com/petergtz/pegomock/pegomock/remove"
Expand Down Expand Up @@ -93,7 +93,8 @@ func Run(cliArgs []string, out io.Writer, in io.Reader, app *kingpin.Application

realPackageOut := *packageOut
if *packageOut == "" {
realPackageOut = filepath.Base(workingDir) + "_test"
realPackageOut, err = DeterminePackageNameIn(workingDir)
app.FatalIfError(err, "Could not determine package name.")
}

realDestination := *destination
Expand Down
3 changes: 2 additions & 1 deletion pegomock/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import (
"path/filepath"
"strings"

"gopkg.in/alecthomas/kingpin.v2"
kingpin "gopkg.in/alecthomas/kingpin.v2"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
main "github.com/petergtz/pegomock/pegomock"

. "github.com/petergtz/pegomock/pegomock/testutil"

"testing"
Expand Down
64 changes: 64 additions & 0 deletions pegomock/package_name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"fmt"
"io/ioutil"
"path/filepath"
"strings"

"golang.org/x/tools/go/loader"
)

func DeterminePackageNameIn(dir string) (string, error) {
fileInfos, e := ioutil.ReadDir(dir)
if e != nil {
return "", fmt.Errorf("Could not get files in directory %v: %v", dir, e.Error())
}
var filenames []string
var testFilenames []string
for _, fileInfo := range fileInfos {
if fileInfo.IsDir() || !strings.HasSuffix(fileInfo.Name(), ".go") {
continue
}
if strings.HasSuffix(fileInfo.Name(), "_test.go") {
testFilenames = append(testFilenames, fileInfo.Name())
} else {
filenames = append(filenames, fileInfo.Name())
}
}
packageName, e := packageNameFrom(dir, testFilenames)
if e != nil {
return "", e
}
if packageName != "" {
return packageName, nil
}
packageName, e = packageNameFrom(dir, filenames)
if e != nil {
return "", e
}
if packageName != "" {
return packageName + "_test", nil
}
packageName = strings.Replace(filepath.Base(dir), "-", "_", -1)
if packageName != "" {
return packageName + "_test", nil
}
panic("Unexpected error when determining the package name of the mock file.")
}

func packageNameFrom(dir string, filenames []string) (string, error) {
conf := loader.Config{
Cwd: dir,
AllowErrors: true,
}
conf.CreateFromFilenames("", filenames...)
program, e := conf.Load()
if e != nil {
panic(e)
}
if len(program.Created[0].Errors) != 0 {
return "", fmt.Errorf("Error while determining Go package name from Go file in %v: %v", dir, program.Created[0].Errors)
}
return program.Created[0].Pkg.Name(), nil
}
73 changes: 73 additions & 0 deletions pegomock/package_name_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package main_test

import (
"go/build"
"os"

. "github.com/petergtz/pegomock/pegomock/testutil"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
main "github.com/petergtz/pegomock/pegomock"
)

var _ = Describe("DetermineBla", func() {
var (
packageDir string
)
BeforeEach(func() {
packageDir = joinPath(build.Default.GOPATH, "src", "package_dir")
})

JustBeforeEach(func() {
Expect(os.MkdirAll(packageDir, 0755)).To(Succeed())
})

AfterEach(func() {
Expect(os.RemoveAll(packageDir)).To(Succeed())
})

Context("only one go file", func() {
It("names the package name after package name + _test suffix", func() {
WriteFile(joinPath(packageDir, "mydisplay.go"), "package package_name")

Expect(main.DeterminePackageNameIn(packageDir)).To(Equal("package_name_test"))
})
})

Context("multiple go files with different package names", func() {
It("fails", func() {
WriteFile(joinPath(packageDir, "mydisplay.go"), "package package_name")
WriteFile(joinPath(packageDir, "other.go"), "package other_package_name")

_, e := main.DeterminePackageNameIn(packageDir)
Expect(e).To(MatchError(ContainSubstring("Error while determining Go package")))
})
})

Context("go file and go test file", func() {
It("determines the package name from the test file", func() {
WriteFile(joinPath(packageDir, "mydisplay.go"), "package package_name")
WriteFile(joinPath(packageDir, "mydisplay_test.go"), "package non_conventional_package_name_test")

Expect(main.DeterminePackageNameIn(packageDir)).To(Equal("non_conventional_package_name_test"))
})
})

Context("no files", func() {
It("names the package after the directory name base", func() {
Expect(main.DeterminePackageNameIn(packageDir)).To(Equal("package_dir_test"))
})

Context("current dir with dashes in name", func() {
BeforeEach(func() {
packageDir = joinPath(build.Default.GOPATH, "src", "package-dir-with-dashes")
})

It("names the package after the directory name base, but replace dashes with underscores", func() {
Expect(main.DeterminePackageNameIn(packageDir)).To(Equal("package_dir_with_dashes_test"))
})
})
})

})

0 comments on commit b23876c

Please sign in to comment.