fix(firebase): sub path validation

This commit is contained in:
Ivan 2024-04-19 08:50:40 -04:00 committed by GitHub
parent abe8c80451
commit a47070b9d8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 9 deletions

View file

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"path/filepath" "path/filepath"
"strings"
"github.com/jandedobbeleer/oh-my-posh/src/platform" "github.com/jandedobbeleer/oh-my-posh/src/platform"
"github.com/jandedobbeleer/oh-my-posh/src/properties" "github.com/jandedobbeleer/oh-my-posh/src/properties"
@ -53,7 +54,7 @@ func (f *Firebase) Enabled() bool {
// Test if the current directory is a project path // Test if the current directory is a project path
// and if it is, return the project name // and if it is, return the project name
for key, value := range data.ActiveProject { for key, value := range data.ActiveProject {
if key == f.env.Pwd() { if strings.HasPrefix(f.env.Pwd(), key) {
f.Project = value f.Project = value
return true return true
} }

View file

@ -10,22 +10,31 @@ import (
) )
func TestFirebaseSegment(t *testing.T) { func TestFirebaseSegment(t *testing.T) {
config := `{
"activeProjects": {
"path": "project-name"
}
}`
cases := []struct { cases := []struct {
Case string Case string
CfgData string
ActiveConfig string ActiveConfig string
ActivePath string
ExpectedEnabled bool ExpectedEnabled bool
ExpectedString string ExpectedString string
}{ }{
{ {
Case: "happy path", Case: "happy path",
ExpectedEnabled: true, ExpectedEnabled: true,
ActiveConfig: `{ ActiveConfig: config,
"activeProjects": { ActivePath: "path",
"path": "project-name" ExpectedString: "project-name",
} },
}`, {
ExpectedString: "project-name", Case: "happy subpath",
ExpectedEnabled: true,
ActiveConfig: config,
ActivePath: "path/subpath",
ExpectedString: "project-name",
}, },
{ {
Case: "no active config", Case: "no active config",
@ -46,7 +55,7 @@ func TestFirebaseSegment(t *testing.T) {
for _, tc := range cases { for _, tc := range cases {
env := new(mock.MockedEnvironment) env := new(mock.MockedEnvironment)
env.On("Home").Return("home") env.On("Home").Return("home")
env.On("Pwd").Return("path") env.On("Pwd").Return(tc.ActivePath)
fcPath := filepath.Join("home", ".config", "configstore", "firebase-tools.json") fcPath := filepath.Join("home", ".config", "configstore", "firebase-tools.json")
env.On("FileContent", fcPath).Return(tc.ActiveConfig) env.On("FileContent", fcPath).Return(tc.ActiveConfig)
env.On("Error", mock2.Anything).Return() env.On("Error", mock2.Anything).Return()