黄瓜场景后删除文件夹

发布于 2024-10-15 06:06:22 字数 1385 浏览 3 评论 0原文

我有 2 个模拟回形针图像上传的黄瓜场景。场景完成后,我想再次删除这些文件夹。

我有以下附件文件夹结构: :网址=> “/system/:attachment/:listing_id/:id/:style_:filename

回形针会自动删除 :id/:style_:filename 文件夹,但不会删除父文件夹。

我的列表控制器中有以下内容(1 个列表有许多图像),它非常适合在删除列表时删除带有列表 id 的图像文件夹。该步骤运行后,我需要在 Cucumber 中模拟相同的内容。

def destroy 
    @listing = Listing.find(params[:id])  

    # if destroy was a success, remove the listing image folder
    if @listing.destroy 


    end 

    require 'fileutils'
    dir = Rails.root + '/system/photos/' + @listing.id.to_s()
    FileUtils.rm_rf(dir)


    respond_to do |format| 
        format.html { redirect_to(listings_url) } 
        format.xml { head :ok } 
    end 
end 

我可以 a) 告诉 cucumber 在运行场景后删除 :listing_id 文件夹名称,或者 b) 告诉 cucumber 在最后一步删除列表?

我尝试将其添加到我的黄瓜 env.rb 文件中:

AfterStep('@paperclip') do
      # This will only run before steps within scenarios tagged
      # with @cucumis AND @sativus.
        # delete folders that were created with paperclip during the test
        require 'fileutils'
        #@listing.id = 55
        #dir = Rails.root + '/system/photos/' + @listing.id.to_s()
        dir = Rails.root + '/system/photos/55' # NOT WORKING
        FileUtils.rm_rf(dir)
    end

但这会导致问题,因为 1)我不知道如何从该场景中获取 @listing.id,2)即使我对其进行硬编码(如上所述)它不会删除它。

有什么想法吗?

I have 2 cucumber scenarios that simulate paperclip image upload. I want to remove those folders again once scenarios are complete.

I have the following attachment folder structure:
:url => "/system/:attachment/:listing_id/:id/:style_:filename"

Paperclip automatically deletes the :id/:style_:filename folder but not the parent folder.

I have a the following in my listings controller (1 listing has many images) which works great to remove the image folder with the listing id when the listing is deleted. I need to simulate the same in Cucumber after the step is run.

def destroy 
    @listing = Listing.find(params[:id])  

    # if destroy was a success, remove the listing image folder
    if @listing.destroy 


    end 

    require 'fileutils'
    dir = Rails.root + '/system/photos/' + @listing.id.to_s()
    FileUtils.rm_rf(dir)


    respond_to do |format| 
        format.html { redirect_to(listings_url) } 
        format.xml { head :ok } 
    end 
end 

I could a) tell cucumber to delete the :listing_id folder name after running through the scenario or b) tell cucumber to delete the listing as the final step?

I've tried adding this to my cucumber env.rb file:

AfterStep('@paperclip') do
      # This will only run before steps within scenarios tagged
      # with @cucumis AND @sativus.
        # delete folders that were created with paperclip during the test
        require 'fileutils'
        #@listing.id = 55
        #dir = Rails.root + '/system/photos/' + @listing.id.to_s()
        dir = Rails.root + '/system/photos/55' # NOT WORKING
        FileUtils.rm_rf(dir)
    end

But that causes problems because 1) I don't know how to get the @listing.id from that scenario, and 2) even when I hardcode it (as above) it doesn't remove it.

Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

记忆里有你的影子 2024-10-22 06:06:22

已经有点旧了,但是当我自己遇到同样的问题时,我就是这么做的:

dir = Rails.root + 'images/'
dir.rmtree if dir.directory?

# or the short form, if you know the directory will be there
(Rails.root + 'images/').rmtree

所以我猜问题出在文件夹开头的“/”。至少对我来说,它不适用于该斜杠。

Already a bit older, but as I just ran over the same issue myself here is what I did:

dir = Rails.root + 'images/'
dir.rmtree if dir.directory?

# or the short form, if you know the directory will be there
(Rails.root + 'images/').rmtree

So I guess the problem was your '/' at the beginning of the folder. At least for me it didn't work with that slash.

ぃ双果 2024-10-22 06:06:22

我想发表评论,但没有足够的理由(或者我认为)这样做。

确实没有理由这不起作用。您是否确认 FileUtils.rm_rf(dir) 实际上删除了测试环境中的目录?

您可以在“脚本/控制台测试”中对此进行测试。

I would leave a comment, but don't have the points (or so I would assume) to do so.

There really is no reason that this shouldn't work. Have you confirmed that FileUtils.rm_rf(dir) does in fact remove the directory in the test environment?

You can test this in 'script/console test'.

羞稚 2024-10-22 06:06:22

您可以连接 Cucumber 的 at_exit 来删除您喜欢的任何文件夹。我正在使用 features/support/uploads_cleaner.rb 中的附加代码。

# Removes uploaded files when all scenarios for the current test process
# are finished. Ready for parallel_tests, too.

require 'fileutils'

at_exit do
  directory_name = "#{ Rails.env }#{ ENV['TEST_ENV_NUMBER'] }"
  uploads_path = Rails.root.join('public/system', directory_name)
  FileUtils.remove_dir(uploads_path) if uploads_path.directory?
end

从此makandra 卡转发。

You can hook into Cucumber's at_exit to remove any folders you like. I'm using the attached code in features/support/uploads_cleaner.rb.

# Removes uploaded files when all scenarios for the current test process
# are finished. Ready for parallel_tests, too.

require 'fileutils'

at_exit do
  directory_name = "#{ Rails.env }#{ ENV['TEST_ENV_NUMBER'] }"
  uploads_path = Rails.root.join('public/system', directory_name)
  FileUtils.remove_dir(uploads_path) if uploads_path.directory?
end

Reposted from this makandra Card.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文