Easily generate test files of any size
The problem
Say you have a file upload validation in one of your Rails models. This validation limits the size of an uploaded file to 10 Mb.
You now want to test the upload validation size limit works correctly (manually or automated).
So you start searching your Documents folder, hoping to find a couple files that will work.
You want one file that's just below 10 Mb limit and another that is just above 10 Mb. This can be cumbersome and you just may not have local files that fit the bill.
What can we do instead?
The solution
How about dynamically generate a file of the exact size we need?
We can accomplish this using the dd unix command. So let's try creating a file of exactly 10 Mb in size.
10240+0 records in
10240+0 records outt
10485760 bytes transferred in 0.859163 secs (12204622 bytes/sec)
What were saying here is, read data from /dev/random, into a new file called 'test.txt', in 1024-byte size blocks (1 KB), and give me 10,240 of them (10 Mbs is 10,240 KB).
We can verify this file is the correct size with
-rw-r--r--+ 1 supairish staff 10M Jun 4 17:42 test.txt
Excellent!
So just rerun this command with a smaller or larger count number to get the exact file sizes you need and you're good to go.
Just remember: 1 Mb is 1,024 KB
You could also wrap this command in a spec helper to use in MiniSpec or Rspec and generate files of random size ranges during your runs.
Just remember to cleanup/delete in your teardowns so you don't accidentally eat up disk space.
Need help with your project?
We specialize in Ruby on Rails and JavaScript projects. Code audits, maintenance and feature development on existing apps, or new application development. We've got you covered.