Skip to content

Commit

Permalink
v0.0.33: un-silence command line error
Browse files Browse the repository at this point in the history
  • Loading branch information
benburkert committed May 10, 2024
1 parent 70e545a commit 19d5f64
Show file tree
Hide file tree
Showing 34 changed files with 443 additions and 622 deletions.
50 changes: 24 additions & 26 deletions api/apitest/apitest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@ package apitest

import (
"context"
"flag"
"fmt"
"path/filepath"
"strings"

"io"
"log"
"net"
"net/http"
"os"
"os/exec"
"time"

_ "github.com/anchordotdev/cli/testflags"
"github.com/gofrs/flock"
"github.com/spf13/pflag"
"golang.org/x/sync/errgroup"
)

var (
proxy = flag.Bool("prism-proxy", false, "run prism in proxy mode")
verbose = flag.Bool("prism-verbose", false, "verbose output for prism/rails servers")
oapiConfig = flag.String("oapi-config", "config/openapi.yml", "openapi spec file path")
lockfile = flag.String("api-lockfile", "tmp/apitest.lock", "rails server lockfile path")
proxy, _ = pflag.CommandLine.GetBool("prism-proxy")
verbose, _ = pflag.CommandLine.GetBool("prism-verbose")
oapiConfig, _ = pflag.CommandLine.GetString("oapi-config")
lockfile, _ = pflag.CommandLine.GetString("api-lockfile")
)

