forked from jetify-com/devbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nix: allow user's shellrc to update PATH; add tests for shellrc templ…
…ate (jetify-com#106) At the end of the devbox shellrc file, we're setting: export PATH="$PURE_NIX_PATH:$ORIGINAL_PATH" This will overwrite anything that the user's shellrc might've added to PATH. Change ORIGINAL_PATH to PATH to preserve those changes. There's also no need for ORIGINAL_PATH to be an environment variable, so set it directly in the template instead. To prevent regressions, some tests that generate a devbox shellrc file and compare it to an expected golden file. This ensures we don't unknowingly change the way the shellrc is generated. If we do intentionally change the devbox shellrc, the tests can automatically update the golden files with -update: go test ./nix -update It's up to the author of the change to verify that any new shellrc files still work in a real shell.
- Loading branch information
Showing
15 changed files
with
400 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package nix | ||
|
||
import ( | ||
"errors" | ||
"flag" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
// update overwrites golden files with the new test results. | ||
var update = flag.Bool("update", false, "update the golden files with the test results") | ||
|
||
func TestWriteDevboxShellrc(t *testing.T) { | ||
testdirs, err := filepath.Glob("testdata/shellrc/*") | ||
if err != nil { | ||
t.Fatal("Error globbing testdata:", err) | ||
} | ||
|
||
// Load up all the necessary data from each testdata/shellrc directory | ||
// into a slice of tests cases. | ||
tests := make([]struct { | ||
name string | ||
env []string | ||
hook string | ||
shellrcPath string | ||
goldShellrcPath string | ||
goldShellrc []byte | ||
}, len(testdirs)) | ||
for i, path := range testdirs { | ||
test := &tests[i] | ||
test.name = filepath.Base(path) | ||
if b, err := os.ReadFile(filepath.Join(path, "env")); err == nil { | ||
test.env = strings.Split(string(b), "\n") | ||
} | ||
if b, err := os.ReadFile(filepath.Join(path, "hook")); err == nil { | ||
test.hook = string(b) | ||
} | ||
test.shellrcPath = filepath.Join(path, "shellrc") | ||
if _, err := os.Stat(test.shellrcPath); errors.Is(err, os.ErrNotExist) { | ||
test.shellrcPath = "noshellrc" | ||
} | ||
test.goldShellrcPath = filepath.Join(path, "shellrc.golden") | ||
test.goldShellrc, err = os.ReadFile(test.goldShellrcPath) | ||
if err != nil { | ||
t.Fatal("Got error reading golden file:", err) | ||
} | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
gotPath, err := writeDevboxShellrc(test.shellrcPath, test.hook, test.env) | ||
if err != nil { | ||
t.Fatal("Got writeDevboxShellrc error:", err) | ||
} | ||
gotShellrc, err := os.ReadFile(gotPath) | ||
if err != nil { | ||
t.Fatalf("Got error reading generated shellrc at %s: %v", gotPath, err) | ||
} | ||
|
||
// Overwrite the golden file if the -update flag was | ||
// set, and then proceed normally. The test should | ||
// always pass in this case. | ||
if *update { | ||
err = os.WriteFile(test.goldShellrcPath, gotShellrc, 0666) | ||
if err != nil { | ||
t.Error("Error updating golden files:", err) | ||
} | ||
} | ||
goldShellrc, err := os.ReadFile(test.goldShellrcPath) | ||
if err != nil { | ||
t.Fatal("Got error reading golden file:", err) | ||
} | ||
diff := cmp.Diff(goldShellrc, gotShellrc) | ||
if diff != "" { | ||
t.Errorf(strings.TrimSpace(` | ||
Generated shellrc != shellrc.golden (-shellrc.golden +shellrc): | ||
%s | ||
If the new shellrc is correct, you can update the golden file with: | ||
go test -run "^%s$" -update`), diff, t.Name()) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
PATH=/a/test/path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
echo "Hello from a devbox shell hook!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Set up the prompt | ||
|
||
autoload -Uz promptinit | ||
promptinit | ||
#prompt adam1 | ||
|
||
setopt histignorealldups sharehistory | ||
|
||
# Use emacs keybindings even if our EDITOR is set to vi | ||
bindkey -e | ||
|
||
# Keep 1000 lines of history within the shell and save it to ~/.zsh_history: | ||
HISTSIZE=1000 | ||
SAVEHIST=1000 | ||
HISTFILE=~/.zsh_history | ||
|
||
# Use modern completion system | ||
autoload -Uz compinit | ||
compinit | ||
|
||
zstyle ':completion:*' auto-description 'specify: %d' | ||
zstyle ':completion:*' completer _expand _complete _correct _approximate | ||
zstyle ':completion:*' format 'Completing %d' | ||
zstyle ':completion:*' group-name '' | ||
zstyle ':completion:*' menu select=2 | ||
eval "$(dircolors -b)" | ||
zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS} | ||
zstyle ':completion:*' list-colors '' | ||
zstyle ':completion:*' list-prompt %SAt %p: Hit TAB for more, or the character to insert%s | ||
zstyle ':completion:*' matcher-list '' 'm:{a-z}={A-Z}' 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=* l:|=*' | ||
zstyle ':completion:*' menu select=long | ||
zstyle ':completion:*' select-prompt %SScrolling active: current selection at %p%s | ||
zstyle ':completion:*' use-compctl false | ||
zstyle ':completion:*' verbose true | ||
|
||
zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#)*=0=01;31' | ||
zstyle ':completion:*:kill:*' command 'ps -u $USER -o pid,%cpu,tty,cputime,cmd' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Begin Devbox Pre-init Hook | ||
|
||
# Don't allow the user's shellrc to re-source Nix since we're already in Nix. | ||
export __ETC_PROFILE_NIX_SOURCED=1 | ||
|
||
# Put the Nix packages at the beginning of the PATH to give them priority over | ||
# programs outside of devbox. | ||
export PATH="$PURE_NIX_PATH" | ||
|
||
# Append any paths that were in the environment at the time the user launched | ||
# devbox. This gives the shell access to non-devbox programs, while still | ||
# preferring the ones that Nix installed. | ||
PATH="$PATH:/a/test/path" | ||
|
||
# End Devbox Pre-init Hook | ||
|
||
# Begin testdata/shellrc/basic/shellrc | ||
|
||
# Set up the prompt | ||
|
||
autoload -Uz promptinit | ||
promptinit | ||
#prompt adam1 | ||
|
||
setopt histignorealldups sharehistory | ||
|
||
# Use emacs keybindings even if our EDITOR is set to vi | ||
bindkey -e | ||
|
||
# Keep 1000 lines of history within the shell and save it to ~/.zsh_history: | ||
HISTSIZE=1000 | ||
SAVEHIST=1000 | ||
HISTFILE=~/.zsh_history | ||
|
||
# Use modern completion system | ||
autoload -Uz compinit | ||
compinit | ||
|
||
zstyle ':completion:*' auto-description 'specify: %d' | ||
zstyle ':completion:*' completer _expand _complete _correct _approximate | ||
zstyle ':completion:*' format 'Completing %d' | ||
zstyle ':completion:*' group-name '' | ||
zstyle ':completion:*' menu select=2 | ||
eval "$(dircolors -b)" | ||
zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS} | ||
zstyle ':completion:*' list-colors '' | ||
zstyle ':completion:*' list-prompt %SAt %p: Hit TAB for more, or the character to insert%s | ||
zstyle ':completion:*' matcher-list '' 'm:{a-z}={A-Z}' 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=* l:|=*' | ||
zstyle ':completion:*' menu select=long | ||
zstyle ':completion:*' select-prompt %SScrolling active: current selection at %p%s | ||
zstyle ':completion:*' use-compctl false | ||
zstyle ':completion:*' verbose true | ||
|
||
zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#)*=0=01;31' | ||
zstyle ':completion:*:kill:*' command 'ps -u $USER -o pid,%cpu,tty,cputime,cmd' | ||
|
||
# End testdata/shellrc/basic/shellrc | ||
|
||
# Begin Devbox Post-init Hook | ||
|
||
# Update the $PATH again so that the Nix packages take priority over the | ||
# programs outside of devbox. | ||
export PATH="$PURE_NIX_PATH:$PATH" | ||
|
||
# Prepend to the prompt to make it clear we're in a devbox shell. | ||
export PS1="(devbox) $PS1" | ||
|
||
# End Devbox Post-init Hook | ||
|
||
# Begin Devbox User Hook | ||
|
||
echo "Hello from a devbox shell hook!" | ||
|
||
# End Devbox User Hook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
PATH=/a/test/path |
Oops, something went wrong.