Over the years we've gradually removed all use of various third-party
reflection-based libraries for deep-copying and projecting data structures
from main code, because they proved to be a maintenance nightmare and thus
better replaced by direct code that is less fun to write but far clearer
to read.
However, we've still got a few examples of _test_ code hanging on to these
helpers because the tradeoffs for unit tests tend to be different. That's
valid, but it's annoying to have to include several large and very general
dependencies just to support some tests whose needs are well-known and
relatively simple.
Therefore this new helper function copy.DeepCopyValue aims to be a more
straightforward replacement for mitchellh/copystructure (which also
indirectly subsumes mitchellh/reflectwalk) that eschews all of the
customizability that those libraries offered and instead focuses narrowly
on what our few remaining unit tests need.
In particular, this replacement doesn't offer any means for registering
helper functions to translate struct types with unexported fields. Those
instead just get left set to their zero value in the result. Since our
focus is only on unit test code, we can know ahead of time when that
limitation is relevant and code around it in the calling test rather than
complicating this utility code.
This commit replaces `ioutil.TempDir` with `t.TempDir` in tests. The
directory created by `t.TempDir` is automatically removed when the test
and all its subtests complete.
Prior to this commit, temporary directory created using `ioutil.TempDir`
needs to be removed manually by calling `os.RemoveAll`, which is omitted
in some tests. The error handling boilerplate e.g.
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Fatal(err)
}
}
is also tedious, but `t.TempDir` handles this for us nicely.
Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
helper/copy CopyDir was used heavily in tests. It differes from
internal/copydir in a few ways, the main one being that it creates the
dst directory while the internal version expected the dst to exist
(there are other differences, which is why I did not just switch tests
to using internal's CopyDir).
I moved the CopyDir func from helper/copy into command_test.go; I could
also have moved it into internal/copy and named it something like
CreateDirAndCopy so if that seems like a better option please let me
know.
helper/copy/CopyFile was used in a couple of spots so I moved it into
internal, at which point I thought it made more sense to rename the
package copy (instead of copydir).
There's also a `go mod tidy` included.