gotest.tools/gotestsum@v1.11.0/contrib/notify/notify_linux.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"os"
     7  	"os/exec"
     8  	"strconv"
     9  )
    10  
    11  func main() {
    12  	total := envInt("TOTAL")
    13  	skipped := envInt("SKIPPED")
    14  	failed := envInt("FAILED")
    15  	errors := envInt("ERRORS")
    16  
    17  	icon := "test-pass"
    18  	title := "Passed"
    19  	switch {
    20  	case errors > 0:
    21  		icon = "dialog-warning"
    22  		title = "Errored"
    23  	case failed > 0:
    24  		icon = "test-fail"
    25  		title = "Failed"
    26  	case skipped > 0:
    27  		title = "Passed with skipped"
    28  	}
    29  
    30  	subtitle := fmt.Sprintf("%d Tests Run", total)
    31  	if errors > 0 {
    32  		subtitle += fmt.Sprintf(", %d Errored", errors)
    33  	}
    34  	if failed > 0 {
    35  		subtitle += fmt.Sprintf(", %d Failed", failed)
    36  	}
    37  	if skipped > 0 {
    38  		subtitle += fmt.Sprintf(", %d Skipped", skipped)
    39  	}
    40  
    41  	cmd := exec.Command("notify-send", "--icon", icon, title, subtitle)
    42  	log.Printf("%#v", cmd.Args)
    43  	cmd.Stdout = os.Stdout
    44  	cmd.Stderr = os.Stderr
    45  	if err := cmd.Run(); err != nil {
    46  		log.Fatalf("Failed to exec: %v", err)
    47  	}
    48  }
    49  
    50  func envInt(name string) int {
    51  	val := os.Getenv("TESTS_" + name)
    52  	n, err := strconv.Atoi(val)
    53  	if err != nil {
    54  		return 0
    55  	}
    56  	return n
    57  }