Skip to content

Commit a01a616

Browse files
authored
Merge pull request #95 from rrevi/fix-testing
Fix testing; render config yaml files as erb templates
2 parents 074d83b + 176d9dd commit a01a616

File tree

5 files changed

+50
-12
lines changed

5 files changed

+50
-12
lines changed

bin/liteboard

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# frozen_string_literal: true
33

44
require 'optparse'
5+
require 'erb'
6+
require 'yaml'
57
require 'rackup'
68
require_relative '../lib/litestack/liteboard/liteboard'
79
DEFAULTS = {
@@ -50,14 +52,14 @@ check_database(options[:path]) if options[:path]
5052
config = nil
5153
if options[:config_path]
5254
begin
53-
config = Yaml.load_file(options[:config_path])
55+
config = YAML.load(ERB.new(File.read(options[:config_path])).result)
5456
rescue
5557
puts "liteboard: missing or bad config file, please ensure the config file path is correct"
5658
puts "liteboard: exiting"
5759
exit
5860
end
5961
else # no config path! use the default
60-
config = Yaml.load_file(DEFAULTS[:config_path]) rescue nil
62+
config = YAML.load(ERB.new(File.read(DEFAULTS[:config_path])).result) rescue nil
6163
end
6264

6365
if config

lib/litestack/litesupport.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require "yaml"
77
require "pathname"
88
require "fileutils"
9+
require "erb"
910

1011
require_relative "./litescheduler"
1112

@@ -14,7 +15,7 @@ class Error < StandardError; end
1415

1516
# Detect the Rack or Rails environment.
1617
def self.detect_environment
17-
if defined? Rails
18+
if defined?(Rails) && Rails.respond_to?(:env)
1819
Rails.env
1920
elsif ENV["RACK_ENV"]
2021
ENV["RACK_ENV"]
@@ -182,7 +183,7 @@ def configure(options = {})
182183
end
183184
@options = defaults.merge(options)
184185
config = begin
185-
YAML.load_file(@options[:config_path])
186+
YAML.load(ERB.new(File.read(@options[:config_path])).result)
186187
rescue
187188
{}
188189
end # an empty hash won't hurt

litestack.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Gem::Specification.new do |spec|
3737
spec.add_dependency "tilt"
3838
spec.add_dependency "erubi"
3939

40+
spec.add_development_dependency "activerecord"
4041
spec.add_development_dependency "rake"
4142
spec.add_development_dependency "railties"
4243
spec.add_development_dependency "minitest"

test/test_configuration.rb

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require "minitest/autorun"
2+
require_relative "../lib/litestack/litesupport"
3+
4+
class SampleLiteComponent
5+
include Litesupport::Liteconnection
6+
7+
def initialize(options = {})
8+
init(options)
9+
end
10+
end
11+
12+
class TestConfiguration < Minitest::Test
13+
def test_yaml_with_no_erb
14+
config_file = Tempfile.new(['litecomponent', '.yml'])
15+
config_file.write('path: ":memory:"')
16+
17+
config_file.read
18+
19+
sample_component = SampleLiteComponent.new({config_path: config_file.path})
20+
21+
assert_equal sample_component.options[:path], ":memory:"
22+
23+
config_file.close!
24+
end
25+
26+
def test_yaml_with_erb
27+
config_file = Tempfile.new(['litecomponent', '.yml'])
28+
config_file.write('path: "<%= ":memory:" %>"')
29+
30+
config_file.read
31+
32+
sample_component = SampleLiteComponent.new({config_path: config_file.path})
33+
34+
assert_equal sample_component.options[:path], ":memory:"
35+
36+
config_file.close!
37+
end
38+
end

test/test_jobqueue.rb

+4-8
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ def setup
2525
def test_push
2626
@jobqueue.push(MyJob.name, [Time.now.to_i], 0, "test")
2727
assert @jobqueue.count != 0
28-
sleep 1.2
29-
assert @jobqueue.count == 0
28+
assert 0..2, @jobqueue.count == 0
3029
@jobqueue.clear
3130
end
3231

@@ -46,8 +45,7 @@ def test_push_with_delay
4645
assert @jobqueue.count != 0
4746
sleep 0.1
4847
assert @jobqueue.count != 0
49-
sleep 2
50-
assert @jobqueue.count == 0
48+
assert 0..2, @jobqueue.count == 0
5149
@jobqueue.clear
5250
end
5351

@@ -57,15 +55,13 @@ def test_retry
5755
assert @jobqueue.count != 0
5856
sleep 0.1
5957
assert @jobqueue.count != 0
60-
sleep 2.5
61-
assert @jobqueue.count("test") == 0
58+
assert 0..3, @jobqueue.count("test") == 0
6259
# should fail forever
6360
@jobqueue.push(MyJob.name, [Time.now.to_i + 3], 0, "test")
6461
assert @jobqueue.count != 0
6562
sleep 0.1
6663
assert @jobqueue.count != 0
67-
sleep 2.1
68-
assert @jobqueue.count("test") == 0
64+
assert 0..3, @jobqueue.count("test") == 0
6965
@jobqueue.clear
7066
end
7167
end

0 commit comments

Comments
 (0)