nugrant 1.4.2 → 2.0.0.dev1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +1 -2
  3. data/.travis.yml +0 -6
  4. data/CHANGELOG.md +3 -79
  5. data/Gemfile +8 -8
  6. data/README.md +10 -146
  7. data/lib/nugrant.rb +4 -19
  8. data/lib/nugrant/bag.rb +8 -12
  9. data/lib/nugrant/config.rb +55 -24
  10. data/lib/nugrant/helper/bag.rb +19 -17
  11. data/lib/nugrant/parameters.rb +37 -9
  12. data/lib/nugrant/vagrant/v1/command/parameters.rb +13 -13
  13. data/lib/nugrant/vagrant/v1/command/root.rb +2 -7
  14. data/lib/nugrant/vagrant/v1/config/user.rb +1 -10
  15. data/lib/nugrant/vagrant/v2/command/parameters.rb +1 -1
  16. data/lib/nugrant/vagrant/v2/command/root.rb +2 -7
  17. data/lib/nugrant/vagrant/v2/config/user.rb +1 -10
  18. data/lib/nugrant/version.rb +1 -1
  19. data/nugrant.gemspec +1 -4
  20. data/test/lib/nugrant/test_bag.rb +19 -33
  21. data/test/lib/nugrant/test_config.rb +92 -100
  22. data/test/lib/nugrant/test_parameters.rb +186 -203
  23. data/test/resources/README.md +11 -11
  24. data/test/resources/{vagrantfiles/v1.empty → Vagrantfile.v1.empty} +0 -0
  25. data/test/resources/{vagrantfiles/v1.fake → Vagrantfile.v1.fake} +0 -0
  26. data/test/resources/{vagrantfiles/v1.real → Vagrantfile.v1.real} +0 -0
  27. data/test/resources/{vagrantfiles/v2.empty → Vagrantfile.v2.empty} +0 -0
  28. data/test/resources/{vagrantfiles/v2.fake → Vagrantfile.v2.fake} +1 -4
  29. data/test/resources/{vagrantfiles/v2.real → Vagrantfile.v2.real} +0 -0
  30. data/test/resources/json/params_current_1.json +6 -0
  31. data/test/resources/json/params_current_2.json +29 -0
  32. data/test/resources/{yml → yaml}/params_array.yml +0 -0
  33. data/test/resources/{yml → yaml}/params_boolean.yml +0 -0
  34. data/test/resources/{yml → yaml}/params_combinations.yml +0 -0
  35. data/test/resources/yaml/params_current_1.yml +4 -0
  36. data/test/resources/yaml/params_current_2.yml +23 -0
  37. data/test/resources/{yml → yaml}/params_defaults_at_root.yml +0 -0
  38. data/test/resources/{yml → yaml}/params_defaults_not_at_root.yml +0 -0
  39. data/test/resources/{yml → yaml}/params_empty.yml +0 -0
  40. data/test/resources/{yml → yaml}/params_list.yml +0 -0
  41. data/test/resources/{yml → yaml}/params_simple.yml +0 -0
  42. data/test/resources/{yml → yaml}/params_system_1.yml +0 -0
  43. data/test/resources/{yml → yaml}/params_system_2.yml +0 -0
  44. data/test/resources/{yml → yaml}/params_unix_eol.yml +0 -0
  45. data/test/resources/{yml → yaml}/params_user_1.yml +0 -0
  46. data/test/resources/{yml → yaml}/params_user_2.yml +0 -0
  47. data/test/resources/{yml → yaml}/params_windows_eol.yml +0 -0
  48. metadata +71 -95
  49. data/lib/nugrant/helper/env/exporter.rb +0 -208
  50. data/lib/nugrant/helper/env/namer.rb +0 -47
  51. data/lib/nugrant/helper/stack.rb +0 -86
  52. data/lib/nugrant/vagrant/errors.rb +0 -27
  53. data/lib/nugrant/vagrant/v1/command/env.rb +0 -107
  54. data/lib/nugrant/vagrant/v2/command/env.rb +0 -107
  55. data/locales/en.yml +0 -13
  56. data/test/lib/nugrant/helper/env/test_exporter.rb +0 -238
  57. data/test/lib/nugrant/helper/test_stack.rb +0 -149
  58. data/test/resources/json/params_project_1.json +0 -6
  59. data/test/resources/json/params_project_2.json +0 -29
  60. data/test/resources/json/params_user_nil_values.json +0 -9
  61. data/test/resources/vagrantfiles/v1.defaults_mixed_string_symbols +0 -18
  62. data/test/resources/vagrantfiles/v1.defaults_using_string +0 -18
  63. data/test/resources/vagrantfiles/v1.defaults_using_symbol +0 -18
  64. data/test/resources/vagrantfiles/v1.missing_parameter +0 -3
  65. data/test/resources/vagrantfiles/v2.defaults_mixed_string_symbols +0 -20
  66. data/test/resources/vagrantfiles/v2.defaults_null_values_in_vagrantuser +0 -25
  67. data/test/resources/vagrantfiles/v2.defaults_using_string +0 -20
  68. data/test/resources/vagrantfiles/v2.defaults_using_symbol +0 -20
  69. data/test/resources/vagrantfiles/v2.missing_parameter +0 -5
  70. data/test/resources/yml/params_project_1.yml +0 -4
  71. data/test/resources/yml/params_project_2.yml +0 -23
  72. data/test/resources/yml/params_user_nil_values.yml +0 -5
