My head is done !

My head is done !

I finally made it, a cut down version of GNU head

This was fun, a bit of headache some times…

Since this training book has been built in TDD ( Test driven development ) way I had to do some back and for between the expected result and what result I had.

So this was not at all programming issues but purely achieving what the tests case where expecting.

I will not bother you with all the issues I?had, also..

For couple of use case I have changed the test scenario to match my output :D

I know this sounds crazy but I would like to show you the both case where I did so.

Let’s first have a look at a diff between the two tests script, the original one and mine.


I have colored all 3 differences for ease of read.

The first one this is purely cosmetic and for my personal convenience I have a different project name and consequently different binary name.


1st Case:

In this case we are testing the program to make sure it reports an error if we use both parameters “-n” and “-c” at the same time since it is not supported.

I'm using clap v2 branch ( exactly v2.34 ) to build this cli tool.

Also, with clap to manage the incompatibility between two parameters there is a dedicated method for that which is conflict_with.

As far as I know, this method generate a builtin output, you probably see where I’m going with it....

The output of the clap create that I’m using is not the same as the one expected by the test case.

This is the output expected by the test case:


Expected output in msg variable

This is the output of my program:


output from my "header" with Clap v2.34

So unless, I rewrite clap to change the output behavior in this condition, which was clearly not an option for me, I decided to modify the expected output of the test case to match mine since it is perfectly reasonable and as expected.

2nd Case:

In this case found the expected result a bit weird for me, even more it is expecting a totally different behavior than GNU head.

In this test case, the goal is to skip bad files/files that don’t exist and throw an error message for those files.

This is the original tests scenario:

This is my version:

Let me show you the output of my version of head and GNU head

This is the output of my head:

This the output of GNU head:

This is interesting because we have the same structure and output expect for the format of the error message.

This is where I found strange that the expected output must be the error message and only the error message for all files instead of behaving more closely to what is the output of GNU head.

Consequently I consider this test to pass as soon as the expected error message is contained in the output and not is the all output.

Also, I?think I will go have a look at the code to produce this output,I'm curious how he handle that one.

Fixing bugs:

In the previous article I spent some time talking about End line character and specially the return carrier.

I have manage to fix this issue by refactoring my code.

Effectively in the previous version I was using the lines() method which is consuming the return carrier or line return when iterating on each lines.

Consequently I have moved to the read_line() method which is outputting everything including the return carrier or line return that are in the string.

At the same time, I have discovered the loop{} keyword which is very useful to do very simple loop then break out on specified condition.

This is what, this piece of code looks like now using read_line().

This is a much concise code, I have to appreciate that !


Here is the latest and final version of header on github

Enjoy Rust !

要查看或添加评论,请登录

社区洞察

其他会员也浏览了