This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the dumb-mistakes-and-gotchas category.
Last Updated: 2024-11-21
I was getting sporadic failures on CircleCI due to a downloaded file not being found. Here was my code for configuring a download path and accessing downloaded files:
Capybara.save_path = Rails.root
def downloaded_file(filename)
Capybara.save_path.join('downloads', filename)
end
def clear_test_downloads
FileUtils.rm_rf(Capybara.save_path.join('/downloads'))
end
The issue was that the downloaded_file
method was searching in the
./downloads
directory whereas the clear_test_downloads
method was operating
on /downloads
- i.e. the absolute path - way outside my project. This was
incorrect and potentially dangerous.
This mistake occurred because I wrongly assumed that the PathName
object, of
which Rails.root
is one, would interpret the /downloads
as being a sub-directory
off of the current one. Never assume.
The fix:
# Share the download path to prevent them going out of sync
def download_path
Capybara.save_path.join('downloads')
end
def clear_test_downloads
FileUtils.rm_rf(download_path)
end
def downloaded_file(filename)
download_path.join(filename)
end