Skip to content

Commit

Permalink
Ensure that emitted relative paths start with ./
Browse files Browse the repository at this point in the history
  • Loading branch information
cortesi committed Dec 7, 2017
1 parent 69ec874 commit 5a10bf0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 32 deletions.
54 changes: 27 additions & 27 deletions modd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ func TestWatch(t *testing.T) {
func() { touch("a/touched") },
"touched",
[]string{
":all: a/initial",
":a: a/initial",
":skipit: a/touched",
":all: a/touched",
":a: a/touched",
":all: ./a/initial",
":a: ./a/initial",
":skipit: ./a/touched",
":all: ./a/touched",
":a: ./a/touched",
},
)
_testWatch(
Expand All @@ -154,12 +154,12 @@ func TestWatch(t *testing.T) {
},
"touched",
[]string{
":all: a/initial",
":a: a/initial",
":skipit: a/touched b/touched",
":all: a/touched b/touched",
":a: a/touched",
":b: b/touched",
":all: ./a/initial",
":a: ./a/initial",
":skipit: ./a/touched ./b/touched",
":all: ./a/touched ./b/touched",
":a: ./a/touched",
":b: ./b/touched",
},
)
_testWatch(
Expand All @@ -169,11 +169,11 @@ func TestWatch(t *testing.T) {
},
"touched",
[]string{
":all: a/initial",
":a: a/initial",
":skipit: a/inner/touched.xxx",
":all: a/inner/touched.xxx",
":c: a/inner/touched.xxx",
":all: ./a/initial",
":a: ./a/initial",
":skipit: ./a/inner/touched.xxx",
":all: ./a/inner/touched.xxx",
":c: ./a/inner/touched.xxx",
},
)
_testWatch(
Expand All @@ -183,12 +183,12 @@ func TestWatch(t *testing.T) {
},
"touched",
[]string{
":all: a/initial",
":a: a/initial",
":skipit: a/direct",
":all: a/direct",
":a: a/direct",
":d: a/direct",
":all: ./a/initial",
":a: ./a/initial",
":skipit: ./a/direct",
":all: ./a/direct",
":a: ./a/direct",
":d: ./a/direct",
},
)
_testWatch(
Expand All @@ -198,11 +198,11 @@ func TestWatch(t *testing.T) {
},
"touched",
[]string{
":all: a/initial",
":a: a/initial",
":skipit: direct",
":all: direct",
":e: direct",
":all: ./a/initial",
":a: ./a/initial",
":skipit: ./direct",
":all: ./direct",
":e: ./direct",
},
)
}
21 changes: 18 additions & 3 deletions varcmd/varcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package varcmd

import (
"fmt"
"path/filepath"
"path"
"regexp"
"strings"

Expand All @@ -15,7 +15,7 @@ var name = regexp.MustCompile(`(\\*)@\w+`)
func getDirs(paths []string) []string {
m := map[string]bool{}
for _, p := range paths {
p := filepath.Dir(p)
p := path.Dir(p)
m[p] = true
}
keys := []string{}
Expand All @@ -33,11 +33,26 @@ func quotePath(path string) string {
return "\"" + path + "\""
}

// The paths we receive from Go's path manipulation functions are "cleaned",
// which removes redundancy, but also removes the leading "./" needed by many
// command-line tools. This function turns cleaned paths into "really relative"
// paths.
func realRel(p string) string {
// They should already be clean, but let's make sure.
p = path.Clean(p)
if path.IsAbs(p) {
return p
} else if p == "." {
return "./"
}
return "./" + p
}

// mkArgs prepares a list of paths for the command line
func mkArgs(paths []string) string {
escaped := make([]string, len(paths))
for i, s := range paths {
escaped[i] = quotePath(s)
escaped[i] = quotePath(realRel(s))
}
return strings.Join(escaped, " ")
}
Expand Down
4 changes: 2 additions & 2 deletions varcmd/varcmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestVarCmd(t *testing.T) {
t.Fatalf("unexpected error: %s", err)
}

expect := `"tdir/tfile" "tdir"`
expect := `"./tdir/tfile" "./tdir"`
if ret != expect {
t.Errorf("Expected: %#v, got %#v", expect, ret)
}
Expand All @@ -91,7 +91,7 @@ func TestVarCmd(t *testing.T) {
if err != nil {
t.Fatal("unexpected error")
}
expected := `"foo" "."`
expected := `"./foo" "./"`
if ret != expected {
t.Errorf("Expected: %#v, got %#v", expected, ret)
}
Expand Down

0 comments on commit 5a10bf0

Please sign in to comment.