github.com/thediveo/gons@v0.9.9/reexec/internal/testsupport/testingargs.go (about)

     1  // Copyright 2020 Harald Albrecht.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package testsupport
    16  
    17  import (
    18  	"fmt"
    19  )
    20  
    21  // TestingEnabled is set to true when we're under testing; gathering coverage
    22  // profile data might be enabled.
    23  var TestingEnabled = false
    24  
    25  // CoverageOutputDir is the directory in which to create profile files and the
    26  // like. When run from "go test", our binary always runs in the source
    27  // directory for the package under test. The CLI argument "-test.outputdir"
    28  // corresponding with this variable lets "go test" tell our binary to write
    29  // the files in the directory where the "go test" command is run.
    30  var CoverageOutputDir = ""
    31  
    32  // CoverageProfile is the name of a coverage profile data file; if empty, then
    33  // no coverage profile is to be saved. This variable corresponds with the
    34  // "-test.coverprofile" CLI argument.
    35  var CoverageProfile = ""
    36  
    37  // EnableTesting is a module-internal function used by the gons/reexec/testing
    38  // (sub) package; it tells this reexec package when we're in testing mode, and
    39  // also passes coverage profiling-related test parameters to us. We need these
    40  // parameters when re-executing child processes and in order to allocate
    41  // coverage profile data files to these children.
    42  func EnableTesting(outputdir, coverprofile string) {
    43  	TestingEnabled = true
    44  	CoverageOutputDir = outputdir
    45  	CoverageProfile = coverprofile
    46  }
    47  
    48  // CoverageProfiles is a list of coverage profile data filenames created by
    49  // re-executed child processes when under test.
    50  var CoverageProfiles = []string{}
    51  
    52  // TestingArgs returns additional testing arguments while under test;
    53  // otherwise it returns an empty slice of arguments.
    54  func TestingArgs() []string {
    55  	testargs := []string{}
    56  	if TestingEnabled {
    57  		if CoverageProfile != "" {
    58  			name := CoverageProfile +
    59  				fmt.Sprintf("_%d", len(CoverageProfiles))
    60  			CoverageProfiles = append(CoverageProfiles, name)
    61  			if CoverageProfile != "" {
    62  				testargs = append(testargs,
    63  					"-test.coverprofile="+name)
    64  			}
    65  			if CoverageOutputDir != "" {
    66  				testargs = append(testargs,
    67  					"-test.outputdir="+CoverageOutputDir)
    68  			}
    69  		}
    70  		// Let's suppose for a moment that no sane developer will ever use the
    71  		// following name for one of her/his tests ... except for "THEM" :p
    72  		testargs = append(testargs,
    73  			"-test.run=nadazilchnixdairgendwoimnirvanavonbielefeld",
    74  		)
    75  	}
    76  	return testargs
    77  }