I'm responsible strictly for documenting and unit-testing a Golang project. In the part of the SUT I'm currently on, a real file, identified by a file path, is being checked for its "Last modified by" time.
Its code is thus:
// AssetTimePath returns a timestamped path
//
// Parameters:
//
// - `s` : the URL of the file
//
// Returns:
//
// - the timestamped path
// - any errors
func (view *View) AssetTimePath(s string) (string, *errors.ErrorSt) {
if strings.HasPrefix(s, "//") {
return s, nil
}
s = strings.TrimLeft(s, "/")
abs, err := filepath.Abs(s)
if err != nil {
return "", errors.File().AddDetails(err.Error())
}
time, err2 := utilities.FileTime(abs)
if err2 != nil {
return "", err2
}
return view.PrependBaseURI(s + "?" + time), nil
}
The problem
To happy-path test this function, I would need to create a real file, maybe somehow track its absolute pathname, and delete the file afterwards. This seems inconsistent with unit-testing, as it is concerned with isolating all other factors to test individual units of work (such as the fact that there's even a string containing, but not ending with, ?
iff the file exists).
Is it ever acceptable, in a unit-test, to create a real file for testing? If not, is this even fully testable?
UPDATE : I overlooked the fact that I could just pass in the name of the very file the tests, or even this part of the SUT, is written in!
The spirit of this question still remains...