Permalink
Browse files

Support converting packages that only contain subpackages

Came across this testing out `ginkgo convert` against the bosh agent. There
are some packages (eg: davcli) that contain no Go files or testfiles, but
only contain more packages.
  • Loading branch information...
1 parent 37e49ff commit b7fceebde529967a73fc354e53f922f9cd1f5e2b @tjarratt tjarratt committed with Onsi Fakhouri Feb 5, 2014
@@ -0,0 +1,9 @@
+package subpackage
+
+import (
+ "testing"
+)
+
+func TestNestedSubPackages(t *testing.T) {
+ t.Fail(true)
+}
@@ -0,0 +1,13 @@
+package subpackage
+
+import (
+ . "github.com/onsi/ginkgo"
+)
+
+func init() {
+ Describe("Testing with Ginkgo", func() {
+ It("TestNestedSubPackages", func() {
+ GinkgoT().Fail(true)
+ })
+ })
+}
View
@@ -10,7 +10,7 @@ import (
func init() {
Describe("using ginkgo convert", func() {
- BeforeEach(deleteFilesInTmp)
+ BeforeEach(deleteTmpFiles)
BeforeEach(buildGinkgo)
It("rewrites xunit tests as ginkgo tests", func() {
@@ -64,6 +64,16 @@ func init() {
})
})
+ It("converts go tests in deeply nested packages (some may not contain go files)", func() {
+ withTempDir(func(dir string) {
+ runGinkgoConvert()
+
+ testsuite := readConvertedFileNamed(dir, "nested_without_gofiles", "subpackage", "nested_subpackage_test.go")
+ goldmaster := readGoldMasterNamed("nested_subpackage_test.go")
+ Expect(testsuite).To(Equal(goldmaster))
+ })
+ })
+
It("creates ginkgo test suites for all nested packages", func() {
withTempDir(func(dir string) {
runGinkgoConvert()
@@ -160,7 +170,7 @@ func readConvertedFileNamed(pathComponents ...string) string {
return string(bytes)
}
-func deleteFilesInTmp() {
+func deleteTmpFiles() {
cwd, err := os.Getwd()
Expect(err).NotTo(HaveOccurred())
tempDir := filepath.Join(cwd, "tmp")
@@ -15,9 +15,9 @@ import (
* also be added for this package, and all of its child packages.
*/
func RewritePackage(packageName string) {
- pkg, err := build.Default.Import(packageName, ".", build.ImportMode(0))
+ pkg, err := packageWithName(packageName)
if err != nil {
- panic(fmt.Sprintf("unexpected error reading package: '%s'\n%s\n", os.Args[1], err.Error()))
+ panic(fmt.Sprintf("unexpected error reading package: '%s'\n%s\n", packageName, err.Error()))
}
for _, filename := range findTestsInPackage(pkg) {
@@ -47,7 +47,7 @@ func findTestsInPackage(pkg *build.Package) (testfiles []string) {
}
packageName := filepath.Join(pkg.ImportPath, file.Name())
- subPackage, err := build.Default.Import(packageName, ".", build.ImportMode(0))
+ subPackage, err := packageWithName(packageName)
if err != nil {
panic(fmt.Sprintf("unexpected error reading package: '%s'\n%s\n", packageName, err.Error()))
}
@@ -70,6 +70,7 @@ func addGinkgoSuiteForPackage(pkg *build.Package) {
}
suite_test_file := filepath.Join(pkg.Dir, pkg.Name+"_suite_test.go")
+
_, err = os.Stat(suite_test_file)
if err == nil {
return // test file already exists, this should be a no-op
@@ -92,14 +93,28 @@ func addGinkgoSuiteForPackage(pkg *build.Package) {
}
}
-
/*
* Shells out to `go fmt` to format the package
*/
func goFmtPackage(pkg *build.Package) {
output, err := exec.Command("go", "fmt", pkg.ImportPath).Output()
if err != nil {
- panic(fmt.Sprintf("Error running 'go fmt %s'.\nstdout: %s\n%s\n", pkg.ImportPath, output, err.Error()))
+ fmt.Printf("Warning: Error running 'go fmt %s'.\nstdout: %s\n%s\n", pkg.ImportPath, output, err.Error())
+ }
+}
+
+/*
+ * Attempts to return a package with its test files already read.
+ * The ImportMode arg to build.Import lets you specify if you want go to read the
+ * buildable go files inside the package, but it fails if the package has no go files
+ */
+func packageWithName(name string) (pkg *build.Package, err error) {
+ pkg, err = build.Default.Import(name, ".", build.ImportMode(0))
+ if err == nil {
+ return
}
+
+ pkg, err = build.Default.Import(name, ".", build.ImportMode(1))
+ return
}

0 comments on commit b7fceeb

Please sign in to comment.