1
0
mirror of https://blitiri.com.ar/repos/chasquid synced 2026-10-29 15:02:30 +00:00

test: Use modern testing features instead of custom helpers

Many of our tests rely on helpers that predate features which are now
available in the standard library.

This patch replaces them with the standard equivalents:

- MustTempDir/RemoveIfOk -> t.ArtifactDir() or t.TempDir() (depending on
  whether it is useful to keep the data on request).
- GetFreePort + waitForServer -> opening the listeners before starting
  the servers. To support this, localrpc.Server gains a Serve method. We
  may re-introduce these in the future if more use cases appear.
- WaitFor and real-time timing checks -> testing/synctest.
- context.Background() -> t.Context().
- Benchmark b.N loops -> b.Loop().
- reflect.DeepEqual -> cmp.Diff, and proto.Equal for protobufs.
This commit is contained in:
Alberto Bertogli
2026-10-03 10:34:35 +01:00
parent 8d763f450d
commit 8b19760f4e
30 changed files with 495 additions and 763 deletions
+10 -10
View File
@@ -42,8 +42,8 @@ func testWriteFile(fname string, data []byte, perm os.FileMode, ops ...FileOp) e
}
func TestWriteFile(t *testing.T) {
dir := testlib.MustTempDir(t)
defer testlib.RemoveIfOk(t, dir)
dir := t.ArtifactDir()
t.Chdir(dir)
// Write a new file.
content := []byte("content 1")
@@ -65,8 +65,8 @@ func TestWriteFile(t *testing.T) {
}
func TestWriteFileWithOp(t *testing.T) {
dir := testlib.MustTempDir(t)
defer testlib.RemoveIfOk(t, dir)
dir := t.ArtifactDir()
t.Chdir(dir)
var opFile string
op := func(f string) error {
@@ -88,8 +88,8 @@ func TestWriteFileWithOp(t *testing.T) {
}
func TestWriteFileWithFailingOp(t *testing.T) {
dir := testlib.MustTempDir(t)
defer testlib.RemoveIfOk(t, dir)
dir := t.ArtifactDir()
t.Chdir(dir)
var opFile string
opOK := func(f string) error {
@@ -166,8 +166,8 @@ func (f *testFile) Close() error {
var _ osFile = &testFile{}
func TestErrors(t *testing.T) {
dir := testlib.MustTempDir(t)
defer testlib.RemoveIfOk(t, dir)
dir := t.ArtifactDir()
t.Chdir(dir)
oldCreateTemp := createTemp
defer func() { createTemp = oldCreateTemp }()
@@ -248,8 +248,8 @@ func checkNotExists(t *testing.T, fname string) {
func TestFsyncFileOp(t *testing.T) {
// Check it as an operation on a normal file.
dir := testlib.MustTempDir(t)
defer testlib.RemoveIfOk(t, dir)
dir := t.ArtifactDir()
t.Chdir(dir)
content := []byte("content 1")
err := testWriteFile("file1", content, 0660, FsyncFileOp)