github.com/msoap/go-carpet@v1.10.1-0.20240316220419-b690da179708/unix_only_test.go (about) 1 //go:build !windows 2 // +build !windows 3 4 package main 5 6 import ( 7 "reflect" 8 "testing" 9 ) 10 11 func Test_getCoverForDir(t *testing.T) { 12 t.Run("error", func(t *testing.T) { 13 _, _, err := getCoverForDir("./testdata/not_exists.out", []string{}, Config{colors256: false}) 14 if err == nil { 15 t.Errorf("1. getCoverForDir() error failed") 16 } 17 }) 18 19 t.Run("cover", func(t *testing.T) { 20 bytes, _, err := getCoverForDir("./testdata/cover_00.out", []string{}, Config{colors256: false}) 21 if err != nil { 22 t.Errorf("2. getCoverForDir() failed: %v", err) 23 } 24 expect, err := readFile("./testdata/colored_00.txt") 25 if err != nil { 26 t.Errorf("3. getCoverForDir() failed: %v", err) 27 } 28 if !reflect.DeepEqual(bytes, expect) { 29 t.Errorf("4. getCoverForDir() not equal") 30 } 31 }) 32 33 t.Run("cover with 256 colors", func(t *testing.T) { 34 bytes, _, err := getCoverForDir("./testdata/cover_00.out", []string{}, Config{colors256: true}) 35 if err != nil { 36 t.Errorf("5. getCoverForDir() failed: %v", err) 37 } 38 expect, err := readFile("./testdata/colored_01.txt") 39 if err != nil { 40 t.Errorf("6. getCoverForDir() failed: %v", err) 41 } 42 if !reflect.DeepEqual(bytes, expect) { 43 t.Errorf("7. getCoverForDir() not equal") 44 } 45 }) 46 47 t.Run("cover with 256 colors with error", func(t *testing.T) { 48 _, _, err := getCoverForDir("./testdata/cover_01.out", []string{}, Config{colors256: true}) 49 if err == nil { 50 t.Errorf("8. getCoverForDir() not exists go file") 51 } 52 }) 53 54 t.Run("cover 01 without 256 colors", func(t *testing.T) { 55 bytes, _, err := getCoverForDir("./testdata/cover_00.out", []string{"file_01.go"}, Config{colors256: false}) 56 if err != nil { 57 t.Errorf("9. getCoverForDir() failed: %v", err) 58 } 59 expect, err := readFile("./testdata/colored_02.txt") 60 if err != nil { 61 t.Errorf("10. getCoverForDir() failed: %v", err) 62 } 63 if !reflect.DeepEqual(bytes, expect) { 64 t.Errorf("11. getCoverForDir() not equal") 65 } 66 }) 67 68 t.Run("cover 02 without 256 colors", func(t *testing.T) { 69 bytes, _, err := getCoverForDir("./testdata/cover_02.out", []string{}, Config{colors256: false}) 70 if err != nil { 71 t.Errorf("12. getCoverForDir() failed: %v", err) 72 } 73 expect, err := readFile("./testdata/colored_03.txt") 74 if err != nil { 75 t.Errorf("13. getCoverForDir() failed: %v", err) 76 } 77 if !reflect.DeepEqual(bytes, expect) { 78 t.Errorf("14. getCoverForDir() not equal\ngot:\n%s\nexpect:\n%s\n", bytes, expect) 79 } 80 }) 81 } 82 83 func Test_getCoverForDir_mincov_flag(t *testing.T) { 84 t.Run("covered 100% mincov 100%", func(t *testing.T) { 85 conf := Config{ 86 colors256: false, 87 minCoverage: 100.0, 88 } 89 90 // cover_00.out has 100% coverage of 2 files 91 _, profileBlocks, err := getCoverForDir("./testdata/cover_00.out", []string{"file_01.go"}, conf) 92 if err != nil { 93 t.Errorf("getCoverForDir() failed with error: %s", err) 94 } 95 96 expectLen := 2 97 actualLen := len(profileBlocks) 98 99 if expectLen != actualLen { 100 t.Errorf("1. minimum coverage 100%% should print all the blocks. want %v, got: %v", expectLen, actualLen) 101 } 102 }) 103 104 t.Run("covered 100% mincov 50%", func(t *testing.T) { 105 conf := Config{ 106 colors256: false, 107 minCoverage: 50.0, 108 } 109 110 // cover_00.out has 100% coverage of 2 files 111 _, profileBlocks, err := getCoverForDir("./testdata/cover_00.out", []string{"file_01.go"}, conf) 112 if err != nil { 113 t.Errorf("getCoverForDir() failed with error: %s", err) 114 } 115 116 expectLen := 0 117 actualLen := len(profileBlocks) 118 119 if expectLen != actualLen { 120 t.Errorf("2. minimum coverage 50%% for 100%% covered source should print nothing. want %v, got: %v", expectLen, actualLen) 121 } 122 }) 123 }