jeudi 18 août 2016

How do you structure imports for testing inside a module of your project?

I am attempting to understand how to structure a go project into sub modules stored in separate source code repositories (on host example.com), however when I do, I am not sure how to run the tests that are within a module. What am doing wrong in the following example, any help is much appreciated!!

mkdir -p src/http://ift.tt/2b1lg8f

echo "package tool"       >> src/http://ift.tt/2bkJ2XL
echo ""                   >> src/http://ift.tt/2bkJ2XL
echo "type Book struct {" >> src/http://ift.tt/2bkJ2XL
echo "  Title string"     >> src/http://ift.tt/2bkJ2XL
echo "}"                  >> src/http://ift.tt/2bkJ2XL
echo ""                   >> src/http://ift.tt/2bkJ2XL

echo "package tool"       >> src/http://ift.tt/2b1lwnN
echo ""                   >> src/http://ift.tt/2b1lwnN
echo "import ("           >> src/http://ift.tt/2b1lwnN
echo "  \"tool\""         >> src/http://ift.tt/2b1lwnN
echo "  \"testing\""      >> src/http://ift.tt/2b1lwnN
echo ")"                  >> src/http://ift.tt/2b1lwnN
echo ""                   >> src/http://ift.tt/2b1lwnN
echo "func TestBook(t *testing.T) { }" >> src/http://ift.tt/2b1lwnN
echo ""                   >> src/http://ift.tt/2b1lwnN

export GOPATH=`pwd`

go test http://ift.tt/2b1lg8f

When I run this test, this is the error I am seeing:

# http://ift.tt/2b1lg8f
src/http://ift.tt/2bkJIw7: cannot find package "tool" in any of:
    /usr/local/go/src/tool (from $GOROOT)
    /Users/john/app/src/tool (from $GOPATH)
FAIL    http://ift.tt/2b1lg8f [setup failed]

Obviously book_test.go can't import the "tool" package, an you could probably put in the full path, but when I look in github, no one does that in their modules. So I don't understand what I am doing wrong.

Aucun commentaire:

Enregistrer un commentaire