nugrant 1.2.0 → 1.3.0

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.
@@ -0,0 +1,73 @@
1
+ require 'nugrant'
2
+ require 'nugrant/helper/env'
3
+
4
+ module Nugrant
5
+ module Vagrant
6
+ module V1
7
+ module Command
8
+ class Env < ::Vagrant::Command::Base
9
+ def initialize(arguments, environment)
10
+ super(arguments, environment)
11
+
12
+ @unset = false
13
+ @script = false
14
+ @show_help = false
15
+ end
16
+
17
+ def create_parser()
18
+ return OptionParser.new do |parser|
19
+ parser.banner = "Usage: vagrant user env [<options>]"
20
+ parser.separator ""
21
+
22
+ parser.separator "Outputs the commands that should be executed to export\n" +
23
+ "the various parameter as environment variables. By default,\n" +
24
+ "existing ones are overridden."
25
+ parser.separator ""
26
+
27
+ parser.separator "Available options:"
28
+ parser.separator ""
29
+
30
+ parser.on("-u", "--[no-]unset", "Generates commands needed to unset environment variables, default false") do |unset|
31
+ @unset = unset
32
+ end
33
+
34
+ parser.on("-s", "--[no-]script", "Generates a bash script instead of simply showing command, default false") do |script|
35
+ @script = script
36
+ end
37
+
38
+ parser.on("-h", "--help", "Print this help") do
39
+ @show_help = true
40
+ end
41
+ end
42
+ end
43
+
44
+ def help(parser)
45
+ @env.ui.info(parser.help, :prefix => false)
46
+ end
47
+
48
+ def execute
49
+ parser = create_parser()
50
+ arguments = parse_options(parser)
51
+
52
+ return help(parser) if @show_help
53
+
54
+ @logger.debug("Nugrant 'Env'")
55
+ with_target_vms(arguments) do |vm|
56
+ config = vm.config.user
57
+ parameters = config ? config.parameters : Nugrant::Parameters.new()
58
+ bag = parameters.__all
59
+
60
+ options = {:type => @unset ? :unset : :export}
61
+
62
+ Helper::Env.write_commands(bag, options) if not @script
63
+ Helper::Env.write_script(bag, options) if @script
64
+
65
+ # No need to execute for the other VMs
66
+ return 0
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -37,18 +37,18 @@ module Nugrant
37
37
  end
38
38
 
39
39
  parser.on("-u", "--user", "Show only user parameters") do
40
- @show_user = true
41
- end
42
-
43
- parser.on("-p", "--project", "Show only project parameters") do
44
- @show_project = true
45
- end
46
-
47
- parser.separator ""
48
- parser.separator "When no options is provided, the command prints the names and values \n" +
49
- "of all parameters that would be available for usage in the Vagrantfile.\n" +
50
- "The hierarchy of the parameters is respected, so the final values are\n" +
51
- "displayed."
40
+ @show_user = true
41
+ end
42
+
43
+ parser.on("-p", "--project", "Show only project parameters") do
44
+ @show_project = true
45
+ end
46
+
47
+ parser.separator ""
48
+ parser.separator "When no options is provided, the command prints the names and values \n" +
49
+ "of all parameters that would be available for usage in the Vagrantfile.\n" +
50
+ "The hierarchy of the parameters is respected, so the final values are\n" +
51
+ "displayed."
52
52
  end
53
53
  end
54
54
 
@@ -110,7 +110,7 @@ module Nugrant
110
110
 
111
111
  print_parameters(kind, {
112
112
  'config' => {
113
- 'user' => bag.__to_hash()
113
+ 'user' => bag.__to_hash(:string_key => true)
114
114
  }
115
115
  })
116
116
  end
@@ -1,4 +1,6 @@
1
1
  require 'nugrant'
2
+ require 'nugrant/vagrant/v1/command/env'
3
+ require 'nugrant/vagrant/v1/command/parameters'
2
4
  require 'nugrant/version'
3
5
 
4
6
  module Nugrant
