Title: Powerful Command-Line Applications in Go: TestLoadNoFile should expect a non-nil error (page 229)

In func TestLoadNoFile , we create a temp file, delete it, and try to load it.

The test is incorrectly written to expect a nil error when we try to load the (deleted) file. It should expect an error.

cobra/pScan.v3/scan/hostsList_test.go

...
  if err := hl.Load(tf.Name()); err != nil {
    t.Errorf("Expected no error, got %q instead\n", err)
  }

Should be:

...
	if err = hl.Load(tf.Name()); err == nil {
		t.Errorf("Expected error, got nil\n")
	}
1 Like

Thanks @adamwoolhether . Let me check.

Ricardo

Hi @adamwoolhether , thanks for posting.

The test as shown in the book is actually correct because the method Load shouldn’t return an error when the file doesn’t exist. Please check the method definition a few pages before.

You can also check it by writing the test as you suggested and verifying that it fails.

The idea behind this logic is allowing the user to specify a file name in the configuration and, if it doesn’t exist, the application creates it when saving the first host.

Hope this helps.
Ricardo

Ah! You’re right. I had used our ErrNotExists var instead of os.ErrNotExist in the func Loud(). Sorry for that! I’ll delete this thread if i can. If not i’ll be more thoughtful moving onward.