about summary refs log tree commit homepage
path: root/test/test_env.rb
blob: 3bbb372aace264ae342e75a73041e1c60f5eeb2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Copyright (C) 2013-2015 all contributors <dtas-all@nongnu.org>
# License: GPL-3.0+ (https://www.gnu.org/licenses/gpl-3.0.txt)
# frozen_string_literal: true
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

  def test_ary
    ENV['HELLO'] = 'HIHI'
    ENV['PAATH'] = '/usr/local/bin:/usr/bin:/bin'
    env = { 'BLAH' => [ '$HELLO/WORLD', '$PAATH', '$(echo hello world)' ] }
    res = env_expand(env, expand: true)
    exp = [ "HIHI/WORLD", ENV['PAATH'], 'hello world' ]
    assert_equal exp, Shellwords.split(res['BLAH'])
    env = {
      'BLAH' => [ '$(echo hello world)' ],
      'MOAR' => [ '$BLAH', 'OMG HALP SPACES' ]
    }
    res = env_expand(env, expand: true)
    exp = ["hello\\ world", "OMG HALP SPACES"]
    assert_equal exp, Shellwords.split(res['MOAR'])
  end
end