2017-07-14 1 views
0

私はこのテストをしようとしていますが、内部のFile.foreachが文句を言います。変数aのファイルを検索しますが、そこにある間はファイルを開くことはできません。このコンストラクタは別のループ内でファイルを開く正しい方法を使用していますか?ruby​​ file.foreach

(これは最終的なテストではありません、私は物事が起こって取得しようとしている)

describe "2.1.18" do 
    it "CIFS: Only valid users are allowed" do 
    if File.exist?('/etc/samba/samba.d/shares.conf') 
     authorized_regexp = /^\s*(valid\s+users\s+\W{1}\s+.+)/ 
     path_regexp = /path/ 
     inc_regexp = /^\s*include/ 
     path_count = 0 
     a_count = 0 

     File.foreach('/etc/samba/samba.d/shares.conf') do |line| 
     if mtch = line.match(authorized_regexp) 
      a_count += 1 
     else 
      if mtch = line.match(inc_regexp) 
      _, a = line.split('=') 
      File.foreach(a) do |pp| 
       if mtch = pp.match(authorized_regexp) 
       a_count += 1 
       end 
      end 
      end 
     end 
     if mtch = line.match(path_regexp) 
      path_count += 1 
     end 
     end 
     expect(a_count).to eq(path_count) 
    end 
    end 
end 

エラーメッセージ ノーム2.1.18

BIDET CIFS: Only valid users are allowed (FAILED - 1) 
Failures: 

    1) 2.1.18 CIFS: Alleen geautoriseerde gebruikers hebben toegang 
Failure/Error: File.foreach(a) do |pp| 
Errno::ENOENT: 

    No such file or directory @ rb_sysopen - /etc/samba/liv_smb.conf 
# ./test_spec.rb:16:in `foreach' 
# ./test_spec.rb:16:in `block (3 levels) in <top (required)>' 
# ./test_spec.rb:10:in `foreach' 
# ./test_spec.rb:10:in `block (2 levels) in <top (required)>' 
Finished in 0.00119 seconds (files took 0.11126 seconds to load) 
1 example, 1 failure 

それは開くことができませんファイル。

# ls -l /etc/samba/liv_smb.conf 

-rw-r--r-- 1 root root 247 Jul 14 09:49 /etc/samba/liv_smb.conf 

EDIT:mudasobwaが問題を解決し

、File.foreach(a.strip)は、トリックを行いました!

+0

'File.foreach(a.strip)'が役立つはずですが、後ろに '\ n'があると思います。 – mudasobwa

+0

あなたが支配する!それは、ありがとう。 –

+0

これは、この質問のループを閉じ、同じジャムの他の人を助けることができるので、自己回答として投稿する価値があります。 – tadman

答えて

0

mudasobwaがソリューションを提供しました。私は 'a'変数に 'strip'を追加する必要がありました。改行が含まれているからです。

 File.foreach(a.strip) do |pp| 
      if mtch = pp.match(authorized_regexp) 
      a_count += 1 
      end 
     end 
関連する問題