Skip to content

Commit

Permalink
Misc fixes (#1042)
Browse files Browse the repository at this point in the history
  • Loading branch information
eandre authored Feb 26, 2024
1 parent 7ec340e commit 24b31e6
Show file tree
Hide file tree
Showing 12 changed files with 315 additions and 276 deletions.
2 changes: 1 addition & 1 deletion cli/cmd/encore/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func (d *Daemon) serveDaemon() {
func (d *Daemon) serveRuntime() {
log.Info().Stringer("addr", d.Runtime.Addr()).Msg("serving runtime")
rec := trace2.NewRecorder(d.Trace)
srv := runtime.NewServer(d.RunMgr, rec)
srv := engine.NewServer(d.RunMgr, rec)
d.exit <- http.Serve(d.Runtime, srv)
}

Expand Down
10 changes: 9 additions & 1 deletion cli/cmd/encore/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,16 @@ type convertLogOptions struct {

type convertLogOption func(*convertLogOptions)

func colorize(enable bool) convertLogOption {
return func(clo *convertLogOptions) {
clo.Color = enable
}
}

func convertJSONLogs(opts ...convertLogOption) outputConverter {
options := convertLogOptions{}
// Default to colorized output.
options := convertLogOptions{Color: true}

for _, opt := range opts {
opt(&options)
}
Expand Down
10 changes: 9 additions & 1 deletion cli/cmd/encore/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/logrusorgru/aurora/v3"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/term"

"encr.dev/cli/cmd/encore/cmdutil"
"encr.dev/cli/cmd/encore/root"
Expand All @@ -21,6 +22,8 @@ import (
)

var (
color bool
noColor bool // for "--no-color" compatibility
debug bool
watch bool
listen string
Expand All @@ -47,13 +50,18 @@ func init() {
},
}

isTerm := term.IsTerminal(int(os.Stdout.Fd()))

rootCmd.AddCommand(runCmd)
runCmd.Flags().BoolVar(&debug, "debug", false, "Compile for debugging (disables some optimizations)")
runCmd.Flags().BoolVarP(&watch, "watch", "w", true, "Watch for changes and live-reload")
runCmd.Flags().StringVar(&listen, "listen", "", "Address to listen on (for example \"0.0.0.0:4000\")")
runCmd.Flags().UintVarP(&port, "port", "p", 4000, "Port to listen on")
runCmd.Flags().BoolVar(&jsonLogs, "json", false, "Display logs in JSON format")
runCmd.Flags().StringVarP(&nsName, "namespace", "n", "", "Namespace to use (defaults to active namespace)")
runCmd.Flags().BoolVar(&color, "color", isTerm, "Whether to display colorized output")
runCmd.Flags().BoolVar(&noColor, "no-color", false, "Equivalent to --color=false")
runCmd.Flags().MarkHidden("no-color")
browser.AddFlag(runCmd)
}

Expand Down Expand Up @@ -113,7 +121,7 @@ func runApp(appRoot, wd string) {

var converter outputConverter
if !jsonLogs {
converter = convertJSONLogs()
converter = convertJSONLogs(colorize(color && !noColor))
}
code := streamCommandOutput(stream, converter)
if code == 0 {
Expand Down
5 changes: 2 additions & 3 deletions cli/cmd/encore/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ func runTests(appRoot, testDir string, args []string, traceFile string, codegenD
cancel()
}()

converter := convertJSONLogs(func(clo *convertLogOptions) {
clo.Color = !noColor
})
converter := convertJSONLogs(colorize(!noColor))
if slices.Contains(args, "-json") {
converter = convertTestEventOutputOnly(converter)
}
Expand Down Expand Up @@ -106,6 +104,7 @@ func init() {
// so that the help text is correct.
testCmd.Flags().Bool("codegen-debug", false, "Dump generated code (for debugging Encore's code generation)")
testCmd.Flags().String("trace", "", "Specifies a trace file to write trace information about the parse and compilation process to.")
testCmd.Flags().Bool("no-color", false, "Disable colorized output")

}

Expand Down
2 changes: 1 addition & 1 deletion cli/daemon/engine/runtime.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package runtime
package engine

import (
"bufio"
Expand Down
9 changes: 8 additions & 1 deletion e2e-tests/echo_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,15 @@ func doTestEndToEndWithApp(t *testing.T, env []string) {
})

c.Run("go_generated_client", func(c *qt.C) {
cmd := exec.Command("go", "run", ".", app.Addr)
encoreGoroot := os.Getenv("ENCORE_GOROOT")
c.Assert(encoreGoroot, qt.Not(qt.Equals), "")
goPath := filepath.Join(encoreGoroot, "bin", "go")
cmd := exec.Command(goPath, "run", ".", app.Addr)
cmd.Dir = filepath.Join("testdata", "echo_client")
cmd.Env = append(os.Environ(),
"GOROOT="+encoreGoroot,
"PATH="+fmt.Sprintf("%s%s%s", filepath.Join(encoreGoroot, "/bin"), string(filepath.ListSeparator), os.Getenv("PATH")),
)

out, err := cmd.CombinedOutput()
c.Assert(err, qt.IsNil, qt.Commentf("Got error running generated Go client: %s", out))
Expand Down
8 changes: 7 additions & 1 deletion e2e-tests/testdata/echo_client/js/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -559,8 +559,14 @@ class BaseClient {
this.baseURL = baseURL
this.headers = {
"Content-Type": "application/json",
"User-Agent": "slug-Generated-JS-Client (Encore/devel)",
}

// Add User-Agent header if the script is running in the server
// because browsers do not allow setting User-Agent headers to requests
if (typeof window === "undefined") {
this.headers["User-Agent"] = "slug-Generated-JS-Client (Encore/devel)";
}

this.requestInit = options.requestInit ?? {}

// Setup what fetch function we'll be using in the base client
Expand Down
8 changes: 7 additions & 1 deletion e2e-tests/testdata/echo_client/ts/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -801,8 +801,14 @@ class BaseClient {
this.baseURL = baseURL
this.headers = {
"Content-Type": "application/json",
"User-Agent": "slug-Generated-TS-Client (Encore/devel)",
}

// Add User-Agent header if the script is running in the server
// because browsers do not allow setting User-Agent headers to requests
if (typeof window === "undefined") {
this.headers["User-Agent"] = "slug-Generated-TS-Client (Encore/devel)";
}

this.requestInit = options.requestInit ?? {};

// Setup what fetch function we'll be using in the base client
Expand Down
1 change: 1 addition & 0 deletions e2e-tests/testdata/testscript/graceful_shutdown.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
run
shutdown
checklog '{"message": "shutting down"}'

-- svc/svc.go --
Expand Down
6 changes: 6 additions & 0 deletions e2e-tests/testscript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ func doRun(t *testing.T, experiments []string) {
setVal(ts, "app", app)
setVal(ts, "log", log)
},
"shutdown": func(ts *ts.TestScript, neg bool, args []string) {
app := getVal[*RunAppData](ts, "app")
app.Run.ProcGroup().Close()
},
"test": func(ts *ts.TestScript, neg bool, args []string) {
log := &testscriptLogger{ts: ts}
exp := ts.Getenv("ENCORE_EXPERIMENT")
Expand Down Expand Up @@ -212,6 +216,8 @@ func doRun(t *testing.T, experiments []string) {
ts.Fatalf("usage: checklog <pattern|file>")
}

time.Sleep(100 * time.Millisecond)

var want []jsonObj
var pattern jsonObj
err := json.Unmarshal([]byte(args[0]), &pattern)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require (
github.com/golang/protobuf v1.5.3
github.com/google/go-cmp v0.5.9
github.com/google/go-containerregistry v0.11.0
github.com/google/renameio/v2 v2.0.0
github.com/gorilla/websocket v1.5.0
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79
github.com/jackc/pgconn v1.14.0
Expand Down Expand Up @@ -97,7 +98,6 @@ require (
github.com/golang/glog v1.1.0 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/btree v1.0.1 // indirect
github.com/google/renameio/v2 v2.0.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
Expand Down
Loading

0 comments on commit 24b31e6

Please sign in to comment.