Good way to use and test local packages in Google App Engine apps written in golang?

huangapple go评论108阅读模式
英文:

Good way to use and test local packages in Google App Engine apps written in golang?

问题

我有一个问题,我正在测试使用本地包的GAE golang应用程序。
项目的结构大致如下:

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

handler 包在 main.go 中被导入。

package main

import "handler"

一切都很好(例如,goapp serve),直到我开始编写测试。
goapp test 抱怨找不到 handler 包。似乎 goapp servegoapp testGOPATH 是不同的。我找到的一个解决方案是将 handler 包放在项目路径之外,并使用完全限定的名称导入它(例如,github.com/.../handler),但这对我来说没有意义,因为它将项目拆分成紧密耦合的不同位置。有没有好的方法来使用和测试本地包?

在这个主题上找到了以下资源。

英文:

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.

答案1

得分: 2

这个有点晚了,但如果有人遇到同样的问题,以下是处理方法:

  1. 按照以下方式组织你的代码:

     /whereever
     └── /myproject
         └──/src
            ├── app.yaml
            ├── main.go
            └── /handler
                └── handler.go
    
  2. 在项目代码中使用不带限定符的导入,例如:

     import handler
    
  3. 从 src 文件夹内运行 goapp servegoapp deploy。serve/deploy 相对于 app.yaml 工作。

     cd /whereever/myproject/src
     goapp serve 
    
  4. goapp test 设置 GOPATH,它基于 GOPATH 运行。

     cd /whereever/myproject    #GOPATH 需要一个名为 src 的子文件夹
     GOPATH=$GOPATH:`pwd`       #将当前文件夹添加到 GOPATH
     goapp test ./...           #运行所有子文件夹中的测试
    
  5. 如有必要,重置你的 GOPATH。

     source ~/.profile #假设你已经在 ~/.profile 中永久设置了 GOPATH
    

总的来说,"按预期工作",但仍然有些麻烦。

英文:

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.

答案2

得分: 1

你需要使用完全限定名称导入handler,但不需要将其移出项目。如果你的项目文件夹结构如下:

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

那么你的handler导入应该是这样的:

import "myproject/handler"
英文:

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"

huangapple
  • 本文由 发表于 2015年4月22日 08:46:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/29785752.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定