├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── config └── config.exs ├── lib └── env_helper.ex ├── mix.exs ├── mix.lock └── test ├── env_helper_test.exs └── test_helper.exs /.gitignore: -------------------------------------------------------------------------------- 1 | /_build 2 | /cover 3 | /deps 4 | /docs 5 | /doc 6 | erl_crash.dump 7 | *.ez 8 | *.swp 9 | *.swo 10 | *.tar 11 | .tool-versions 12 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: elixir 2 | elixir: 3 | - 1.5 4 | otp_release: 5 | - 18.2.1 6 | env: 7 | - MIX_ENV=test 8 | script: mix coveralls.travis 9 | after_script: 10 | - mix deps.get --only docs 11 | - MIX_ENV=docs mix inch.report 12 | 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Manheim 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # EnvHelper 2 | 3 | [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://raw.githubusercontent.com/manheim/env_helper/master/LICENSE) 4 | [![Travis](https://img.shields.io/travis/manheim/env_helper.svg?maxAge=2592000&style=flat-square)](https://travis-ci.org/manheim/env_helper) 5 | [![Hex.pm](https://img.shields.io/hexpm/v/env_helper.svg?maxAge=2592000&style=flat-square)](https://hex.pm/packages/env_helper) 6 | [![Coveralls](https://img.shields.io/coveralls/manheim/env_helper.svg?maxAge=2592000&style=flat-square)](https://coveralls.io/github/manheim/env_helper) 7 | [![Inline docs](http://inch-ci.org/github/manheim/env_helper.svg)](http://inch-ci.org/github/manheim/env_helper) 8 | 9 | Using system variables is good practice, and env helper helps you practice it. [Documentation available online](https://hexdocs.pm/env_helper/api-reference.html) 10 | 11 | ## Installation 12 | 13 | The package can be installed from hex.pm: 14 | 15 | 1. Add env_helper to your list of dependencies in `mix.exs`: 16 | 17 | ```elixir 18 | def deps do 19 | [{:env_helper, "~> 0.2.0"}] 20 | end 21 | ``` 22 | 23 | ## Usage 24 | 25 | #### General 26 | 27 | Create a module and import `EnvHelper`, then use the `system_env` and `app_env` macros to to define functions for that module. 28 | 29 | #### system_env 30 | 31 | ```elixir 32 | defmodule Settings do 33 | import EnvHelper 34 | system_env(:base_url, "localhost:9000") 35 | end 36 | ``` 37 | 38 | This maps the method `Settings.base_url` to the system environment variable `BASE_URL`, or to the default value if the environment variable is not set. 39 | 40 | Some settings might need to be integer values in your code, but will be strings when read from the environment helper. In such cases you can use the `:string_to_integer` flag: 41 | 42 | ```elixir 43 | system_env(:port, 9876, :string_to_integer) 44 | ``` 45 | 46 | Which will ensure that `PORT` environment variable will be interpreted as an integer. 47 | 48 | #### app_env 49 | 50 | In config/test.exs: 51 | 52 | ```elixir 53 | config :my_app, :port, 5678 54 | ``` 55 | 56 | In settings.ex: 57 | 58 | ```elixir 59 | defmodule Settings do 60 | import EnvHelper 61 | app_env(:port, [:my_app, :port], 1234) 62 | end 63 | ``` 64 | 65 | Assuming that the `config/dev.exs` file does not define a `:my_app, :port` variable, in the test environment `Settings.port` is `5678` and in dev it will be `1234`. 66 | -------------------------------------------------------------------------------- /config/config.exs: -------------------------------------------------------------------------------- 1 | # This file is responsible for configuring your application 2 | # and its dependencies with the aid of the Mix.Config module. 3 | use Mix.Config 4 | 5 | # This configuration is loaded before any dependency and is restricted 6 | # to this project. If another project depends on this project, this 7 | # file won't be loaded nor affect the parent project. For this reason, 8 | # if you want to provide default values for your application for 9 | # 3rd-party users, it should be done in your "mix.exs" file. 10 | 11 | # You can configure for your application as: 12 | # 13 | # config :env_helper, key: :value 14 | # 15 | # And access this configuration in your application as: 16 | # 17 | # Application.get_env(:env_helper, :key) 18 | # 19 | # Or configure a 3rd-party app: 20 | # 21 | # config :logger, level: :info 22 | # 23 | 24 | # It is also possible to import configuration files, relative to this 25 | # directory. For example, you can emulate configuration per environment 26 | # by uncommenting the line below and defining dev.exs, test.exs and such. 27 | # Configuration from the imported file will override the ones defined 28 | # here (which is why it is important to import them last). 29 | # 30 | # import_config "#{Mix.env}.exs" 31 | -------------------------------------------------------------------------------- /lib/env_helper.ex: -------------------------------------------------------------------------------- 1 | defmodule EnvHelper do 2 | @moduledoc """ 3 | Helpers for enviroment and application variables. 4 | """ 5 | 6 | @doc """ 7 | creates a method `name/0` which returns either `alt` or the environment variable set for the upcase version `name`. 8 | 9 | ## Paramenters 10 | * `name` :: atom 11 | 12 | The name of a system environment variable, downcase, as an atom 13 | * `alt` :: any 14 | 15 | The value used if the system variable is not set. 16 | 17 | ## Example 18 | 19 | defmodule EnvSets do 20 | import EnvHelper 21 | system_env(:app_url, "localhost:2000") 22 | end 23 | 24 | > EnvSets.app_url 25 | "localhost:2000" 26 | 27 | > System.put_env("APP_URL", "app.io") 28 | :ok 29 | 30 | > EnvSets.app_url 31 | "app.io" 32 | """ 33 | defmacro system_env(name, alt) do 34 | env_name = Atom.to_string(name) |> String.upcase() 35 | 36 | quote do 37 | def unquote(name)() do 38 | System.get_env(unquote(env_name)) || unquote(alt) 39 | end 40 | end 41 | end 42 | 43 | @doc """ 44 | creates a method `name/0` which returns either `alt` or the environment variable set for the upcase version `name`, cast to an integer. 45 | 46 | ## Paramenters 47 | * `name` :: atom 48 | 49 | The name of a system environment variable, downcase, as an atom 50 | * `alt` :: any 51 | 52 | The value used if the system variable is not set. 53 | * :string_to_integer 54 | 55 | forces the returned value to be an integer. 56 | 57 | ## Example 58 | 59 | defmodule EnvSets do 60 | import EnvHelper 61 | system_env(:app_port, 2000, :string_to_integer) 62 | end 63 | > EnvSets.app_port 64 | 2000 65 | > System.put_env("APP_PORT", "2340") 66 | :ok 67 | > EnvSets.app_port 68 | 2340 69 | > System.put_env("APP_PORT", 2360) 70 | :ok 71 | > EnvSets.app_url 72 | 2360 73 | 74 | creates a method `name/0` which returns either `alt` or the environment variable set for the upcase version `name`, cast to a boolean. 75 | 76 | ## Paramenters 77 | * `name` :: atom 78 | 79 | The name of a system environment variable, downcase, as an atom 80 | * `alt` :: any 81 | 82 | The value used if the system variable is not set. 83 | * :string_to_boolean 84 | 85 | forces the returned value to be a boolean. 86 | 87 | ## Example 88 | 89 | defmodule EnvSets do 90 | import EnvHelper 91 | system_env(:auto_connect, false, :string_to_boolean) 92 | end 93 | > EnvSets.auto_connect 94 | false 95 | > System.put_env("AUTO_CONNECT", "true") 96 | :ok 97 | > EnvSets.auto_connect 98 | true 99 | > System.put_env("AUTO_CONNECT", "false") 100 | :ok 101 | > EnvSets.auto_connect 102 | false 103 | 104 | creates a method `name/0` which returns either `alt` or the environment variable set for the upcase version `name`, cast to a List. 105 | 106 | ## Paramenters 107 | * `name` :: atom 108 | 109 | The name of a system environment variable, downcase, as an atom 110 | * `alt` :: any 111 | 112 | The value used if the system variable is not set. 113 | * :string_to_list 114 | 115 | forces the returned value to be a list. 116 | 117 | ## Example 118 | 119 | defmodule EnvSets do 120 | import EnvHelper 121 | system_env(:destination_urls, "url_one,url_two", :string_to_list) 122 | end 123 | > EnvSets.destination_urls 124 | ["url_one", "url_two"] 125 | > System.put_env("DESTINATION_URLS", "url_three,url_four") 126 | :ok 127 | > EnvSets.destination_urls 128 | ["url_three", "url_four"] 129 | > System.put_env("DESTINATION_URLS", "url_five") 130 | :ok 131 | > EnvSets.destination_urls 132 | ["url_five"] 133 | 134 | creates a method `name/0` which returns either `alt` or the environment variable set for the upcase version `name`, cast to a charlist. 135 | 136 | ## Paramenters 137 | * `name` :: atom 138 | 139 | The name of a system environment variable, downcase, as an atom 140 | * `alt` :: any 141 | 142 | The value used if the system variable is not set. 143 | * :string_to_charlist 144 | 145 | forces the returned value to be a charlist. 146 | 147 | ## Example 148 | 149 | defmodule EnvSets do 150 | import EnvHelper 151 | system_env(:query, 'select * from table', :string_to_charlist) 152 | end 153 | > EnvSets.query() 154 | 'select * from table' 155 | > System.put_env("QUERY", "select count(*) from table") 156 | :ok 157 | > EnvSets.query() 158 | 'select count(*) from table' 159 | 160 | """ 161 | @spec system_env(atom, any, :string_to_integer) :: integer 162 | @spec system_env(atom, any, :string_to_boolean) :: boolean 163 | @spec system_env(atom, any, :string_to_list) :: list() 164 | @spec system_env(atom, any, :string_to_charlist) :: charlist() 165 | defmacro system_env(name, alt, :string_to_integer) do 166 | env_name = Atom.to_string(name) |> String.upcase() 167 | 168 | quote do 169 | def unquote(name)() do 170 | value = System.get_env(unquote(env_name)) || unquote(alt) 171 | if is_binary(value), do: String.to_integer(value), else: value 172 | end 173 | end 174 | end 175 | defmacro system_env(name, alt, :string_to_boolean) do 176 | env_name = Atom.to_string(name) |> String.upcase() 177 | 178 | quote do 179 | def unquote(name)() do 180 | value = System.get_env(unquote(env_name)) || unquote(alt) 181 | 182 | if is_binary(value) do 183 | String.downcase(value) not in ["", "false", "nil"] 184 | else 185 | value && true 186 | end 187 | end 188 | end 189 | end 190 | defmacro system_env(name, alt, :string_to_list) do 191 | env_name = Atom.to_string(name) |> String.upcase() 192 | 193 | quote do 194 | def unquote(name)() do 195 | value = System.get_env(unquote(env_name)) || unquote(alt) 196 | 197 | if is_binary(value) do 198 | value 199 | |> String.split(",") 200 | |> Enum.map(&String.trim/1) 201 | else 202 | value 203 | end 204 | end 205 | end 206 | end 207 | defmacro system_env(name, alt, :string_to_charlist) do 208 | env_name = Atom.to_string(name) |> String.upcase() 209 | 210 | quote do 211 | def unquote(name)() do 212 | value = System.get_env(unquote(env_name)) || unquote(alt) 213 | if is_binary(value), do: String.to_charlist(value), else: value 214 | end 215 | end 216 | end 217 | 218 | @doc """ 219 | defines a method `name/0` which returns either the application variable for `appname[key]` or the provided `alt` value. 220 | 221 | Works best in simple cases, such as `config :appname, :key, value` 222 | 223 | ## Example (simple app variable) 224 | 225 | defmodule AppEnvs do 226 | import EnvHelper 227 | app_env(:port, [:appname, :port], 1234) 228 | end 229 | 230 | > AppEnvs.port 231 | 1234 232 | > Application.put_env(:appname, :port, 5678) 233 | :ok 234 | > AppEnvs.port 235 | 5678 236 | 237 | ## Example (nested app variable) 238 | 239 | defmodule AppEnvs do 240 | import EnvHelper 241 | 242 | defmodule Section do 243 | app_env(:secret, [:appname, :section, :secret], "default secret") 244 | end 245 | end 246 | 247 | > AppEnvs.Section.secret 248 | "default secret" 249 | > Application.puts_env(:appname, :section, [secret: "my super secret"]) 250 | :ok 251 | > AppEnvs.Section.secret 252 | "my super secret" 253 | """ 254 | defmacro app_env(name, [appname, key], alt) do 255 | quote do 256 | def unquote(name)() do 257 | Application.get_env(unquote(appname), unquote(key)) || unquote(alt) 258 | end 259 | end 260 | end 261 | 262 | defmacro app_env(name, [appname, key, subkey], alt) do 263 | quote do 264 | def unquote(name)() do 265 | opts = Application.get_env(unquote(appname), unquote(key)) || [] 266 | 267 | case List.keyfind(opts, unquote(subkey), 0) do 268 | {_, value} -> value 269 | nil -> unquote(alt) 270 | end 271 | end 272 | end 273 | end 274 | end 275 | -------------------------------------------------------------------------------- /mix.exs: -------------------------------------------------------------------------------- 1 | defmodule EnvHelper.Mixfile do 2 | use Mix.Project 3 | 4 | def project do 5 | [ 6 | app: :env_helper, 7 | version: "0.3.0", 8 | elixir: "~> 1.5", 9 | build_embedded: Mix.env() == :prod, 10 | start_permanent: Mix.env() == :prod, 11 | description: description(), 12 | test_coverage: [tool: ExCoveralls], 13 | preferred_cli_env: [coveralls: :test, "coveralls.detail": :test, "coveralls.post": :test], 14 | package: package(), 15 | deps: deps() 16 | ] 17 | end 18 | 19 | # Configuration for the OTP application 20 | # 21 | # Type "mix help compile.app" for more information 22 | def application do 23 | [applications: [:logger]] 24 | end 25 | 26 | defp description do 27 | """ 28 | A simple add on to make working with environment variables slightly easier. 29 | """ 30 | end 31 | 32 | defp package do 33 | [ 34 | maintainers: ["Paul Daigle"], 35 | licenses: ["MIT"], 36 | links: %{"GitHub" => "https://github.com/manheim/env_helper"} 37 | ] 38 | end 39 | 40 | # Dependencies can be Hex packages: 41 | # 42 | # {:mydep, "~> 0.3.0"} 43 | # 44 | # Or git/path repositories: 45 | # 46 | # {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"} 47 | # 48 | # Type "mix help deps" for more examples and options 49 | defp deps do 50 | [ 51 | {:excoveralls, "~> 0.10", only: :test}, 52 | {:ex_doc, "~> 0.18.0", only: :dev}, 53 | {:inch_ex, ">=0.0.0", only: :docs} 54 | ] 55 | end 56 | end 57 | -------------------------------------------------------------------------------- /mix.lock: -------------------------------------------------------------------------------- 1 | %{"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm"}, 2 | "certifi": {:hex, :certifi, "2.5.1", "867ce347f7c7d78563450a18a6a28a8090331e77fa02380b4a21962a65d36ee5", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm"}, 3 | "earmark": {:hex, :earmark, "1.3.1", "73812f447f7a42358d3ba79283cfa3075a7580a3a2ed457616d6517ac3738cb9", [:mix], [], "hexpm"}, 4 | "ex_doc": {:hex, :ex_doc, "0.18.4", "4406b8891cecf1352f49975c6d554e62e4341ceb41b9338949077b0d4a97b949", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}], "hexpm"}, 5 | "excoveralls": {:hex, :excoveralls, "0.10.6", "e2b9718c9d8e3ef90bc22278c3f76c850a9f9116faf4ebe9678063310742edc2", [:mix], [{:hackney, "~> 1.13", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm"}, 6 | "exjsx": {:hex, :exjsx, "3.2.1", "1bc5bf1e4fd249104178f0885030bcd75a4526f4d2a1e976f4b428d347614f0f", [:mix], [{:jsx, "~> 2.8.0", [hex: :jsx, optional: false]}]}, 7 | "hackney": {:hex, :hackney, "1.15.1", "9f8f471c844b8ce395f7b6d8398139e26ddca9ebc171a8b91342ee15a19963f4", [:rebar3], [{:certifi, "2.5.1", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "6.0.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.4", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"}, 8 | "idna": {:hex, :idna, "6.0.0", "689c46cbcdf3524c44d5f3dde8001f364cd7608a99556d8fbd8239a5798d4c10", [:rebar3], [{:unicode_util_compat, "0.4.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"}, 9 | "inch_ex": {:hex, :inch_ex, "2.0.0", "24268a9284a1751f2ceda569cd978e1fa394c977c45c331bb52a405de544f4de", [:mix], [{:bunt, "~> 0.2", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm"}, 10 | "jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"}, 11 | "jsx": {:hex, :jsx, "2.8.1", "1453b4eb3615acb3e2cd0a105d27e6761e2ed2e501ac0b390f5bbec497669846", [:mix, :rebar3], []}, 12 | "makeup": {:hex, :makeup, "0.8.0", "9cf32aea71c7fe0a4b2e9246c2c4978f9070257e5c9ce6d4a28ec450a839b55f", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"}, 13 | "makeup_elixir": {:hex, :makeup_elixir, "0.13.0", "be7a477997dcac2e48a9d695ec730b2d22418292675c75aa2d34ba0909dcdeda", [:mix], [{:makeup, "~> 0.8", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm"}, 14 | "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm"}, 15 | "mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm"}, 16 | "nimble_parsec": {:hex, :nimble_parsec, "0.5.0", "90e2eca3d0266e5c53f8fbe0079694740b9c91b6747f2b7e3c5d21966bba8300", [:mix], [], "hexpm"}, 17 | "parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm"}, 18 | "poison": {:hex, :poison, "3.0.0", "625ebd64d33ae2e65201c2c14d6c85c27cc8b68f2d0dd37828fde9c6920dd131", [:mix], []}, 19 | "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.4", "f0eafff810d2041e93f915ef59899c923f4568f4585904d010387ed74988e77b", [:make, :mix, :rebar3], [], "hexpm"}, 20 | "unicode_util_compat": {:hex, :unicode_util_compat, "0.4.1", "d869e4c68901dd9531385bb0c8c40444ebf624e60b6962d95952775cac5e90cd", [:rebar3], [], "hexpm"}} 21 | -------------------------------------------------------------------------------- /test/env_helper_test.exs: -------------------------------------------------------------------------------- 1 | defmodule TestSettings do 2 | import EnvHelper 3 | system_env(:this, "that") 4 | system_env(:this_gist, "bat") 5 | system_env(:force_int, "4821", :string_to_integer) 6 | system_env(:must_int, 5432, :string_to_integer) 7 | system_env(:should_int, 5699, :string_to_integer) 8 | system_env(:force_bool_true, "true", :string_to_boolean) 9 | system_env(:force_bool_false, "false", :string_to_boolean) 10 | system_env(:force_bool_nil, "nil", :string_to_boolean) 11 | system_env(:force_bool_empty, "", :string_to_boolean) 12 | system_env(:force_bool_words, "not a boolean", :string_to_boolean) 13 | system_env(:must_bool, false, :string_to_boolean) 14 | system_env(:as_a_list, "url_one,url_two", :string_to_list) 15 | system_env(:as_a_system_list, "url_one,url_two", :string_to_list) 16 | system_env(:as_a_one_item_list, "url_one", :string_to_list) 17 | system_env(:passed_a_list, ["url_one" ,"url_two"], :string_to_list) 18 | system_env(:as_a_charlist, 'a charlist', :string_to_charlist) 19 | system_env(:as_a_string_charlist, "a charlist", :string_to_charlist) 20 | system_env(:as_a_system_charlist, 'a charlist', :string_to_charlist) 21 | 22 | app_env(:unset, [:env_helper, :unset], "didn't set") 23 | app_env(:set, [:env_helper, :set], "didn't set") 24 | 25 | defmodule Section do 26 | app_env(:setvar, [:env_helper, :section, :setvar], "didn't set") 27 | app_env(:secret, [:env_helper, :section, :secret], "default secret") 28 | end 29 | end 30 | 31 | defmodule SettingsTest do 32 | use ExUnit.Case 33 | doctest EnvHelper 34 | require TestSettings 35 | 36 | setup do 37 | System.put_env("THIS_GIST", "ghast") 38 | System.put_env("MUST_INT", "4321") 39 | System.put_env("MUST_BOOL", "true") 40 | System.put_env("AS_A_SYSTEM_LIST", "url_three, url_four") 41 | System.put_env("AS_A_SYSTEM_CHARLIST", "b charlist") 42 | end 43 | 44 | test "creates methods for simple cases" do 45 | assert TestSettings.this == "that" 46 | assert TestSettings.this_gist == "ghast" 47 | end 48 | 49 | test "allows forcing binary to integer" do 50 | assert TestSettings.must_int == 4321 51 | assert TestSettings.force_int == 4821 52 | assert TestSettings.should_int == 5699 53 | end 54 | 55 | test "allows forcing binary to boolean" do 56 | assert TestSettings.force_bool_true == true 57 | assert TestSettings.force_bool_false == false 58 | assert TestSettings.force_bool_nil == false 59 | assert TestSettings.force_bool_empty == false 60 | assert TestSettings.force_bool_words == true 61 | assert TestSettings.must_bool == true 62 | end 63 | 64 | test "creates lists from strings" do 65 | assert TestSettings.as_a_list() == ["url_one","url_two"] 66 | assert TestSettings.as_a_one_item_list() == ["url_one"] 67 | assert TestSettings.as_a_system_list() == ["url_three","url_four"] 68 | assert TestSettings.passed_a_list() == ["url_one","url_two"] 69 | end 70 | 71 | test "creates charlists from strings" do 72 | assert TestSettings.as_a_charlist() == 'a charlist' 73 | assert TestSettings.as_a_string_charlist() == 'a charlist' 74 | assert TestSettings.as_a_system_charlist() == 'b charlist' 75 | end 76 | 77 | test "picks up application variables" do 78 | Application.put_env :env_helper, :set, "was set" 79 | assert TestSettings.unset == "didn't set" 80 | assert TestSettings.set == "was set" 81 | 82 | Application.put_env :env_helper, :section, [setvar: "was set"] 83 | assert TestSettings.Section.secret == "default secret" 84 | assert TestSettings.Section.setvar == "was set" 85 | end 86 | end 87 | -------------------------------------------------------------------------------- /test/test_helper.exs: -------------------------------------------------------------------------------- 1 | ExUnit.start() 2 | --------------------------------------------------------------------------------