]> Cypherpunks repositories - gostls13.git/commitdiff
gotest: enable unit tests for cmd directories
authorRuss Cox <rsc@golang.org>
Thu, 3 Feb 2011 19:54:01 +0000 (14:54 -0500)
committerRuss Cox <rsc@golang.org>
Thu, 3 Feb 2011 19:54:01 +0000 (14:54 -0500)
R=r
CC=golang-dev
https://golang.org/cl/4001056

src/Make.cmd
src/cmd/gotest/gotest

index 34f5663bc8cd1f16fab88949f0e3954e57209610..2b9aba4a5afa75ea16a90c2dec0644dc729aa261 100644 (file)
@@ -27,3 +27,22 @@ CLEANFILES+=$(TARG)
 
 nuke: clean
        rm -f $(QUOTED_GOBIN)/$(TARG)
+
+# for gotest
+testpackage: _test/main.a
+
+testpackage-clean:
+       rm -f _test/main.a _gotest_.$O
+
+testpackage: _test/main.a
+
+_test/main.a: _gotest_.$O
+       @mkdir -p _test
+       rm -f $@
+       gopack grc $@ _gotest_.$O
+
+_gotest_.$O: $(GOFILES) $(GOTESTFILES)
+       $(GC) -o $@ $(GOFILES) $(GOTESTFILES)
+
+importpath:
+       echo main
index 87c6800893f8290bfbcadd1a95da6d6e3da983b4..69eaae730e0e9f6cebe8fab83b8147079a63d151 100755 (executable)
@@ -119,6 +119,12 @@ nmgrep() {
        done
 }
 
+localname() {
+       # The package main has been renamed to __main__ when imported.
+       # Adjust its uses.
+       echo $1 | sed 's/^main\./__main__./'
+}
+
 importpath=$(gomake -s importpath)
 {
        # test functions are named TestFoo
@@ -139,9 +145,20 @@ importpath=$(gomake -s importpath)
        echo
        # imports
        if echo "$tests" | egrep -v '_test\.' >/dev/null; then
-               if [ "$importpath" != "testing" ]; then
+               case "$importpath" in
+               testing)
+                       ;;
+               main)
+                       # Import path main is reserved, so import with
+                       # explicit reference to ./_test/main instead.
+                       # Also, the file we are writing defines a function named main,
+                       # so rename this import to __main__ to avoid name conflict.
+                       echo 'import __main__ "./_test/main"'
+                       ;;
+               *)
                        echo 'import "'$importpath'"'
-               fi
+                       ;;
+               esac
        fi
        if $havex; then
                echo 'import "./_xtest_"'
@@ -153,23 +170,20 @@ importpath=$(gomake -s importpath)
        echo 'var tests = []testing.InternalTest{'
        for i in $tests
        do
-               echo '  {"'$i'", '$i'},'
+               j=$(localname $i)
+               echo '  {"'$i'", '$j'},'
        done
        echo '}'
        # benchmark array
-       if [ "$benchmarks" = "" ]
-       then
-               # keep the empty array gofmt-safe.
-               # (not an issue for the test array, which is never empty.)
-               echo 'var benchmarks = []testing.InternalBenchmark{}'
-       else
-               echo 'var benchmarks = []testing.InternalBenchmark{'
-               for i in $benchmarks
-               do
-                       echo '  {"'$i'", '$i'},'
-               done
-               echo '}'
-       fi
+       # The comment makes the multiline declaration
+       # gofmt-safe even when there are no benchmarks.
+       echo 'var benchmarks = []testing.InternalBenchmark{ //'
+       for i in $benchmarks
+       do
+               j=$(localname $i)
+               echo '  {"'$i'", '$j'},'
+       done
+       echo '}'
        # body
        echo
        echo 'func main() {'