From 150988618524bf17378bd8770231cda7c9bbb43a Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Sat, 16 Jan 2021 19:42:39 +0300 Subject: [PATCH] More apenwarr/redo redo-whichdo behaviour resembling --- do.go | 2 +- doc/news.texi | 7 ++++ main.go | 14 +++++-- t/apenwarr/260-whichdo/all.do | 1 + t/apenwarr/260-whichdo/default.y.z.do | 1 + t/apenwarr/260-whichdo/defaults.do | 16 ++++++++ t/apenwarr/260-whichdo/exists.do | 25 ++++++++++++ t/apenwarr/260-whichdo/fakesub/.empty | 0 t/apenwarr/260-whichdo/fakesub2/default.do | 1 + t/apenwarr/260-whichdo/fakesub2/snork.do | 1 + t/apenwarr/260-whichdo/nonexists.do | 45 ++++++++++++++++++++++ t/apenwarr/README | 4 +- t/apewarr-260-whichdo | 1 + usage.go | 2 +- 14 files changed, 113 insertions(+), 7 deletions(-) create mode 100644 t/apenwarr/260-whichdo/all.do create mode 100644 t/apenwarr/260-whichdo/default.y.z.do create mode 100644 t/apenwarr/260-whichdo/defaults.do create mode 100644 t/apenwarr/260-whichdo/exists.do create mode 100644 t/apenwarr/260-whichdo/fakesub/.empty create mode 100644 t/apenwarr/260-whichdo/fakesub2/default.do create mode 100644 t/apenwarr/260-whichdo/fakesub2/snork.do create mode 100644 t/apenwarr/260-whichdo/nonexists.do create mode 120000 t/apewarr-260-whichdo diff --git a/do.go b/do.go index 1846414..7d0ed51 100644 --- a/do.go +++ b/do.go @@ -82,7 +82,7 @@ func findDo(fdDep *os.File, cwd, tgt string) (string, int, error) { } } levels = append(levels, "..") - dirAbs, err := filepath.Abs(updir) + dirAbs, err := filepath.Abs(path.Join(cwd, updir)) if err != nil { panic(err) } diff --git a/doc/news.texi b/doc/news.texi index 0be2b80..5484635 100644 --- a/doc/news.texi +++ b/doc/news.texi @@ -1,6 +1,13 @@ @node News @unnumbered News +@anchor{Release 0.13.0} +@section Release 0.13.0 +@itemize +@item + @command{redo-whichdo} resembles @code{apenwarr/redo}'s one behaviour more. +@end itemize + @anchor{Release 0.12.3} @section Release 0.12.3 @itemize diff --git a/main.go b/main.go index ed82856..873582a 100644 --- a/main.go +++ b/main.go @@ -28,7 +28,6 @@ import ( "path" "path/filepath" "strconv" - "strings" "go.cypherpunks.ru/recfile" ) @@ -270,7 +269,6 @@ CmdSwitch: } os.Remove(fdTmp.Name()) cwd, tgt := cwdAndTgt(tgts[0]) - cwdRel := cwdMustRel(cwd) doFile, upLevels, err := findDo(fdTmp, cwd, tgt) if err != nil { break @@ -293,7 +291,17 @@ CmdSwitch: if doFile == "" { ok = false } else { - fmt.Println(path.Join(cwdRel, strings.Repeat("..", upLevels), doFile)) + p := make([]string, 0, upLevels+2) + p = append(p, cwd) + for i := 0; i < upLevels; i++ { + p = append(p, "..") + } + p = append(p, doFile) + rel, err := filepath.Rel(Cwd, path.Join(p...)) + if err != nil { + panic(err) + } + fmt.Println(rel) } default: log.Fatalln("unknown command", cmdName) diff --git a/t/apenwarr/260-whichdo/all.do b/t/apenwarr/260-whichdo/all.do new file mode 100644 index 0000000..d4d93b6 --- /dev/null +++ b/t/apenwarr/260-whichdo/all.do @@ -0,0 +1 @@ +redo exists nonexists defaults diff --git a/t/apenwarr/260-whichdo/default.y.z.do b/t/apenwarr/260-whichdo/default.y.z.do new file mode 100644 index 0000000..397db75 --- /dev/null +++ b/t/apenwarr/260-whichdo/default.y.z.do @@ -0,0 +1 @@ +: diff --git a/t/apenwarr/260-whichdo/defaults.do b/t/apenwarr/260-whichdo/defaults.do new file mode 100644 index 0000000..0fe3214 --- /dev/null +++ b/t/apenwarr/260-whichdo/defaults.do @@ -0,0 +1,16 @@ +exec >&2 + +a=$(cd fakesub2 && redo-whichdo d/snork) +# if sh doesn't abort after the above, then it found a .do file as expected + +b=$(cat <&2 + +a=$(cd fakesub && redo-whichdo ../a/b/x.y.z) +# if sh doesn't abort after the above, then it found a .do file as expected + +# Note: we expect redo-whichdo to return paths relative to $PWD at the time +# it's run, which in this case is fakesub. +# Likely bugs would be to return paths relative to the start dir, the .redo +# dir, the current target dir, the requested target dir, etc. +b=$(cat <&2 + +# Testing the search path for non-existent do files is a little tricky. +# We can't be sure where our current directory is, so we don't know how +# far up the stack redo will need to search. +# +# To dodge the problem, let's "cd /" first so that we're testing a target +# relative to a known location (the root directory). + +if [ -e '/default.do' -o \ + -e '/default.z.do' -o \ + -e '/default.y.z.do' ]; then + echo "Weird: /default.*.do exists; can't run this test." + exit 99 +fi + +# redo-whichdo *should* fail here, so don't abort the script for that. +set +e +a=$(cd / && redo-whichdo __nonexist/a/x.y.z) +rv=$? +set -e + +if [ "$rv" -eq 0 ]; then + echo "redo-whichdo should return nonzero for a missing .do file." + exit 10 +fi + +b=$(cat <