fix inconsistent type handling in VarSet#expand_varref - #73

This commit is contained in:
Josh Holtrop 2019-04-14 14:10:13 -04:00
parent 7cc68b2ce1
commit 4967f89a76
3 changed files with 27 additions and 18 deletions

View File

@ -94,46 +94,42 @@ module Rscons
# @return [nil, String, Array, Symbol, TrueClass, FalseClass] # @return [nil, String, Array, Symbol, TrueClass, FalseClass]
# Expanded value with "$!{var}" variable references replaced. # Expanded value with "$!{var}" variable references replaced.
def expand_varref(varref, lambda_args) def expand_varref(varref, lambda_args)
if varref.is_a?(String) case varref
when Symbol, true, false, nil
varref
when String
if varref =~ /^(.*)\$\{([^}]+)\}(.*)$/ if varref =~ /^(.*)\$\{([^}]+)\}(.*)$/
prefix, varname, suffix = $1, $2, $3 prefix, varname, suffix = $1, $2, $3
prefix = expand_varref(prefix, lambda_args) unless prefix.empty? prefix = expand_varref(prefix, lambda_args) unless prefix.empty?
varval = expand_varref(self[varname], lambda_args) varval = expand_varref(self[varname], lambda_args)
# suffix needs no expansion since the regex matches the last occurence # suffix needs no expansion since the regex matches the last occurence
if varval.is_a?(String) or varval.nil? case varval
when Symbol, true, false, nil, String
if prefix.is_a?(Array) if prefix.is_a?(Array)
prefix.map {|p| "#{p}#{varval}#{suffix}"} prefix.map {|p| "#{p}#{varval}#{suffix}"}
else else
"#{prefix}#{varval}#{suffix}" "#{prefix}#{varval}#{suffix}"
end end
elsif varval.is_a?(Array) when Array
if prefix.is_a?(Array) if prefix.is_a?(Array)
varval.map {|vv| prefix.map {|p| "#{p}#{vv}#{suffix}"}}.flatten varval.map {|vv| prefix.map {|p| "#{p}#{vv}#{suffix}"}}.flatten
else else
varval.map {|vv| "#{prefix}#{vv}#{suffix}"} varval.map {|vv| "#{prefix}#{vv}#{suffix}"}
end end
else else
raise "I do not know how to expand a variable reference to a #{varval.class.name} (from #{varname.inspect} => #{self[varname].inspect})" raise "Unknown construction variable type: #{varval.class} (from #{varname.inspect} => #{self[varname].inspect})"
end end
else else
varref varref
end end
elsif varref.is_a?(Array) when Array
varref.map do |ent| varref.map do |ent|
expand_varref(ent, lambda_args) expand_varref(ent, lambda_args)
end.flatten end.flatten
elsif varref.is_a?(Proc) when Proc
expand_varref(varref[*lambda_args], lambda_args) expand_varref(varref[*lambda_args], lambda_args)
elsif varref.nil?
nil
elsif varref.is_a?(Symbol)
varref
elsif varref.is_a?(TrueClass)
varref
elsif varref.is_a?(FalseClass)
varref
else else
raise "Unknown varref type: #{varref.class} (#{varref.inspect})" raise "Unknown construction variable type: #{varref.class} (#{varref.inspect})"
end end
end end

View File

@ -184,7 +184,7 @@ module Rscons
env["foo"] = {} env["foo"] = {}
expect(env.expand_varref(["-p${path}", "${flags}"])).to eq ["-pdir1", "-pdir2", "-x", "-y", "-z"] expect(env.expand_varref(["-p${path}", "${flags}"])).to eq ["-pdir1", "-pdir2", "-x", "-y", "-z"]
expect(env.expand_varref("foo")).to eq "foo" expect(env.expand_varref("foo")).to eq "foo"
expect {env.expand_varref("${foo}")}.to raise_error /Unknown.varref.type/ expect {env.expand_varref("${foo}")}.to raise_error /Unknown.construction.variable.type/
expect(env.expand_varref("${specialflag}")).to eq "-z" expect(env.expand_varref("${specialflag}")).to eq "-z"
expect(env.expand_varref("${path}")).to eq ["dir1", "dir2"] expect(env.expand_varref("${path}")).to eq ["dir1", "dir2"]
end end

View File

@ -166,13 +166,26 @@ module Rscons
expect(v.expand_varref(false, :lambda_args)).to eq(false) expect(v.expand_varref(false, :lambda_args)).to eq(false)
end end
it "raises an error when given an invalid argument" do it "raises an error when given an invalid argument" do
expect { v.expand_varref({a: :b}, :lambda_args) }.to raise_error /Unknown varref type: Hash/ expect { v.expand_varref({a: :b}, :lambda_args) }.to raise_error /Unknown construction variable type: Hash/
end end
it "raises an error when an expanded variable is an unexpected type" do it "raises an error when an expanded variable is an unexpected type" do
expect(v).to receive(:[]).at_least(1).times.with("bad").and_return("bad_val") expect(v).to receive(:[]).at_least(1).times.with("bad").and_return("bad_val")
expect(v).to receive(:expand_varref).with("bad_val", :lambda_args).and_return({a: :b}) expect(v).to receive(:expand_varref).with("bad_val", :lambda_args).and_return({a: :b})
expect(v).to receive(:expand_varref).and_call_original expect(v).to receive(:expand_varref).and_call_original
expect { v.expand_varref("${bad}", :lambda_args) }.to raise_error /I do not know how to expand a variable reference to a Hash/ expect { v.expand_varref("${bad}", :lambda_args) }.to raise_error /Unknown construction variable type: Hash/
end
it "expands symbols within a string outside an array" do
v['var'] = :a_symbol
expect(v.expand_varref("this is ${var}", :lambda_args)).to eq "this is a_symbol"
end
it "expands booleans within a string outside an array" do
v['var'] = false
expect(v.expand_varref("this is ${var}", :lambda_args)).to eq "this is false"
end
it "expands symbols and booleans in an array" do
v['var'] = [:a_symbol, false]
expanded = v.expand_varref("this is ${var}", :lambda_args)
expect(expanded).to eq(["this is a_symbol", "this is false"])
end end
end end