type Server struct {
Expand All @@ -45,7 +45,7 @@ func (s *Server) Close() {
}

func (s *Server) IsProxy() bool {
return *proxy
return proxy
}

func (s *Server) GeneratePAT(email string) (string, error) {
Expand All @@ -65,7 +65,7 @@ func (s *Server) GeneratePAT(email string) (string, error) {
}

func (s *Server) Start(ctx context.Context) error {
if *proxy {
if proxy {
return s.StartProxy(ctx)
}
return s.StartMock(ctx)
Expand Down Expand Up @@ -105,7 +105,7 @@ func (s *Server) StartMock(ctx context.Context) error {
func (s *Server) StartProxy(ctx context.Context) error {
ctx, stopfn := context.WithCancel(ctx)

lock := flock.New(filepath.Join(s.RootDir, *lockfile))
lock := flock.New(filepath.Join(s.RootDir, lockfile))
if err := lock.Lock(); err != nil {
stopfn()
return err
Expand Down Expand Up @@ -185,7 +185,7 @@ func (s *Server) startMock(ctx context.Context) (string, func() error, error) {
}

args := []string{
"npx", "prism", "mock", *oapiConfig,
"npx", "prism", "mock", oapiConfig,
"--port", port, "--host", host,
}

Expand All @@ -209,7 +209,7 @@ func (s *Server) startCmd(ctx context.Context, args []string) (func() error, err
}

wait := cmd.Wait
if *verbose {
if verbose {
var err error
if wait, err = drainCmd(cmd); err != nil {
return nil, err
Expand Down Expand Up @@ -255,7 +255,7 @@ func (s *Server) startProxy(ctx context.Context, upstreamAddr string) (string, f
upstreamURL := "http://" + upstreamAddr + "/"

args := []string{
"npx", "prism", "proxy", *oapiConfig, upstreamURL,
"npx", "prism", "proxy", oapiConfig, upstreamURL,
"--port", port, "--host", host,
}

Expand All @@ -267,8 +267,18 @@ func (s *Server) startProxy(ctx context.Context, upstreamAddr string) (string, f
}

func (s *Server) waitTCP(addr string) error {
var dialer net.Dialer

duration := 16 * time.Second
ctx, cancel := context.WithTimeoutCause(
context.Background(),
duration,
fmt.Errorf("waitTcp %s timeout waiting for: %s", duration, addr),
)
defer cancel()

for {
if conn, err := net.Dial("tcp4", addr); err == nil {
if conn, err := dialer.DialContext(ctx, "tcp4", addr); err == nil {
conn.Close()
return nil
} else if !isConnRefused(err) {
Expand All @@ -279,18 +289,6 @@ func (s *Server) waitTCP(addr string) error {
}
}

func (s *Server) waitHTTP(errc chan<- error) {
for {
res, err := http.Get(s.URL)
if err != nil {
log.Printf("err = %q %#v", err, err)
} else {
log.Printf("res = %#v", res)
return
}
}
}

func drainCmd(cmd *exec.Cmd) (func() error, error) {
stdout, err := cmd.StdoutPipe()
if err != nil {
Expand Down
3 changes: 0 additions & 3 deletions auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package auth

import (
"context"
"flag"
"testing"

"github.com/anchordotdev/cli/api/apitest"
Expand All @@ -15,8 +14,6 @@ var srv = &apitest.Server{
}

func TestMain(m *testing.M) {
flag.Parse()

if err := srv.Start(context.Background()); err != nil {
panic(err)
}
Expand Down
5 changes: 3 additions & 2 deletions cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/anchordotdev/cli"
_ "github.com/anchordotdev/cli/testflags"
"github.com/anchordotdev/cli/ui"
"github.com/anchordotdev/cli/ui/uitest"
tea "github.com/charmbracelet/bubbletea"
Expand Down Expand Up @@ -87,7 +88,7 @@ func TestError(t *testing.T) {
require.EqualError(t, returnedError, testErr.Error())

tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second*3))
teatest.RequireEqualOutput(t, drv.FinalOut())
uitest.TestGolden(t, drv.Golden())
}()
defer cli.Cleanup(&returnedError, nil, ctx, drv, CmdError, []string{})
returnedError = cmd.UI().RunTUI(ctx, drv)
Expand Down Expand Up @@ -133,7 +134,7 @@ func TestPanic(t *testing.T) {
require.EqualError(t, returnedError, "test panic")

tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second*3))
teatest.RequireEqualOutput(t, drv.FinalOut())
uitest.TestGolden(t, drv.Golden())
}()
defer cli.Cleanup(&returnedError, nil, ctx, drv, CmdPanic, []string{})
_ = cmd.UI().RunTUI(ctx, drv)
Expand Down
16 changes: 0 additions & 16 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ func NewCmd[T UIer](parent *cobra.Command, name string, fn func(*cobra.Command))
Args: def.Args,
Short: def.Short,
Long: def.Long,
SilenceErrors: true,
SilenceUsage: true,
}

Expand All @@ -232,21 +231,6 @@ func NewCmd[T UIer](parent *cobra.Command, name string, fn func(*cobra.Command))

fn(cmd)

// FIXME: ideally we would only set these in TEST
// allow pass through of update arg for teatest golden tests
cmd.Flags().Bool("update", false, "update .golden files")
if err := cmd.Flags().MarkHidden("update"); err != nil {
panic(err)
}
cmd.Flags().Bool("prism-proxy", false, "run prism in proxy mode")
if err := cmd.Flags().MarkHidden("prism-proxy"); err != nil {
panic(err)
}
cmd.Flags().Bool("prism-verbose", false, "run prism in verbose mode")
if err := cmd.Flags().MarkHidden("prism-verbose"); err != nil {
panic(err)
}

cmd.RunE = func(cmd *cobra.Command, args []string) (returnedError error) {
cfg := ConfigFromCmd(cmd)
if cfg.Test.SkipRunE {
Expand Down
4 changes: 2 additions & 2 deletions cmdtest/cmdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"

"github.com/anchordotdev/cli"
"github.com/charmbracelet/x/exp/teatest"
"github.com/anchordotdev/cli/ui/uitest"
"github.com/joeshaw/envdecode"
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -49,7 +49,7 @@ func TestHelp(t *testing.T, cmd *cobra.Command, args ...string) {
out, err := io.ReadAll(b)
require.NoError(t, err)

teatest.RequireEqualOutput(t, out)
uitest.TestGolden(t, string(out))
}

func execute(cmd *cobra.Command, args ...string) (*bytes.Buffer, error) {
Expand Down
7 changes: 1 addition & 6 deletions detection/detection_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package detection

import (
"flag"
"slices"
"testing"
"testing/fstest"
)

var (
_ = flag.Bool("prism-verbose", false, "ignored")
_ = flag.Bool("prism-proxy", false, "ignored")
_ = flag.Bool("update", false, "ignored")
_ "github.com/anchordotdev/cli/testflags"
)

func TestScore_String(t *testing.T) {
Expand Down
8 changes: 1 addition & 7 deletions diagnostic/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,12 @@ import (
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"flag"
"net"
"net/http"
"testing"

"github.com/anchordotdev/cli/internal/must"
)

var (
_ = flag.Bool("prism-verbose", false, "ignored")
_ = flag.Bool("prism-proxy", false, "ignored")
_ = flag.Bool("update", false, "ignored")
_ "github.com/anchordotdev/cli/testflags"
)

func TestServerSupportsDualProtocols(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21
require (
github.com/MakeNowJust/heredoc v1.0.0
github.com/atotto/clipboard v0.1.4
github.com/aymanbagabas/go-udiff v0.2.0
github.com/charmbracelet/bubbles v0.18.0
github.com/charmbracelet/bubbletea v0.25.0
github.com/charmbracelet/lipgloss v0.10.0
Expand Down Expand Up @@ -33,7 +34,6 @@ require (
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect
github.com/alessio/shellescape v1.4.1 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/aymanbagabas/go-udiff v0.2.0 // indirect
github.com/charmbracelet/x/exp/golden v0.0.0-20240222125807-0344fda748f8 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
Expand Down
8 changes: 1 addition & 7 deletions keyring/keyring_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
package keyring

import (
"flag"
"testing"

"github.com/anchordotdev/cli"
_ "github.com/anchordotdev/cli/testflags"
"github.com/zalando/go-keyring"
)

var (
_ = flag.Bool("prism-verbose", false, "ignored")
_ = flag.Bool("prism-proxy", false, "ignored")
_ = flag.Bool("update", false, "ignored")
)

func TestKeyring(t *testing.T) {
cfg := new(cli.Config)
cfg.Keyring.MockMode = true
Expand Down
10 changes: 4 additions & 6 deletions lcl/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/anchordotdev/cli/auth"
"github.com/anchordotdev/cli/lcl/models"
"github.com/anchordotdev/cli/trust"
truststoreModels "github.com/anchordotdev/cli/truststore/models"
"github.com/anchordotdev/cli/ui"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -86,17 +87,14 @@ func (c Audit) perform(ctx context.Context, drv *ui.Driver) (*LclAuditResult, er
drv.Send(models.AuditResourcesFoundMsg{})
}

drv.Activate(ctx, &models.AuditTrust{})
drv.Activate(ctx, &truststoreModels.TrustStoreAudit{})

// FIXME: use config anc?
trustStoreAuditResult, err := trust.PerformAudit(ctx, c.anc, c.orgSlug, c.realmSlug)
auditInfo, err := trust.PerformAudit(ctx, c.anc, c.orgSlug, c.realmSlug)
if err != nil {
return nil, err
}

missingCount := len(trustStoreAuditResult.Missing)
result.trusted = missingCount == 0
drv.Send(models.AuditTrustMissingMsg(missingCount))
drv.Send(truststoreModels.AuditInfoMsg(auditInfo))

// TODO: audit local app status

Expand Down
2 changes: 1 addition & 1 deletion lcl/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func (c LclConfig) perform(ctx context.Context, drv *ui.Driver) error {
RealmSlug: c.realmSlug,
}

if err := cmdTrust.UI().RunTUI(ctx, drv); err != nil {
if err = cmdTrust.Perform(ctx, drv); err != nil {
return err
}
}
Expand Down

0 comments on commit 19d5f64

Please sign in to comment.