nil:NilClass 的未定义方法 'scan' (NoMethodError)
Undefined method 'scan' for nil:NilClass (NoMethodError)
坚持这一点,此布局用于 chef inspec 测试,但利用 ruby 来获取文件的内容。然而,通过这个测试,我实际上并没有针对文件进行测试,所以我试图了解如何解决这个问题,代码如下:
%w(/etc/bashrc /etc/profile).each do |path|
file(path).content.scan(/^\s*umask\s+(\d{3})\b/).flatten.each do |umask|
BASELINE = '0027'
(1..3).each do |i| # leading char is '0' octal indicator
describe umask[i].to_i do
it { should be <= BASELINE[i].to_i }
end
end
end
end
end
这是给我带来麻烦的行
file(path).content.scan(/^\s*umask\s+(\d{3})\b/).flatten.each do |umask|
您可以将 file(path).content
更改为与文件内容匹配的字符串。
"Sample_string".scan(/^\s*umask\s+(\d{3})\b/).flatten.each do |umask|
原因是 file(path).content
returns nil
如果您不是针对真实文件进行测试。并且 nil
没有 scan
方法,这就是您收到错误的原因。
就错误而言,即“Undefined method 'scan' for nil:NilClass” ,只有在执行 inspec 运行 时,如果正在传递的文件在文件系统上不存在或不可读,才会出现此错误。
此外,提供的信息不完整,因为不清楚两个文件中的 umask 设置是什么,即它是 3 位数字还是 4 位数字?
因为在进行扫描时您正在寻找 3 位 umask "scan(/^\sumask\s+(\d{3} )\b/)*" 并且你已经设置了 "BASELINE = '0027'" 这是一个 4 位数字。所以,肯定会出问题。
如果文件中有“umask 027”,那么,它应该是:
检查 BASELINE = '027',搜索 3 位 umask
%w(/etc/bashrc /etc/profile).each do |path|
file(path).content.scan(/^\s*umask\s+(\d{3})\b/).flatten.each do |umask|
BASELINE = '027'
(1..3).each do |i| # leading char is '0' octal indicator
describe umask[i].to_i do
it { should be <= BASELINE[i].to_i }
end
end
end
end
否则你的文件中有“umask 0027”,那么,它应该是:
检查scan(/^\s*umask\s+(\d{4})\b/),搜索4位umask
%w(/etc/bashrc /etc/profile).each do |path|
file(path).content.scan(/^\s*umask\s+(\d{4})\b/).flatten.each do |umask|
BASELINE = '027'
(1..3).each do |i| # leading char is '0' octal indicator
describe umask[i].to_i do
it { should be <= BASELINE[i].to_i }
end
end
end
end
坚持这一点,此布局用于 chef inspec 测试,但利用 ruby 来获取文件的内容。然而,通过这个测试,我实际上并没有针对文件进行测试,所以我试图了解如何解决这个问题,代码如下:
%w(/etc/bashrc /etc/profile).each do |path|
file(path).content.scan(/^\s*umask\s+(\d{3})\b/).flatten.each do |umask|
BASELINE = '0027'
(1..3).each do |i| # leading char is '0' octal indicator
describe umask[i].to_i do
it { should be <= BASELINE[i].to_i }
end
end
end
end
end
这是给我带来麻烦的行
file(path).content.scan(/^\s*umask\s+(\d{3})\b/).flatten.each do |umask|
您可以将 file(path).content
更改为与文件内容匹配的字符串。
"Sample_string".scan(/^\s*umask\s+(\d{3})\b/).flatten.each do |umask|
原因是 file(path).content
returns nil
如果您不是针对真实文件进行测试。并且 nil
没有 scan
方法,这就是您收到错误的原因。
就错误而言,即“Undefined method 'scan' for nil:NilClass” ,只有在执行 inspec 运行 时,如果正在传递的文件在文件系统上不存在或不可读,才会出现此错误。
此外,提供的信息不完整,因为不清楚两个文件中的 umask 设置是什么,即它是 3 位数字还是 4 位数字?
因为在进行扫描时您正在寻找 3 位 umask "scan(/^\sumask\s+(\d{3} )\b/)*" 并且你已经设置了 "BASELINE = '0027'" 这是一个 4 位数字。所以,肯定会出问题。
如果文件中有“umask 027”,那么,它应该是: 检查 BASELINE = '027',搜索 3 位 umask
%w(/etc/bashrc /etc/profile).each do |path|
file(path).content.scan(/^\s*umask\s+(\d{3})\b/).flatten.each do |umask|
BASELINE = '027'
(1..3).each do |i| # leading char is '0' octal indicator
describe umask[i].to_i do
it { should be <= BASELINE[i].to_i }
end
end
end
end
否则你的文件中有“umask 0027”,那么,它应该是:
检查scan(/^\s*umask\s+(\d{4})\b/),搜索4位umask
%w(/etc/bashrc /etc/profile).each do |path|
file(path).content.scan(/^\s*umask\s+(\d{4})\b/).flatten.each do |umask|
BASELINE = '027'
(1..3).each do |i| # leading char is '0' octal indicator
describe umask[i].to_i do
it { should be <= BASELINE[i].to_i }
end
end
end
end