Sha256: 74f7da505f888e1af8ff3733d8addfc00cfe88d2ea574dfce982660e0b2c88e8

Contents?: true

Size: 1.98 KB

Versions: 18

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true

require "hanami/settings"

RSpec.describe "Settings / Using types", :app_integration do
  before do
    @env = ENV.to_h
  end

  after do
    ENV.replace(@env)
  end

  specify "types from a provided types module can be used as setting constructors to coerce values" do
    with_tmp_directory(Dir.mktmpdir) do
      write "config/app.rb", <<~RUBY
        require "hanami"

        module TestApp
          class App < Hanami::App
          end
        end
      RUBY

      write "config/settings.rb", <<~RUBY
        module TestApp
          class Settings < Hanami::Settings
            Bool = Types::Params::Bool

            setting :numeric, constructor: Types::Params::Integer
            setting :flag, constructor: Bool
          end
        end
      RUBY

      ENV["NUMERIC"] = "42"
      ENV["FLAG"] = "true"

      require "hanami/prepare"

      expect(Hanami.app["settings"].numeric).to eq 42
      expect(Hanami.app["settings"].flag).to be true
    end
  end

  specify "errors raised from setting constructors are collected and re-raised in aggregate, and will prevent the app from booting" do
    with_tmp_directory(Dir.mktmpdir) do
      write "config/app.rb", <<~RUBY
        require "hanami"

        module TestApp
          class App < Hanami::App
          end
        end
      RUBY

      write "config/settings.rb", <<~RUBY
        module TestApp
          class Settings < Hanami::Settings
            setting :numeric, constructor: Types::Params::Integer
            setting :flag, constructor: Types::Params::Bool
          end
        end
      RUBY

      ENV["NUMERIC"] = "never gonna"
      ENV["FLAG"] = "give you up"

      numeric_error = "numeric: invalid value for Integer"
      flag_error = "flag: give you up cannot be coerced"

      expect {
        require "hanami/prepare"
      }.to raise_error(
        Hanami::Settings::InvalidSettingsError,
        /#{numeric_error}.+#{flag_error}|#{flag_error}.+#{numeric_error}/m
      )
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
hanami-2.2.1 spec/integration/settings/using_types_spec.rb
hanami-2.2.0 spec/integration/settings/using_types_spec.rb
hanami-2.2.0.rc1 spec/integration/settings/using_types_spec.rb
hanami-2.2.0.beta2 spec/integration/settings/using_types_spec.rb
hanami-2.2.0.beta1 spec/integration/settings/using_types_spec.rb
hanami-2.1.0 spec/integration/settings/using_types_spec.rb
hanami-2.1.0.rc3 spec/integration/settings/using_types_spec.rb
hanami-2.1.0.rc2 spec/integration/settings/using_types_spec.rb
hanami-2.1.0.rc1 spec/integration/settings/using_types_spec.rb
hanami-2.1.0.beta2.1 spec/integration/settings/using_types_spec.rb
hanami-2.1.0.beta2 spec/integration/settings/using_types_spec.rb
hanami-2.1.0.beta1 spec/integration/settings/using_types_spec.rb
hanami-2.0.3 spec/integration/settings/using_types_spec.rb
hanami-2.0.2 spec/integration/settings/using_types_spec.rb
hanami-2.0.1 spec/integration/settings/using_types_spec.rb
hanami-2.0.0 spec/integration/settings/using_types_spec.rb
hanami-2.0.0.rc1 spec/integration/settings/using_types_spec.rb
hanami-2.0.0.beta4 spec/integration/settings/using_types_spec.rb