github.com/blend/go-sdk@v1.20220411.3/cmd/coverage/main_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package main
     9  
    10  import (
    11  	"testing"
    12  
    13  	"github.com/blend/go-sdk/assert"
    14  )
    15  
    16  type coverProfileTestCase struct {
    17  	BaseDir  string
    18  	FileName string
    19  	Expected string
    20  }
    21  
    22  func TestGlob(t *testing.T) {
    23  	assert := assert.New(t)
    24  
    25  	assert.True(glob("", ""))
    26  	assert.True(glob("*", "asdf"))
    27  	assert.True(glob("*/testo/*", "asdf/testo/blah"))
    28  	assert.True(glob("*/*", "asdf/testo"))
    29  	assert.False(glob("*/testo/*", "asdf"))
    30  	assert.False(glob("*/*/*/testo", "asdf/testo"))
    31  	assert.True(glob("*/*/*/testo", "asdf/x/x/testo"))
    32  }
    33  
    34  func TestJoinCoverPath(t *testing.T) {
    35  	testCases := []coverProfileTestCase{
    36  		{
    37  			"/",
    38  			"foo/bar/baz.go",
    39  			"/foo/bar/baz.go",
    40  		},
    41  		{
    42  			"/users/foo/bar",
    43  			"foo/bar/baz.go",
    44  			"/users/foo/bar/baz.go",
    45  		},
    46  		{
    47  			"/users/example-string/workspace/go/src/github.com/blend/go-sdk/",
    48  			"github.com/blend/go-sdk/assert/assert.go",
    49  			"/users/example-string/workspace/go/src/github.com/blend/go-sdk/assert/assert.go",
    50  		},
    51  		{
    52  			"/go/src/git.blendlabs.com/blend/fees",
    53  			"git.blendlabs.com/blend/fees/pkg/fees/fees.go",
    54  			"/go/src/git.blendlabs.com/blend/fees/pkg/fees/fees.go",
    55  		},
    56  	}
    57  
    58  	var actual string
    59  	for _, testCase := range testCases {
    60  		actual = joinCoverPath(testCase.BaseDir, testCase.FileName)
    61  		if actual != testCase.Expected {
    62  			t.Errorf("%s does not match %s", actual, testCase.Expected)
    63  		}
    64  	}
    65  }
    66  
    67  func TestPackageFilename(t *testing.T) {
    68  	assert := assert.New(t)
    69  
    70  	assert.Equal("testo/asdf", packageFilename("testo", "asdf"))
    71  }
    72  
    73  func TestEnforceCoverage(t *testing.T) {
    74  	assert := assert.New(t)
    75  
    76  	// errors cases
    77  	assert.NotNil(enforceCoverage("./", "asdf"))
    78  	assert.NotNil(enforceCoverage("/usr/lib", "50"))
    79  
    80  	assert.Nil(writeCoverage("/tmp", ""))
    81  	assert.NotNil(enforceCoverage("/tmp", "90"))
    82  
    83  	assert.Nil(writeCoverage("/tmp", "90"))
    84  	assert.NotNil(enforceCoverage("/tmp", "70"))
    85  
    86  	assert.Nil(writeCoverage("/tmp", "0"))
    87  	assert.Nil(enforceCoverage("/tmp", "0"))
    88  
    89  	assert.Nil(writeCoverage("/tmp", "70"))
    90  	assert.Nil(enforceCoverage("/tmp", "90"))
    91  }
    92  
    93  func TestExtractCoverage(t *testing.T) {
    94  	assert := assert.New(t)
    95  
    96  	assert.Equal("0", extractCoverage(""))
    97  	assert.Equal("50", extractCoverage("coverage: 50% of statements"))
    98  }
    99  
   100  func TestParseCoverage(t *testing.T) {
   101  	assert := assert.New(t)
   102  
   103  	assert.Equal(94, parseCoverage("94%"))
   104  	assert.Equal(94, parseCoverage("94"))
   105  }
   106  
   107  func TestColorCoverage(t *testing.T) {
   108  	assert := assert.New(t)
   109  
   110  	assert.Equal("\x1b[32m90.00\x1b[0m", colorCoverage(90))
   111  	assert.Equal("\x1b[33m75.00\x1b[0m", colorCoverage(75))
   112  	assert.Equal("\x1b[31m30.00\x1b[0m", colorCoverage(30))
   113  	assert.Equal("\x1b[90m0.00\x1b[0m", colorCoverage(0))
   114  }
   115  
   116  func TestFormatCoverage(t *testing.T) {
   117  	assert := assert.New(t)
   118  
   119  	assert.Equal("75.00", formatCoverage(75))
   120  }