1
0
mirror of https://github.com/golang/go synced 2024-11-13 17:40:23 -07:00

cmd/go: fix directory->import path conversion

Fixes #3306.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/5821048
This commit is contained in:
Russ Cox 2012-03-14 15:12:57 -04:00
parent 70e58a2f9b
commit 95a8bab7b6
2 changed files with 53 additions and 30 deletions

View File

@ -17,6 +17,7 @@ import (
"sort" "sort"
"strings" "strings"
"time" "time"
"unicode"
) )
// A Package describes a single package found in a directory. // 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 // a special case, so that all the code to deal with ordinary imports works
// automatically. // automatically.
func dirToImportPath(dir string) string { 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, // loadImport scans the directory named by path, which must be an import path,

View File

@ -22,37 +22,50 @@ do
done done
# Test local (./) imports. # Test local (./) imports.
./testgo build -o hello testdata/local/easy.go testlocal() {
./hello >hello.out local="$1"
if ! grep -q '^easysub\.Hello' hello.out; then ./testgo build -o hello "testdata/$local/easy.go"
echo "testdata/local/easy.go did not generate expected output" ./hello >hello.out
cat hello.out if ! grep -q '^easysub\.Hello' hello.out; then
ok=false echo "testdata/$local/easy.go did not generate expected output"
fi 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
}
./testgo build -o hello testdata/local/easysub/main.go # Test local imports
./hello >hello.out testlocal local
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 # Test local imports again, with bad characters in the directory name.
./hello >hello.out bad='#$%:, &()*;<=>?\^{}'
if ! grep -q '^sub\.Hello' hello.out || ! grep -q '^subsub\.Hello' hello.out ; then rm -rf "testdata/$bad"
echo "testdata/local/hard.go did not generate expected output" cp -R testdata/local "testdata/$bad"
cat hello.out testlocal "$bad"
ok=false rm -rf "testdata/$bad"
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
# Test tests with relative imports. # Test tests with relative imports.
if ! ./testgo test ./testdata/testimport; then if ! ./testgo test ./testdata/testimport; then