Skip to content

Commit

Permalink
Add edit command to testscripts
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed May 10, 2020
1 parent 9a3673a commit a708f66
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
21 changes: 21 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
Expand All @@ -28,6 +29,7 @@ func TestChezmoi(t *testing.T) {
Dir: filepath.Join("testdata", "scripts"),
Cmds: map[string]func(*testscript.TestScript, bool, []string){
"chhome": chHome,
"edit": edit,
},
Condition: func(cond string) (bool, error) {
switch cond {
Expand Down Expand Up @@ -72,6 +74,25 @@ func chHome(ts *testscript.TestScript, neg bool, args []string) {
}
}

// edit edits all of its arguments by appending "# edited\n" to them.
func edit(ts *testscript.TestScript, neg bool, args []string) {
if neg {
ts.Fatalf("unsupported ! edit")
}
for _, arg := range args {
filename := ts.MkAbs(arg)
data, err := ioutil.ReadFile(filename)
if err != nil {
ts.Fatalf("edit: %v", err)
}
data = append(data, []byte("# edited\n")...)
// FIXME preserve permissions
if err := ioutil.WriteFile(filename, data, 0o644); err != nil {
ts.Fatalf("edit: %v", err)
}
}
}

func setup(env *testscript.Env) error {
var (
binDir = filepath.Join(env.WorkDir, "bin")
Expand Down
5 changes: 5 additions & 0 deletions testdata/scripts/edit.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
chezmoi edit $HOME/.bashrc
grep -count=1 '# edited' $HOME/.local/share/chezmoi/dot_bashrc

-- home/user/.local/share/chezmoi/dot_bashrc --
# contents of .bashrc
4 changes: 2 additions & 2 deletions testdata/scripts/forget.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
chezmoi add $HOME${/}.bashrc
cmp $HOME${/}.local${/}share${/}chezmoi${/}dot_bashrc $HOME${/}.bashrc

chezmoi edit $HOME${/}.bashrc
edit $HOME${/}.local${/}share${/}chezmoi${/}dot_bashrc
grep -count=1 '# edited' $HOME${/}.local${/}share${/}chezmoi${/}dot_bashrc

chezmoi apply
grep -count=1 '# edited' $HOME${/}.bashrc

chezmoi edit $HOME${/}.bashrc
edit $HOME${/}.local${/}share${/}chezmoi${/}dot_bashrc
grep -count=2 '# edited' $HOME${/}.local${/}share${/}chezmoi${/}dot_bashrc

chezmoi forget $HOME${/}.bashrc
Expand Down
2 changes: 1 addition & 1 deletion testdata/scripts/hg.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ cmp $HOME${/}.bashrc $WORK${/}home${/}user${/}.bashrc

# create another commit
chhome home${/}user
chezmoi edit $HOME${/}.bashrc
edit $HOME${/}.local${/}share${/}chezmoi${/}dot_bashrc
chezmoi hg -- add dot_bashrc
chezmoi hg -- commit -m 'Update dot_bashrc'

Expand Down
2 changes: 1 addition & 1 deletion testdata/scripts/updategit.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ cmp $HOME${/}.bashrc $WORK${/}home${/}user${/}.bashrc

# create a new commit
chhome home${/}user
chezmoi edit $HOME${/}.bashrc
edit $HOME${/}.local${/}share${/}chezmoi${/}dot_bashrc
chezmoi git -- add dot_bashrc
chezmoi git -- commit -m 'Update dot_bashrc'

Expand Down

0 comments on commit a708f66

Please sign in to comment.