2017-01-12 8 views
0

ディレクトリが存在するかどうかを確認しているときに問題が発生しました。Dir.exist?期待どおりに動作しません。

私は、リポジトリからsvnチェックアウトを実行する小さなRubyアプリケーションを作成しています。次に、作業コピーを探して特定のディレクトリが存在するかどうかを確認します。

私は(私はSVNで動作するように任意のルビーの宝石を見つけていない)open3.popen3を使用して、コマンドラインクライアントを呼び出して、SVNの操作を実行SVNClientクラスを持っている:

class SVNClient 
    require 'open3' 

    def initialize(repoUrl, wcPath, username, password) 
     @repoUrl = repoUrl 
     @wcPath = wcPath 
     @username = username 
     @password = password 
    end 

    def checkout 
     cmd = "svn co %s %s --non-interactive --trust-server-cert --username %s --password %s" % [@repoUrl, @wcPath, @username, @password] 
     stdin, stdout, stderr = Open3.popen3(cmd) 
    end 

    def createDirIfNotExists(dirname) 
     @subdirPath = @wcPath + "/" + dirname 

     a = File.directory? @subdirPath 
     b = File.exist? @subdirPath 
     c = Dir.exist? @subdirPath 
     Rails.logger.debug("#{a}") 
     Rails.logger.debug("#{b}") 
     Rails.logger.debug("#{c}") 


     if !Dir.exist? @subdirPath 
      Rails.logger.debug("#{@subdirPath} does not exist") 
      Dir.mkdir @subdirPath 
     end 
    end 
end 

このクラスは、次のように使用されます。

 wcDir = "/the/workingcopy/dir" 
     logger.debug("#{wcDir}") 
     urlToCkeckout = "http://somerepo/path 

     client = SVNClient.new(urlToCkeckout, wcDir, username, password) 
     client.checkout 
     client.createDirIfNotExists("subdir") 

は今、私はいくつかのテストをやっていると私はチェックアウトを行うときに、"subdir"ディレクトリは作業コピーディレクトリ内にあることを確信しています。しかし、createDirIfNotExistsメソッドでは、Dir.exist?呼び出しがfalseを返します(また、File.directory?File.exist?)、「...存在しません」というログメッセージが表示されます。
私はここに何かが分かりませんか?私はディレクトリのアクセス許可をチェックして、それは良いように見えます。

ところで、コードはRailsアプリケーション内で実行されます。

答えて

1

ソリューションは、コマンドがDIRの有無をチェックする前に終了していることを確認するために、私のcheckout方法でopen3.popen3のブロックバージョンを使用することです:今

 Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr| 
      pid = wait_thr.pid # pid of the started process. 
      Rails.logger.debug("#{pid}") 
     end 

createDirIfNotExistsへの呼び出しは、チェックアウト後に起こりますディレクトリが正しく見つかりました。

関連する問題