summaryrefslogtreecommitdiff
path: root/run.go
blob: 0447fca0b3704569de54267ef3c54e2d3d4bda02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// xts-run is an alternative xts-run for use with the
// X Test Suite – http://cgit.freedesktop.org/xorg/test/xts
//
// Differences from the upstream xts-run:
//
// - Only long options are supported
//
// - Creates config file in results directory (instead of $TMPDIR)
//
// - Adds ability to run a single (or multiple) sub-test(s) (similar to run_assert)
//
// - Avoids shelling out to xts-config (and therefore xset/xdpyinfo) (faster remote startup)
//
// - Allows the user to configure the stripped font path for remote servers
package main

import (
	"flag"
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"syscall"
	"time"
)

var config = flag.String("config", "", "Location of xts configuration file")
var output = flag.String("output", "xts-results", "Output directory to use")
var version = flag.Bool("version", false, "Display version number and exit")
var extconfig = flag.Bool("extconfig", false, "Use external xts-config")

func env(evar string, def string) string {
	rv := os.Getenv(evar)
	if rv == "" {
		rv = def
	}
	return rv
}

func createConfigExt(outdir string) error {
	perl := env("PERL", "perl")
	tetRoot := env("TET_ROOT", "/usr/local/share")
	config_in := filepath.Join(tetRoot, "xts5", "tetexec.cfg.in")
	xtsConfig := os.Getenv("XTS_CONFIG")
	if xtsConfig == "" {
		var err error
		xtsConfig, err = exec.LookPath("xts-config")
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(1)
		}
	}

	in, err := os.Open(config_in)
	if err != nil {
		return err
	}
	defer in.Close()
	out, err := os.Create(*config)
	if err != nil {
		return err
	}
	defer out.Close()

	cmd := exec.Command(perl, xtsConfig)
	cmd.Stdin = in
	cmd.Stdout = out
	cmd.Stderr = os.Stderr
	return cmd.Run()
}

func tcc(args []string) (int, error) {
	tcc := env("TCC", "tcc")
	cmd := exec.Command(tcc, args...)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	err := cmd.Run()
	ret := 0
	if err != nil {
		if ee, ok := err.(*exec.ExitError); ok {
			if state, ok := ee.ProcessState.Sys().(syscall.WaitStatus); ok {
				ret = state.ExitStatus()
				err = nil
			}
		}
	}
	return ret, err
}

func main() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage: %s [OPTION]... [SCENARIO]\n", os.Args[0])
		flag.PrintDefaults()
	}
	flag.Parse()
	if *version {
		fmt.Println("xts-run alternative version 0.0.3")
		return
	}

	// Create the output directory
	outdir := filepath.Join(*output, time.Now().Format("2006-01-02-15:04:05"))
	err := os.MkdirAll(outdir, 0777)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	// Create the config file if necessary
	if *config == "" {
		*config = filepath.Join(outdir, "tetexec.cfg")
		var err error
		if *extconfig {
			err = createConfigExt(outdir)
		} else {
			err = createConfig(outdir)
		}
		if err != nil {
			fmt.Fprintln(os.Stdout, err)
			os.Exit(1)
		}
	} else {
		if fi, err := os.Stat(*config); err != nil || fi.IsDir() {
			fmt.Fprintln(os.Stderr, "error: cannot read config file "+*config)
			if err != nil {
				fmt.Fprintln(os.Stderr, err)
			}
			os.Exit(1)
		}
	}

	// run the tests
	ret := 0
	if flag.NArg() > 1 {
		ret, err = runAssert(outdir)
	} else {
		args := []string{"-e", "-i", outdir, "-x", *config, "xts5"}
		if flag.NArg() >= 1 {
			// Scenario
			args = append(args, flag.Arg(0))
		}
		ret, err = tcc(args)
	}
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}

	// generate a short report
	xtsReport := env("XTS_REPORT", "xts-report")
	out, err := os.Create(filepath.Join(outdir, "summary"))
	if err != nil {
		fmt.Fprintln(os.Stdout, err)
		os.Exit(1)
	}
	defer out.Close()
	cmd := exec.Command(xtsReport, "-d2", "-f", filepath.Join(outdir, "journal"))
	cmd.Stderr = os.Stderr
	cmd.Stdout = out
	err = cmd.Run()
	if err != nil {
		fmt.Fprintln(os.Stdout, err)
		os.Exit(1)
	}

	// return the tcc exit code
	os.Exit(ret)
}