
adamwoolhether
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")
}
Marked As Solved

rgerardi
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
Popular Prag Prog topics










Other popular topics










Latest in PragProg
Latest (all)
Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /js
- /rails
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /haskell
- /emacs
- /svelte
- /onivim
- /typescript
- /crystal
- /c-plus-plus
- /tailwind
- /kotlin
- /gleam
- /react
- /flutter
- /elm
- /ocaml
- /vscode
- /opensuse
- /ash
- /centos
- /php
- /deepseek
- /scala
- /zig
- /html
- /debian
- /nixos
- /lisp
- /agda
- /sublime-text
- /textmate
- /react-native
- /kubuntu
- /arch-linux
- /revery
- /ubuntu
- /manjaro
- /django
- /spring
- /diversity
- /lua
- /nodejs
- /c
- /julia
- /slackware
- /neovim