nugrant 1.1.0 → 1.2.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.
Files changed (34) hide show
  1. checksums.yaml +8 -8
  2. data/.gitignore +1 -1
  3. data/CHANGELOG.md +24 -0
  4. data/Gemfile +8 -8
  5. data/README.md +11 -1
  6. data/lib/nugrant.rb +19 -8
  7. data/lib/nugrant/bag.rb +5 -5
  8. data/lib/nugrant/helper/bag.rb +2 -2
  9. data/lib/nugrant/helper/stack.rb +86 -0
  10. data/lib/nugrant/parameters.rb +1 -1
  11. data/lib/nugrant/vagrant/errors.rb +27 -0
  12. data/lib/nugrant/vagrant/v1/config/user.rb +5 -0
  13. data/lib/nugrant/vagrant/v2/config/user.rb +5 -0
  14. data/lib/nugrant/version.rb +1 -1
  15. data/locales/en.yml +13 -0
  16. data/nugrant.gemspec +2 -0
  17. data/test/lib/nugrant/helper/test_stack.rb +149 -0
  18. data/test/lib/nugrant/test_bag.rb +27 -13
  19. data/test/lib/nugrant/test_parameters.rb +18 -13
  20. data/test/resources/vagrantfiles/v1.defaults_mixed_string_symbols +18 -0
  21. data/test/resources/vagrantfiles/v1.defaults_using_string +18 -0
  22. data/test/resources/vagrantfiles/v1.defaults_using_symbol +18 -0
  23. data/test/resources/{Vagrantfile.v1.empty → vagrantfiles/v1.empty} +0 -0
  24. data/test/resources/{Vagrantfile.v1.fake → vagrantfiles/v1.fake} +0 -0
  25. data/test/resources/vagrantfiles/v1.missing_parameter +3 -0
  26. data/test/resources/{Vagrantfile.v1.real → vagrantfiles/v1.real} +0 -0
  27. data/test/resources/vagrantfiles/v2.defaults_mixed_string_symbols +20 -0
  28. data/test/resources/vagrantfiles/v2.defaults_using_string +20 -0
  29. data/test/resources/vagrantfiles/v2.defaults_using_symbol +20 -0
  30. data/test/resources/{Vagrantfile.v2.empty → vagrantfiles/v2.empty} +0 -0
  31. data/test/resources/{Vagrantfile.v2.fake → vagrantfiles/v2.fake} +1 -1
  32. data/test/resources/vagrantfiles/v2.missing_parameter +5 -0
  33. data/test/resources/{Vagrantfile.v2.real → vagrantfiles/v2.real} +0 -0
  34. metadata +49 -14
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NTBjZWZmNDZmODZhZjM5YWQ2ZmVkNWI5YjRkZDUwZjc2YTdlNWY2YQ==
4
+ Y2U0MTBmOTMyYzhmYzk5NDBjOTRlZmNhMWYyMDkyYzM3ZTBkOGRmZA==
5
5
  data.tar.gz: !binary |-
6
- MTI2MWU0MWJmNTEwZjI5ZWQxZTRkYWZhMDAwODM2N2I3NGQ1NWFhMQ==
6
+ OGFkNTY2MjRjZTQ1ODhhM2FjNDBlNzVkZDE4NTA1NDdiODExNTExZA==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- N2VjMzZkZGI4MTE3MDdlYjM1YTY5MzY4ZjIwZjQ3ZmM4Y2VmMjMzMzJlODE2
10
- Mjk4ODFiNjNhNDdjM2U1ZWViMGYwMmE3OWU3NTI1ZmY2MTJkY2M5ZWI3MzNj
11
- ZjM5NmM4NjY1OGI2NWY4OTY3YWMxMWEwMjdjYzRiODI0OTYxNTE=
9
+ MjNmODVmZGQ1YjFlYjAxMjFjZWRmMWQyMDE0N2ZhNTlkM2RjNjM2NDJjYjVj
10
+ NGE1NGE0NzE0MzNmMDc5MTNmODZiMWI3MWZmMDA4ZWM3NzI4ZmYxOTMzMTI5
11
+ Njc0Zjg5ZTAwZTFmMzg5MTJhMjBiNzhlNzFmNzRhYTVlNmZlYzk=
12
12
  data.tar.gz: !binary |-
13
- Nzc0NzFiZTE1MDQ3MTE2ZDhlMWMzMTk3ZGYzMzQ3NDFlNWQ2ZDJiYWJjYzRi
14
- OGRhYmIzODg3N2U5OWY0NjgwYjQyY2I5NDM0ZjBjMTVjNjM3ZDU0MWExMTVj
15
- MDE4ODE4OTVkZDliNTg4YTAxMDRlYzBjODAwMTUwODFmMjU5YjE=
13
+ NTFjZTVhZjIyNDEwN2VlMGMxZDg1MTYxY2ExZDVmZGMzYzI5NjExODgxZjY0
14
+ NWQ5NTBhNzYxNWRlYTUzNTE1NzU5MWY5YTE0MTJkMGEyYzdkNjM2OWNlMmZh
15
+ MmVkOGRiNmQ2OGIyZWVmMTVkN2FkZGZlZDY5MzdhY2YxY2I2M2Q=
data/.gitignore CHANGED
@@ -18,4 +18,4 @@ test/tmp
18
18
  test/version_tmp
