为什么在切换ruby版本时rspec puppet因未定义的方法错误而失败?

时间:2015-11-15 00:33:58

标签: ruby rspec puppet rspec-puppet

我使用ruby版本1.8.7开发了我的puppet模块,在以下gem文件上运行bundle install并且rspec测试成功运行(木偶运行也是如此):

source 'https://rubygems.org'

if puppetversion = ENV['PUPPET_GEM_VERSION']
  gem 'puppet', puppetversion, :require => false
else
  gem 'puppet', '3.7.5'
end

gem 'metadata-json-lint'
gem 'puppetlabs_spec_helper', '>= 0.1.0'
gem 'puppet-lint', '>= 1.0.0'
gem 'facter', '>= 1.7.0'
gem 'rspec-puppet-facts'

# rspec must be v2 for ruby 1.8.7
if RUBY_VERSION >= '1.8.7' and RUBY_VERSION < '1.9'
  gem 'rspec', '~> 2.0'
end

然而,当我在我的CI服务器上使用ruby bundle install在同一Gemfile上执行1.9.3时,我遇到了以下rspec失败:

See verbose errors here

常见的错误是:

Puppet::Error:
       Failed to parse template silex/var/config/settings.json.erb:
         Filepath: /var/tmp/silex/spec/fixtures/modules/silex/templates/var/config/settings.json.erb
         Line: 4
         Detail: undefined method `each_with_index' for "default":String

使用each_with_index方法的模板:

{
    "elasticsearch.settings": {
        "connections": [
        <% @elastic_hosts.each_with_index do |host,i| -%>
        {"host":"<%= host['ip'] %>", "port": <%= host['port'] %> }<%= ',' if i < (@elastic_hosts.size - 1) -%>
        <% end -%>
        ],
        "index": "<%= @elastic_index %>"
    }
    },
    "cache.lifetime": <%= @elastic_cache_lifetime %>,
    "environment": "dev",
    "trusted": [
        <% @trusted_hosts.each_with_index do |host,i| -%>
        {"host":"<%= host['host'] %>"}<%= ',' if i < (@trusted_hosts.size - 1) -%>
        <% end -%>
    ],
    "homepage.settings": {
      "hero.count": 20,
      "list.count": 20
    }
}

我未能理解为什么这会因为不同版本的ruby而失败?我也尝试过ruby 2.1.1,结果相同。结果仅在具有ruby 1.8.7的CI服务器上成功(与我用于开发模块的版本相同)。

更新1

看起来因为elastic_hosts是一个哈希映射而我有一个字符串它失败了。我确实将此问题与trusted_hosts(另一个哈希映射)一起修复,导致大多数测试通过,但错误仍然存​​在。

See errors here

注意:错误与上面突出显示的常见错误相同。

我还使用完整内容更新了上面的模板文件。

My config spec file

如果你查看我的配置规范文件,我没有在任何地方将default指定为字符串,但它告诉我这是错误..

FIX

我怀疑我的init规范类已设置默认值并添加elastic_hoststrusted_hosts的hiera查找解决了问题。

更新2

此行为在版本1.8.7和版本&gt;之间可重现。 1.9.3。它似乎与如何处理哈希有关。

1 个答案:

答案 0 :(得分:3)

答案很简单:因为each方法已从较新版本的Ruby中删除。一个原因是该方法在UTF-8字符串的上下文中令人困惑:是each迭代每个字符还是每个字节

但有each_char,您可以将其与with_index结合使用:

string.each_char.with_index do |char, i|
  # ...
end

此外,您在@elastic_hosts上调用该方法,该代码段中的内容显然应该是hosts的数组。但错误消息告诉我们@elastic_hosts是字符串default。因此,使用each_with_index的方法调用失败是有道理的,但似乎迭代本身没有意义。我建议检查@elastic_hosts是否设置正确。

相关问题