github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/ci/test/test.go (about)

     1  /*
     2   * Copyright (C) 2019 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package test
    19  
    20  import (
    21  	"strings"
    22  
    23  	"github.com/magefile/mage/sh"
    24  )
    25  
    26  // TestWithCoverage runs unit tests with coverage report
    27  func TestWithCoverage() error {
    28  	packages, err := unitTestPackages()
    29  	if err != nil {
    30  		return err
    31  	}
    32  	args := append([]string{"test", "-race", "-timeout", "5m", "-cover", "-coverprofile", "coverage.txt", "-covermode", "atomic"}, packages...)
    33  
    34  	env := make(map[string]string)
    35  	env["GORACE"] = "halt_on_error=1"
    36  	return sh.RunWith(env, "go", args...)
    37  }
    38  
    39  // Test runs unit tests
    40  func Test() error {
    41  	packages, err := unitTestPackages()
    42  	if err != nil {
    43  		return err
    44  	}
    45  	args := append([]string{"test", "-race", "-timeout", "5m"}, packages...)
    46  
    47  	env := make(map[string]string)
    48  	env["GORACE"] = "halt_on_error=1"
    49  	return sh.RunWith(env, "go", args...)
    50  }
    51  
    52  func unitTestPackages() ([]string, error) {
    53  	allPackages, err := listPackages()
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  	packages := make([]string, 0)
    58  	for _, p := range allPackages {
    59  		if !strings.Contains(p, "e2e") {
    60  			packages = append(packages, p)
    61  		}
    62  	}
    63  	return packages, nil
    64  }
    65  
    66  func listPackages() ([]string, error) {
    67  	output, err := sh.Output("go", "list", "./...")
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  	return strings.Split(strings.Replace(output, "\r\n", "\n", -1), "\n"), nil
    72  }