Closed perillo closed 3 years ago
The T.Setenv
function is useful because it sets global state and then undoes that global state when the test is complete. That is code that it is easy for a test to get wrong, so it makes some sense to provide as part of the testing package.
That argument doesn't apply here. There is nothing specific to the testing package about providing a function that writes a string to a file with a default mode. We could do that in the os package.
This proposal has been added to the active column of the proposals project and will now be reviewed at the weekly proposal review meetings. — rsc for the proposal review group
Based on the discussion above, this proposal seems like a likely decline. — rsc for the proposal review group
While working on some CL on the tests I noted a common pattern: calling
os.WriteFile
to create a temporary file using a string as input.Here is the list, obtained using
grep -F "os.WriteFile(" **/*_test.go | grep -F '[]byte'
:The list is quite long. In contrast, while working on #45448 I noted that the new
T.Setenv
could be used only in few cases.There is an interesting fact in the list: the
perm
argument is not always 0644; the interesting cases are0
(in one cases it is for windows so it make senses) and0755
used on non executable files.Using the proposed
TB.WriteFile
should reduce a bit of noise in the tests.I'm not sure if
TB.WriteTempFile(name, data string) string
(where the returned value is the file name) is a better API (assuming that each call ofTB.WriteTempFile
reuses the same temporary directory).Thanks.