Skip to content

Commit

Permalink
feat(path): allow force adding path entries
Browse files Browse the repository at this point in the history
  • Loading branch information
kiliantyler authored and JanDeDobbeleer committed Apr 8, 2024
1 parent add80f1 commit 6771ffd
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/shell/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Path struct {
Value Template `yaml:"value"`
If If `yaml:"if"`
Persist bool `yaml:"persist"`
Force bool `yaml:"force"`

template string
}
Expand Down Expand Up @@ -55,7 +56,7 @@ func (p *Path) render() string {
continue
}

if context.Current.Path.Contains(line) {
if context.Current.Path.Contains(line) && !p.Force {
continue
}

Expand Down
134 changes: 134 additions & 0 deletions src/shell/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,137 @@ $env:PATH = '/Users/jan/.tools/bin:' + $env:PATH`,
assert.Equal(t, tc.Expected, DotFile.String(), tc.Case)
}
}

func TestPathForce(t *testing.T) {
cases := []struct {
Case string
Shell string
Path *Path
OS string
Expected string
}{
{
Case: "Unknown shell",
Shell: "FOO",
Path: &Path{Value: "/usr/local/bin"},
},
{
Case: "PWSH - Force",
Shell: PWSH,
Path: &Path{Value: "/usr/local/bin", Force: true},
Expected: `$env:PATH = '/usr/local/bin:' + $env:PATH`,
},
{
Case: "PWSH - Not Force",
Shell: PWSH,
Path: &Path{Value: "/usr/local/bin"},
Expected: ``,
},
{
Case: "CMD - Force",
Shell: CMD,
Path: &Path{Value: "/usr/local/bin", Force: true},
Expected: `os.setenv("PATH", "/usr/local/bin;" .. os.getenv("PATH"))`,
},
{
Case: "CMD - Not Force",
Shell: CMD,
Path: &Path{Value: "/usr/local/bin"},
Expected: ``,
},
{
Case: "FISH - Force",
Shell: FISH,
Path: &Path{Value: "/usr/local/bin", Force: true},
Expected: `fish_add_path /usr/local/bin`,
},
{
Case: "FISH - Not Force",
Shell: FISH,
Path: &Path{Value: "/usr/local/bin"},
Expected: ``,
},
{
Case: "NU - Force",
Shell: NU,
Path: &Path{Value: "/usr/local/bin", Force: true},
Expected: `$env.PATH = ($env.PATH | prepend "/usr/local/bin")`,
},
{
Case: "NU - Not Force",
Shell: NU,
Path: &Path{Value: "/usr/local/bin"},
Expected: ``,
},
{
Case: "NU - Windows Force",
Shell: NU,
OS: context.WINDOWS,
Path: &Path{Value: "C:\\bin\nD:\\bin", Force: true},
Expected: `$env.Path = ($env.Path | prepend "C:\\bin")
$env.Path = ($env.Path | prepend "D:\\bin")`,
},
{
Case: "NU - Windows Not Force",
Shell: NU,
OS: context.WINDOWS,
Path: &Path{Value: "C:\\bin\nD:\\bin"},
Expected: ``,
},
{
Case: "TCSH - Force",
Shell: TCSH,
Path: &Path{Value: "/usr/local/bin", Force: true},
Expected: `set path = ( /usr/local/bin $path );`,
},
{
Case: "TCSH - Not Force",
Shell: TCSH,
Path: &Path{Value: "/usr/local/bin"},
Expected: ``,
},
{
Case: "XONSH - Force",
Shell: XONSH,
Path: &Path{Value: "/usr/local/bin", Force: true},
Expected: `$PATH.add('/usr/local/bin', True, False)`,
},
{
Case: "XONSH - Not Force",
Shell: XONSH,
Path: &Path{Value: "/usr/local/bin"},
Expected: ``,
},
{
Case: "ZSH - Force",
Shell: ZSH,
Path: &Path{Value: "/usr/local/bin", Force: true},
Expected: `export PATH="/usr/local/bin:$PATH"`,
},
{
Case: "ZSH - Not Force",
Shell: ZSH,
Path: &Path{Value: "/usr/local/bin"},
Expected: ``,
},
{
Case: "ZSH - Windows Force",
Shell: ZSH,
OS: context.WINDOWS,
Path: &Path{Value: "/usr/local/bin", Force: true},
Expected: `export PATH="/usr/local/bin;$PATH"`,
},
{
Case: "ZSH - Windows Not Force",
Shell: ZSH,
OS: context.WINDOWS,
Path: &Path{Value: "/usr/local/bin"},
Expected: ``,
},
}

for _, tc := range cases {
context.Current = &context.Runtime{Shell: tc.Shell, Home: "/Users/jan", OS: tc.OS, Path: &context.Path{"/usr/local/bin", "C:\\bin", "D:\\bin"}}
assert.Equal(t, tc.Expected, tc.Path.string(), tc.Case)
}
}
2 changes: 2 additions & 0 deletions website/docs/setup/path.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ path:
- value: |
{{ .Home }}/go/bin/
{{ env "VOLTA_HOME" }}/bin
force: true
```
### Path
Expand All @@ -27,6 +28,7 @@ path:
| `value` | `string` | the path entires you want to add, separated by a newline. Supports [templating][templates] |
| `if` | `string` | golang [template][go-text-template] conditional statement, see [if][if] |
| `persist` | `boolean` | if you want to persist the path entry into the registry for the current user (Windows only) |
| `force` | `boolean` | if you want to always export the path even if it already exists in your current shell |

[templates]: templates.mdx
[go-text-template]: https://golang.org/pkg/text/template/
Expand Down

0 comments on commit 6771ffd

Please sign in to comment.