19
19
  tmp
20
20
 
21
- test/resources/Vagrantfile
21
+ test/resources/vagrantfiles/Vagrantfile
data/CHANGELOG.md CHANGED
@@ -1,3 +1,27 @@
1
+ # 1.2.0 (October 24th, 2013)
2
+
3
+ * Now showing better error message to the end-user when a parameter
4
+ cannot be found. The message displays which key could not be found.
5
+ Moreover, we show the context within the Vagrantfile where we think
6
+ the error occurred:
7
+
8
+ ```
9
+ Nugrant: Parameter 'param' was not found, is it defined in
10
+ your .vagrantuser file? Here where we think the error
11
+ could be in your Vagrantfile:
12
+
13
+ 1: Vagrant.configure("2") do |config|
14
+ 2:>> puts config.user.param
15
+ 3: end
16
+ ```
17
+
18
+ See [GH-8] (https://github.com/maoueh/nugrant/issues/8).
19
+ * Ensured that keys used within a `Bag` are always symbol. This make
20
+ sure that it is possible to retrieve a value with any access method.
21
+ See [GH-9](https://github.com/maoueh/nugrant/issues/9).
22
+ * Now using [multi_json](https://rubygems.org/gems/multi_json)
23
+ for JSON handling.
24
+
1
25
  # 1.1.0 (May 17th, 2013)
2
26
 
3
27
  * Rewrite completely classes `Parameters` and `Bag`.
data/Gemfile CHANGED
@@ -5,16 +5,16 @@ gemspec
5
5
  group :development do
6
6
  vagrant_dependencies = {
7
7
  'v1' => {
8
- 'home' => "~/.vagrant.d.v1",
9
- 'gem' => Proc.new do
10
- gem "vagrant", "~> 1.0.7"
11
- end,
8
+ 'home' => "~/.vagrant.d.v1",
9
+ 'gem' => Proc.new do
10
+ gem "vagrant", "~> 1.0.7"
11
+ end,
12
12
  },
13
13
  'v2' => {
14
- 'home' => "~/.vagrant.d",
15
- 'gem' => Proc.new do
16
- gem "vagrant", :git => "git://github.com/mitchellh/vagrant.git"
17
- end,
14
+ 'home' => "~/.vagrant.d",
15
+ 'gem' => Proc.new do
16
+ gem "vagrant", :git => "git://github.com/mitchellh/vagrant.git", :tag => "v1.3.5"
17
+ end,
18
18
  },
19
19
  }
20
20
 
data/README.md CHANGED
@@ -1,4 +1,14 @@
1
- # Nugrant [![Build Status](https://travis-ci.org/maoueh/nugrant.png)](https://travis-ci.org/maoueh/nugrant)
1
+ # Nugrant
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/nugrant.png)][gem]
4
+ [![Build Status](https://secure.travis-ci.org/maoueh/nugrant.png?branch=master)][travis]
5
+ [![Dependency Status](https://gemnasium.com/maoueh/nugrant.png?travis)][gemnasium]
6
+ [![Code Climate](https://codeclimate.com/github/maoueh/nugrant.png)][codeclimate]
7
+
8
+ [gem]: https://rubygems.org/gems/nugrant
9
+ [travis]: http://travis-ci.org/maoueh/nugrant
10
+ [gemnasium]: https://gemnasium.com/maoueh/nugrant
11
+ [codeclimate]: https://codeclimate.com/github/maoueh/nugrant
2
12
 
3
13
  Nugrant is a library to easily handle parameters that need to be
4
14
  injected into an application via different sources (system, user,
data/lib/nugrant.rb CHANGED
@@ -7,7 +7,26 @@ unless defined?(KeyError)
7
7
  end
8
8
  end
9
9
 
10
+ module Nugrant
11
+ def self.create_parameters(options)
12
+ config = Nugrant::Config.new(options)
13
+
14
+ return Nugrant::Parameters.new(config)
15
+ end
16
+
17
+ def self.setup_i18n()
18
+ I18n.load_path << File.expand_path("locales/en.yml", Nugrant.source_root)
19
+ I18n.reload!
20
+ end
21
+
22
+ def self.source_root
23
+ @source_root ||= Pathname.new(File.expand_path("../../", __FILE__))
24
+ end
25
+ end
26
+
10
27
  if defined?(Vagrant)
28
+ Nugrant.setup_i18n()
29
+
11
30
  case
12
31
  when defined?(Vagrant::Plugin::V2)
13
32
  require 'nugrant/vagrant/v2/plugin'
@@ -17,11 +36,3 @@ if defined?(Vagrant)
17
36
  abort("You are trying to use Nugrant with an unsupported Vagrant version [#{Vagrant::VERSION}]")
18
37
  end
19
38
  end
20
-
21
- module Nugrant
22
- def self.create_parameters(options)
23
- config = Nugrant::Config.new(options)
24
-
25
- return Nugrant::Parameters.new(config)
26
- end
27
- end
data/lib/nugrant/bag.rb CHANGED
@@ -15,11 +15,11 @@ module Nugrant
15
15
  end
16
16
 
17
17
  def method_missing(method, *args, &block)
18
- return __fetch(method.to_s)
18
+ return __fetch(method)
19
19
  end
20
20
 
21
21
  def has?(key)
22
- return @__elements.has_key?(key)
22
+ return @__elements.has_key?(key.to_sym)
23
23
  end
24
24
 
25
25
  def empty?()
@@ -76,12 +76,12 @@ module Nugrant
76
76
 
77
77
  hash.each do |key, value|
78
78
  if not value.kind_of?(Hash)
79
- @__elements[key] = value
79
+ @__elements[key.to_sym] = value
80
80
  next
81
81
  end
82
82
 
83
83
  # It is a hash, transform it into a bag
84
- @__elements[key] = Bag.new(value)
84
+ @__elements[key.to_sym] = Bag.new(value)
85
85
  end
86
86
  end
87
87
 
@@ -90,7 +90,7 @@ module Nugrant
90
90
  raise KeyError, "Undefined parameter '#{key}'"
91
91
  end
92
92
 
93
- return @__elements[key]
93
+ return @__elements[key.to_sym]
94
94
  end
95
95
  end
96
96
  end
@@ -1,4 +1,4 @@
1
- require 'json'
1
+ require 'multi_json'
2
2
  require 'yaml'
3
3
 
4
4
  require 'nugrant/bag'
@@ -29,7 +29,7 @@ module Nugrant
29
29
  end
30
30
 
31
31
  def self.parse_json(data_string)
32
- JSON.parse(data_string)
32
+ MultiJson.load(data_string)
33
33
  end
34
34
 
35
35
  def self.parse_yml(data_string)
@@ -0,0 +1,86 @@
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
@@ -21,7 +21,7 @@ module Nugrant
21
21
  end
22
22
 
23
23
  def method_missing(method, *args, &block)
24
- return @__all[method.to_s]
24
+ return @__all[method]
25
25
  end
26
26
 
27
27
  def empty?()
@@ -0,0 +1,27 @@
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,4 +1,5 @@
1
1
  require 'nugrant'
2
+ require 'nugrant/vagrant/errors'
2
3
 
3
4
  module Nugrant
4
5
  module Vagrant
@@ -13,10 +14,14 @@ module Nugrant
13
14
 
14
15
  def [](param_name)
15
16
  return @parameters[param_name]
17
+ rescue KeyError
18
+ raise Errors::ParameterNotFoundError, :key => param_name
16
19
  end
17
20
 
18
21
  def method_missing(method, *args, &block)
19
22
  return @parameters.method_missing(method, *args, &block)
23
+ rescue KeyError
24
+ raise Errors::ParameterNotFoundError, :key => method
20
25
  end
21
26
 
22
27
  def each(&block)
@@ -1,4 +1,5 @@
1
1
  require 'nugrant'
2
+ require 'nugrant/vagrant/errors'
2
3
 
3
4
  module Nugrant
4
5
  module Vagrant
@@ -13,10 +14,14 @@ module Nugrant
13
14
 
14
15
  def [](param_name)
15
16
  return @parameters[param_name]
17
+ rescue KeyError
18
+ raise Errors::ParameterNotFoundError, :key => param_name
16
19
  end
17
20
 
18
21
  def method_missing(method, *args, &block)
19
22
  return @parameters.method_missing(method, *args, &block)
23
+ rescue KeyError
24
+ raise Errors::ParameterNotFoundError, :key => method
20
25
  end
21
26
 
22
27
  def each(&block)
@@ -1,3 +1,3 @@
1
1
  module Nugrant
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
data/locales/en.yml ADDED
@@ -0,0 +1,13 @@
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.
data/nugrant.gemspec CHANGED
@@ -28,5 +28,7 @@ Gem::Specification.new do |gem|
28
28
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
29
29
  gem.require_paths = ["lib"]
30
30
 
31
+ gem.add_dependency 'multi_json', '~> 1.8'
32
+
31
33
  gem.add_development_dependency "rake"
32
34
  end
@@ -0,0 +1,149 @@
1
+ require 'test/unit'
2
+
3
+ require 'nugrant/helper/stack'
4
+
5
+ module Nugrant
6
+ module Helper
7
+ class TestStack < Test::Unit::TestCase
8
+ def assert_error_location(expected, entry, matcher = nil)
9
+ assert_equal(expected, Stack::extract_error_location(entry, :matcher => matcher))
10
+ end
11
+
12
+ def assert_error_region(expected, region)
13
+ expected_lines = expected.split()
14
+ region_lines = region.split()
15
+
16
+ assert_equal(expected_lines.length(), region_lines.length())
17
+
18
+ expected_lines.each_with_index do |expected_line, index|
19
+ assert_equal(expected_line.strip(), region_lines[index].strip())
20
+ end
21
+ end
22
+
23
+ def create_stack(options = {})
24
+ pattern = options[:pattern] || "Vagrantfile:%s"
25
+ count = options[:count] || 4
26
+
27
+ stack = []
28
+ (0..count).each do |index|
29
+ stack << pattern.gsub("%s", index.to_s())
30
+ end
31
+
32
+ stack
33
+ end
34
+
35
+ def create_location(name, line)
36
+ resource_path = File.expand_path("#{File.dirname(__FILE__)}/../../../resources/vagrantfiles")
37
+
38
+ {:file => "#{resource_path}/#{name}", :line => line}
39
+ end
40
+
41
+ def test_fetch_error_region_from_location()
42
+ location = create_location("v1.defaults_using_symbol", 4)
43
+ error_region = Stack::fetch_error_region_from_location(location)
44
+ expected_region = <<-EOT
45
+ 1: Vagrant::Config.run do |config|
46
+ 2: config.user.defaults = {
47
+ 3: :single => 1,
48
+ 4:>> :local => {
49
+ 5: :first => "value1",
50
+ 6: :second => "value2"
51
+ 7: }
52
+ 8: }
53
+ EOT
54
+
55
+ assert_error_region(expected_region, error_region)
56
+ end
57
+
58
+ def test_fetch_error_region_from_location_custom_prefix()
59
+ location = create_location("v1.defaults_using_symbol", 4)
60
+ error_region = Stack::fetch_error_region_from_location(location, :prefix => "**")
61
+ expected_region = <<-EOT
62
+ **1: Vagrant::Config.run do |config|
63
+ **2: config.user.defaults = {
64
+ **3: :single => 1,
65
+ **4:>> :local => {
66
+ **5: :first => "value1",
67
+ **6: :second => "value2"
68
+ **7: }
69
+ **8: }
70
+ EOT
71
+
72
+ assert_error_region(expected_region, error_region)
73
+ end
74
+
75
+ def test_fetch_error_region_from_location_custom_width()
76
+ location = create_location("v1.defaults_using_symbol", 4)
77
+ error_region = Stack::fetch_error_region_from_location(location, :width => 2)
78
+ expected_region = <<-EOT
79
+ 2: config.user.defaults = {
80
+ 3: :single => 1,
81
+ 4:>> :local => {
82
+ 5: :first => "value1",
83
+ 6: :second => "value2"
84
+ EOT
85
+
86
+ assert_error_region(expected_region, error_region)
87
+ end
88
+
89
+ def test_fetch_error_region_from_location_wrong_location()
90
+ location = {:file => nil, :line => nil}
91
+ assert_equal("Unknown", Stack::fetch_error_region_from_location(location))
92
+ assert_equal("Failed", Stack::fetch_error_region_from_location(location, :unknown => "Failed"))
93
+
94
+ location = {:file => "Vagrantfile", :line => nil}
95
+ assert_equal("Vagrantfile", Stack::fetch_error_region_from_location(location))
96
+
97
+ location = {:file => "NonExistingVagrantfile", :line => 4}
98
+ assert_equal("NonExistingVagrantfile:4", Stack::fetch_error_region_from_location(location))
99
+ end
100
+
101
+ def test_find_entry()
102
+ entries = ["First", "Second:", "Third:a", "Fourth:4"]
103
+
104
+ assert_equal("Fourth:4", Stack::find_entry(entries))
105
+ assert_equal("Third:a", Stack::find_entry(entries, :matcher => /^(.+):([a-z]+)/))
106
+ end
107
+
108
+ def test_extract_error_location_default_matcher()
109
+ # Matches
110
+ assert_error_location({:file => "/work/irb/workspace.rb", :line => 80}, "/work/irb/workspace.rb:80:in `eval'")
111
+ assert_error_location({:file => "workspace.rb", :line => 80}, "workspace.rb:80:in `eval'")
112
+ assert_error_location({:file => "/work/irb/workspace.rb", :line => 80}, "/work/irb/workspace.rb:80")
113
+
114
+ # No match
115
+ assert_error_location({:file => nil, :line => nil}, "/work/irb/workspace.rb?80")
116
+ assert_error_location({:file => nil, :line => nil}, "/work/irb/workspace.rb")
117
+ assert_error_location({:file =>nil, :line => nil}, "")
118
+ end
119
+
120
+ def test_extract_error_location_custom_matcher()
121
+ # Matches
122
+ assert_error_location(
123
+ {:file => "/work/Vagrantfile", :line => 80},
124
+ "/work/Vagrantfile:80:in `eval'",
125
+ /(.*Vagrantfile):([0-9]+)/
126
+ )
127
+
128
+ assert_error_location(
129
+ {:file => "Vagrantfile", :line => 80},
130
+ "Vagrantfile:80:in `eval'",
131
+ /(.*Vagrantfile):([0-9]+)/
132
+ )
133
+
134
+ assert_error_location(
135
+ {:file => "/work/irb/Vagrantfile", :line => 80},
136
+ "/work/irb/Vagrantfile:80",
137
+ /(.*Vagrantfile):([0-9]+)/
138
+ )
139
+
140
+ # Partial match
141
+ assert_error_location(
142
+ {:file => "/work/Vagrantfile", :line => nil},
143
+ "/work/Vagrantfile:80:in `eval'",
144
+ /(.*Vagrantfile)/
145
+ )
146
+ end
147
+ end
148
+ end
149
+ end
@@ -8,18 +8,28 @@ module Nugrant
8
8
  return Bag.new(parameters)
9
9
  end
10
10
 
11
+ def assert_all_access_equal(value, bag, key)
12
+ assert_equal(value, bag.method_missing(key.to_sym), "bag.#{key.to_sym.inspect}")
13
+ assert_equal(value, bag[key.to_s], "bag[#{key.to_s.inspect}]")
14
+ assert_equal(value, bag[key.to_sym], "bag[#{key.to_sym.inspect}]")
15
+ end
16
+
17
+ def assert_all_access_bag(value, bag, key)
18
+ assert_bag(value, bag.method_missing(key.to_sym))
19
+ assert_bag(value, bag[key.to_s])
20
+ assert_bag(value, bag[key.to_sym])
21
+ end
22
+
11
23
  def assert_bag(parameters, bag)
12
24
  assert_kind_of(Bag, bag)
13
25
 
14
26
  parameters.each do |key, value|
15
27
  if not value.kind_of?(Hash)
16
- assert_equal(value, bag.send(key))
17
- assert_equal(value, bag[key])
28
+ assert_all_access_equal(value, bag, key)
18
29
  next
19
30
  end
20
31
 
21
- assert_bag(value, bag.send(key))
22
- assert_bag(value, bag[key])
32
+ assert_all_access_bag(value, bag, key)
23
33
  end
24
34
  end
25
35
 
@@ -81,6 +91,10 @@ module Nugrant
81
91
  assert_raise(KeyError) do
82
92
  bag["invalid_value"]
83
93
  end
94
+
95
+ assert_raise(KeyError) do
96
+ bag[:invalid_value]
97
+ end
84
98
  end
85
99
 
86
100
  def test_to_hash()
@@ -89,29 +103,29 @@ module Nugrant
89
103
  assert_kind_of(Hash, hash)
90
104
  assert_equal({}, hash)
91
105
 
92
- hash = create_bag({"value" => {"one" => "value", "two" => "value"}}).__to_hash()
106
+ hash = create_bag({"value" => {:one => "value", "two" => "value"}}).__to_hash()
93
107
 
94
108
  assert_kind_of(Hash, hash)
95
- assert_kind_of(Hash, hash['value'])
96
- assert_kind_of(String, hash['value']['one'])
97
- assert_kind_of(String, hash['value']['two'])
98
- assert_equal({"value" => {"one" => "value", "two" => "value"}}, hash)
109
+ assert_kind_of(Hash, hash[:value])
110
+ assert_kind_of(String, hash[:value][:one])
111
+ assert_kind_of(String, hash[:value][:two])
112
+ assert_equal({:value => {:one => "value", :two => "value"}}, hash)
99
113
  end
100
114
 
101
115
  def test_merge_array()
102
116
  bag1 = create_bag({"first" => [1, 2]})
103
- bag2 = create_bag({"first" => [2, 3]})
117
+ bag2 = create_bag({:first => [2, 3]})
104
118
 
105
119
  bag1.__merge!(bag2);
106
120
 
107
- assert_equal({"first" => [1, 2, 3]}, bag1.__to_hash())
121
+ assert_equal({:first => [1, 2, 3]}, bag1.__to_hash())
108
122
 
109
123
  bag1 = create_bag({"first" => [1, 2]})
110
- bag2 = create_bag({"first" => "string"})
124
+ bag2 = create_bag({:first => "string"})
111
125
 
112
126
  bag1.__merge!(bag2);
113
127
 
114
- assert_equal({"first" => "string"}, bag1.__to_hash())
128
+ assert_equal({:first => "string"}, bag1.__to_hash())
115
129
  end
116
130
  end
117
131
  end
@@ -22,10 +22,15 @@ class Nugrant::TestParameters < Test::Unit::TestCase
22
22
  })
23
23
  end
24
24
 
25
+ def assert_all_access_equal(value, parameters, key)
26
+ assert_equal(value, parameters.method_missing(key.to_sym), "parameters.#{key.to_sym.inspect}")
27
+ assert_equal(value, parameters[key.to_s], "parameters[#{key.to_s.inspect}]")
28
+ assert_equal(value, parameters[key.to_sym], "parameters[#{key.to_sym.inspect}]")
29
+ end
30
+
25
31
  def assert_level(parameters, results)
26
32
  results.each do |key, value|
27
- assert_equal(value, parameters.send(key), "array[#{key}]")
28
- assert_equal(value, parameters[key], "array[#{key}]")
33
+ assert_all_access_equal(value, parameters, key)
29
34
  end
30
35
 
31
36
  assert_key_error(parameters, "0.0.0")
@@ -146,13 +151,13 @@ class Nugrant::TestParameters < Test::Unit::TestCase
146
151
  def run_test_file_invalid(invalid_value)
147
152
  filetypes.each do |params_filetype|
148
153
  parameters = create_parameters(params_filetype, "params_simple", invalid_path, invalid_path)
149
- assert_equal("value", parameters["test"])
154
+ assert_all_access_equal("value", parameters, "test")
150
155
 
151
156
  parameters = create_parameters(params_filetype, invalid_path, "params_simple", invalid_path)
152
- assert_equal("value", parameters["test"])
157
+ assert_all_access_equal("value", parameters, "test")
153
158
 
154
159
  parameters = create_parameters(params_filetype, invalid_path, invalid_path, "params_simple")
155
- assert_equal("value", parameters["test"])
160
+ assert_all_access_equal("value", parameters, "test")
156
161
 
157
162
  parameters = create_parameters(params_filetype, invalid_path, invalid_path, invalid_path)
158
163
  assert_not_nil(parameters)
@@ -171,8 +176,8 @@ class Nugrant::TestParameters < Test::Unit::TestCase
171
176
  filetypes.each do |params_filetype|
172
177
  parameters = create_parameters(params_filetype, file_path, invalid_path, invalid_path)
173
178
 
174
- assert_equal("value1", parameters['level1'])
175
- assert_equal("value2", parameters['level2']['first'])
179
+ assert_all_access_equal("value1", parameters, 'level1')
180
+ assert_all_access_equal("value2", parameters['level2'], 'first')
176
181
  end
177
182
  end
178
183
 
@@ -180,13 +185,13 @@ class Nugrant::TestParameters < Test::Unit::TestCase
180
185
  filetypes.each do |params_filetype|
181
186
  parameters = create_parameters(params_filetype, "params_defaults_at_root", invalid_path, invalid_path)
182
187
 
183
- assert_equal("value", parameters.defaults)
188
+ assert_all_access_equal("value", parameters, :defaults)
184
189
  end
185
190
 
186
191
  filetypes.each do |params_filetype|
187
192
  parameters = create_parameters(params_filetype, "params_defaults_not_at_root", invalid_path, invalid_path)
188
193
 
189
- assert_equal("value", parameters.level.defaults)
194
+ assert_all_access_equal("value", parameters.level, :defaults)
190
195
  end
191
196
  end
192
197
 
@@ -195,8 +200,8 @@ class Nugrant::TestParameters < Test::Unit::TestCase
195
200
  parameters = create_parameters(params_filetype, "params_simple", invalid_path, invalid_path)
196
201
  parameters.defaults = {"test" => "override1", "level" => "new1"}
197
202
 
198
- assert_equal("value", parameters['test'])
199
- assert_equal("new1", parameters['level'])
203
+ assert_all_access_equal("value", parameters, 'test')
204
+ assert_all_access_equal("new1", parameters, 'level')
200
205
  end
201
206
  end
202
207
 
@@ -205,7 +210,7 @@ class Nugrant::TestParameters < Test::Unit::TestCase
205
210
  parameters = create_parameters(params_filetype, "params_empty", invalid_path, invalid_path)
206
211
  parameters.defaults = {"test" => "value"}
207
212
 
208
- assert_equal("value", parameters['test'])
213
+ assert_all_access_equal("value", parameters, 'test')
209
214
  end
210
215
  end
211
216
 
@@ -215,7 +220,7 @@ class Nugrant::TestParameters < Test::Unit::TestCase
215
220
  parameters = create_parameters(params_filetype, "params_#{wrong_type}", invalid_path, invalid_path)
216
221
  parameters.defaults = {"test" => "value"}
217
222
 
218
- assert_equal("value", parameters['test'])
223
+ assert_all_access_equal("value", parameters, 'test')
219
224
  end
220
225
  end
221
226
  end
@@ -0,0 +1,18 @@
1
+ Vagrant::Config.run do |config|
2
+ config.user.defaults = {
3
+ 'single' => 1,
4
+ :local => {
5
+ 'first' => "value1",
6
+ :second => "value2"
7
+ }
8
+ }
9
+
10
+ puts config.user.single
11
+ puts config.user[:single]
12
+
13
+ puts config.user.local.first
14
+ puts config.user[:local]["first"]
15
+
16
+ puts config.user.local.second
17
+ puts config.user["local"][:second]
18
+ end
@@ -0,0 +1,18 @@
1
+ Vagrant::Config.run do |config|
2
+ config.user.defaults = {
3
+ 'single' => 1,
4
+ 'local' => {
5
+ 'first' => "value1",
6
+ 'second' => "value2"
7
+ }
8
+ }
9
+
10
+ puts config.user.single
11
+ puts config.user[:single]
12
+
13
+ puts config.user.local.first
14
+ puts config.user[:local]["first"]
15
+
16
+ puts config.user.local.second
17
+ puts config.user["local"][:second]
18
+ end
@@ -0,0 +1,18 @@
1
+ Vagrant::Config.run do |config|
2
+ config.user.defaults = {
3
+ :single => 1,
4
+ :local => {
5
+ :first => "value1",
6
+ :second => "value2"
7
+ }
8
+ }
9
+
10
+ puts config.user.single
11
+ puts config.user[:single]
12
+
13
+ puts config.user.local.first
14
+ puts config.user[:local]["first"]
15
+
16
+ puts config.user.local.second
17
+ puts config.user["local"][:second]
18
+ end
@@ -0,0 +1,3 @@
1
+ Vagrant::Config.run do |config|
2
+ puts config.user.unkown_for_sure
3
+ end
@@ -0,0 +1,20 @@
1
+ Vagrant.require_plugin('nugrant')
2
+
3
+ Vagrant.configure("2") do |config|
4
+ config.user.defaults = {
5
+ 'single' => 1,
6
+ :local => {
7
+ 'first' => "value1",
8
+ :second => "value2"
9
+ }
10
+ }
11
+
12
+ puts config.user.single
13
+ puts config.user[:single]
14
+
15
+ puts config.user.local.first
16
+ puts config.user[:local]["first"]
17
+
18
+ puts config.user.local.second
19
+ puts config.user["local"][:second]
20
+ end
@@ -0,0 +1,20 @@
1
+ Vagrant.require_plugin('nugrant')
2
+
3
+ Vagrant.configure("2") do |config|
4
+ config.user.defaults = {
5
+ 'single' => 1,
6
+ 'local' => {
7
+ 'first' => "value1",
8
+ 'second' => "value2"
9
+ }
10
+ }
11
+
12
+ puts config.user.single
13
+ puts config.user[:single]
14
+
15
+ puts config.user.local.first
16
+ puts config.user[:local]["first"]
17
+
18
+ puts config.user.local.second
19
+ puts config.user["local"][:second]
20
+ end
@@ -0,0 +1,20 @@
1
+ Vagrant.require_plugin('nugrant')
2
+
3
+ Vagrant.configure("2") do |config|
4
+ config.user.defaults = {
5
+ :single => 1,
6
+ :local => {
7
+ :first => "value1",
8
+ :second => "value2"
9
+ }
10
+ }
11
+
12
+ puts config.user.single
13
+ puts config.user[:single]
14
+
15
+ puts config.user.local.first
16
+ puts config.user[:local]["first"]
17
+
18
+ puts config.user.local.second
19
+ puts config.user["local"][:second]
20
+ end
@@ -7,7 +7,7 @@ Vagrant.configure("2") do |config|
7
7
  'second' => "value2"
8
8
  }
9
9
  }
10
-
10
+
11
11
  config.user.local.each do |name, value|
12
12
  puts "Parameters #{name}: #{value}"
13
13
  end
@@ -0,0 +1,5 @@
1
+ Vagrant.require_plugin('nugrant')
2
+
3
+ Vagrant.configure("2") do |config|
4
+ puts config.user.unkown_for_sure
5
+ end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nugrant
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthieu Vachon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-17 00:00:00.000000000 Z
11
+ date: 2013-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: multi_json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: rake
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -52,8 +66,10 @@ files:
52
66
  - lib/nugrant/bag.rb
53
67
  - lib/nugrant/config.rb
54
68
  - lib/nugrant/helper/bag.rb
69
+ - lib/nugrant/helper/stack.rb
55
70
  - lib/nugrant/helper/yaml.rb
56
71
  - lib/nugrant/parameters.rb
72
+ - lib/nugrant/vagrant/errors.rb
57
73
  - lib/nugrant/vagrant/v1/command/parameters.rb
58
74
  - lib/nugrant/vagrant/v1/command/root.rb
59
75
  - lib/nugrant/vagrant/v1/config/user.rb
@@ -64,18 +80,14 @@ files:
64
80
  - lib/nugrant/vagrant/v2/plugin.rb
65
81
  - lib/nugrant/version.rb
66
82
  - lib/vagrant_init.rb
83
+ - locales/en.yml
67
84
  - nugrant.gemspec
85
+ - test/lib/nugrant/helper/test_stack.rb
68
86
  - test/lib/nugrant/test_bag.rb
69
87
  - test/lib/nugrant/test_config.rb
70
88
  - test/lib/nugrant/test_parameters.rb
71
89
  - test/resources/.vagrantuser
72
90
  - test/resources/README.md
73
- - test/resources/Vagrantfile.v1.empty
74
- - test/resources/Vagrantfile.v1.fake
75
- - test/resources/Vagrantfile.v1.real
76
- - test/resources/Vagrantfile.v2.empty
77
- - test/resources/Vagrantfile.v2.fake
78
- - test/resources/Vagrantfile.v2.real
79
91
  - test/resources/json/params_array.json
80
92
  - test/resources/json/params_boolean.json
81
93
  - test/resources/json/params_combinations.json
@@ -92,6 +104,20 @@ files:
92
104
  - test/resources/json/params_user_1.json
93
105
  - test/resources/json/params_user_2.json
94
106
  - test/resources/json/params_windows_eol.json
107
+ - test/resources/vagrantfiles/v1.defaults_mixed_string_symbols
108
+ - test/resources/vagrantfiles/v1.defaults_using_string
109
+ - test/resources/vagrantfiles/v1.defaults_using_symbol
110
+ - test/resources/vagrantfiles/v1.empty
111
+ - test/resources/vagrantfiles/v1.fake
112
+ - test/resources/vagrantfiles/v1.missing_parameter
113
+ - test/resources/vagrantfiles/v1.real
114
+ - test/resources/vagrantfiles/v2.defaults_mixed_string_symbols
115
+ - test/resources/vagrantfiles/v2.defaults_using_string
116
+ - test/resources/vagrantfiles/v2.defaults_using_symbol
117
+ - test/resources/vagrantfiles/v2.empty
118
+ - test/resources/vagrantfiles/v2.fake
119
+ - test/resources/vagrantfiles/v2.missing_parameter
120
+ - test/resources/vagrantfiles/v2.real
95
121
  - test/resources/yml/params_array.yml
96
122
  - test/resources/yml/params_boolean.yml
97
123
  - test/resources/yml/params_combinations.yml
@@ -132,17 +158,12 @@ signing_key:
132
158
  specification_version: 4
133
159
  summary: Library to handle user specific parameters from various location.
134
160
  test_files:
161
+ - test/lib/nugrant/helper/test_stack.rb
135
162
  - test/lib/nugrant/test_bag.rb
136
163
  - test/lib/nugrant/test_config.rb
137
164
  - test/lib/nugrant/test_parameters.rb
138
165
  - test/resources/.vagrantuser
139
166
  - test/resources/README.md
140
- - test/resources/Vagrantfile.v1.empty
141
- - test/resources/Vagrantfile.v1.fake
142
- - test/resources/Vagrantfile.v1.real
143
- - test/resources/Vagrantfile.v2.empty
144
- - test/resources/Vagrantfile.v2.fake
145
- - test/resources/Vagrantfile.v2.real
146
167
  - test/resources/json/params_array.json
147
168
  - test/resources/json/params_boolean.json
148
169
  - test/resources/json/params_combinations.json
@@ -159,6 +180,20 @@ test_files:
159
180
  - test/resources/json/params_user_1.json
160
181
  - test/resources/json/params_user_2.json
161
182
  - test/resources/json/params_windows_eol.json
183
+ - test/resources/vagrantfiles/v1.defaults_mixed_string_symbols
184
+ - test/resources/vagrantfiles/v1.defaults_using_string
185
+ - test/resources/vagrantfiles/v1.defaults_using_symbol
186
+ - test/resources/vagrantfiles/v1.empty
187
+ - test/resources/vagrantfiles/v1.fake
188
+ - test/resources/vagrantfiles/v1.missing_parameter
189
+ - test/resources/vagrantfiles/v1.real
190
+ - test/resources/vagrantfiles/v2.defaults_mixed_string_symbols
191
+ - test/resources/vagrantfiles/v2.defaults_using_string
192
+ - test/resources/vagrantfiles/v2.defaults_using_symbol
193
+ - test/resources/vagrantfiles/v2.empty
194
+ - test/resources/vagrantfiles/v2.fake
195
+ - test/resources/vagrantfiles/v2.missing_parameter
196
+ - test/resources/vagrantfiles/v2.real
162
197
  - test/resources/yml/params_array.yml
163
198
  - test/resources/yml/params_boolean.yml
164
199
  - test/resources/yml/params_combinations.yml