Skip to content

Commit

Permalink
gopls/internal/server: don't interact with os.UserConfigDir from tests
Browse files Browse the repository at this point in the history
This change makes it such that tests must set GOPLS_CONFIG_DIR if they
want to exercise behavior related to the telemetry prompt. Tests should
not interact with os.UserConfigDir.

Change-Id: I9f6dfdea2408f75085855a82fc50fad20cda82e3
Reviewed-on: https://go-review.googlesource.com/c/tools/+/625795
Reviewed-by: Hyang-Ah Hana Kim <[email protected]>
Auto-Submit: Robert Findley <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
  • Loading branch information
findleyr committed Nov 5, 2024
1 parent 59933b6 commit 9d40727
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions gopls/internal/server/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"path/filepath"
"strconv"
"testing"
"time"

"golang.org/x/telemetry"
Expand Down Expand Up @@ -64,19 +65,6 @@ func (s *server) getenv(key string) string {
return os.Getenv(key)
}

// configDir returns the root of the gopls configuration dir. By default this
// is os.UserConfigDir/gopls, but it may be overridden for tests.
func (s *server) configDir() (string, error) {
if d := s.getenv(GoplsConfigDirEnvvar); d != "" {
return d, nil
}
userDir, err := os.UserConfigDir()
if err != nil {
return "", err
}
return filepath.Join(userDir, "gopls"), nil
}

// telemetryMode returns the current effective telemetry mode.
// By default this is x/telemetry.Mode(), but it may be overridden for tests.
func (s *server) telemetryMode() string {
Expand Down Expand Up @@ -119,11 +107,20 @@ func (s *server) maybePromptForTelemetry(ctx context.Context, enabled bool) {
}

// Only prompt if we can read/write the prompt config file.
configDir, err := s.configDir()
if err != nil {
errorf("unable to determine config dir: %v", err)
configDir := s.getenv(GoplsConfigDirEnvvar) // set for testing
if configDir == "" && testing.Testing() {
// Unless tests set GoplsConfigDirEnvvar, the prompt is a no op.
// We don't want tests to interact with os.UserConfigDir().
return
}
if configDir == "" {
userDir, err := os.UserConfigDir()
if err != nil {
errorf("unable to determine user config dir: %v", err)
return
}
configDir = filepath.Join(userDir, "gopls")
}

// Read the current prompt file.

Expand Down

0 comments on commit 9d40727

Please sign in to comment.