马提尼模板和测试

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

Martini templates and tests

问题

我正在尝试将我的应用程序文件与测试文件分开。大致结构如下:

main.go
views/
  layouts/
    layout.html
spec/
  main_test.go

main.go中,我创建了一个Martini应用程序,并告诉Martini.render在哪里查找视图:

func CreateApplication() {
  m := martini.Classic()
  m.Use(render.Renderer(render.Options{
    Directory: "views",
    Layout: "layouts/layout",
    Extensions: []string{".html"},
  }))
}

当我从根文件夹使用go run时,这一切都运行得很好。然而,当我尝试从spec/main_test.go文件中使用CreateApplication()函数时,它现在在spec/views中查找视图,因为那是运行文件夹。

我尝试使用runtime.Caller()来获取绝对路径,但这在编译二进制文件时会完全搞乱。

我想知道如何使这个工作?我希望CreateApplication()在被调用的任何位置都能正常工作。

英文:

I'm trying to split my application files from my testing files. It looks something like this:

main.go
views/
  layouts/
    layout.html
spec/
  main_test.go

main.go creates a Martini app and tells Martini.render where to look for the views:

func CreateApplication() {
  m := martini.Classic()
  m.Use(render.Renderer(render.Options{
    Directory: "views",
    Layout: "layouts/layout",
    Extensions: []string{".html"},
  }))
}

That all works really well when I'm using go run from the root folder. However, when I try to use the CreateApplication() function from the spec/main_test.go file, it's now looking for the views in spec/views because that's the run folder.

I went down the route of trying to use runtime.Caller() to get the absolute path, but that totally messes up when compilling the a binary.

I guess my question is how I can make this work? I want that CreateApplication() to work the same no matter where it was called from.

答案1

得分: 1

我经常遇到这个问题。在这种情况下,我会在子目录中创建一个符号链接,将其链接到根目录中保存模板的文件夹。到目前为止,我使用这种方法还没有遇到任何问题,但是当应用程序进入生产环境时,我会删除这些符号链接。实际上,我有一个脚本,在开始测试之前创建符号链接,然后在完成后删除它们。

在你的情况下,看起来是这样的(我使用的是Ubuntu或Cygwin):

main.go
views/
  layouts/
    layout.html
spec/
  main_test.go

$ cd spec
$ ln -s ../views views

main.go
views/
  layouts/
    layout.html
spec/
  main_test.go
  views <- 这是符号链接

现在,当从spec/运行你的测试时,可以找到views目录。希望对你有帮助,如果我的方法有什么问题,我很愿意知道!

英文:

I often run into this problem. What I do in such cases, is to create a symlink from the child directory to the folder in the root directory which holds the templates. Up until now I haven't had any problems using this appraoch, but when an app goes to production I delete those symlinks. I actually have a script that creates the symlinks before I start testing, and then deletes them after I'm done.

In your case, it would look like this (I'm on Ubuntu or Cygwin):

main.go
views/
  layouts/
    layout.html
spec/
  main_test.go

$ cd spec
$ ln -s ../views views

main.go
views/
  layouts/
    layout.html
spec/
  main_test.go
  views <- this is the symlink

Now, when running your tests from spec/ the views directoy is found. I hope it helps you, and if my approach is somehow flawed, I'm eager to know!

huangapple
  • 本文由 发表于 2014年11月9日 12:10:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/26824917.html
匿名

发表评论

匿名网友

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

确定