【发布时间】:2015-03-08 06:53:33
【问题描述】:
我有一个 Leiningen Clojure 项目,它在 project.clj 中指定一个 Postgres 数据库,如下所示:
:profiles {:test {:env {:db-address "//non-standard/location" ...} ...} ...}
现在我试图在数据库位于标准位置的机器上运行我的测试,但我不知道如何以尊重该位置的方式运行我的测试。
我添加了一个额外的个人资料条目:
:other {:env {:db-address "//standard/location"}}}
...然后运行lein with-profile other test,认为只有:db-address 条目会覆盖:test 中的条目,但我仍然得到Connection refused。
我在:other 中完全复制了:test 条目,只是我只更改了:db-address 条目,但这没有任何效果,无论我运行lein with-profile other test 还是lein with-profile +other test。
如果我随后完全删除 :test 配置文件,leiningen 将完全失败,即使文档说 lein with-profile other test 应该忽略其他配置文件。
我非常难过。如果我在运行lein 之前用project.clj 中的标准位置文本替换非标准位置,我可以运行我的测试,但我希望有一些更官方和更少hacky 的东西。任何帮助将不胜感激。
编辑添加:
我尝试以两种方式使用^:replace 标签。
:other {:env {:db-address ^:replace "//standard/location" } }
这会导致错误Metadata can only be applied to IMetas。
:other {:env ^:replace { ... map copied from :test profile ... } }
这会导致错误java.io.FileNotFoundException: Could not locate ring/mock/request__init.class or ring/mock/request.clj on classpath: , compiling [...]。所以显然我的 CLASSPATH 在这种情况下被核了?莫名其妙。
编辑进一步添加:
上面的“空类路径”错误显然是由于:test 配置文件中的:dependencies 参数我没有转移到:other 配置文件。
【问题讨论】:
-
这应该可以。我能想到一些它没有的原因。您是否尝试过在您的
:other配置文件中使用^replace,例如:db-address ^:replace "//some-location",或者在整个:env键上使用更好。您是否在启动时打印调试过:db-address的值?您是否有一个环境变量 DB_ADDRESS 可以覆盖 project.clj 中的那个? -
应该可以。我唯一能想到的是两个配置文件都被加载并且
test覆盖other。试试 Kungi 的建议,或者这样:lein with-profile -test,+other test -
啊,是的,我尝试使用
:^replace标签,只是忘了提及。有关详细信息,请参见上文。 -
Kungi:您的评论基本上让我解决了这个问题。如果您将其发布为官方答案,我会接受。
-
我会为标准位置创建一个额外的配置文件,然后简单地运行
lein with-profile +standard-test test用于标准数据库位置的测试,lein with-profile +other-test test用于另一个。此外,您可以为这两种情况创建别名,例如{:aliases {"test" ["with-profile" "+standard-test" "test"] "test-other" ["with-profile" "+other-test" "test"] } }。