From d2f75170a6c1548b85335c7e84a000ed67a0158b Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 7 Sep 2013 21:25:17 +0000 Subject: implement environment variable expansion This will allow users to more-easily edit configs and feel like a real shell. We no longer mistakenly expand nil env variables to "" anymore, either. --- test/test_env.rb | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 test/test_env.rb (limited to 'test/test_env.rb') diff --git a/test/test_env.rb b/test/test_env.rb new file mode 100644 index 0000000..92fc53c --- /dev/null +++ b/test/test_env.rb @@ -0,0 +1,55 @@ +# Copyright (C) 2013, Eric Wong and all contributors +# License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt) +require_relative 'helper' +require 'dtas/process' +class TestEnv < Testcase + include DTAS::Process + def setup + @orig = ENV.to_hash + end + + def teardown + ENV.clear + ENV.update(@orig) + end + + def test_expand + ENV["HELLO"] = 'HIHI' + expect = { "BLAH" => "HIHI/WORLD" } + opts = {} + + env = { "BLAH" => "$HELLO/WORLD" } + assert_equal(expect, env_expand(env, opts)) + + env = { "BLAH" => "${HELLO}/WORLD" } + assert_equal(expect, env_expand(env, opts)) + + env = { "BLAH" => "$(echo $HELLO)/WORLD" } + assert_equal(expect, env_expand(env, opts)) + + env = { "BLAH" => "`echo $HELLO/WORLD`" } + assert_equal(expect, env_expand(env, opts)) + + env = { "BLAH" => "HIHI/WORLD" } + assert_equal(expect, env_expand(env, opts)) + + # disable expansion + env = expect = { "BLAH" => "`echo $HELLO/WORLD`" } + assert_equal(expect, env_expand(env, expand: false)) + + # numeric expansion always happens + env = { "BLAH" => 1 } + assert_equal({"BLAH"=>"1"}, env_expand(env, expand: false)) + env = { "BLAH" => 1 } + assert_equal({"BLAH"=>"1"}, env_expand(env, {})) + + expect = { "BLAH" => nil } + env = expect.dup + assert_equal expect, env_expand(env, expand:false) + assert_equal expect, env_expand(env, expand:true) + + # recursive expansion + res = env_expand({"PATH"=>"$PATH"}, expand: true) + assert_equal ENV["PATH"], res["PATH"] + end +end -- cgit v1.2.3-24-ge0c7