Powerful Command-Line Applications in Go: Missing .todo.json location 1453 ebook

When creating the first cli client for the Todo api,the working file is hardcoded as “.todo.json”. In the main_test.go users are never instructed to created the file but they are instructed to remove the file with os.Remove(fileName). Even if a person were to manually make the file they would have to do so everytime because their test will delete it.

You should instruct users to create the file at the beginning of the tests like when testing the API alone otherwise users will get a non-descriptive error that their test failed unless they build and run it manually at which point it will say “open .todo.json: no such file or directory”

@rgerardi

Sorry, the main problem is actually that the first cli client starts by trying to read from a non-existent file and promptly exits if it doesn’t exist, preventing the cli arguments to make it to the function that will create a task and subsequently the file to store it in.

image

Hi @crumdev , thank you for the feedback.

Actually, the Get method covers the non-existing file and it does not return an error.

The error in this test occurs if you ran the tool manually one time, creating a file .todo.json which the test code does not delete before the test. However, the test code deletes the file after, which means the test works the second time around.

This is, unfortunately, a known condition with this test. A few pages later, when running the test again, I mention that you need to delete the file to prevent the error.

Also, later in this chapter you modify the program to take the file name as an environment variable which you can use to prevent the name clash.

I hope this helps.

Regards,
Ricardo