Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
Merge branch 'development' into task-#1941
Browse files Browse the repository at this point in the history
  • Loading branch information
paganotoni committed Apr 14, 2020
2 parents 6988adb + b47d822 commit 74911df
Show file tree
Hide file tree
Showing 22 changed files with 2,042 additions and 1,674 deletions.
4 changes: 2 additions & 2 deletions Dockerfile.build
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ RUN npm install -g --no-progress yarn \
RUN curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.24.0

# Pulling docker binary from releases
RUN wget https://github.com/gobuffalo/buffalo/releases/download/v0.16.1/buffalo_0.16.1_Linux_x86_64.tar.gz \
&& tar -xzf buffalo_0.16.1_Linux_x86_64.tar.gz \
RUN wget https://github.com/gobuffalo/buffalo/releases/download/v0.16.3/buffalo_0.16.3_Linux_x86_64.tar.gz \
&& tar -xzf buffalo_0.16.3_Linux_x86_64.tar.gz \
&& mv buffalo $(go env GOPATH)/bin/buffalo

RUN buffalo version
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile.slim.build
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ RUN npm i -g --no-progress yarn \
&& yarn config set yarn-offline-mirror-pruning true

# Pulling docker binary from releases
RUN wget https://github.com/gobuffalo/buffalo/releases/download/v0.16.1/buffalo_0.16.1_Linux_x86_64.tar.gz \
&& tar -xzf buffalo_0.16.1_Linux_x86_64.tar.gz \
RUN wget https://github.com/gobuffalo/buffalo/releases/download/v0.16.3/buffalo_0.16.3_Linux_x86_64.tar.gz \
&& tar -xzf buffalo_0.16.3_Linux_x86_64.tar.gz \
&& mv buffalo $(go env GOPATH)/bin/buffalo

RUN buffalo version
Expand Down
5 changes: 4 additions & 1 deletion buffalo/cmd/fix/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var replace = map[string]string{
"github.com/gobuffalo/suite": "github.com/gobuffalo/suite/v3",
"github.com/gobuffalo/buffalo-pop/": "github.com/gobuffalo/buffalo-pop/v2",
"github.com/gobuffalo/buffalo-pop/pop/popmw": "github.com/gobuffalo/buffalo-pop/v2/pop/popmw",
"github.com/gobuffalo/plush": "github.com/gobuffalo/plush/v4",
}

