2

I have a problem of testing my GAE golang app uses local packages. The probject looks something like this.

.
├── app.yaml
├── main.go
└── handler
    └── handler.go

handler package is imported in main.go.

package main

import "handler"

Everything (e.g., goapp serve) works fine until I started writing tests. goapp test complains that handler package is not found. Seems like GOPATH of goapp serve and goapp test are different. One solution I found is to put handler package outside of the project path and import it with fully qualified name (e.g., github.com/.../handler) but it doesn't make sense to me to split the project in to separate places where they're tightly coupled. Are there any good way to use and test local packages?

Following resources are found on this topic.

Shouichi
  • 1,090
  • 1
  • 10
  • 25
  • try to create an app folder and move app.yaml and main.go this is how i used to work with app engine and go – MIkCode Apr 22 '15 at 07:36

2 Answers2

2

This comes late, but if anyone runs into the same issue, here is how to deal with it:

  1. Organize your code this way

    /whereever
    └── /myproject
        └──/src
           ├── app.yaml
           ├── main.go
           └── /handler
               └── handler.go
    
  2. Use unqualified imports for your project code e.g.

    import handler
    
  3. Run goapp serve and goapp deploy from inside the src folder. serve/deploy work relative to the app.yaml

    cd /whereever/myproject/src
    goapp serve 
    
  4. Set GOPATH for goapp test , it work based on GOPATH

    cd /whereever/myproject    #GOPATH expects a subfolder called src
    GOPATH=$GOPATH:`pwd`       #add the current folder to the GOPATH
    goapp test ./...           #run all tests in any subfolders
    
  5. Reset your GOPATH if necessary

    source ~/.profile #this assumes ~/.profile is where you have permanently set your GOPATH
    

All in all, "working as intended", but still a pain.

Suau
  • 4,628
  • 22
  • 28
1

You need to import handler with a fully qualified name, however you don't need to move it out of the project to do so. If your project folder looks like this:

/go
└── /myproject
    ├── app.yaml
    ├── main.go
    └── /handler
        └── handler.go

Then your handler import should look like this:

import "myproject/handler"
Zikes
  • 5,888
  • 1
  • 30
  • 44
  • The problem is that putting the project inside the GOPATH and import handler with a fully qualified name causes the import conflict. The error message is `go-app-builder: Failed parsing input: app file handler.go conflicts with same file imported from GOPATH`. – Shouichi Apr 23 '15 at 02:43
  • This does not work, still complains with `cannot find package "myproject/x" in any of...` –  May 28 '15 at 06:05
  • Make sure the `x.go` file has `package x` rather than `package main`. – Zikes May 28 '15 at 13:50
  • go doc for the package will tell you the import path to use for it - see https://stackoverflow.com/a/58754175/1569204 – Bruce Adams Nov 07 '19 at 17:35