0
votes

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?

3

3 Answers

0
votes

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.

0
votes

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'.

0
votes

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.