Source file src/runtime/coverage/coverage.go
1 // Copyright 2022 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 coverage 6 7 import ( 8 "internal/coverage/cfile" 9 "io" 10 ) 11 12 // initHook is invoked from main.init in programs built with -cover. 13 // The call is emitted by the compiler. 14 func initHook(istest bool) { 15 cfile.InitHook(istest) 16 } 17 18 // WriteMetaDir writes a coverage meta-data file for the currently 19 // running program to the directory specified in 'dir'. An error will 20 // be returned if the operation can't be completed successfully (for 21 // example, if the currently running program was not built with 22 // "-cover", or if the directory does not exist). 23 func WriteMetaDir(dir string) error { 24 return cfile.WriteMetaDir(dir) 25 } 26 27 // WriteMeta writes the meta-data content (the payload that would 28 // normally be emitted to a meta-data file) for the currently running 29 // program to the writer 'w'. An error will be returned if the 30 // operation can't be completed successfully (for example, if the 31 // currently running program was not built with "-cover", or if a 32 // write fails). 33 func WriteMeta(w io.Writer) error { 34 return cfile.WriteMeta(w) 35 } 36 37 // WriteCountersDir writes a coverage counter-data file for the 38 // currently running program to the directory specified in 'dir'. An 39 // error will be returned if the operation can't be completed 40 // successfully (for example, if the currently running program was not 41 // built with "-cover", or if the directory does not exist). The 42 // counter data written will be a snapshot taken at the point of the 43 // call. 44 func WriteCountersDir(dir string) error { 45 return cfile.WriteCountersDir(dir) 46 } 47 48 // WriteCounters writes coverage counter-data content for the 49 // currently running program to the writer 'w'. An error will be 50 // returned if the operation can't be completed successfully (for 51 // example, if the currently running program was not built with 52 // "-cover", or if a write fails). The counter data written will be a 53 // snapshot taken at the point of the invocation. 54 func WriteCounters(w io.Writer) error { 55 return cfile.WriteCounters(w) 56 } 57 58 // ClearCounters clears/resets all coverage counter variables in the 59 // currently running program. It returns an error if the program in 60 // question was not built with the "-cover" flag. Clearing of coverage 61 // counters is also not supported for programs not using atomic 62 // counter mode (see more detailed comments below for the rationale 63 // here). 64 func ClearCounters() error { 65 return cfile.ClearCounters() 66 } 67