Skip to content

Commit

Permalink
fix(gcp): correct path on Windows
Browse files Browse the repository at this point in the history
resolves #2814
  • Loading branch information
JanDeDobbeleer committed Sep 23, 2022
1 parent 7ea9213 commit 1d244de
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 43 deletions.
38 changes: 22 additions & 16 deletions src/segments/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import (
"oh-my-posh/environment"
"oh-my-posh/properties"
"path"
"path/filepath"

"gopkg.in/ini.v1"
)

const (
GCPNOACTIVECONFIG = "NO ACTIVE CONFIG FOUND"
)

type Gcp struct {
props properties.Properties
env environment.Environment
Expand Down Expand Up @@ -37,39 +40,42 @@ func (g *Gcp) Enabled() bool {
}

cfgpath := path.Join(cfgDir, "configurations", "config_"+configFile)
cfg := g.env.FileContent(cfgpath)

if len(cfg) == 0 {
g.env.Log(environment.Error, "Gcp.Enabled()", "config file is empty")
return false
}

cfg, err := ini.Load(cfgpath)
data, err := ini.Load([]byte(cfg))
if err != nil {
g.env.Log(environment.Error, "Gcp.Enabled()", err.Error())
return false
}

g.Project = cfg.Section("core").Key("project").String()
g.Account = cfg.Section("core").Key("account").String()
g.Region = cfg.Section("compute").Key("region").String()
g.Project = data.Section("core").Key("project").String()
g.Account = data.Section("core").Key("account").String()
g.Region = data.Section("compute").Key("region").String()

return true
}

func (g *Gcp) getActiveConfig(cfgDir string) (string, error) {
ap := path.Join(cfgDir, "active_config")
absolutePath, err := filepath.Abs(ap)
if err != nil {
return "", err
}

fileContent := g.env.FileContent(absolutePath)
fileContent := g.env.FileContent(ap)
if len(fileContent) == 0 {
return "", errors.New("NO ACTIVE CONFIG FOUND")
return "", errors.New(GCPNOACTIVECONFIG)
}
return fileContent, nil
}

func (g *Gcp) getConfigDirectory() string {
cfgDir := g.env.Getenv("CLOUDSDK_CONFIG")
if len(cfgDir) == 0 {
cfgDir = path.Join(g.env.Home(), ".config", "gcloud")
if len(cfgDir) != 0 {
return cfgDir
}

return cfgDir
if g.env.GOOS() == environment.WINDOWS {
return path.Join(g.env.Getenv("APPDATA"), "gcloud")
}
return path.Join(g.env.Home(), ".config", "gcloud")
}
123 changes: 102 additions & 21 deletions src/segments/gcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package segments

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

"oh-my-posh/environment"
Expand All @@ -13,50 +12,132 @@ import (
)

func TestGcpSegment(t *testing.T) {
standardTemplate := "{{ if .Error }}{{ .Error }}{{ else }}{{ .Project }}{{ end }}"
allTemplate := "{{.Project}} :: {{.Region}} :: {{.Account}}"

cases := []struct {
Case string
Template string
ConfigPath string
CfgData string
ActiveConfig string
ExpectedEnabled bool
ExpectedString string
}{
{
Case: "all information",
Template: allTemplate,
ConfigPath: "../test/",
ActiveConfig: "gcptest",
Case: "happy path",
ExpectedEnabled: true,
ExpectedString: "test-test-test :: europe-test1 :: test@example.com",
ActiveConfig: "production",
CfgData: `
[core]
account = test@example.com
project = test-test-test
[compute]
region = europe-test1
`,
ExpectedString: "test-test-test :: europe-test1 :: test@example.com",
},
{
Case: "non-existent config file",
Template: standardTemplate,
ConfigPath: "../invalid/",
ActiveConfig: "nofile",
Case: "no active config",
ExpectedEnabled: false,
},
{
Case: "invalid active config file",
Template: standardTemplate,
ConfigPath: "../invalid/",
Case: "empty config",
ActiveConfig: "production",
ExpectedEnabled: false,
},
{
Case: "bad config",
ActiveConfig: "production",
CfgData: "{bad}",
ExpectedEnabled: false,
},
}

for _, tc := range cases {
env := new(mock.MockedEnvironment)
env.On("Getenv", "CLOUDSDK_CONFIG").Return(tc.ConfigPath)
fcPath, _ := filepath.Abs(path.Join(tc.ConfigPath, "active_config"))
env.On("Getenv", "CLOUDSDK_CONFIG").Return("config")
fcPath := path.Join("config", "active_config")
env.On("FileContent", fcPath).Return(tc.ActiveConfig)
cfgpath := path.Join("config", "configurations", "config_production")
env.On("FileContent", cfgpath).Return(tc.CfgData)
env.On("Log", environment.Error, "Gcp.Enabled()", mock2.Anything).Return()
g := &Gcp{
env: env,
}
assert.Equal(t, tc.ExpectedEnabled, g.Enabled(), tc.Case)
if tc.ExpectedEnabled {
assert.Equal(t, tc.ExpectedString, renderTemplate(env, tc.Template, g), tc.Case)
assert.Equal(t, tc.ExpectedString, renderTemplate(env, "{{.Project}} :: {{.Region}} :: {{.Account}}", g), tc.Case)
}
}
}

func TestGetConfigDirectory(t *testing.T) {
cases := []struct {
Case string
GOOS string
Home string
AppData string
CloudSDKConfig string
Expected string
}{
{
Case: "CLOUDSDK_CONFIG",
CloudSDKConfig: "/Users/posh/.config/gcloud",
Expected: "/Users/posh/.config/gcloud",
},
{
Case: "Windows",
GOOS: environment.WINDOWS,
AppData: "/Users/posh/.config",
Expected: "/Users/posh/.config/gcloud",
},
{
Case: "default",
Home: "/Users/posh2/",
Expected: "/Users/posh2/.config/gcloud",
},
}

for _, tc := range cases {
env := new(mock.MockedEnvironment)
env.On("Getenv", "CLOUDSDK_CONFIG").Return(tc.CloudSDKConfig)
env.On("Getenv", "APPDATA").Return(tc.AppData)
env.On("Home").Return(tc.Home)
env.On("GOOS").Return(tc.GOOS)
g := &Gcp{
env: env,
}
assert.Equal(t, tc.Expected, g.getConfigDirectory(), tc.Case)
}
}

func TestGetActiveConfig(t *testing.T) {
cases := []struct {
Case string
ActiveConfig string
ExpectedString string
ExpectedError string
}{
{
Case: "No active config",
ExpectedError: GCPNOACTIVECONFIG,
},
{
Case: "No active config",
ActiveConfig: "production",
ExpectedString: "production",
},
}

for _, tc := range cases {
env := new(mock.MockedEnvironment)
env.On("FileContent", "active_config").Return(tc.ActiveConfig)
g := &Gcp{
env: env,
}
got, err := g.getActiveConfig("")
assert.Equal(t, tc.ExpectedString, got, tc.Case)
if len(tc.ExpectedError) > 0 {
assert.EqualError(t, err, tc.ExpectedError, tc.Case)
} else {
assert.NoError(t, err, tc.Case)
}
}
}
6 changes: 0 additions & 6 deletions src/test/configurations/config_gcptest

This file was deleted.

0 comments on commit 1d244de

Please sign in to comment.