@@ -15,9 +17,12 @@ module Nugrant
15
17
  @argv = @arguments
16
18
 
17
19
  @subcommands = ::Vagrant::Registry.new()
20
+ @subcommands.register(:env) do
21
+ Command::Env
22
+ end
23
+
18
24
  @subcommands.register(:parameters) do
19
- require File.expand_path("../parameters", __FILE__)
20
- Parameters
25
+ Command::Parameters
21
26
  end
22
27
 
23
28
  @show_help = false
@@ -0,0 +1,73 @@
1
+ require 'nugrant'
2
+ require 'nugrant/helper/env'
3
+
4
+ module Nugrant
5
+ module Vagrant
6
+ module V2
7
+ module Command
8
+ class Env < ::Vagrant.plugin("2", :command)
9
+ def initialize(arguments, environment)
10
+ super(arguments, environment)
11
+
12
+ @unset = false
13
+ @script = false
14
+ @show_help = false
15
+ end
16
+
17
+ def create_parser()
18
+ return OptionParser.new do |parser|
19
+ parser.banner = "Usage: vagrant user env [<options>]"
20
+ parser.separator ""
21
+
22
+ parser.separator "Outputs the commands that should be executed to export\n" +
23
+ "the various parameter as environment variables. By default,\n" +
24
+ "existing ones are overridden."
25
+ parser.separator ""
26
+
27
+ parser.separator "Available options:"
28
+ parser.separator ""
29
+
30
+ parser.on("-u", "--[no-]unset", "Generates commands needed to unset environment variables, default false") do |unset|
31
+ @unset = unset
32
+ end
33
+
34
+ parser.on("-s", "--[no-]script", "Generates a bash script instead of simply showing command, default false") do |script|
35
+ @script = script
36
+ end
37
+
38
+ parser.on("-h", "--help", "Print this help") do
39
+ @show_help = true
40
+ end
41
+ end
42
+ end
43
+
44
+ def help(parser)
45
+ @env.ui.info(parser.help, :prefix => false)
46
+ end
47
+
48
+ def execute
49
+ parser = create_parser()
50
+ arguments = parse_options(parser)
51
+
52
+ return help(parser) if @show_help
53
+
54
+ @logger.debug("Nugrant 'Env'")
55
+ with_target_vms(arguments) do |vm|
56
+ config = vm.config.user
57
+ parameters = config ? config.parameters : Nugrant::Parameters.new()
58
+ bag = parameters.__all
59
+
60
+ options = {:type => @unset ? :unset : :export}
61
+
62
+ Helper::Env.write_commands(bag, options) if not @script
63
+ Helper::Env.write_script(bag, options) if @script
64
+
65
+ # No need to execute for the other VMs
66
+ return 0
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -110,7 +110,7 @@ module Nugrant
110
110
 
111
111
  print_parameters(kind, {
112
112
  'config' => {
113
- 'user' => bag.__to_hash()
113
+ 'user' => bag.__to_hash(:string_key => true)
114
114
  }
115
115
  })
116
116
  end
@@ -1,4 +1,6 @@
1
1
  require 'nugrant'
2
+ require 'nugrant/vagrant/v2/command/env'
3
+ require 'nugrant/vagrant/v2/command/parameters'
2
4
  require 'nugrant/version'
3
5
 
4
6
  module Nugrant
@@ -15,9 +17,12 @@ module Nugrant
15
17
  @argv = @arguments
16
18
 
17
19
  @subcommands = ::Vagrant::Registry.new()
20
+ @subcommands.register(:env) do
21
+ Command::Env
22
+ end
23
+
18
24
  @subcommands.register(:parameters) do
19
- require File.expand_path("../parameters", __FILE__)
20
- Parameters
25
+ Command::Parameters
21
26
  end
22
27
 
23
28
  @show_help = false
@@ -1,3 +1,3 @@
1
1
  module Nugrant
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.0"
3
3
  end
@@ -25,10 +25,11 @@ Gem::Specification.new do |gem|
25
25
 
