Continuing on from the last blog post on working with files...
A classic thing we'll want to do, other than just reading in some data, is append data to a file. As we saw in the last post we can think of the mode of a+ as 'append', as opposed to the ramblings of the Ruby Docs on the mode. As the docs say, a+ gives us read-write and starts at the end of the file, when it exists, creating a new one if not. That 'creating a new one' seems to be standard fair with any writing action, good to know.
Add a new entry
In our example of a simple text file which contains various test type names, we want to add a further test type. As mentioned, this could be building up test data or writing our results of running tests so we have some form of results log. Actually I just thought of another thing to consider. Can we dynamically name our files so we don't overwrite them each test run, add that to the list...
The operations should be pretty straight forward - open the file and add our new entry :) We'll add a check... let's then print the contents of the file so we know our new entry got added. Here's the method the fist cut
Nothing got printed?! It took a while and a good bit of searching about to crack this one. If we recall how the different modes work there's constant references to doing stuff at the start or end of file. That idea of position in a file is significant, here we added to the end of the file and that's where we are, so Ruby is struggling to read it. If we look back over the Ruby Docs for the I/O class we find there's a rewind method that takes us to the start of the input. Let's call rewind against our openFile variable and see what happens.
Works wonders, oddly I moved it around in the method and it still works. I guess so long as the instruction is there it works when our file_add_entry method is called. Best to have it in order though for readability of our code.
Where does the write happen?
Another interesting thing here was the effect of the position of the cursor in the text file before running the method. Have a look at the following image, where we can see the cursor in the text file. As we can see it affects where our method writes the additional string within the file.
Looks like our a+ really does append the line to the end of the file. Interestingly, if we run the method a couple of times without deleting the previous entry we can see Load being added twice but on separate lines, not tacked onto the end as we might expect. Why so? You'd think we'd get PerformanceLoadLoad for example, simple this one, remember puts sneaks in a newline /n.
These little subtleties are certainly key to know and good examples of what can have you straining at the screen asking "Why is that happening?!" ;}
Given we testers can and would like to make a lot of use of data files, logs and so on there's a lot of other experimentation that could be done around these aspect alone.
Have a look at the attached file for the examples I've run externalFile.rb
Add a Comment