Skip to content

Commit

Permalink
apply tests
Browse files Browse the repository at this point in the history
  • Loading branch information
itayd committed May 26, 2022
1 parent 19e5417 commit eea1f6c
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 8 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,12 @@ $ pounce -s ''

# TODO

- [ ] apply tests.
- [ ] deal with colons in file names.
- [ ] deal with no EOL last line.
- [ ] apply: only generate backup files if content actually changed.
- [ ] apply: read/write piece wise.
- [ ] deal with no EOL on last line of files.
- [ ] `\n` vs `\r\n`?
- [ ] example in README could be better.
- [ ] cli flags can be better phrased.
- [ ] apply: only generate backup files if content actually changed?
- [ ] apply: read/write piece wise.
- [ ] cli flags docs can be better phrased.

# Disclaimer

Expand Down
7 changes: 7 additions & 0 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@ func trimPath(p string) string {

var commonConfig struct {
print, nowd, collect, apply bool
dir string
}

var commonFlags = []cli.Flag{
&cli.StringFlag{
Name: "dir",
Aliases: []string{"d"},
Destination: &commonConfig.dir,
Usage: "set working directory",
},
&cli.BoolFlag{
Name: "print",
Aliases: []string{"p"},
Expand Down
8 changes: 7 additions & 1 deletion pounce.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ var app = cli.App{
Version: version,
Flags: append(append(commonFlags, collectFlags...), applyFlags...),
Action: func(c *cli.Context) error {
if dir := commonConfig.dir; dir != "" {
if err := os.Chdir(dir); err != nil {
return fmt.Errorf("chdir: %w", err)
}
}

args := c.Args().Slice()

if commonConfig.collect && commonConfig.apply ||
Expand Down Expand Up @@ -68,7 +74,7 @@ func both(werr io.Writer, args []string) error {
cmd.Stderr = os.Stderr

if err := cmd.Run(); err != nil {
if err := err.(*exec.ExitError); err != nil {
if err, ok := err.(*exec.ExitError); ok && err != nil {
return fmt.Errorf("editor exited with code %d", err.ExitCode())
}

Expand Down
1 change: 0 additions & 1 deletion tests/apply.clitest

This file was deleted.

85 changes: 85 additions & 0 deletions tests/both.clitest
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
$ dir0=$(mktemp -d)
$ trap "rm -fR ${dir0}" 0
$ cd "${dir0}"
$ cp -r "${TESTROOT}/fixtures" .
$ ${POUNCE} -d fixtures -c --nowd -rs '' > before
$ cat before | sed -e 's/dogs/cats/g' > after
$ ${POUNCE} -d fixtures -a < after
$ find fixtures -type f -not -name binary -exec cat {} \;
I like cats.
Cats are the best.
meow
pigs are the best!
fish are the best!
cats are the best!
cats are the best!
meow
$
$ # with backup files
$ dir1=$(mktemp -d)
$ trap "rm -fR ${dir1}" 0
$ cd "${dir1}"
$ cp -r "${TESTROOT}/fixtures" .
$ ${POUNCE} -d fixtures -a --bak _bak < "${dir0}/after"
$ find . -type f -print
./fixtures/README.md_bak
./fixtures/README.md
./fixtures/some/dir/animals.txt
./fixtures/some/dir/animals.txt_bak
./fixtures/some/binary
$ cat ./fixtures/README.md_bak
I like dogs.
Cats are the best.
meow
$ cat ./fixtures/README.md
I like cats.
Cats are the best.
meow
$
$ # with editor
$ dir2=$(mktemp -d)
$ trap "rm -fR ${dir2}" 0
$ cd "${dir2}"
$ cp -r "${TESTROOT}/fixtures" .
$ EDITOR=cat ${POUNCE} --nowd -rs 'meow'
fixtures/README.md:3:meow
fixtures/some/dir/animals.txt:5:meow
$ echo "#!/bin/sh" > ./mod.sh
$ echo "exec sed -i '' -e 's/dog/cat/g' \$1" >> ./mod.sh
$ chmod +x ./mod.sh
$ EDITOR=./mod.sh ${POUNCE} --nowd -rs 'dog'
$ find . -type f -not -name binary -and -not -name mod.sh -exec cat {} \;
I like cats.
Cats are the best.
meow
pigs are the best!
fish are the best!
cats are the best!
cats are the best!
meow
$
$ # wd
$ dir3=$(mktemp -d)
$ trap "rm -fR ${dir3}" 0
$ cd "${dir3}"
$ cp -r "${TESTROOT}/fixtures" .
$ ${POUNCE} -d fixtures -c -rs '' > before
$ cat before | sed -e 's/dogs/cats/g' > after
$ ${POUNCE} -a < after #=> --exit 1
$ ${POUNCE} -d fixtures -a < after
$
$ # abs
$ dir4=$(mktemp -d)
$ trap "rm -fR ${dir4}" 0
$ cd "${dir4}"
$ cp -r "${TESTROOT}/fixtures" .
$ ${POUNCE} --abs -d fixtures -c -rs '' | sed -e 's/dog/cat/g' | ${POUNCE} -d fixtures -a
$ find fixtures -type f -not -name binary -and -not -name mod.sh -exec cat {} \;
I like cats.
Cats are the best.
meow
pigs are the best!
fish are the best!
cats are the best!
cats are the best!
meow

0 comments on commit eea1f6c

Please sign in to comment.