github.com/msoap/go-carpet@v1.10.1-0.20240316220419-b690da179708/go-carpet_test.go (about)

     1  package main
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/mgutz/ansi"
     9  	"golang.org/x/tools/cover"
    10  )
    11  
    12  func assertDontPanic(t *testing.T, fn func(), name string) {
    13  	defer func() {
    14  		if recoverInfo := recover(); recoverInfo != nil {
    15  			t.Errorf("The code panic: %s\npanic: %s", name, recoverInfo)
    16  		}
    17  	}()
    18  	fn()
    19  }
    20  
    21  // usage:
    22  //
    23  //	defer testChdir(t, "/path")()
    24  func testChdir(t *testing.T, dir string) func() {
    25  	cwd, err := os.Getwd()
    26  	if err != nil {
    27  		t.Fatal(err)
    28  	}
    29  
    30  	if err := os.Chdir(dir); err != nil {
    31  		t.Fatal(err)
    32  	}
    33  
    34  	return func() {
    35  		err := os.Chdir(cwd)
    36  		if err != nil {
    37  			t.Fatal(err)
    38  		}
    39  	}
    40  }
    41  
    42  func Test_readFile(t *testing.T) {
    43  	file, err := readFile("go-carpet_test.go")
    44  	if err != nil {
    45  		t.Errorf("readFile(): got error: %s", err)
    46  	}
    47  	if len(file) == 0 {
    48  		t.Errorf("readFile(): file empty")
    49  	}
    50  	if string(file[:12]) != "package main" {
    51  		t.Errorf("readFile(): failed read first line")
    52  	}
    53  
    54  	_, err = readFile("dont exists file")
    55  	if err == nil {
    56  		t.Errorf("File exists error:")
    57  	}
    58  }
    59  
    60  func Test_getDirsWithTests(t *testing.T) {
    61  	dirs, err := getDirsWithTests(false, ".")
    62  	if len(dirs) == 0 || err != nil {
    63  		t.Errorf("getDirsWithTests(): dir list is empty")
    64  	}
    65  	dirs, err = getDirsWithTests(false)
    66  	if len(dirs) == 0 || err != nil {
    67  		t.Errorf("getDirsWithTests(): dir list is empty")
    68  	}
    69  	dirs, err = getDirsWithTests(false, ".", ".")
    70  	if len(dirs) != 1 || err != nil {
    71  		t.Errorf("getDirsWithTests(): the same directory failed")
    72  	}
    73  
    74  	defer testChdir(t, "./testdata")()
    75  	dirs, err = getDirsWithTests(false, ".")
    76  	if len(dirs) != 1 || err != nil {
    77  		t.Errorf("getDirsWithTests(): without vendor dirs")
    78  	}
    79  
    80  	dirs, err = getDirsWithTests(true, ".")
    81  	if len(dirs) != 4 || err != nil {
    82  		t.Errorf("getDirsWithTests(): with vendor dirs")
    83  	}
    84  }
    85  
    86  func Test_getTempFileName(t *testing.T) {
    87  	tmpFileName, err := getTempFileName()
    88  	if err != nil {
    89  		t.Errorf("getTempFileName() got error")
    90  	}
    91  	defer func() {
    92  		err = os.RemoveAll(tmpFileName)
    93  		if err != nil {
    94  			t.Errorf("getTempFileName() RemoveAll failed")
    95  		}
    96  	}()
    97  
    98  	if len(tmpFileName) == 0 {
    99  		t.Errorf("getTempFileName() failed")
   100  	}
   101  
   102  	// on RO-dir
   103  	defer testChdir(t, "/")()
   104  	_, err = getTempFileName()
   105  	if err == nil {
   106  		t.Errorf("getTempFileName() not got error")
   107  	}
   108  }
   109  
   110  func Test_getShadeOfGreen(t *testing.T) {
   111  	testData := []struct {
   112  		normCover float64
   113  		result    string
   114  	}{
   115  		{
   116  			normCover: 0,
   117  			result:    "29",
   118  		},
   119  		{
   120  			normCover: 1,
   121  			result:    "51",
   122  		},
   123  		{
   124  			normCover: 0.99999,
   125  			result:    "51",
   126  		},
   127  		{
   128  			normCover: 0.5,
   129  			result:    "40",
   130  		},
   131  		{
   132  			normCover: -1,
   133  			result:    "29",
   134  		},
   135  		{
   136  			normCover: 11,
   137  			result:    "51",
   138  		},
   139  		{
   140  			normCover: 100500,
   141  			result:    "51",
   142  		},
   143  	}
   144  
   145  	for i, item := range testData {
   146  		result := getShadeOfGreen(item.normCover)
   147  		if result != item.result {
   148  			t.Errorf("\n%d.\nexpected: %v\nreal    : %v", i, item.result, result)
   149  		}
   150  	}
   151  }
   152  
   153  func Test_getColorWriter(t *testing.T) {
   154  	assertDontPanic(t, func() { getColorWriter() }, "getColorWriter()")
   155  }
   156  
   157  func Test_getColorHeader(t *testing.T) {
   158  	result := getColorHeader("filename.go", true)
   159  	expected := ansi.ColorCode("yellow") + "filename.go" + ansi.ColorCode("reset") + "\n" +
   160  		ansi.ColorCode("black+h") + "~~~~~~~~~~~" + ansi.ColorCode("reset") + "\n"
   161  
   162  	if result != expected {
   163  		t.Errorf("1. getColorHeader() failed")
   164  	}
   165  
   166  	result = getColorHeader("filename.go", false)
   167  	expected = ansi.ColorCode("yellow") + "filename.go" + ansi.ColorCode("reset") + "\n"
   168  
   169  	if result != expected {
   170  		t.Errorf("2. getColorHeader() failed")
   171  	}
   172  }
   173  
   174  func Test_getCoverForFile(t *testing.T) {
   175  	fileProfile := &cover.Profile{
   176  		FileName: "filename.go",
   177  		Mode:     "count",
   178  		Blocks: []cover.ProfileBlock{
   179  			{
   180  				StartLine: 2,
   181  				StartCol:  5,
   182  				EndLine:   2,
   183  				EndCol:    10,
   184  				NumStmt:   1,
   185  				Count:     1,
   186  			},
   187  		},
   188  	}
   189  	fileContent := []byte("1 line\n123 green 456\n3 line red and other")
   190  
   191  	coloredBytes := getCoverForFile(fileProfile, fileContent, Config{colors256: false})
   192  	expectOut := getColorHeader("filename.go - 100.0%", true) +
   193  		"1 line\n" +
   194  		"123 " + ansi.ColorCode("green") + "green" + ansi.ColorCode("reset") + " 456\n" +
   195  		"3 line red and other\n"
   196  	if string(coloredBytes) != expectOut {
   197  		t.Errorf("1. getCoverForFile() failed")
   198  	}
   199  
   200  	// with red blocks
   201  	fileProfile.Blocks = append(fileProfile.Blocks,
   202  		cover.ProfileBlock{
   203  			StartLine: 3,
   204  			StartCol:  8,
   205  			EndLine:   3,
   206  			EndCol:    11,
   207  			NumStmt:   0,
   208  			Count:     0,
   209  		},
   210  	)
   211  	coloredBytes = getCoverForFile(fileProfile, fileContent, Config{colors256: false})
   212  	expectOut = getColorHeader("filename.go - 100.0%", true) +
   213  		"1 line\n" +
   214  		"123 " + ansi.ColorCode("green") + "green" + ansi.ColorCode("reset") + " 456\n" +
   215  		"3 line " + ansi.ColorCode("red") + "red" + ansi.ColorCode("reset") + " and other\n"
   216  	if string(coloredBytes) != expectOut {
   217  		t.Errorf("2. getCoverForFile() failed")
   218  	}
   219  
   220  	// 256 colors
   221  	coloredBytes = getCoverForFile(fileProfile, fileContent, Config{colors256: true})
   222  	expectOut = getColorHeader("filename.go - 100.0%", true) +
   223  		"1 line\n" +
   224  		"123 " + ansi.ColorCode("48") + "green" + ansi.ColorCode("reset") + " 456\n" +
   225  		"3 line " + ansi.ColorCode("red") + "red" + ansi.ColorCode("reset") + " and other\n"
   226  	if string(coloredBytes) != expectOut {
   227  		t.Errorf("3. getCoverForFile() failed")
   228  	}
   229  
   230  	coloredBytes = getCoverForFile(fileProfile, fileContent, Config{summary: true})
   231  	expectOut = "filename.go - 100.0%\n"
   232  	if string(coloredBytes) != expectOut {
   233  		t.Errorf("4. getCoverForFile() failed; got:\n%s\nwant:\n%s", coloredBytes, expectOut)
   234  	}
   235  }
   236  
   237  func Test_runGoTest(t *testing.T) {
   238  	err := runGoTest("./not exists dir", "", []string{}, true)
   239  	if err == nil {
   240  		t.Errorf("runGoTest() error failed")
   241  	}
   242  }
   243  
   244  func Test_guessAbsPathInGOPATH(t *testing.T) {
   245  	GOPATH := ""
   246  	absPath, err := guessAbsPathInGOPATH(GOPATH, "file.golang")
   247  	if absPath != "" || err == nil {
   248  		t.Errorf("1. guessAbsPathInGOPATH() empty GOPATH")
   249  	}
   250  
   251  	cwd, _ := os.Getwd()
   252  
   253  	GOPATH = filepath.Join(cwd, "testdata")
   254  	absPath, err = guessAbsPathInGOPATH(GOPATH, "file.golang")
   255  	if err != nil {
   256  		t.Errorf("2. guessAbsPathInGOPATH() error: %s", err)
   257  	}
   258  	if absPath != filepath.Join(cwd, "testdata", "src", "file.golang") {
   259  		t.Errorf("3. guessAbsPathInGOPATH() empty GOPATH")
   260  	}
   261  
   262  	GOPATH = filepath.Join(cwd, "testdata") + string(os.PathListSeparator) + "/tmp"
   263  	absPath, err = guessAbsPathInGOPATH(GOPATH, "file.golang")
   264  	if err != nil {
   265  		t.Errorf("4. guessAbsPathInGOPATH() error: %s", err)
   266  	}
   267  	if absPath != filepath.Join(cwd, "testdata", "src", "file.golang") {
   268  		t.Errorf("5. guessAbsPathInGOPATH() empty GOPATH")
   269  	}
   270  
   271  	GOPATH = "/tmp" + string(os.PathListSeparator) + "/"
   272  	absPath, err = guessAbsPathInGOPATH(GOPATH, "file.golang")
   273  	if absPath != "" || err == nil {
   274  		t.Errorf("6. guessAbsPathInGOPATH() file not in GOPATH")
   275  	}
   276  }
   277  
   278  func Test_getStatForProfileBlocks(t *testing.T) {
   279  	profileBlocks := []cover.ProfileBlock{
   280  		{
   281  			StartLine: 2,
   282  			StartCol:  5,
   283  			EndLine:   2,
   284  			EndCol:    10,
   285  			NumStmt:   1,
   286  			Count:     1,
   287  		},
   288  	}
   289  
   290  	stat := getStatForProfileBlocks(profileBlocks)
   291  	if stat != 100.0 {
   292  		t.Errorf("1. getStatForProfileBlocks() failed")
   293  	}
   294  
   295  	profileBlocks = append(profileBlocks,
   296  		cover.ProfileBlock{
   297  			StartLine: 3,
   298  			StartCol:  5,
   299  			EndLine:   3,
   300  			EndCol:    10,
   301  			NumStmt:   1,
   302  			Count:     0,
   303  		},
   304  	)
   305  	stat = getStatForProfileBlocks(profileBlocks)
   306  	if stat != 50.0 {
   307  		t.Errorf("2. getStatForProfileBlocks() failed")
   308  	}
   309  
   310  	profileBlocks = append(profileBlocks,
   311  		cover.ProfileBlock{
   312  			StartLine: 4,
   313  			StartCol:  5,
   314  			EndLine:   4,
   315  			EndCol:    10,
   316  			NumStmt:   1,
   317  			Count:     0,
   318  		},
   319  		cover.ProfileBlock{
   320  			StartLine: 4,
   321  			StartCol:  5,
   322  			EndLine:   4,
   323  			EndCol:    10,
   324  			NumStmt:   1,
   325  			Count:     0,
   326  		},
   327  	)
   328  	stat = getStatForProfileBlocks(profileBlocks)
   329  	if stat != 25.0 {
   330  		t.Errorf("3. getStatForProfileBlocks() failed")
   331  	}
   332  }