Add basic spec for map functions definition.

This commit is contained in:
Glenn Y. Rolland 2015-08-12 00:00:16 +02:00
parent 6adaec530c
commit 51c8dea76a
2 changed files with 72 additions and 37 deletions

View file

@ -9,16 +9,6 @@ module Qasim ; module Map
class ParseError < RuntimeError ; end class ParseError < RuntimeError ; end
class ConnectError < RuntimeError ; end class ConnectError < RuntimeError ; end
module_function :from_file
def from_file
end
def to_file
end
# #
# replace magic values withing map lines # replace magic values withing map lines
# #
@ -27,27 +17,36 @@ module Qasim ; module Map
# $(var) => variable value from environment # $(var) => variable value from environment
# ${var} => variable value from environment # ${var} => variable value from environment
# #
def env_substitute str, lineno def env_substitute text
local_env = ENV.clone seek = true
while str =~ /\$(\w+)/ do str = text
while seek do
seek = false
case str case str
when /\$\{(.+)\}/ then when /^(.*)\${([^}]+?)}(.*)$/ then
pattern = $1 before, pattern, after = [$1, $2, $3]
str.gsub!(/\$\{#{pattern}\}/,local_env[pattern]) pattern_value = env_substitute(pattern)
when /\$(\w+)/ then pattern_value = (ENV[pattern_value] || "")
pattern = $1 str = before + pattern_value + after
str.gsub!(/\$#{pattern}/,local_env[pattern]) seek = true
else when /^(.*)\$(\w+)(.*)$/ then
puts "w: unknown pattern: %s at str %d" % [str, lineno] before, pattern, after = [$1, $2, $3]
pattern_value = (ENV[pattern] || "")
str = before + pattern_value + after
seek = true
end end
end end
str str
end end
# #
# Load map description from file # Load description from file and create a Map object
# #
def from_file filename def from_file appcfg, filename
config = {}
map = nil
f = File.open filename f = File.open filename
linect = 0 linect = 0
f.each do |line| f.each do |line|
@ -58,35 +57,33 @@ module Qasim ; module Map
case line case line
when /^\s*REMOTE_USER\s*=\s*(.*)\s*$/ then when /^\s*REMOTE_USER\s*=\s*(.*)\s*$/ then
@user = $1 config[:user] = $1
#rdebug "d: remote_user => #{$1}"
when /^\s*REMOTE_PORT\s*=\s*(.*)\s*$/ then when /^\s*REMOTE_PORT\s*=\s*(.*)\s*$/ then
@port = $1.to_i config[:port] = $1.to_i
#rdebug "d: remote_port => #{$1}"
when /^\s*REMOTE_HOST\s*=\s*(.*)\s*$/ then when /^\s*REMOTE_HOST\s*=\s*(.*)\s*$/ then
@host = $1 config[:host] = $1
#rdebug "d: remote_host => #{$1}"
when /^\s*REMOTE_CYPHER\s*=\s*(.*)\s*$/ then when /^\s*REMOTE_CYPHER\s*=\s*(.*)\s*$/ then
if CYPHERS.map{|x| x.to_s}.include? $1 then if CYPHERS.map(&:to_s).include? $1 then
@host = $1.to_sym config[:cypher] = $1.to_sym
end end
when /^\s*MAP\s*=\s*(.*)\s+(.*)\s*$/ then when /^\s*MAP\s*=\s*(.*)\s+(.*)\s*$/ then
@links[$1] = $2 config[:links] ||= {}
#rdebug "d: link #{$1} => #{$2}" config[:links][$1] = $2
when /^\s*$/,/^\s*#/ then when /^\s*$/,/^\s*#/ then
#rdebug "d: dropping empty line"
else else
raise MapParseError, "parse error at #{@filename}:#{linect}" raise MapParseError, "parse error at #{@filename}:#{linect}"
end end
end end
f.close f.close
map = Ssh.new config, filename
return map
end end
# #
# Write map description to file # Write map description to file
# #
def write path=nil def to_file path=nil
@path=path unless path.nil? @path=path unless path.nil?
File.open(@path, "w") do |f| File.open(@path, "w") do |f|
@ -96,4 +93,5 @@ module Qasim ; module Map
f.puts "REMOTE_CYPHER=%s" % @cypher f.puts "REMOTE_CYPHER=%s" % @cypher
end end
end end
module_function :from_file, :env_substitute
end ; end end ; end

View file

@ -2,6 +2,43 @@
require_relative 'spec_helper' require_relative 'spec_helper'
require 'qasim/map' require 'qasim/map'
describe Qasim::Config do describe Qasim::Map do
# something describe 'env_substitute' do
it "returns a normal string unchanged" do
str = "5cb0c49325df2d526116ef7b49eb7329"
assert_equal Qasim::Map.env_substitute(str), str
end end
it "replaces a ${variable} with its value from environment" do
str = "SOMETHING = ${HOME}"
ref = "SOMETHING = #{ENV['HOME']}"
assert_equal ref, Qasim::Map.env_substitute(str)
end
it "replaces a $variable with its value from environment" do
str = "SOMETHING = $HOME"
ref = "SOMETHING = #{ENV['HOME']}"
assert_equal ref, Qasim::Map.env_substitute(str)
end
it "does not replace original string" do
str = "SOMETHING = $HOME"
res = "SOMETHING = $HOME"
Qasim::Map.env_substitute(str)
assert_equal res, str
end
it "works recursively" do
ENV['QASIM_MAP_TEST1']='QASIM_MAP_TEST2'
ENV['QASIM_MAP_TEST2']='OK'
str = "SOMETHING = ${$QASIM_MAP_TEST1}"
ref = "SOMETHING = OK"
assert_equal ref, Qasim::Map.env_substitute(str)
end
end
describe 'from_file' do
end
end