var ic = ImportConverter{
Expand Down Expand Up @@ -66,7 +67,9 @@ var checks = []Check{
DeprecrationsCheck,
fixDocker,
encodeApp,
Plugins,
Plugins{}.RemoveOld,
Plugins{}.CleanCache,
Plugins{}.Reinstall,
Plush,
}

Expand Down
42 changes: 40 additions & 2 deletions buffalo/cmd/fix/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,31 @@ import (
"context"
"fmt"
"os"
"path"
"strings"

cmdPlugins "github.com/gobuffalo/buffalo/buffalo/cmd/plugins"
"github.com/gobuffalo/buffalo/genny/plugins/install"
"github.com/gobuffalo/buffalo/internal/takeon/github.com/markbates/errx"
"github.com/gobuffalo/buffalo/plugins"
"github.com/gobuffalo/buffalo/plugins/plugdeps"
"github.com/gobuffalo/genny/v2"
"github.com/gobuffalo/meta"
)

// Plugins will fix plugins between releases
func Plugins(r *Runner) error {
//Plugins fixes the plugin configuration of the project by
//manipulating the plugins .toml file.
type Plugins struct{}

//CleanCache cleans the plugins cache folder by removing it
func (pf Plugins) CleanCache(r *Runner) error {
fmt.Println("~~~ Cleaning plugins cache ~~~")
os.RemoveAll(plugins.CachePath)
return nil
}

//Reinstall installs latest versions of the plugins
func (pf Plugins) Reinstall(r *Runner) error {
plugs, err := plugdeps.List(r.App)
if err != nil && (errx.Unwrap(err) != plugdeps.ErrMissingConfig) {
return err
Expand All @@ -32,3 +45,28 @@ func Plugins(r *Runner) error {
fmt.Println("~~~ Reinstalling plugins ~~~")
return run.Run()
}

//RemoveOld removes old and deprecated plugins
func (pf Plugins) RemoveOld(r *Runner) error {
fmt.Println("~~~ Removing old plugins ~~~")

run := genny.WetRunner(context.Background())
app := meta.New(".")
plugs, err := plugdeps.List(app)
if err != nil && (errx.Unwrap(err) != plugdeps.ErrMissingConfig) {
return err
}

a := strings.TrimSpace("github.com/gobuffalo/buffalo-pop")
bin := path.Base(a)
plugs.Remove(plugdeps.Plugin{
Binary: bin,
GoGet: a,
})

fmt.Println("~~~ Removing github.com/gobuffalo/buffalo-pop plugin ~~~")

run.WithRun(cmdPlugins.NewEncodePluginsRunner(app, plugs))

return run.Run()
}
22 changes: 22 additions & 0 deletions buffalo/cmd/plugins/encode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package plugins

import (
"bytes"

"github.com/gobuffalo/buffalo/plugins/plugdeps"
"github.com/gobuffalo/genny/v2"
"github.com/gobuffalo/meta"
)

// NewEncodePluginsRunner will return a runner that will encode the plugins file
func NewEncodePluginsRunner(app meta.App, plugs *plugdeps.Plugins) func(r *genny.Runner) error {
return func(r *genny.Runner) error {
p := plugdeps.ConfigPath(app)
bb := &bytes.Buffer{}
if err := plugs.Encode(bb); err != nil {
return err
}

return r.File(genny.NewFile(p, bb))
}
}
14 changes: 1 addition & 13 deletions buffalo/cmd/plugins/remove.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package plugins

import (
"bytes"
"context"
"fmt"
"path"
Expand Down Expand Up @@ -46,18 +45,7 @@ var removeCmd = &cobra.Command{
})
}

run.WithRun(func(r *genny.Runner) error {
p := plugdeps.ConfigPath(app)
bb := &bytes.Buffer{}
if err := plugs.Encode(bb); err != nil {
return err
}
return r.File(genny.NewFile(p, bb))
})
if err != nil {
return err
}

run.WithRun(NewEncodePluginsRunner(app, plugs))
return run.Run()
},
}
Expand Down
45 changes: 28 additions & 17 deletions buffalo/cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,38 +101,43 @@ func findSchema() io.Reader {
func testRunner(args []string) error {
var mFlag bool
var query string
cargs := []string{}
pargs := []string{}

var larg string
for i, a := range args {
switch a {
case "-run", "-m", "-timeout":
query = args[i+1]

commandArgs := []string{}
packageArgs := []string{}

var lastArg string
for index, arg := range args {
switch arg {
case "-run", "-m":
query = args[index+1]
mFlag = true
case "-v":
cargs = append(cargs, "-v")
case "-v", "-timeout":
commandArgs = append(commandArgs, arg)
default:
if larg != "-run" && larg != "-m" && larg != "-timeout" {
pargs = append(pargs, a)
if lastArg == "-timeout" {
commandArgs = append(commandArgs, arg)
} else if lastArg != "-run" && lastArg != "-m" {
packageArgs = append(packageArgs, arg)
}
}
larg = a

lastArg = arg
}

cmd := newTestCmd(cargs)
cmd := newTestCmd(commandArgs)
if mFlag {
return mFlagRunner{
query: query,
args: cargs,
pargs: pargs,
args: commandArgs,
pargs: packageArgs,
}.Run()
}

pkgs, err := testPackages(pargs)
pkgs, err := testPackages(packageArgs)
if err != nil {
return err
}

cmd.Args = append(cmd.Args, pkgs...)
logrus.Info(strings.Join(cmd.Args, " "))
return cmd.Run()
Expand All @@ -153,12 +158,15 @@ func (m mFlagRunner) Run() error {
if err != nil {
return err
}

var errs bool
for _, p := range pkgs {
os.Chdir(pwd)

if p == app.PackagePkg {
continue
}

p = strings.TrimPrefix(p, app.PackagePkg+string(filepath.Separator))
os.Chdir(p)

Expand All @@ -168,7 +176,9 @@ func (m mFlagRunner) Run() error {
} else {
cmd.Args = append(cmd.Args, "-run", m.query)
}

logrus.Info(strings.Join(cmd.Args, " "))

if err := cmd.Run(); err != nil {
errs = true
}
Expand All @@ -193,6 +203,7 @@ func testPackages(givenArgs []string) ([]string, error) {
if len(givenArgs) > 0 {
return givenArgs, nil
}

args := []string{}
out, err := exec.Command(envy.Get("GO_BIN", "go"), "list", "./...").Output()
if err != nil {
Expand Down
13 changes: 9 additions & 4 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ func defaultErrorHandler(status int, origErr error, c Context) error {
env := c.Value("env")
requestCT := defaults.String(httpx.ContentType(c.Request()), defaultErrorCT)

c.LogField("status", status)
c.Logger().Error(origErr)
c.Response().WriteHeader(status)

Expand All @@ -170,7 +171,9 @@ func defaultErrorHandler(status int, origErr error, c Context) error {
c.Response().Write(responseBody)
return nil
}

trace := origErr.Error()

switch strings.ToLower(requestCT) {
case "application/json", "text/json", "json":
c.Response().Header().Set("content-type", "application/json")
Expand All @@ -197,14 +200,13 @@ func defaultErrorHandler(status int, origErr error, c Context) error {
if err := c.Request().ParseForm(); err != nil {
trace = fmt.Sprintf("%s\n%s", err.Error(), trace)
}
routes := c.Value("routes")

routes := c.Value("routes")
cd := c.Data()
// if cd, ok := c.(*DefaultContext); ok {
// data := cd.Data()

delete(cd, "app")
delete(cd, "routes")
// }

data := map[string]interface{}{
"routes": routes,
"error": trace,
Expand All @@ -218,13 +220,16 @@ func defaultErrorHandler(status int, origErr error, c Context) error {
return fmt.Sprintf("%+v", v)
},
}

ctx := plush.NewContextWith(data)
t, err := plush.Render(devErrorTmpl, ctx)
if err != nil {
return err
}

res := c.Response()
_, err = res.Write([]byte(t))

return err
}
return nil
Expand Down
41 changes: 41 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,32 @@ package buffalo
import (
"fmt"
"net/http"
"os"
"testing"

"github.com/gobuffalo/httptest"
"github.com/gobuffalo/logger"
"github.com/sirupsen/logrus"

"github.com/stretchr/testify/require"
)

//testLoggerHook is useful to test whats being logged.
type testLoggerHook struct {
errors []*logrus.Entry
}

func (lh *testLoggerHook) Fire(entry *logrus.Entry) error {
lh.errors = append(lh.errors, entry)
return nil
}

func (lh *testLoggerHook) Levels() []logrus.Level {
return []logrus.Level{
logrus.ErrorLevel,
}
}

func Test_defaultErrorHandler_SetsContentType(t *testing.T) {
r := require.New(t)
app := New(Options{})
Expand All @@ -24,6 +43,28 @@ func Test_defaultErrorHandler_SetsContentType(t *testing.T) {
r.Equal("text/html; charset=utf-8", ct)
}

func Test_defaultErrorHandler_Logger(t *testing.T) {
r := require.New(t)
app := New(Options{})
app.GET("/", func(c Context) error {
return c.Error(http.StatusUnauthorized, fmt.Errorf("boom"))
})

testHook := &testLoggerHook{}
l := logrus.New()
l.SetOutput(os.Stdout)
l.AddHook(testHook)
log := logger.Logrus{
FieldLogger: l,
}
app.Logger = log

w := httptest.New(app)
res := w.HTML("/").Get()
r.Equal(http.StatusUnauthorized, res.Code)
r.Equal(http.StatusUnauthorized, testHook.errors[0].Data["status"])
}

func Test_defaultErrorHandler_JSON(t *testing.T) {
r := require.New(t)
app := New(Options{})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require("expose-loader?$!expose-loader?jQuery!jquery");
require("bootstrap/dist/js/bootstrap.bundle.js");
require("@fortawesome/fontawesome-free/js/all.js");

$(() => {

Expand Down
Loading

0 comments on commit 74911df

Please sign in to comment.