26
26
  gem.files = `git ls-files`.split($/)
27
27
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
28
- gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
28
+ gem.test_files = gem.files.grep(%r{^(test/lib)/})
29
29
  gem.require_paths = ["lib"]
30
30
 
31
- gem.add_dependency 'multi_json', '~> 1.8'
31
+ gem.add_dependency "multi_json", "~> 1.8"
32
32
 
33
33
  gem.add_development_dependency "rake"
34
+ gem.add_development_dependency "minitest", "~> 5.0.8"
34
35
  end
@@ -0,0 +1,150 @@
1
+ require 'minitest/autorun'
2
+
3
+ require 'nugrant/bag'
4
+ require 'nugrant/helper/env'
5
+
6
+ module Nugrant
7
+ module Helper
8
+ class TestEnv < ::Minitest::Test
9
+ def create_bag(parameters)
10
+ return Nugrant::Bag.new(parameters)
11
+ end
12
+
13
+ def assert_export(expected, key, value, options = {})
14
+ actual = Helper::Env.export_command(key, value, options)
15
+
16
+ assert_equal(expected, actual)
17
+ end
18
+
19
+ def assert_unset(expected, key, options = {})
20
+ actual = Helper::Env.unset_command(key, options)
21
+
22
+ assert_equal(expected, actual)
23
+ end
24
+
25
+ def assert_export_commands(expected, bag, options = {})
26
+ actual = Helper::Env.export_commands(bag, options)
27
+
28
+ assert_equal(expected.sort!(), actual.sort!())
29
+ end
30
+
31
+ def assert_unset_commands(expected, bag, options = {})
32
+ actual = Helper::Env.unset_commands(bag, options)
33
+
34
+ assert_equal(expected, actual)
35
+ end
36
+
37
+ def test_export_command
38
+ assert_export("export TEST=\\\"running\\ with\\ space\\\"", "TEST", "\"running with space\"")
39
+ assert_export("export TEST=running with space", "TEST", "running with space", :escape_value => false)
40
+ end
41
+
42
+ def test_export_commands
43
+ bag = create_bag({
44
+ :existing => "downcase",
45
+ :level1 => {
46
+ :level2 => {
47
+ :first => "first with space",
48
+ :second => "\"second\\"
49
+ },
50
+ :third => "third"
51
+ }
52
+ })
53
+
54
+ stub_env(:existing => "exist", :EXISTING => "exist") do
55
+ assert_export_commands([
56
+ "export EXISTING=downcase",
57
+ "export LEVEL1_LEVEL2_FIRST=first\\ with\\ space",
58
+ "export LEVEL1_LEVEL2_SECOND=\\\"second\\\\",
59
+ "export LEVEL1_THIRD=third",
60
+ ], bag)
61
+
62
+ assert_export_commands([
63
+ "export LEVEL1_LEVEL2_FIRST=first\\ with\\ space",
64
+ "export LEVEL1_LEVEL2_SECOND=\\\"second\\\\",
65
+ "export LEVEL1_THIRD=third",
66
+ ], bag, :override => false)
67
+
68
+ assert_export_commands([
69
+ "export EXISTING=downcase",
70
+ "export LEVEL1_LEVEL2_FIRST=first with space",
71
+ "export LEVEL1_LEVEL2_SECOND=\"second\\",
72
+ "export LEVEL1_THIRD=third",
73
+ ], bag, :override => true, :escape_value => false)
74
+
75
+ default_namer = Helper::Env.default_namer(".")
76
+ prefix_namer = Helper::Env.prefix_namer("CONFIG", default_namer)
77
+
78
+ assert_export_commands([
79
+ "export CONFIG.EXISTING=downcase",
80
+ "export CONFIG.LEVEL1.LEVEL2.FIRST=first with space",
81
+ "export CONFIG.LEVEL1.LEVEL2.SECOND=\"second\\",
82
+ "export CONFIG.LEVEL1.THIRD=third",
83
+ ], bag, :override => true, :escape_value => false, :namer => prefix_namer)
84
+ end
85
+ end
86
+
87
+ def test_unset_command
88
+ assert_unset("unset TEST", "TEST")
89
+ end
90
+
91
+ def test_unset_commands
92
+ bag = create_bag({
93
+ :existing => "downcase",
94
+ :level1 => {
95
+ :level2 => {
96
+ :first => "first",
97
+ :second => "second"
98
+ },
99
+ :third => "third"
100
+ }
101
+ })
102
+
103
+ stub_env(:existing => "exist", :EXISTING => "exist") do
104
+ assert_unset_commands([
105
+ "unset EXISTING",
106
+ "unset LEVEL1_LEVEL2_FIRST",
107
+ "unset LEVEL1_LEVEL2_SECOND",
108
+ "unset LEVEL1_THIRD",
109
+ ], bag)
110
+
111
+ assert_unset_commands([
112
+ "unset LEVEL1_LEVEL2_FIRST",
113
+ "unset LEVEL1_LEVEL2_SECOND",
114
+ "unset LEVEL1_THIRD",
115
+ ], bag, :override => false)
116
+
117
+ default_namer = Helper::Env.default_namer(".")
118
+ prefix_namer = Helper::Env.prefix_namer("CONFIG", default_namer)
119
+
120
+ assert_unset_commands([
121
+ "unset CONFIG.EXISTING",
122
+ "unset CONFIG.LEVEL1.LEVEL2.FIRST",
123
+ "unset CONFIG.LEVEL1.LEVEL2.SECOND",
124
+ "unset CONFIG.LEVEL1.THIRD",
125
+ ], bag, :override => true, :namer => prefix_namer)
126
+ end
127
+ end
128
+
129
+ def replace_env(variables)
130
+ ENV.clear()
131
+
132
+ variables = Hash[variables.map do |name, value|
133
+ [name.to_s, value]
134
+ end]
135
+
136
+ ENV.update(variables)
137
+ end
138
+
139
+ def stub_env(new = {})
140
+ old = ENV.to_hash()
141
+
142
+ replace_env(new)
143
+ yield
144
+
145
+ ensure
146
+ replace_env(old)
147
+ end
148
+ end
149
+ end
150
+ end
@@ -1,10 +1,10 @@
1
- require 'test/unit'
1
+ require 'minitest/autorun'
2
2
 
