Ruby increase file size on the fly for testing

我只是一个虾纸丫 提交于 2019-12-23 12:09:34

问题


This may sound weird, or why do you want to do that.

I'm trying to write a cucumber feature to test uploading large image file (>16M) So, I don't want to store the large file on github or in my project. I'm using imagemagick to create an image but as far as I can do is just 1M. Can I increase the file size in ruby? I don't care about the content inside the file, just the file size. Thanks, is there anyway I could trick cucumber to believe me that I'm uploading a large file size?

Thanks


回答1:


The dd(1) tool can make a file quite large while using very little disk space:

$ dd if=/dev/zero of=huge bs=1024 count=1 seek=100000
1+0 records in
1+0 records out
1024 bytes (1.0 kB) copied, 4.9857e-05 s, 20.5 MB/s
$ ls -lh huge
-rw-r--r-- 1 user user 98M 2011-07-03 02:43 huge
$ du -h huge 
12K huge

The file huge looks to be 102400000 bytes long. (Roughly 98M.) But it only takes 12 kilobytes on disk, because the seek parameter to dd(1) causes it to start writing way "into" the file. If the earlier bytes are read, the OS will supply an endless stream of zeros. (0x00 kind of zeros, not the ASCII kind of zeros: "0".)

If you wanted to replicate this in Ruby, you'd use the File#seek function:

irb> f=File.new("huge", "w")
=> #<File:huge>
irb> f.seek(100000 * 1024)
=> 0
irb> f.write("hello")
=> 5
irb> f.close()
=> nil
irb> ^D
$ ls -lh huge
-rw-r--r-- 1 sarnold sarnold 98M 2011-07-03 02:47 huge

Just ruby code:

f=File.new("100MB", "w")
f.seek(100000 * 1024)
f.write("hello")
f.close()


来源:https://stackoverflow.com/questions/6562085/ruby-increase-file-size-on-the-fly-for-testing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!