Ejemplo n.º 1
0
/* output diff */
func checkStringsEqual(expected, actual string) (ok bool, diff string, err error) {
	if expected == actual {
		ok = true
		return
	}

	var exp *os.File
	exp, err = ioutil.TempFile("", "expected")
	if err != nil {
		return
	}
	defer exp.Close()
	defer os.Remove(exp.Name())
	_, err = exp.Write([]byte(expected))
	if err != nil {
		return
	}
	exp.Sync()

	var act *os.File
	act, err = ioutil.TempFile("", "actual")
	if err != nil {
		return
	}
	defer act.Close()
	defer os.Remove(act.Name())
	_, err = act.Write([]byte(actual))
	if err != nil {
		return
	}
	act.Sync()

	// diff's exit status is 1 if the files differ, and Go returns an error
	// when the exit status is non-zero
	cmd := exec.Command("diff", "-u", exp.Name(), act.Name())
	var cmdOutput []byte
	cmdOutput, err = cmd.Output()
	if err != nil {
		var typeOk bool
		var exitErr *exec.ExitError
		exitErr, typeOk = err.(*exec.ExitError)
		if !typeOk {
			return
		}

		var status syscall.WaitStatus
		status, typeOk = exitErr.Sys().(syscall.WaitStatus)
		if !typeOk || status.ExitStatus() > 1 {
			return
		}
		err = nil
	}
	diff = string(cmdOutput)
	return
}
Ejemplo n.º 2
0
Archivo: utils.go Proyecto: 9seconds/ah
// GetStatusCode returns an exit code from exec.ExitError
func GetStatusCode(err *exec.ExitError) int {
	if err == nil {
		return 0
	}

	waitStatus, ok := err.Sys().(syscall.WaitStatus)
	if !ok {
		Logger.Panic("It seems you have an unsupported OS")
	}
	return waitStatus.ExitStatus()
}
Ejemplo n.º 3
0
func exitCode(err *exec.ExitError) int {
	return err.Sys().(syscall.WaitStatus).ExitStatus()
}