clj / test / ird / test_env.clj
115 words
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
(ns ird.test-env
  "Helpers for live integration tests: reads IRD_API_KEY from the
  environment, falling back to a .env file in the project root."
  (:require
   [clojure.java.io :as io]
   [clojure.string :as str]))

(defn- parse-dotenv
  "Parse KEY=value lines from F (a .env file), ignoring blanks and comments."
  [f]
  (into {}
        (keep (fn [line]
                (let [line (str/trim line)]
                  (when (and (seq line)
                             (not (str/starts-with? line "#"))
                             (str/includes? line "="))
                    (let [[k v] (str/split line #"=" 2)]
                      [(str/trim k) (str/trim v)])))))
        (str/split-lines (slurp f))))

(defn api-key
  "The IRD_API_KEY for live tests, from the environment or ./.env. Nil when
  unavailable (integration tests should then skip, not fail)."
  []
  (or (System/getenv "IRD_API_KEY")
      (let [f (io/file ".env")]
        (when (.exists f)
          (get (parse-dotenv f) "IRD_API_KEY")))))

static mirror of HEAD · about · clone: git clone https://ird.cod-sursa.ro/clj.git