mirror of
https://blitiri.com.ar/repos/chasquid
synced 2026-10-29 15:02:30 +00:00
8b19760f4e
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.
41 lines
885 B
Go
41 lines
885 B
Go
package testlib
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
func TestRewrite(t *testing.T) {
|
|
fname := t.TempDir() + "/file"
|
|
Rewrite(t, fname, "hola")
|
|
Rewrite(t, fname, "chau")
|
|
|
|
data, err := os.ReadFile(fname)
|
|
if err != nil {
|
|
t.Fatalf("failed to read file: %v", err)
|
|
}
|
|
if string(data) != "chau" {
|
|
t.Errorf("file content: got %q, expected %q", data, "chau")
|
|
}
|
|
}
|
|
|
|
func TestGenerateCert(t *testing.T) {
|
|
conf, err := GenerateCert(t.TempDir())
|
|
if err != nil {
|
|
t.Errorf("GenerateCert returned error: %v", err)
|
|
}
|
|
if conf.ServerName != "localhost" {
|
|
t.Errorf("Config server name %q != localhost", conf.ServerName)
|
|
}
|
|
if conf.RootCAs == nil {
|
|
t.Errorf("Config had an empty RootCAs pool")
|
|
}
|
|
}
|
|
|
|
func TestGenerateCertBadDir(t *testing.T) {
|
|
conf, err := GenerateCert("/doesnotexist/")
|
|
if err == nil || conf != nil {
|
|
t.Fatalf("GenerateCert returned non-error: %v / %v", conf, err)
|
|
}
|
|
}
|