From 4c0bda0037f2b5c9b49a4bee9f879eb922cffdd5 Mon Sep 17 00:00:00 2001 From: Antonio Pagano <645522+paganotoni@users.noreply.github.com> Date: Wed, 23 Dec 2020 19:41:52 -0500 Subject: [PATCH] Development (#2072) * adding direct version on the docker build * fixing plugin installation * Fix npm's package.json permissions (#2005) Changed created file permissions from 644 (`-w----r--`) to 0644 (`rw-r--r--`) * Bump spf13/viper to v1.7.0 (#1999) * bunping version * changing version on dockerfile * patch to fix the keywords check on new app * removing unneeded append * Task fixing html binder (#2016) * binder was not wired * packing * adding new version number * pulling the buffalo binary from gobinaries.com * Task updating formam (#2025) * v0.16.13 (#2019) * adding direct version on the docker build * fixing plugin installation * Fix npm's package.json permissions (#2005) Changed created file permissions from 644 (`-w----r--`) to 0644 (`rw-r--r--`) * Bump spf13/viper to v1.7.0 (#1999) * bunping version * changing version on dockerfile * patch to fix the keywords check on new app * removing unneeded append * Task fixing html binder (#2016) * binder was not wired * packing * adding new version number * pulling the buffalo binary from gobinaries.com Co-authored-by: Disconnect3d Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com> * updating formam Co-authored-by: Disconnect3d Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com> * Fixes #1987 and #2023 (#2026) * v0.16.13 (#2019) * adding direct version on the docker build * fixing plugin installation * Fix npm's package.json permissions (#2005) Changed created file permissions from 644 (`-w----r--`) to 0644 (`rw-r--r--`) * Bump spf13/viper to v1.7.0 (#1999) * bunping version * changing version on dockerfile * patch to fix the keywords check on new app * removing unneeded append * Task fixing html binder (#2016) * binder was not wired * packing * adding new version number * pulling the buffalo binary from gobinaries.com Co-authored-by: Disconnect3d Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com> * fixing #1987 and #2023 * adding missing converstion * removing commented code Co-authored-by: Disconnect3d Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com> * changing version number * moving to latest 2 go versions (#2038) * Task binding cleanup (#2039) * moving to latest 2 go versions * cleaning up the decoder on the binding package * Task update formam (#2041) * v0.16.14 (#2030) * adding direct version on the docker build * fixing plugin installation * Fix npm's package.json permissions (#2005) Changed created file permissions from 644 (`-w----r--`) to 0644 (`rw-r--r--`) * Bump spf13/viper to v1.7.0 (#1999) * bunping version * changing version on dockerfile * patch to fix the keywords check on new app * removing unneeded append * Task fixing html binder (#2016) * binder was not wired * packing * adding new version number * pulling the buffalo binary from gobinaries.com * Task updating formam (#2025) * v0.16.13 (#2019) * adding direct version on the docker build * fixing plugin installation * Fix npm's package.json permissions (#2005) Changed created file permissions from 644 (`-w----r--`) to 0644 (`rw-r--r--`) * Bump spf13/viper to v1.7.0 (#1999) * bunping version * changing version on dockerfile * patch to fix the keywords check on new app * removing unneeded append * Task fixing html binder (#2016) * binder was not wired * packing * adding new version number * pulling the buffalo binary from gobinaries.com Co-authored-by: Disconnect3d Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com> * updating formam Co-authored-by: Disconnect3d Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com> * Fixes #1987 and #2023 (#2026) * v0.16.13 (#2019) * adding direct version on the docker build * fixing plugin installation * Fix npm's package.json permissions (#2005) Changed created file permissions from 644 (`-w----r--`) to 0644 (`rw-r--r--`) * Bump spf13/viper to v1.7.0 (#1999) * bunping version * changing version on dockerfile * patch to fix the keywords check on new app * removing unneeded append * Task fixing html binder (#2016) * binder was not wired * packing * adding new version number * pulling the buffalo binary from gobinaries.com Co-authored-by: Disconnect3d Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com> * fixing #1987 and #2023 * adding missing converstion * removing commented code Co-authored-by: Disconnect3d Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com> * changing version number Co-authored-by: Disconnect3d Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com> * updates formam version #2040 Co-authored-by: Disconnect3d Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com> * updating flect versionm (#2042) * updating version * updating some libraries to pass the github security warnings (#2044) * Removing Grifts folder (#2050) * removing the grifts folder * removing grifts folder from codeclimate exceptions * fixes the non-existing .git folder case (#2053) * calling persist explicitly (#2054) * adding version number * Pointing README to new go docs link * running packr2 * Warns if response is attempted to be written twice (#2059) * v0.16.16 (#2055) * adding direct version on the docker build * fixing plugin installation * Fix npm's package.json permissions (#2005) Changed created file permissions from 644 (`-w----r--`) to 0644 (`rw-r--r--`) * Bump spf13/viper to v1.7.0 (#1999) * bunping version * changing version on dockerfile * patch to fix the keywords check on new app * removing unneeded append * Task fixing html binder (#2016) * binder was not wired * packing * adding new version number * pulling the buffalo binary from gobinaries.com * Task updating formam (#2025) * v0.16.13 (#2019) * adding direct version on the docker build * fixing plugin installation * Fix npm's package.json permissions (#2005) Changed created file permissions from 644 (`-w----r--`) to 0644 (`rw-r--r--`) * Bump spf13/viper to v1.7.0 (#1999) * bunping version * changing version on dockerfile * patch to fix the keywords check on new app * removing unneeded append * Task fixing html binder (#2016) * binder was not wired * packing * adding new version number * pulling the buffalo binary from gobinaries.com Co-authored-by: Disconnect3d Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com> * updating formam Co-authored-by: Disconnect3d Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com> * Fixes #1987 and #2023 (#2026) * v0.16.13 (#2019) * adding direct version on the docker build * fixing plugin installation * Fix npm's package.json permissions (#2005) Changed created file permissions from 644 (`-w----r--`) to 0644 (`rw-r--r--`) * Bump spf13/viper to v1.7.0 (#1999) * bunping version * changing version on dockerfile * patch to fix the keywords check on new app * removing unneeded append * Task fixing html binder (#2016) * binder was not wired * packing * adding new version number * pulling the buffalo binary from gobinaries.com Co-authored-by: Disconnect3d Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com> * fixing #1987 and #2023 * adding missing converstion * removing commented code Co-authored-by: Disconnect3d Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com> * changing version number * moving to latest 2 go versions (#2038) * Task binding cleanup (#2039) * moving to latest 2 go versions * cleaning up the decoder on the binding package * Task update formam (#2041) * v0.16.14 (#2030) * adding direct version on the docker build * fixing plugin installation * Fix npm's package.json permissions (#2005) Changed created file permissions from 644 (`-w----r--`) to 0644 (`rw-r--r--`) * Bump spf13/viper to v1.7.0 (#1999) * bunping version * changing version on dockerfile * patch to fix the keywords check on new app * removing unneeded append * Task fixing html binder (#2016) * binder was not wired * packing * adding new version number * pulling the buffalo binary from gobinaries.com * Task updating formam (#2025) * v0.16.13 (#2019) * adding direct version on the docker build * fixing plugin installation * Fix npm's package.json permissions (#2005) Changed created file permissions from 644 (`-w----r--`) to 0644 (`rw-r--r--`) * Bump spf13/viper to v1.7.0 (#1999) * bunping version * changing version on dockerfile * patch to fix the keywords check on new app * removing unneeded append * Task fixing html binder (#2016) * binder was not wired * packing * adding new version number * pulling the buffalo binary from gobinaries.com Co-authored-by: Disconnect3d Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com> * updating formam Co-authored-by: Disconnect3d Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com> * Fixes #1987 and #2023 (#2026) * v0.16.13 (#2019) * adding direct version on the docker build * fixing plugin installation * Fix npm's package.json permissions (#2005) Changed created file permissions from 644 (`-w----r--`) to 0644 (`rw-r--r--`) * Bump spf13/viper to v1.7.0 (#1999) * bunping version * changing version on dockerfile * patch to fix the keywords check on new app * removing unneeded append * Task fixing html binder (#2016) * binder was not wired * packing * adding new version number * pulling the buffalo binary from gobinaries.com Co-authored-by: Disconnect3d Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com> * fixing #1987 and #2023 * adding missing converstion * removing commented code Co-authored-by: Disconnect3d Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com> * changing version number Co-authored-by: Disconnect3d Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com> * updates formam version #2040 Co-authored-by: Disconnect3d Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com> * updating flect versionm (#2042) * updating version * updating some libraries to pass the github security warnings (#2044) * Removing Grifts folder (#2050) * removing the grifts folder * removing grifts folder from codeclimate exceptions * fixes the non-existing .git folder case (#2053) * calling persist explicitly (#2054) * adding version number Co-authored-by: Disconnect3d Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com> * only writing the header if it has not been written * bumping version * adding packr2 files Co-authored-by: Disconnect3d Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com> * allows to specify custom partialfeeders * cleaning up a bit for codeclimate * changing the version Co-authored-by: Disconnect3d Co-authored-by: hackerman <3372410+aeneasr@users.noreply.github.com> --- Dockerfile.build | 2 +- Dockerfile.slim.build | 2 +- render/partials_test.go | 33 ++++++++++++++++++++++ render/template.go | 61 +++++++++++++++++++++++++---------------- response.go | 15 ++++++++-- response_test.go | 22 +++++++++++++++ runtime/version.go | 2 +- 7 files changed, 107 insertions(+), 30 deletions(-) create mode 100644 response_test.go diff --git a/Dockerfile.build b/Dockerfile.build index 0f83c15d6..e9cd12591 100644 --- a/Dockerfile.build +++ b/Dockerfile.build @@ -32,7 +32,7 @@ RUN npm install -g --no-progress yarn \ # Install golangci RUN curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.24.0 # Installing buffalo binary -RUN curl -sf https://gobinaries.com/gobuffalo/buffalo/buffalo@v0.16.16 | sh +RUN curl -sf https://gobinaries.com/gobuffalo/buffalo/buffalo@v0.16.18 | sh RUN go get github.com/gobuffalo/buffalo-pop/v2 RUN buffalo version diff --git a/Dockerfile.slim.build b/Dockerfile.slim.build index 4badb0dfe..10097ddd3 100644 --- a/Dockerfile.slim.build +++ b/Dockerfile.slim.build @@ -17,7 +17,7 @@ RUN npm i -g --no-progress yarn \ && yarn config set yarn-offline-mirror-pruning true # Pulling docker binary from releases -RUN curl -sf https://gobinaries.com/gobuffalo/buffalo/buffalo@v0.16.16 | sh +RUN curl -sf https://gobinaries.com/gobuffalo/buffalo/buffalo@v0.16.18 | sh RUN go get github.com/gobuffalo/buffalo-pop/v2 RUN buffalo version diff --git a/render/partials_test.go b/render/partials_test.go index 82420d046..d158ac9b2 100644 --- a/render/partials_test.go +++ b/render/partials_test.go @@ -29,6 +29,39 @@ func Test_Template_Partial(t *testing.T) { r.Equal("Foo > Mark", strings.TrimSpace(bb.String())) } +func Test_Template_PartialCustomFeeder(t *testing.T) { + r := require.New(t) + + box := packd.NewMemoryBox() + r.NoError(box.AddString("base.plush.html", `<%= partial("foo.plush.html") %>`)) + r.NoError(box.AddString("_foo.plush.html", "other")) + + e := NewEngine() + e.TemplatesBox = box + + t.Run("Custom Feeder", func(t *testing.T) { + e.Helpers["partialFeeder"] = func(path string) (string, error) { + return "custom", nil + } + + bb := &bytes.Buffer{} + + re := e.HTML("base.plush.html") + r.NoError(re.Render(bb, Data{})) + r.Equal("custom", strings.TrimSpace(bb.String())) + }) + + t.Run("Default Feeder", func(t *testing.T) { + e.Helpers["partialFeeder"] = nil + + bb := &bytes.Buffer{} + + re := e.HTML("base.plush.html") + r.NoError(re.Render(bb, Data{})) + r.Equal("other", strings.TrimSpace(bb.String())) + }) +} + func Test_Template_Partial_WithoutExtension(t *testing.T) { r := require.New(t) diff --git a/render/template.go b/render/template.go index 7d2daf8bf..b67641d39 100644 --- a/render/template.go +++ b/render/template.go @@ -119,29 +119,7 @@ func (s templateRenderer) exec(name string, data Data) (template.HTML, error) { name = fixExtension(name, ct) // Try to use localized version - templateName := name - if languages, ok := data["languages"].([]string); ok { - ll := len(languages) - if ll > 0 { - // Default language is the last in the list - defaultLanguage := languages[ll-1] - ext := filepath.Ext(name) - rawName := strings.TrimSuffix(name, ext) - - for _, l := range languages { - var candidateName string - if l == defaultLanguage { - break - } - candidateName = rawName + "." + strings.ToLower(l) + ext - if _, err := s.resolve(candidateName); err == nil { - // Replace name with the existing suffixed version - templateName = candidateName - break - } - } - } - } + templateName := s.localizedName(name, data) // Set current_template to context if _, ok := data["current_template"]; !ok { @@ -158,7 +136,11 @@ func (s templateRenderer) exec(name string, data Data) (template.HTML, error) { for k, v := range s.Helpers { helpers[k] = v } - helpers["partialFeeder"] = s.partialFeeder + + // Allows to specify custom partialFeeder + if helpers["partialFeeder"] == nil { + helpers["partialFeeder"] = s.partialFeeder + } helpers = s.addAssetsHelpers(helpers) @@ -178,6 +160,37 @@ func (s templateRenderer) exec(name string, data Data) (template.HTML, error) { return template.HTML(body), nil } +func (s templateRenderer) localizedName(name string, data Data) string { + templateName := name + + languages, ok := data["languages"].([]string) + if !ok || len(languages) == 0 { + return templateName + } + + ll := len(languages) + // Default language is the last in the list + defaultLanguage := languages[ll-1] + ext := filepath.Ext(name) + rawName := strings.TrimSuffix(name, ext) + + for _, l := range languages { + var candidateName string + if l == defaultLanguage { + break + } + + candidateName = rawName + "." + strings.ToLower(l) + ext + if _, err := s.resolve(candidateName); err == nil { + // Replace name with the existing suffixed version + templateName = candidateName + break + } + } + + return templateName +} + func (s templateRenderer) exts(name string) []string { exts := []string{} for { diff --git a/response.go b/response.go index 81f9c7577..77ed02977 100644 --- a/response.go +++ b/response.go @@ -18,9 +18,18 @@ type Response struct { } // WriteHeader sets the status code for a response -func (w *Response) WriteHeader(i int) { - w.Status = i - w.ResponseWriter.WriteHeader(i) +func (w *Response) WriteHeader(code int) { + if code == w.Status { + return + } + + if w.Status > 0 { + fmt.Printf("[WARNING] Headers were already written. Wanted to override status code %d with %d", w.Status, code) + return + } + + w.Status = code + w.ResponseWriter.WriteHeader(code) } // Write the body of the response diff --git a/response_test.go b/response_test.go new file mode 100644 index 000000000..1dbd60fed --- /dev/null +++ b/response_test.go @@ -0,0 +1,22 @@ +package buffalo + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/require" +) + +func Test_Response_MultipleWrite(t *testing.T) { + r := require.New(t) + resWr := httptest.NewRecorder() + res := Response{ + ResponseWriter: resWr, + } + + res.WriteHeader(http.StatusOK) + res.WriteHeader(http.StatusInternalServerError) + + r.Equal(res.Status, http.StatusOK) +} diff --git a/runtime/version.go b/runtime/version.go index 9ec0a5fa2..8d41ab854 100644 --- a/runtime/version.go +++ b/runtime/version.go @@ -1,4 +1,4 @@ package runtime // Version is the current version of the buffalo binary -var Version = "v0.16.16" +var Version = "v0.16.18"