-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use more powerful package name determination algorithm
- Loading branch information
Showing
4 changed files
with
142 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
}) | ||
}) | ||
}) | ||
|
||
}) |