Skip to content

Commit

Permalink
add test for config
Browse files Browse the repository at this point in the history
  • Loading branch information
hahmed committed May 17, 2019
1 parent 4a129e6 commit 91b9142
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 17 deletions.
14 changes: 9 additions & 5 deletions lib/gel/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class Gel::Config
def initialize
@root = file_path
@root = File.expand_path("~/.config/gel")
@path = File.join(@root, "config")
@config = nil
end
Expand All @@ -15,8 +15,12 @@ def all
config
end

def file_path
File.expand_path("~/.config/gel")
def config_exists?
File.exist?(@path)
end

def config_file
File.read(@path)
end

def [](group = nil, key)
Expand All @@ -41,9 +45,9 @@ def []=(group = nil, key, value)

def read
result = {}
if File.exist?(@path)
if config_exists?
context = nil
File.read(@path).each_line do |line|
config_file.each_line do |line|
line.chomp!
if line =~ /\A(\S[^:]*):\z/
context = result[$1] = {}
Expand Down
12 changes: 0 additions & 12 deletions test/command/config_test.rb

This file was deleted.

37 changes: 37 additions & 0 deletions test/config_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

require "test_helper"

class ConfigTest < Minitest::Test
def test_reading_single_item_from_config_successully
config = Class.new(Gel::Config) do
define_method(:config_file) do
File.open(fixture_file("config")).read
end

define_method(:config_exists?) do
true
end
end

klass = config.new
assert_equal '1', klass['jobs']
assert_equal 'true', klass['gem.mit']
end

def test_reading_all_from_config_successully
config = Class.new(Gel::Config) do
define_method(:config_file) do
File.open(fixture_file("config")).read
end

define_method(:config_exists?) do
true
end
end

output = config.new.all
assert output['jobs'] == { '' => '1' }
assert output['gem'] == { 'mit' => 'true' }
end
end
4 changes: 4 additions & 0 deletions test/fixtures/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
jobs:
: 1
gem:
mit: true

0 comments on commit 91b9142

Please sign in to comment.