@@ -1,47 +0,0 @@
1
- module Nugrant
2
- module Helper
3
- module Env
4
- ##
5
- # A namer is a lambda taking as argument an array of segments
6
- # that should return a string representation of those segments.
7
- # How the segments are transformed to a string is up to the
8
- # namer. By using various namer, we can change how a bag key
9
- # is transformed into and environment variable name. This is
10
- # like the strategy pattern.
11
- #
12
- module Namer
13
-
14
- ##
15
- # Returns the default namer, which join segments together
16
- # using a character and upcase the result.
17
- #
18
- # @param `char` The character used to join segments together, default to `"_"`.
19
- #
20
- # @return A lambda that will simply joins segment using the `char` argument
21
- # and upcase the result.
22
- #
23
- def self.default(char = "_")
24
- lambda do |segments|
25
- segments.join(char).upcase()
26
- end
27
- end
28
-
29
- ##
30
- # Returns the prefix namer, which add a prefix to segments
31
- # and delegate its work to another namer.
32
- #
33
- # @param prefix The prefix to add to segments.
34
- # @param delegate_namer A namer that will be used to transform the prefixed segments.
35
- #
36
- # @return A lambda that will simply add prefix to segments and will call
37
- # the delegate_namer with those new segments.
38
- #
39
- def self.prefix(prefix, delegate_namer)
40
- lambda do |segments|
41
- delegate_namer.call([prefix] + segments)
42
- end
43
- end
44
- end
45
- end
46
- end
47
- end
@@ -1,86 +0,0 @@
1
- module Nugrant
2
- module Helper
3
- class Stack
4
- @@DEFAULT_MATCHER = /^(.+):([0-9]+)/
5
-
6
- def self.fetch_error_region(stack, options = {})
7
- entry = find_entry(stack, options)
8
- location = extract_error_location(entry, options)
9
-
10
- return (options[:unknown] || "Unknown") if not location[:file] and not location[:line]
11
- return location[:file] if not location[:line]
12
-
13
- fetch_error_region_from_location(location, options)
14
- end
15
-
16
- def self.fetch_error_region_from_location(location, options = {})
17
- prefix = options[:prefix] || " "
18
- width = options[:width] || 4
19
- file = File.new(location[:file], "r")
20
- line = location[:line]
21
-
22
- index = 0
23
-
24
- lines = []
25
- while (line_string = file.gets())
26
- index += 1
27
- next if (line - index).abs > width
28
-
29
- line_prefix = "#{prefix}#{index}:"
30
- line_prefix += (line == index ? ">> " : " ")
31
-
32
- lines << "#{line_prefix}#{line_string}"
33
- end
34
-
35
- lines.join().chomp()
36
- rescue
37
- return (options[:unknown] || "Unknown") if not location[:file] and not location[:line]
38
- return location[:file] if not location[:line]
39
-
40
- "#{location[:file]}:#{location[:line]}"
41
- ensure
42
- file.close() if file
43
- end
44
-
45
- ##
46
- # Search a stack list (as simple string array) for the first
47
- # entry that match the +:matcher+.
48
- #
49
- def self.find_entry(stack, options = {})
50
- matcher = options[:matcher] || @@DEFAULT_MATCHER
51
-
52
- stack.find do |entry|
53
- entry =~ matcher
54
- end
55
- end
56
-
57
- ##
58
- # Extract error location information from a stack entry using the
59
- # matcher received in arguments.
60
- #
61
- # The usual stack entry format is:
62
- # > /home/users/joe/work/lib/ruby.rb:4:Error message
63
- #
64
- # This function will extract the file and line information from
65
- # the stack entry using the matcher. The matcher is expected to
66
- # have two groups, the first for the file and the second for
67
- # line.
68
- #
69
- # The results is returned in form of a hash with two keys, +:file+
70
- # for the file information and +:line+ for the line information.
71
- #
72
- # If the matcher matched zero group, return +{:file => nil, :line => nil}+.
73
- # If the matcher matched one group, return +{:file => file, :line => nil}+.
74
- # If the matcher matched two groups, return +{:file => file, :line => line}+.
75
- #
76
- def self.extract_error_location(entry, options = {})
77
- matcher = options[:matcher] || @@DEFAULT_MATCHER
78
-
79
- result = matcher.match(entry)
80
- captures = result ? result.captures : []
81
-
82
- {:file => captures[0], :line => captures[1] ? captures[1].to_i() : nil}
83
- end
84
- end
85
- end
86
- end
@@ -1,27 +0,0 @@
1
- require 'vagrant/errors'
2
-
3
- require 'nugrant/helper/stack'
4
-
5
- module Nugrant
6
- module Vagrant
7
- module Errors
8
- class NugrantVagrantError < ::Vagrant::Errors::VagrantError
9
- error_namespace("nugrant.vagrant.errors")
10
- end
11
-
12
- class ParameterNotFoundError < NugrantVagrantError
13
- error_key(:parameter_not_found)
14
-
15
- def initialize(options = nil, *args)
16
- super({:context => compute_context()}.merge(options || {}), *args)
17
- end
18
-
19
- def compute_context()
20
- Helper::Stack.fetch_error_region(caller(), {
21
- :matcher => /(.+Vagrantfile):([0-9]+)/
22
- })
23
- end
24
- end
25
- end
26
- end
27
- end
@@ -1,107 +0,0 @@
1
- require 'nugrant'
2
- require 'nugrant/helper/env/exporter'
3
- require 'nugrant/parameters'
4
-
5
- module Nugrant
6
- module Vagrant
7
- module V1
8
- module Command
9
- class Env < ::Vagrant::Command::Base
10
- def initialize(arguments, environment)
11
- super(arguments, environment)
12
-
13
- @unset = false
14
- @script = false
15
- @format = :terminal
16
- @show_help = false
17
- end
18
-
19
- def create_parser()
20
- return OptionParser.new do |parser|
21
- parser.banner = "Usage: vagrant user env [<options>]"
22
- parser.separator ""
23
-
24
- parser.separator "Outputs the commands that should be executed to export\n" +
25
- "the various parameter as environment variables. By default,\n" +
26
- "existing ones are overridden. The --format argument can be used\n" +
27
- "to choose in which format the variables should be displayed.\n" +
28
- "Changing the format will also change where they are displayed.\n"
29
- parser.separator ""
30
- parser.separator "The `-s, --script` option is deprecated and will be removed in\n" +
31
- "version 2.0. Use `--format script` instead."
32
- parser.separator ""
33
-
34
- parser.separator "Available formats:"
35
- parser.separator " autoenv => Write commands to a file named `.env` in the current directory.\n" +
36
- " See https://github.com/kennethreitz/autoenv for more info."
37
- parser.separator " terminal => Display commands to terminal so they can be sourced."
38
- parser.separator " script => Write commands to a bash script named `nugrant2env.sh` so it can be sourced."
39
- parser.separator ""
40
-
41
- parser.separator "Available options:"
42
- parser.separator ""
43
-
44
- parser.on("-u", "--[no-]unset", "Generates commands needed to unset environment variables, default false") do |unset|
45
- @unset = unset
46
- end
47
-
48
- parser.on("-s", "--[no-]script", "Generates a bash script instead of simply showing command, default false") do |script|
49
- @script = script
50
- end
51
-
52
- parser.on("-f", "--format FORMAT", "Determines in what format variables are outputted, default to terminal") do |format|
53
- @format = format.to_sym()
54
- end
55
-
56
- parser.on("-h", "--help", "Print this help") do
57
- @show_help = true
58
- end
59
- end
60
- end
61
-
62
- def error(message, parser)
63
- @env.ui.info("ERROR: #{message}", :prefix => false)
64
- @env.ui.info("", :prefix => false)
65
-
66
- help(parser)
67
-
68
- return 1
69
- end
70
-
71
- def help(parser)
72
- @env.ui.info(parser.help, :prefix => false)
73
- end
74
-
75
- def execute
76
- parser = create_parser()
77
- arguments = parse_options(parser)
78
-
79
- return error("Invalid format value '#{@format}'", parser) if not Helper::Env::Exporter.valid?(@format)
80
- return help(parser) if @show_help
81
-
82
- @logger.debug("Nugrant 'Env'")
83
- with_target_vms(arguments) do |vm|
84
- config = vm.config.user
85
- parameters = config ? config.parameters : Nugrant::Parameters.new()
86
- bag = parameters.__all
87
-
88
- options = {:type => @unset ? :unset : :export}
89
-
90
- case
91
- when @script || @format == :script
92
- Helper::Env::Exporter.script_exporter(bag, options)
93
- when @format == :autoenv
94
- Helper::Env::Exporter.autoenv_exporter(bag, options)
95
- else
96
- Helper::Env::Exporter.terminal_exporter(bag, options)
97
- end
98
-
99
- # No need to execute for the other VMs
100
- return 0
101
- end
102
- end
103
- end
104
- end
105
- end
106
- end
107
- end
@@ -1,107 +0,0 @@
1
- require 'nugrant'
2
- require 'nugrant/helper/env/exporter'
3
- require 'nugrant/parameters'
4
-
5
- module Nugrant
6
- module Vagrant
7
- module V2
8
- module Command
9
- class Env < ::Vagrant.plugin("2", :command)
10
- def initialize(arguments, environment)
11
- super(arguments, environment)
12
-
13
- @unset = false
14
- @script = false
15
- @format = :terminal
16
- @show_help = false
17
- end
18
-
19
- def create_parser()
20
- return OptionParser.new do |parser|
21
- parser.banner = "Usage: vagrant user env [<options>]"
22
- parser.separator ""
23
-
24
- parser.separator "Outputs the commands that should be executed to export\n" +
25
- "the various parameter as environment variables. By default,\n" +
26
- "existing ones are overridden. The --format argument can be used\n" +
27
- "to choose in which format the variables should be displayed.\n" +
28
- "Changing the format will also change where they are displayed.\n"
29
- parser.separator ""
30
- parser.separator "The `-s, --script` option is deprecated and will be removed in\n" +
31
- "version 2.0. Use `--format script` instead."
32
- parser.separator ""
33
-
34
- parser.separator "Available formats:"
35
- parser.separator " autoenv => Write commands to a file named `.env` in the current directory.\n" +
36
- " See https://github.com/kennethreitz/autoenv for more info."
37
- parser.separator " terminal => Display commands to terminal so they can be sourced."
38
- parser.separator " script => Write commands to a bash script named `nugrant2env.sh` so it can be sourced."
39
- parser.separator ""
40
-
41
- parser.separator "Available options:"
42
- parser.separator ""
43
-
44
- parser.on("-u", "--[no-]unset", "Generates commands needed to unset environment variables, default false") do |unset|
45
- @unset = unset
46
- end
47
-
48
- parser.on("-s", "--[no-]script", "Generates a bash script instead of simply showing command, default false") do |script|
49
- @script = script
50
- end
51
-
52
- parser.on("-f", "--format FORMAT", "Determines in what format variables are outputted, default to terminal") do |format|
53
- @format = format.to_sym()
54
- end
55
-
56
- parser.on("-h", "--help", "Print this help") do
57
- @show_help = true
58
- end
59
- end
60
- end
61
-
62
- def error(message, parser)
63
- @env.ui.info("ERROR: #{message}", :prefix => false)
64
- @env.ui.info("", :prefix => false)
65
-
66
- help(parser)
67
-
68
- return 1
69
- end
70
-
71
- def help(parser)
72
- @env.ui.info(parser.help, :prefix => false)
73
- end
74
-
75
- def execute
76
- parser = create_parser()
77
- arguments = parse_options(parser)
78
-
79
- return error("Invalid format value '#{@format}'", parser) if not Helper::Env::Exporter.valid?(@format)
80
- return help(parser) if @show_help
81
-
82
- @logger.debug("Nugrant 'Env'")
83
- with_target_vms(arguments) do |vm|
84
- config = vm.config.user
85
- parameters = config ? config.parameters : Nugrant::Parameters.new()
86
- bag = parameters.__all
87
-
88
- options = {:type => @unset ? :unset : :export}
89
-
90
- case
91
- when @script || @format == :script
92
- Helper::Env::Exporter.script_exporter(bag, options)
93
- when @format == :autoenv
94
- Helper::Env::Exporter.autoenv_exporter(bag, options)
95
- else
96
- Helper::Env::Exporter.terminal_exporter(bag, options)
97
- end
98
-
99
- # No need to execute for the other VMs
100
- return 0
101
- end
102
- end
103
- end
104
- end
105
- end
106
- end
107
- end
@@ -1,13 +0,0 @@
1
- en:
2
- nugrant:
3
- vagrant:
4
- errors:
5
- parameter_not_found: |-
6
- Nugrant: Parameter '%{key}' was not found, is it defined in
7
- your .vagrantuser file? Here where we think the error
8
- could be in your Vagrantfile:
9
-
10
- %{context}
11
-
12
- If you think it should be found, don't hesitate to fill an
13
- issue @ https://github.com/maoueh/nugrant/issues.
@@ -1,238 +0,0 @@
1
- require 'minitest/autorun'
2
-
3
- require 'nugrant/bag'
4
- require 'nugrant/helper/env/exporter'
5
-
6
- module Nugrant
7
- module Helper
8
- module Env
9
- class TestExporter < ::Minitest::Test
10
- def create_bag(parameters)
11
- return Nugrant::Bag.new(parameters)
12
- end
13
-
14
- def assert_export(expected, key, value, options = {})
15
- actual = Env::Exporter.command(:export, key, value, options)
16
-
17
- assert_equal(expected, actual)
18
- end
19
-
20
- def assert_unset(expected, key, options = {})
21
- actual = Env::Exporter.command(:unset, key, options)
22
-
23
- assert_equal(expected, actual)
24
- end
25
-
26
- def assert_autoenv_exporter(expected, bag, options = {})
27
- io = StringIO.new()
28
- Env::Exporter.autoenv_exporter(bag, options.merge({:io => io}))
29
-
30
- actual = io.string().split(/\r?\n/)
31
-
32
- assert_equal(expected, actual)
33
- end
34
-
35
- def assert_script_exporter(expected, bag, options = {})
36
- io = StringIO.new()
37
- Env::Exporter.script_exporter(bag, options.merge({:io => io}))
38
-
39
- actual = io.string().split(/\r?\n/)
40
-
41
- assert_equal(expected, actual)
42
- end
43
-
44
- def assert_terminal_exporter(expected, bag, options = {})
45
- io = StringIO.new()
46
- Env::Exporter.terminal_exporter(bag, options.merge({:io => io}))
47
-
48
- actual = io.string().split(/\r?\n/)
49
-
50
- assert_equal(expected, actual)
51
- end
52
-
53
- def assert_unset_commands(expected, bag, options = {})
54
- actual = Env::Exporter.unset_commands(bag, options)
55
-
56
- assert_equal(expected, actual)
57
- end
58
-
59
- def test_valid_exporter
60
- assert_equal(true, Env::Exporter.valid?(:autoenv))
61
- assert_equal(true, Env::Exporter.valid?(:script))
62
- assert_equal(true, Env::Exporter.valid?(:terminal))
63
- assert_equal(false, Env::Exporter.valid?(:foreman))
64
- end
65
-
66
- def test_export_command
67
- assert_export("export TEST=\\\"running\\ with\\ space\\\"", "TEST", "\"running with space\"")
68
- assert_export("export TEST=running with space", "TEST", "running with space", :escape_value => false)
69
- end
70
-
71
- def test_unset_command
72
- assert_unset("unset TEST", "TEST")
73
- end
74
-
75
- def test_terminal_exporter_export
76
- bag = create_bag({
77
- :level1 => {
78
- :level2 => {
79
- :first => "first with space",
80
- :second => "\"second\\"
81
- },
82
- :third => "third"
83
- },
84
- :existing => "downcase",
85
- })
86
-
87
- stub_env(:existing => "exist", :EXISTING => "exist") do
88
- assert_terminal_exporter([
89
- "export EXISTING=downcase",
90
- "export LEVEL1_LEVEL2_FIRST=first\\ with\\ space",
91
- "export LEVEL1_LEVEL2_SECOND=\\\"second\\\\",
92
- "export LEVEL1_THIRD=third",
93
- ], bag)
94
-
95
- assert_terminal_exporter([
96
- "export LEVEL1_LEVEL2_FIRST=first\\ with\\ space",
97
- "export LEVEL1_LEVEL2_SECOND=\\\"second\\\\",
98
- "export LEVEL1_THIRD=third",
99
- ], bag, :override => false)
100
-
101
- assert_terminal_exporter([
102
- "export EXISTING=downcase",
103
- "export LEVEL1_LEVEL2_FIRST=first with space",
104
- "export LEVEL1_LEVEL2_SECOND=\"second\\",
105
- "export LEVEL1_THIRD=third",
106
- ], bag, :type => :export, :override => true, :escape_value => false)
107
-
108
- default_namer = Env::Namer.default(".")
109
- prefix_namer = Env::Namer.prefix("CONFIG", default_namer)
110
-
111
- assert_terminal_exporter([
112
- "export CONFIG.EXISTING=downcase",
113
- "export CONFIG.LEVEL1.LEVEL2.FIRST=first with space",
114
- "export CONFIG.LEVEL1.LEVEL2.SECOND=\"second\\",
115
- "export CONFIG.LEVEL1.THIRD=third",
116
- ], bag, :override => true, :escape_value => false, :namer => prefix_namer)
117
- end
118
- end
119
-
120
- def test_terminal_exporter_unset
121
- bag = create_bag({
122
- :level1 => {
123
- :level2 => {
124
- :first => "first",
125
- :second => "second"
126
- },
127
- :third => "third"
128
- },
129
- :existing => "downcase",
130
- })
131
-
132
- stub_env(:existing => "exist", :EXISTING => "exist") do
133
- assert_terminal_exporter([
134
- "unset EXISTING",
135
- "unset LEVEL1_LEVEL2_FIRST",
136
- "unset LEVEL1_LEVEL2_SECOND",
137
- "unset LEVEL1_THIRD",
138
- ], bag, :type => :unset)
139
-
140
- assert_terminal_exporter([
141
- "unset LEVEL1_LEVEL2_FIRST",
142
- "unset LEVEL1_LEVEL2_SECOND",
143
- "unset LEVEL1_THIRD",
144
- ], bag, :override => false, :type => :unset)
145
-
146
- default_namer = Env::Namer.default(".")
147
- prefix_namer = Env::Namer.prefix("CONFIG", default_namer)
148
-
149
- assert_terminal_exporter([
150
- "unset CONFIG.EXISTING",
151
- "unset CONFIG.LEVEL1.LEVEL2.FIRST",
152
- "unset CONFIG.LEVEL1.LEVEL2.SECOND",
153
- "unset CONFIG.LEVEL1.THIRD",
154
- ], bag, :override => true, :namer => prefix_namer, :type => :unset)
155
- end
156
- end
157
-
158
- def test_autoenv_exporter
159
- bag = create_bag({
160
- :level1 => {
161
- :level2 => {
162
- :first => "first",
163
- :second => "second"
164
- },
165
- :third => "third"
166
- },
167
- :existing => "downcase",
168
- })
169
-
170
- assert_autoenv_exporter([
171
- "export EXISTING=downcase",
172
- "export LEVEL1_LEVEL2_FIRST=first",
173
- "export LEVEL1_LEVEL2_SECOND=second",
174
- "export LEVEL1_THIRD=third",
175
- ], bag, :type => :export)
176
-
177
- assert_autoenv_exporter([
178
- "unset EXISTING",
179
- "unset LEVEL1_LEVEL2_FIRST",
180
- "unset LEVEL1_LEVEL2_SECOND",
181
- "unset LEVEL1_THIRD",
182
- ], bag, :type => :unset)
183
- end
184
-
185
- def test_script_exporter
186
- bag = create_bag({
187
- :level1 => {
188
- :level2 => {
189
- :first => "first",
190
- :second => "second"
191
- },
192
- :third => "third"
193
- },
194
- :existing => "downcase",
195
- })
196
-
197
- assert_script_exporter([
198
- "#!/bin/env sh",
199
- "",
200
- "export EXISTING=downcase",
201
- "export LEVEL1_LEVEL2_FIRST=first",
202
- "export LEVEL1_LEVEL2_SECOND=second",
203
- "export LEVEL1_THIRD=third",
204
- ], bag, :type => :export)
205
-
206
- assert_script_exporter([
207
- "#!/bin/env sh",
208
- "",
209
- "unset EXISTING",
210
- "unset LEVEL1_LEVEL2_FIRST",
211
- "unset LEVEL1_LEVEL2_SECOND",
212
- "unset LEVEL1_THIRD",
213
- ], bag, :type => :unset)
214
- end
215
-
216
- def replace_env(variables)
217
- ENV.clear()
218
-
219
- variables = Hash[variables.map do |name, value|
220
- [name.to_s, value]
221
- end]
222
-
223
- ENV.update(variables)
224
- end
225
-
226
- def stub_env(new = {})
227
- old = ENV.to_hash()
228
-
229
- replace_env(new)
230
- yield
231
-
232
- ensure
233
- replace_env(old)
234
- end
235
- end
236
- end
237
- end
238
- end