Repository: gobuffalo/buffalo
Pull request #1242 (2018-08-22 20:50:34)

stops buffalo new from generating incomplete projects if the templates are missing

codecov[bot]: # [Codecov](https://codecov.io/gh/gobuffalo/buffalo/pull/1242?src=pr&el=h1) Report
> Merging [#1242](https://codecov.io/gh/gobuffalo/buffalo/pull/1242?src=pr&el=desc) into [development](https://codecov.io/gh/gobuffalo/buffalo/commit/5c85aaa4c15798b6fa50426ead66f7e168d7218e?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `50%`.

[![Impacted file tree graph](https://codecov.io/gh/gobuffalo/buffalo/pull/1242/graphs/tree.svg?height=150&token=1Yyy8wHMMl&src=pr&width=650)](https://codecov.io/gh/gobuffalo/buffalo/pull/1242?src=pr&el=tree)

```diff
@@               Coverage Diff               @@
##           development    #1242      +/-   ##
===============================================
+ Coverage         53.9%   53.91%   +<.01%     
===============================================
  Files               75       75              
  Lines             3430     3437       +7     
===============================================
+ Hits              1849     1853       +4     
- Misses            1484     1486       +2     
- Partials            97       98       +1
```


| [Impacted Files](https://codecov.io/gh/gobuffalo/buffalo/pull/1242?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [generators/newapp/new.go](https://codecov.io/gh/gobuffalo/buffalo/pull/1242/diff?src=pr&el=tree#diff-Z2VuZXJhdG9ycy9uZXdhcHAvbmV3Lmdv) | `0% <0%> (ø)` | :arrow_up: |
| [generators/newapp/generator.go](https://codecov.io/gh/gobuffalo/buffalo/pull/1242/diff?src=pr&el=tree#diff-Z2VuZXJhdG9ycy9uZXdhcHAvZ2VuZXJhdG9yLmdv) | `77.77% <57.14%> (-2.58%)` | :arrow_down: |
| [middleware/middleware.go](https://codecov.io/gh/gobuffalo/buffalo/pull/1242/diff?src=pr&el=tree#diff-bWlkZGxld2FyZS9taWRkbGV3YXJlLmdv) | | |
| [middleware/content\_type.go](https://codecov.io/gh/gobuffalo/buffalo/pull/1242/diff?src=pr&el=tree#diff-bWlkZGxld2FyZS9jb250ZW50X3R5cGUuZ28=) | `100% <0%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/gobuffalo/buffalo/pull/1242?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/gobuffalo/buffalo/pull/1242?src=pr&el=footer). Last update [5c85aaa...57ddfd8](https://codecov.io/gh/gobuffalo/buffalo/pull/1242?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/generators/newapp/generator.go b/generators/newapp/generator.go
index d397c511..cfa05ce7 100644
--- a/generators/newapp/generator.go
+++ b/generators/newapp/generator.go
@@ -10,11 +10,15 @@ import (
 	"github.com/gobuffalo/buffalo/meta"
 	"github.com/gobuffalo/buffalo/runtime"
 	"github.com/gobuffalo/envy"
+	"github.com/gobuffalo/packr"
 	"github.com/gobuffalo/pop"
 	"github.com/markbates/inflect"
 	"github.com/pkg/errors"
 )
 
+// Templates are the templates needed by this generator
+var Templates = packr.NewBox("../newapp/templates")
+
 // ErrNotInGoPath can be asserted against
 var ErrNotInGoPath = errors.New("currently not in a $GOPATH")
 
@@ -56,8 +60,29 @@ func New(name string) (Generator, error) {
 	return g, g.Validate()
 }
 
+const header = "Your `buffalo` binary installation is corrupted and is missing vital templates for app creation.\n"
+const footer = "Please recheck your installation: https://gobuffalo.io/en/docs/installation."
+const goPathAbuse = `It appears you are using multiple GOPATHs:
+%s
+
+Using multiple GOPATHs can cause issues with many third party tooling. Please try using only one GOPATH.
+`
+
+// ErrTemplatesNotFound means that the `buffalo` binary can't find the template files it needs
+// to complete a task. This usually occurs when building `buffalo` locally and/or when using multipath
+// GOPATHs.
+var ErrTemplatesNotFound = errors.New("templates are missing")
+
 // Validate that the app generator is good
 func (g Generator) Validate() error {
+	if !Templates.Has("actions/app.go.tmpl") {
+		msg := header
+		if len(envy.GoPaths()) > 1 {
+			msg += "\n" + fmt.Sprintf(goPathAbuse, strings.Join(envy.GoPaths(), "\n"))
+		}
+		msg += "\n" + footer
+		return errors.Wrap(ErrTemplatesNotFound, msg)
+	}
 	if g.Name == "" {
 		return errors.New("you must enter a name for your new application")
 	}
diff --git a/generators/newapp/new.go b/generators/newapp/new.go
index fd74aa86..d879b48d 100644
--- a/generators/newapp/new.go
+++ b/generators/newapp/new.go
@@ -15,7 +15,6 @@ import (
 	"github.com/gobuffalo/buffalo/generators/soda"
 	"github.com/gobuffalo/envy"
 	"github.com/gobuffalo/makr"
-	"github.com/gobuffalo/packr"
 	"github.com/pkg/errors"
 )
 
@@ -50,7 +49,7 @@ func (a Generator) Run(root string, data makr.Data) error {
 		}
 	}
 
-	files, err := generators.FindByBox(packr.NewBox("../newapp/templates"))
+	files, err := generators.FindByBox(Templates)
 	if err != nil {
 		return errors.WithStack(err)
 	}


Test patch:
diff --git a/generators/newapp/generator_test.go b/generators/newapp/generator_test.go
new file mode 100644
index 00000000..9602496d
--- /dev/null
+++ b/generators/newapp/generator_test.go
@@ -0,0 +1,33 @@
+package newapp
+
+import (
+	"testing"
+
+	"github.com/gobuffalo/packr"
+	"github.com/pkg/errors"
+	"github.com/stretchr/testify/require"
+)
+
+func Test_Validate_TemplatesFound(t *testing.T) {
+	r := require.New(t)
+
+	g := Generator{}
+	err := g.Validate()
+	r.Error(err)
+	r.NotEqual(ErrTemplatesNotFound, errors.Cause(err))
+}
+
+func Test_Validate_TemplatesMissing(t *testing.T) {
+	r := require.New(t)
+
+	obox := Templates
+	defer func() {
+		Templates = obox
+	}()
+	Templates = packr.NewBox(".")
+
+	g := Generator{}
+	err := g.Validate()
+	r.Error(err)
+	r.Equal(ErrTemplatesNotFound, errors.Cause(err))
+}