Skip to content

Commit

Permalink
Check config file existence
Browse files Browse the repository at this point in the history
  • Loading branch information
bibendi committed Oct 8, 2018
1 parent 51d8f2a commit 486ffe5
Show file tree
Hide file tree
Showing 18 changed files with 88 additions and 55 deletions.
1 change: 0 additions & 1 deletion dip.yml
Expand Up @@ -3,7 +3,6 @@ version: '2'
compose:
files:
- docker-compose.yml
project_name: dip

interaction:
bash:
Expand Down
7 changes: 6 additions & 1 deletion exe/dip
Expand Up @@ -6,7 +6,12 @@ $LOAD_PATH.unshift(lib_path) unless $LOAD_PATH.include?(lib_path)

require 'dip'
require 'dip/cli'
require 'pry-byebug' if Dip.debug?

begin
require 'pry-byebug' if Dip.debug?
rescue LoadError
puts "pry-byebug not found!"
end

Signal.trap('INT') do
warn("\n#{caller.join("\n")}: interrupted")
Expand Down
19 changes: 6 additions & 13 deletions lib/dip.rb
@@ -1,31 +1,24 @@
# frozen_string_literal: true

require "dip/version"
require "dip/config"
require "dip/environment"

module Dip
Error = Class.new(StandardError)

class << self
def config_path
ENV["DIP_FILE"] || File.join(Dir.pwd, "dip.yml")
end

def config
@config ||= Dip::Config.new(config_path)
@config ||= Dip::Config.new
end

def env
@env ||= Dip::Environment.new(config.environment)
end

def test?
ENV["DIP_ENV"] == "test"
@env ||= Dip::Environment.new(config.exist? ? config.environment : {})
end

def debug?
ENV["DIP_ENV"] == "debug"
%w(test debug).each do |key|
define_method("#{key}?") do
ENV["DIP_ENV"] == key
end
end

def reset!
Expand Down
2 changes: 1 addition & 1 deletion lib/dip/commands/compose.rb
Expand Up @@ -11,7 +11,7 @@ class Compose < Dip::Command
def initialize(cmd, argv = [])
@cmd = cmd
@argv = argv
@config = ::Dip.config.compose
@config = ::Dip.config.compose || {}
end

def execute
Expand Down
46 changes: 22 additions & 24 deletions lib/dip/config.rb
Expand Up @@ -5,42 +5,40 @@

module Dip
class Config
def initialize(config_path)
load_or_default(config_path)
end
DEFAULT_PATH = "dip.yml"

def merge(config)
@config.merge!(config)
def initialize
@path = ENV["DIP_FILE"] || File.join(Dir.pwd, DEFAULT_PATH)
end

def environment
@config.fetch(:environment, {})
def exist?
File.exist?(@path)
end

def compose
@config.fetch(:compose, {})
[:environment, :compose, :interaction, :provision].each do |key|
define_method(key) do
config[key]
end
end

def interaction
@config.fetch(:interaction, {})
def to_h
config
end

def provision
@config.fetch(:provision)
private

def config
@config ||= load
end

private
def load
raise ArgumentError, "Dip config not found at path '#{@path}'" unless exist?

def load_or_default(config_path)
@config ||= if File.exist?(config_path) && !Dip.test?
YAML.safe_load(
ERB.new(File.read(config_path)).result,
[], [], true,
symbolize_names: true
)
else
{}
end
@config = YAML.safe_load(
ERB.new(File.read(@path)).result,
[], [], true,
symbolize_names: true
)
end
end
end
2 changes: 1 addition & 1 deletion lib/dip/environment.rb
Expand Up @@ -10,7 +10,7 @@ class Environment
def initialize(default_vars)
@vars = {}

merge(default_vars)
merge(default_vars || {})
end

def merge(new_vars)
Expand Down
9 changes: 9 additions & 0 deletions spec/fixtures/dip.yml
@@ -0,0 +1,9 @@
version: '2'

environment: {}

compose: {}

interaction: {}

provision: []
Expand Up @@ -35,6 +35,7 @@
let(:config) { {compose: {files: %w(file1.yml file2.yml file3.yml)}} }

before do
allow(File).to receive(:exist?).and_call_original
allow(File).to receive(:exist?).with("file1.yml").and_return(true)
allow(File).to receive(:exist?).with("file2.yml").and_return(false)
allow(File).to receive(:exist?).with("file3.yml").and_return(true)
Expand All @@ -50,6 +51,7 @@
let(:env) { {"DIP_OS" => "darwin"} }

before do
allow(File).to receive(:exist?).and_call_original
allow(File).to receive(:exist?).with("file1-darwin.yml").and_return(true)

cli.start "compose run".shellsplit
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
31 changes: 31 additions & 0 deletions spec/lib/dip/config_spec.rb
@@ -0,0 +1,31 @@
# frozen_string_literal: true

describe Dip::Config do
subject { described_class.new }

describe "#exist?" do
context "when file exists" do
it { is_expected.to be_exist }
end

context "when file doesn't exist", env: true do
let(:env) { {"DIP_FILE" => "no.yml"} }

it { is_expected.to_not be_exist }
end
end

[:environment, :compose, :interaction, :provision].each do |key|
describe "##{key}" do
context "when config file doesn't exist", env: true do
let(:env) { {"DIP_FILE" => "no.yml"} }

it { expect { subject.public_send(key) }.to raise_error(ArgumentError) }
end

context "when config exists" do
it { expect(subject.public_send(key)).to_not be_nil }
end
end
end
end
File renamed without changes.
11 changes: 0 additions & 11 deletions spec/lib/dip_spec.rb
Expand Up @@ -5,17 +5,6 @@
expect(Dip::VERSION).not_to be nil
end

describe ".config_path" do
context "when by default" do
it { expect(Dip.config_path).to eq "#{Dir.pwd}/dip.yml" }
end

context "when path fron env", env: true do
let(:env) { {"DIP_FILE" => "exptected/dip.yml"} }
it { expect(Dip.config_path).to eq "exptected/dip.yml" }
end
end

describe ".config" do
it "initializes the config" do
expect(Dip.config).to be_is_a Dip::Config
Expand Down
11 changes: 9 additions & 2 deletions spec/spec_helper.rb
Expand Up @@ -30,11 +30,18 @@
mocks.verify_partial_doubles = true
end

config.define_derived_metadata(file_path: %r{/spec/lib/commands}) do |metadata|
config.define_derived_metadata(file_path: %r{/spec/lib/dip/commands}) do |metadata|
metadata[:runner] = true
end

config.before(:each) do
config.around do |ex|
orig = ENV["DIP_FILE"]
ENV["DIP_FILE"] = File.join(__dir__, "fixtures", "dip.yml")
ex.run
ENV["DIP_FILE"] = orig
end

config.before do
Dip.reset!
end

Expand Down
2 changes: 1 addition & 1 deletion spec/support/shared_contexts/config.rb
Expand Up @@ -2,6 +2,6 @@

shared_context "dip config", config: true do
before do
Dip.config.merge(config)
Dip.config.to_h.merge!(config)
end
end

0 comments on commit 486ffe5

Please sign in to comment.