3
3
  require 'nugrant/helper/stack'
4
4
 
5
5
  module Nugrant
6
6
  module Helper
7
- class TestStack < Test::Unit::TestCase
7
+ class TestStack < ::Minitest::Test
8
8
  def assert_error_location(expected, entry, matcher = nil)
9
9
  assert_equal(expected, Stack::extract_error_location(entry, :matcher => matcher))
10
10
  end
@@ -1,11 +1,11 @@
1
- require 'test/unit'
1
+ require 'minitest/autorun'
2
2
 
3
3
  require 'nugrant/bag'
4
4
 
5
5
  module Nugrant
6
- class TestBag < Test::Unit::TestCase
7
- def create_bag(parameters)
8
- return Bag.new(parameters)
6
+ class TestBag < ::Minitest::Test
7
+ def create_bag(elements)
8
+ return Bag.new(elements)
9
9
  end
10
10
 
11
11
  def assert_all_access_equal(value, bag, key)
@@ -84,15 +84,15 @@ module Nugrant
84
84
  def test_undefined_value()
85
85
  bag = create_bag({"value" => "one"})
86
86
 
87
- assert_raise(KeyError) do
87
+ assert_raises(KeyError) do
88
88
  bag.invalid_value
89
89
  end
90
90
 
91
- assert_raise(KeyError) do
91
+ assert_raises(KeyError) do
92
92
  bag["invalid_value"]
93
93
  end
94
94
 
95
- assert_raise(KeyError) do
95
+ assert_raises(KeyError) do
96
96
  bag[:invalid_value]
97
97
  end
98
98
  end