github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/test/count/count.go (about) 1 package count 2 3 import ( 4 "bytes" 5 "fmt" 6 "io" 7 "net/http" 8 "os" 9 "strconv" 10 "strings" 11 "testing" 12 ) 13 14 const ( 15 // Env is the name for the exported envvar we should check to see which count logging method to use 16 Env = "MUREX_TEST_COUNT" 17 18 // Host is the host name for the HTTP count listener 19 Host = "localhost" 20 21 // Port is the port number which the HTTP count is listening on 22 Port = 38000 23 ) 24 25 // Tests a function to count all the unit tests that have been run 26 func Tests(t *testing.T, count int) { 27 //t.Logf("Go version: '%s'", runtime.Version()) 28 //t.Logf("Go arch: '%s'", runtime.GOARCH) 29 //t.Logf("Go OS: '%s'", runtime.GOOS) 30 31 switch strings.ToLower(os.Getenv(Env)) { 32 case "log": 33 t.Logf("%s tests ran: %d", t.Name(), count) 34 35 case "http": 36 httpReq(t, count) 37 38 default: 39 } 40 } 41 42 func httpReq(t *testing.T, count int) { 43 s := strconv.Itoa(count) 44 buf := new(bytes.Buffer) 45 _, err := buf.WriteString(s) 46 if err != nil { 47 t.Errorf("unable to log test count via HTTP (export %s=http): %s", Env, err) 48 return 49 } 50 51 req, err := http.Post(fmt.Sprintf("http://%s:%d/count", Host, Port), "int", buf) 52 if err != nil { 53 t.Errorf("Unable to log test count via HTTP (export %s=http): %s", Env, err) 54 return 55 } 56 57 b, err := io.ReadAll(req.Body) 58 if err != nil { 59 t.Errorf("Potential mismatch logging test counts via HTTP (export %s=http): %s", Env, err) 60 } 61 62 if string(b) != "OK" { 63 t.Errorf("Potential mismatch logging test counts via HTTP (export %s=http): %s", Env, `Body != "OK"`) 64 } 65 }