Fork me on GitHub
#testing
<
2021-08-25
>
valsen19:08:49

Hi, my project connects to a postgres db using a simple (def db (jdbc/get-datasource (System/getenv "DATABASE_URL") in my db namespace. When running my tests, I want to set that env variable to point to my test db. What would be a recommended way to set that env variable when running my tests? Right now Iā€™m just setting it on the command line: DATABASE_URL=<test-db-url> clj -M:test , but I would ideally like to have it in my deps.edn :test alias. Is that possible?

seancorfield19:08:37

@josephson.victor No, you can't set environment variables after a process starts up.

seancorfield19:08:14

You could use a JVM property instead, which you could set with :jvm-opts in your :test alias.

šŸ™Œ 3
seancorfield19:08:16

Or do something like (or (System/getProperty "database.url") (System/getenv "DATABASE_URL")) then you can override it via -Ddatabase.url=... in JVM opts but still use the env var when that property is not set.

šŸ™Œ 3
valsen19:08:54

Thanks so much, @seancorfield, you have no idea how much I appreciate your help and contributions in the Clojure community šŸ™

šŸ˜Š 3