github.com/opencontainers/umoci@v0.4.8-0.20240508124516-656e4836fb0d/cmd/umoci/main_test.go (about)

     1  /*
     2   * umoci: Umoci Modifies Open Containers' Images
     3   * Copyright (C) 2016-2020 SUSE LLC
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *    http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package main
    19  
    20  import (
    21  	"fmt"
    22  	"os"
    23  	"strings"
    24  	"testing"
    25  )
    26  
    27  // TODO: Replace all of this with "go build -cover".
    28  
    29  // Build:
    30  //  $ go test -c -covermode=count -o umoci \
    31  //            -cover -coverpkg=github.com/opencontainers/umoci/... \
    32  //            github.com/opencontainers/umoci/cmd/umoci
    33  // Run:
    34  //  $ ./umoci __DEVEL--i-heard-you-like-tests -test.coverprofile [file] [args]...
    35  
    36  // TestUmoci is a hack that allows us to figure out what the coverage is during
    37  // integration tests. I would not recommend that you use a binary built using
    38  // this hack outside of a test suite.
    39  func TestUmoci(t *testing.T) {
    40  	var (
    41  		args []string
    42  		run  bool
    43  	)
    44  
    45  	for _, arg := range os.Args {
    46  		switch {
    47  		case arg == "__DEVEL--i-heard-you-like-tests":
    48  			run = true
    49  		case strings.HasPrefix(arg, "-test"):
    50  		case strings.HasPrefix(arg, "__DEVEL"):
    51  		default:
    52  			args = append(args, arg)
    53  		}
    54  	}
    55  
    56  	if run {
    57  		if err := Main(args); err != nil {
    58  			// Output to stderr rather than the test log so that the
    59  			// integration tests can properly handle cleaning up the output.
    60  			fmt.Fprintf(os.Stderr, "%v\n", err)
    61  			t.Fail()
    62  		}
    63  	}
    64  
    65  	// Before returning, we change stdout to /dev/null because "go test"
    66  	// binaries will output information to stdout that interferes with our bats
    67  	// tests (namely the PASS/FAIL line as well as the coverage information).
    68  	//
    69  	// Unfortunately there appears to be no way to block the "--- FAIL:" text
    70  	// in case of an error...
    71  	os.Stdout, _ = os.Create(os.DevNull)
    72  }