Skip to content

Commit

Permalink
fix(firebase): sub path validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-the-terrible authored Apr 19, 2024
1 parent abe8c80 commit a47070b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/segments/firebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"path/filepath"
"strings"

"github.com/jandedobbeleer/oh-my-posh/src/platform"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
Expand Down Expand Up @@ -53,7 +54,7 @@ func (f *Firebase) Enabled() bool {
// Test if the current directory is a project path
// and if it is, return the project name
for key, value := range data.ActiveProject {
if key == f.env.Pwd() {
if strings.HasPrefix(f.env.Pwd(), key) {
f.Project = value
return true
}
Expand Down
25 changes: 17 additions & 8 deletions src/segments/firebase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,31 @@ import (
)

func TestFirebaseSegment(t *testing.T) {
config := `{
"activeProjects": {
"path": "project-name"
}
}`
cases := []struct {
Case string
CfgData string
ActiveConfig string
ActivePath string
ExpectedEnabled bool
ExpectedString string
}{
{
Case: "happy path",
ExpectedEnabled: true,
ActiveConfig: `{
"activeProjects": {
"path": "project-name"
}
}`,
ExpectedString: "project-name",
ActiveConfig: config,
ActivePath: "path",
ExpectedString: "project-name",
},
{
Case: "happy subpath",
ExpectedEnabled: true,
ActiveConfig: config,
ActivePath: "path/subpath",
ExpectedString: "project-name",
},
{
Case: "no active config",
Expand All @@ -46,7 +55,7 @@ func TestFirebaseSegment(t *testing.T) {
for _, tc := range cases {
env := new(mock.MockedEnvironment)
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")
env.On("FileContent", fcPath).Return(tc.ActiveConfig)
env.On("Error", mock2.Anything).Return()
Expand Down

0 comments on commit a47070b

Please sign in to comment.