Skip to content

Commit

Permalink
Added test for getCoverForDir()
Browse files Browse the repository at this point in the history
  • Loading branch information
msoap committed Mar 1, 2016
1 parent 34ee790 commit 0eb5add
Show file tree
Hide file tree
Showing 11 changed files with 187 additions and 5 deletions.
7 changes: 2 additions & 5 deletions go-carpet.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,12 @@ func getCoverForDir(path string, coverFileName string, filesFilter []string, col
for _, fileProfile := range coverProfile {
fileName := ""
if strings.HasPrefix(fileProfile.FileName, "_") {
// absolute path
// absolute path (or relative in tests)
fileName = strings.TrimLeft(fileProfile.FileName, "_")
} else {
// file in GOPATH
fileName = os.Getenv("GOPATH") + "/src/" + fileProfile.FileName
}
if _, err := os.Stat(fileName); os.IsNotExist(err) {
fmt.Printf("File '%s' is not exists\n", fileName)
continue
}

if len(filesFilter) > 0 && !isSliceInString(fileName, filesFilter) {
continue
Expand Down Expand Up @@ -235,6 +231,7 @@ func main() {
coverInBytes, err := getCoverForDir(path, coverFileName, strings.Split(filesFilter, ","), colors256)
if err != nil {
log.Print(err)
continue
}
stdOut.Write(coverInBytes)
}
Expand Down
66 changes: 66 additions & 0 deletions go-carpet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"os"
"reflect"
"testing"

"github.com/mgutz/ansi"
Expand Down Expand Up @@ -164,6 +165,71 @@ func Test_getColorHeader(t *testing.T) {
}
}

func Test_getCoverForDir(t *testing.T) {
bytes, err := getCoverForDir("./test_data", "./test_data/not_exists.out", []string{}, false)
if err == nil {
t.Errorf("1. getCoverForDir() error failed")
}

// ---
bytes, err = getCoverForDir("./test_data", "./test_data/cover_00.out", []string{}, false)
if err != nil {
t.Errorf("2. getCoverForDir() failed")
}
expect, err := readFile("./test_data/colored_00.txt")
if err != nil {
t.Errorf("3. getCoverForDir() failed")
}
if !reflect.DeepEqual(bytes, expect) {
t.Errorf("4. getCoverForDir() not equal")
}

// ---
bytes, err = getCoverForDir("./test_data", "./test_data/cover_00.out", []string{}, true)
if err != nil {
t.Errorf("5. getCoverForDir() failed")
}
expect, err = readFile("./test_data/colored_01.txt")
if err != nil {
t.Errorf("6. getCoverForDir() failed")
}
if !reflect.DeepEqual(bytes, expect) {
t.Errorf("7. getCoverForDir() not equal")
}

// ---
bytes, err = getCoverForDir("./test_data", "./test_data/cover_01.out", []string{}, true)
if err == nil {
t.Errorf("8. getCoverForDir() not exists go file")
}

// ---
bytes, err = getCoverForDir("./test_data", "./test_data/cover_00.out", []string{"file_01.go"}, false)
if err != nil {
t.Errorf("9. getCoverForDir() failed")
}
expect, err = readFile("./test_data/colored_02.txt")
if err != nil {
t.Errorf("10. getCoverForDir() failed")
}
if !reflect.DeepEqual(bytes, expect) {
t.Errorf("11. getCoverForDir() not equal")
}

// ---
bytes, err = getCoverForDir("./test_data", "./test_data/cover_02.out", []string{}, false)
if err != nil {
t.Errorf("12. getCoverForDir() failed")
}
expect, err = readFile("./test_data/colored_03.txt")
if err != nil {
t.Errorf("13. getCoverForDir() failed")
}
if !reflect.DeepEqual(bytes, expect) {
t.Errorf("14. getCoverForDir() not equal")
}
}

func Test_getCoverForFile(t *testing.T) {
fileProfile := &cover.Profile{
FileName: "filename.go",
Expand Down
29 changes: 29 additions & 0 deletions test_data/colored_00.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
./test_data/file_00.golang
~~~~~~~~~~~~~~~~~~~~~~~~~~
package main

func readFile(fileName string) (result []byte, err error) {
fileReader, err := os.Open(fileName)
if err != nil {
return result, err
}
defer fileReader.Close()

result, err = ioutil.ReadAll(fileReader)
return result, err
}

./test_data/file_01.golang
~~~~~~~~~~~~~~~~~~~~~~~~~~
package main

// isStringInSlice - one of the elements of the array contained in the string
func isSliceInString(src string, slice []string) bool {
for _, dst := range slice {
 if strings.Contains(src, dst) {
 return true
 }
 }
return false
}

29 changes: 29 additions & 0 deletions test_data/colored_01.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
./test_data/file_00.golang
~~~~~~~~~~~~~~~~~~~~~~~~~~
package main

func readFile(fileName string) (result []byte, err error) {
fileReader, err := os.Open(fileName)
if err != nil {
return result, err
}
defer fileReader.Close()

result, err = ioutil.ReadAll(fileReader)
return result, err
}

./test_data/file_01.golang
~~~~~~~~~~~~~~~~~~~~~~~~~~
package main

// isStringInSlice - one of the elements of the array contained in the string
func isSliceInString(src string, slice []string) bool {
for _, dst := range slice {
 if strings.Contains(src, dst) {
 return true
 }
 }
return false
}

14 changes: 14 additions & 0 deletions test_data/colored_02.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
./test_data/file_01.golang
~~~~~~~~~~~~~~~~~~~~~~~~~~
package main

// isStringInSlice - one of the elements of the array contained in the string
func isSliceInString(src string, slice []string) bool {
for _, dst := range slice {
 if strings.Contains(src, dst) {
 return true
 }
 }
return false
}

15 changes: 15 additions & 0 deletions test_data/colored_03.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
github.com/msoap/go-carpet/terminal_unix.go
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// +build !windows

package main

import (
"io"
"os"
)

func getColorWriter() io.Writer {
return (io.Writer)(os.Stdout)
}

5 changes: 5 additions & 0 deletions test_data/cover_00.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mode: count
_./test_data/file_00.golang:4.2,4.38 1 2
_./test_data/file_00.golang:5.2,5.17 0 0
_./test_data/file_01.golang:5.28,9.3 1 1
_./test_data/file_01.golang:10.2,10.14 0 0
2 changes: 2 additions & 0 deletions test_data/cover_01.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mode: count
_./test_data/file_not_exists.golang:4.2,4.38 1 2
2 changes: 2 additions & 0 deletions test_data/cover_02.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mode: count
github.com/msoap/go-carpet/terminal_unix.go:11.2,11.31 1 1
12 changes: 12 additions & 0 deletions test_data/file_00.golang
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

func readFile(fileName string) (result []byte, err error) {
fileReader, err := os.Open(fileName)
if err != nil {
return result, err
}
defer fileReader.Close()

result, err = ioutil.ReadAll(fileReader)
return result, err
}
11 changes: 11 additions & 0 deletions test_data/file_01.golang
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

// isStringInSlice - one of the elements of the array contained in the string
func isSliceInString(src string, slice []string) bool {
for _, dst := range slice {
if strings.Contains(src, dst) {
return true
}
}
return false
}

0 comments on commit 0eb5add

Please sign in to comment.