exit.mx raw

   1  // Copyright 2020 The Go Authors. All rights reserved.
   2  // Use of this source code is governed by a BSD-style
   3  // license that can be found in the LICENSE file.
   4  
   5  package testlog
   6  
   7  import (
   8  	"sync"
   9  	_ "unsafe" // for linkname
  10  )
  11  
  12  // PanicOnExit0 reports whether to panic on a call to os.Exit(0).
  13  // This is in the testlog package because, like other definitions in
  14  // package testlog, it is a hook between the testing package and the
  15  // os package. This is used to ensure that an early call to os.Exit(0)
  16  // does not cause a test to pass.
  17  func PanicOnExit0() bool {
  18  	panicOnExit0.mu.Lock()
  19  	defer panicOnExit0.mu.Unlock()
  20  	return panicOnExit0.val
  21  }
  22  
  23  // panicOnExit0 is the flag used for PanicOnExit0. This uses a lock
  24  // because the value can be cleared via a timer call that may race
  25  // with calls to os.Exit
  26  var panicOnExit0 struct {
  27  	mu  sync.Mutex
  28  	val bool
  29  }
  30  
  31  // SetPanicOnExit0 sets panicOnExit0 to v.
  32  //
  33  // SetPanicOnExit0 should be an internal detail,
  34  // but alternate implementations of go test in other
  35  // build systems may need to access it using linkname.
  36  //
  37  // Do not remove or change the type signature.
  38  // See go.dev/issue/67401.
  39  //
  40  //go:linkname SetPanicOnExit0
  41  func SetPanicOnExit0(v bool) {
  42  	panicOnExit0.mu.Lock()
  43  	defer panicOnExit0.mu.Unlock()
  44  	panicOnExit0.val = v
  45  }
  46