Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func newRootCommand(cnf *config.Config, assets *vendorization.VendorAssets) *cob
// Completions must be fast and quiet.
return
}
quiet := viper.GetBool("quiet") && !viper.GetBool("debug") && !viper.GetBool("verbose")
quiet := isQuiet()
if quiet {
viper.Set("no-interaction", true)
cmd.SetErr(io.Discard)
Expand Down Expand Up @@ -294,14 +294,19 @@ func debugLogf(format string, v ...any) {
fmt.Fprintf(color.Error, prefix+" "+strings.TrimSpace(format)+"\n", v...)
}

// isQuiet reports whether quiet mode is on, which --debug and --verbose override.
func isQuiet() bool {
return viper.GetBool("quiet") && !viper.GetBool("debug") && !viper.GetBool("verbose")
}

func exitWithError(err error) {
var execErr *exec.ExitError
if errors.As(err, &execErr) {
exitCode := execErr.ExitCode()
debugLogf(err.Error())
os.Exit(exitCode)
}
if !viper.GetBool("quiet") {
if !isQuiet() {
fmt.Fprintln(color.Error, color.RedString(err.Error()))
}
os.Exit(1)
Expand Down
38 changes: 38 additions & 0 deletions integration-tests/quiet_errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package tests

import (
"os"
"path/filepath"
"testing"

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

// TestQuietErrors checks that --verbose and --debug override --quiet for errors before the legacy CLI runs.
func TestQuietErrors(t *testing.T) {
f := newCommandFactory(t, "", "")
// A file in place of the temporary directory makes the legacy CLI fail to start.
tmpFile := filepath.Join(t.TempDir(), "file")
require.NoError(t, os.WriteFile(tmpFile, nil, 0o600))
f.extraEnv = append(f.extraEnv, EnvPrefix+"TMP="+tmpFile)

cases := []struct {
args []string
wantError bool
}{
{[]string{"-q"}, false},
{[]string{"-qv"}, true},
{[]string{"-q", "--debug"}, true},
{nil, true},
}
for _, c := range cases {
_, stderr, err := f.RunCombinedOutput(append([]string{"project:list"}, c.args...)...)
assertExitCode(t, 1, err)
if c.wantError {
assert.Contains(t, stderr, "failed to initialize PHP CLI", "args: %v", c.args)
} else {
assert.Empty(t, stderr, "args: %v", c.args)
}
}
}
5 changes: 4 additions & 1 deletion integration-tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,10 @@ func (f *cmdFactory) fakeBrowser() {
f.t.Skip("the fake browser is a shell script")
}
dir := f.t.TempDir()
require.NoError(f.t, os.WriteFile(filepath.Join(dir, "xdg-open"), []byte("#!/bin/sh\nexit 0\n"), 0o755))
// The CLI uses "open" on macOS, and "xdg-open" on Linux.
for _, name := range []string{"open", "xdg-open"} {
require.NoError(f.t, os.WriteFile(filepath.Join(dir, name), []byte("#!/bin/sh\nexit 0\n"), 0o755))
}
f.extraEnv = append(f.extraEnv, "DISPLAY=:0", "PATH="+dir+string(os.PathListSeparator)+os.Getenv("PATH"))
}

Expand Down
Loading