diff --git a/src/segments/gcp.go b/src/segments/gcp.go index 163630d9..4b73d6a1 100644 --- a/src/segments/gcp.go +++ b/src/segments/gcp.go @@ -17,11 +17,10 @@ type Gcp struct { Account string Project string Region string - Error string } func (g *Gcp) Template() string { - return " {{ if not .Error }}{{ .Project }}{{ end }} " + return " {{ .Project }} " } func (g *Gcp) Init(props properties.Properties, env environment.Environment) { @@ -33,16 +32,16 @@ func (g *Gcp) Enabled() bool { cfgDir := g.getConfigDirectory() configFile, err := g.getActiveConfig(cfgDir) if err != nil { - g.Error = err.Error() - return true + g.env.Log(environment.Error, "Gcp.Enabled()", err.Error()) + return false } cfgpath := path.Join(cfgDir, "configurations", "config_"+configFile) cfg, err := ini.Load(cfgpath) if err != nil { - g.Error = "GCLOUD CONFIG ERROR" - return true + g.env.Log(environment.Error, "Gcp.Enabled()", err.Error()) + return false } g.Project = cfg.Section("core").Key("project").String() diff --git a/src/segments/gcp_test.go b/src/segments/gcp_test.go index 46d33b56..bba9f20c 100644 --- a/src/segments/gcp_test.go +++ b/src/segments/gcp_test.go @@ -5,9 +5,11 @@ import ( "path/filepath" "testing" + "oh-my-posh/environment" "oh-my-posh/mock" "github.com/stretchr/testify/assert" + mock2 "github.com/stretchr/testify/mock" ) func TestGcpSegment(t *testing.T) { @@ -31,20 +33,15 @@ func TestGcpSegment(t *testing.T) { ExpectedString: "test-test-test :: europe-test1 :: test@example.com", }, { - Case: "non-existent config file", - Template: standardTemplate, - ConfigPath: "../invalid/", - ActiveConfig: "nofile", - ExpectedEnabled: true, - ExpectedString: "GCLOUD CONFIG ERROR", + Case: "non-existent config file", + Template: standardTemplate, + ConfigPath: "../invalid/", + ActiveConfig: "nofile", }, { - Case: "invalid active config file", - Template: standardTemplate, - ConfigPath: "../invalid/", - ActiveConfig: "", - ExpectedEnabled: true, - ExpectedString: "NO ACTIVE CONFIG FOUND", + Case: "invalid active config file", + Template: standardTemplate, + ConfigPath: "../invalid/", }, } @@ -53,10 +50,13 @@ func TestGcpSegment(t *testing.T) { env.On("Getenv", "CLOUDSDK_CONFIG").Return(tc.ConfigPath) fcPath, _ := filepath.Abs(path.Join(tc.ConfigPath, "active_config")) env.On("FileContent", fcPath).Return(tc.ActiveConfig) + env.On("Log", environment.Error, "Gcp.Enabled()", mock2.Anything).Return() g := &Gcp{ env: env, } assert.Equal(t, tc.ExpectedEnabled, g.Enabled(), tc.Case) - assert.Equal(t, tc.ExpectedString, renderTemplate(env, tc.Template, g), tc.Case) + if tc.ExpectedEnabled { + assert.Equal(t, tc.ExpectedString, renderTemplate(env, tc.Template, g), tc.Case) + } } }