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

View file

@ -2,6 +2,43 @@
require_relative 'spec_helper'
require 'qasim/map'
describe Qasim::Config do
# something
describe Qasim::Map do
describe 'env_substitute' do
it "returns a normal string unchanged" do
str = "5cb0c49325df2d526116ef7b49eb7329"
assert_equal Qasim::Map.env_substitute(str), 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 "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