Skip to content

Commit 9d40727

Browse files
committed
gopls/internal/server: don't interact with os.UserConfigDir from tests
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]>
1 parent 59933b6 commit 9d40727

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

gopls/internal/server/prompt.go

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"os"
1212
"path/filepath"
1313
"strconv"
14+
"testing"
1415
"time"
1516

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

67-
// configDir returns the root of the gopls configuration dir. By default this
68-
// is os.UserConfigDir/gopls, but it may be overridden for tests.
69-
func (s *server) configDir() (string, error) {
70-
if d := s.getenv(GoplsConfigDirEnvvar); d != "" {
71-
return d, nil
72-
}
73-
userDir, err := os.UserConfigDir()
74-
if err != nil {
75-
return "", err
76-
}
77-
return filepath.Join(userDir, "gopls"), nil
78-
}
79-
8068
// telemetryMode returns the current effective telemetry mode.
8169
// By default this is x/telemetry.Mode(), but it may be overridden for tests.
8270
func (s *server) telemetryMode() string {
@@ -119,11 +107,20 @@ func (s *server) maybePromptForTelemetry(ctx context.Context, enabled bool) {
119107
}
120108

121109
// Only prompt if we can read/write the prompt config file.
122-
configDir, err := s.configDir()
123-
if err != nil {
124-
errorf("unable to determine config dir: %v", err)
110+
configDir := s.getenv(GoplsConfigDirEnvvar) // set for testing
111+
if configDir == "" && testing.Testing() {
112+
// Unless tests set GoplsConfigDirEnvvar, the prompt is a no op.
113+
// We don't want tests to interact with os.UserConfigDir().
125114
return
126115
}
116+
if configDir == "" {
117+
userDir, err := os.UserConfigDir()
118+
if err != nil {
119+
errorf("unable to determine user config dir: %v", err)
120+
return
121+
}
122+
configDir = filepath.Join(userDir, "gopls")
123+
}
127124

128125
// Read the current prompt file.
129126

0 commit comments

Comments
 (0)