]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: fix directory->import path conversion
authorRuss Cox <rsc@golang.org>
Wed, 14 Mar 2012 19:12:57 +0000 (15:12 -0400)
committerRuss Cox <rsc@golang.org>
Wed, 14 Mar 2012 19:12:57 +0000 (15:12 -0400)
Fixes #3306.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/5821048

src/cmd/go/pkg.go
src/cmd/go/test.bash

index 1b6a8c5124cdcc21629a4d8b6e2961fb59a859d8..46ada4002b7598db2f3e468fc2e10873bc36f9dd 100644 (file)
@@ -17,6 +17,7 @@ import (
        "sort"
        "strings"
        "time"
+       "unicode"
 )
 
 // A Package describes a single package found in a directory.
@@ -174,7 +175,16 @@ func reloadPackage(arg string, stk *importStack) *Package {
 // a special case, so that all the code to deal with ordinary imports works
 // automatically.
 func dirToImportPath(dir string) string {
-       return pathpkg.Join("_", strings.Replace(filepath.ToSlash(dir), ":", "_", -1))
+       return pathpkg.Join("_", strings.Map(makeImportValid, filepath.ToSlash(dir)))
+}
+
+func makeImportValid(r rune) rune {
+       // Should match Go spec, compilers, and ../../pkg/go/parser/parser.go:/isValidImport.
+       const illegalChars = `!"#$%&'()*,:;<=>?[\]^{|}` + "`\uFFFD"
+       if !unicode.IsGraphic(r) || unicode.IsSpace(r) || strings.ContainsRune(illegalChars, r) {
+               return '_'
+       }
+       return r
 }
 
 // loadImport scans the directory named by path, which must be an import path,
index daca144ee0e2c9dbcf6a5cd4ba40f7139c5faaa6..541535101512cd0a4df9d86891bd3d172f86a500 100755 (executable)
@@ -22,37 +22,50 @@ do
 done
 
 # Test local (./) imports.
-./testgo build -o hello testdata/local/easy.go
-./hello >hello.out
-if ! grep -q '^easysub\.Hello' hello.out; then
-       echo "testdata/local/easy.go did not generate expected output"
-       cat hello.out
-       ok=false
-fi
-
-./testgo build -o hello testdata/local/easysub/main.go
-./hello >hello.out
-if ! grep -q '^easysub\.Hello' hello.out; then
-       echo "testdata/local/easysub/main.go did not generate expected output"
-       cat hello.out
-       ok=false
-fi
-
-./testgo build -o hello testdata/local/hard.go
-./hello >hello.out
-if ! grep -q '^sub\.Hello' hello.out || ! grep -q '^subsub\.Hello' hello.out ; then
-       echo "testdata/local/hard.go did not generate expected output"
-       cat hello.out
-       ok=false
-fi
+testlocal() {
+       local="$1"
+       ./testgo build -o hello "testdata/$local/easy.go"
+       ./hello >hello.out
+       if ! grep -q '^easysub\.Hello' hello.out; then
+               echo "testdata/$local/easy.go did not generate expected output"
+               cat hello.out
+               ok=false
+       fi
+       
+       ./testgo build -o hello "testdata/$local/easysub/main.go"
+       ./hello >hello.out
+       if ! grep -q '^easysub\.Hello' hello.out; then
+               echo "testdata/$local/easysub/main.go did not generate expected output"
+               cat hello.out
+               ok=false
+       fi
+       
+       ./testgo build -o hello "testdata/$local/hard.go"
+       ./hello >hello.out
+       if ! grep -q '^sub\.Hello' hello.out || ! grep -q '^subsub\.Hello' hello.out ; then
+               echo "testdata/$local/hard.go did not generate expected output"
+               cat hello.out
+               ok=false
+       fi
+       
+       rm -f err.out hello.out hello
+       
+       # Test that go install x.go fails.
+       if ./testgo install "testdata/$local/easy.go" >/dev/null 2>&1; then
+               echo "go install testdata/$local/easy.go succeeded"
+               ok=false
+       fi
+}
 
-rm -f err.out hello.out hello
+# Test local imports
+testlocal local
 
-# Test that go install x.go fails.
-if ./testgo install testdata/local/easy.go >/dev/null 2>&1; then
-       echo "go install testdata/local/easy.go succeeded"
-       ok=false
-fi
+# Test local imports again, with bad characters in the directory name.
+bad='#$%:, &()*;<=>?\^{}'
+rm -rf "testdata/$bad"
+cp -R testdata/local "testdata/$bad"
+testlocal "$bad"
+rm -rf "testdata/$bad"
 
 # Test tests with relative imports.
 if ! ./testgo test ./testdata/testimport; then