github.com/kjda/gocoverview@v0.0.1/main_test.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"os"
     7  	"os/exec"
     8  	"path/filepath"
     9  	"strconv"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func setup(t *testing.T) string {
    17  	t.Helper()
    18  	tmp, err := ioutil.TempDir("", "")
    19  	if err != nil {
    20  		t.Fatal(err)
    21  	}
    22  	t.Logf("tmp=%s", tmp)
    23  	t.Cleanup(func() {
    24  		os.RemoveAll(tmp)
    25  	})
    26  	if err := cmdRun(tmp, "go", "mod", "init", "example.com/example"); err != nil {
    27  		t.Fatal(err)
    28  	}
    29  
    30  	example := []byte(strings.TrimSpace(`
    31  package example
    32  
    33  func example() bool {
    34  	println("covered")
    35  	if false {
    36  		println("not covered")
    37  	}
    38  	return true
    39  }
    40  `))
    41  	if err := ioutil.WriteFile(filepath.Join(tmp, "example.go"), example, 0644); err != nil {
    42  		t.Fatal(err)
    43  	}
    44  
    45  	exampleTest := []byte(strings.TrimSpace(`
    46  package example
    47  
    48  import "testing"
    49  
    50  func Test_example(t *testing.T) {
    51  	example()
    52  }
    53  `))
    54  	if err := ioutil.WriteFile(filepath.Join(tmp, "example_test.go"), exampleTest, 0644); err != nil {
    55  		t.Fatal(err)
    56  	}
    57  
    58  	return tmp
    59  }
    60  
    61  func chdir(t *testing.T, dir string) {
    62  	t.Helper()
    63  	wd, err := os.Getwd()
    64  	if err != nil {
    65  		t.Fatal(err)
    66  	}
    67  	if err := os.Chdir(dir); err != nil {
    68  		t.Fatal(err)
    69  	}
    70  	t.Cleanup(func() {
    71  		if err := os.Chdir(wd); err != nil {
    72  			t.Fatal(err)
    73  		}
    74  	})
    75  }
    76  
    77  func Test_main(t *testing.T) {
    78  	tmp := setup(t)
    79  	if err := cmdRun(tmp, "go", "test", ".", "-v", "-cover", "-coverprofile", "coverage.txt"); err != nil {
    80  		t.Fatal(err)
    81  	}
    82  	if err := cmdRun(tmp, "git", "init"); err != nil {
    83  		t.Fatal(err)
    84  	}
    85  
    86  	t.Run("simple", func(t *testing.T) {
    87  		want := readFileString(t, "testdata/output.txt")
    88  		chdir(t, tmp)
    89  		var buf bytes.Buffer
    90  		writer = &buf
    91  		t.Cleanup(func() {
    92  			writer = os.Stdout
    93  		})
    94  		main()
    95  		assert.Equal(t, want, strings.TrimSpace(buf.String()))
    96  	})
    97  
    98  	t.Run("json", func(t *testing.T) {
    99  		want := readFileString(t, "testdata/output.json")
   100  		chdir(t, tmp)
   101  		var buf bytes.Buffer
   102  		writer = &buf
   103  		t.Cleanup(func() {
   104  			writer = os.Stdout
   105  		})
   106  		output = "json"
   107  		t.Cleanup(func() { output = "" })
   108  		main()
   109  		assert.JSONEq(t, string(want), buf.String())
   110  	})
   111  
   112  	t.Run("markdown", func(t *testing.T) {
   113  		want := readFileString(t, "testdata/output.md")
   114  		chdir(t, tmp)
   115  		var buf bytes.Buffer
   116  		writer = &buf
   117  		t.Cleanup(func() {
   118  			writer = os.Stdout
   119  		})
   120  		output = "markdown"
   121  		t.Cleanup(func() { output = "" })
   122  		main()
   123  		assert.Equal(t, want, strings.TrimSpace(buf.String()))
   124  	})
   125  
   126  	t.Run("github-actions pull request comment", func(t *testing.T) {
   127  		if testing.Short() {
   128  			t.Skip()
   129  		}
   130  		if ci, _ := strconv.ParseBool(os.Getenv("CI")); !ci {
   131  			t.Skip()
   132  		}
   133  		if commentTest, _ := strconv.ParseBool(os.Getenv("PULL_REQUEST_COMMENT_TEST")); !commentTest {
   134  			t.Skip()
   135  		}
   136  		ci = "github-actions"
   137  		gitDiffBase = ""
   138  		t.Cleanup(func() { ci = ""; gitDiffBase = "origin/master" })
   139  		main()
   140  	})
   141  }
   142  
   143  func readFileString(t *testing.T, filename string) string {
   144  	t.Helper()
   145  	_want, err := ioutil.ReadFile(filename)
   146  	if err != nil {
   147  		t.Fatal(err)
   148  	}
   149  	return strings.ReplaceAll(strings.TrimSpace(string(_want)), "\r\n", "\n")
   150  }
   151  
   152  func cmdRun(dir, name string, args ...string) error {
   153  	cmd := exec.Command(name, args...)
   154  	cmd.Stderr = os.Stderr
   155  	cmd.Stdout = os.Stdout
   156  	cmd.Dir = dir
   157  	return cmd.Run()
   158  }
   159  
   160  func Test_parseModfile(t *testing.T) {
   161  	tmp := setup(t)
   162  	if err := os.Chdir(tmp); err != nil {
   163  		t.Fatal(err)
   164  	}
   165  
   166  	t.Run("empty", func(t *testing.T) {
   167  		m, err := parseModfile()
   168  		if err != nil {
   169  			t.Fatal(err)
   170  		}
   171  		assert.Equal(t, m.Path(), "example.com/example")
   172  	})
   173  
   174  	t.Run("specified", func(t *testing.T) {
   175  		modFile = "./go.mod"
   176  		t.Cleanup(func() { modFile = "" })
   177  		m, err := parseModfile()
   178  		if err != nil {
   179  			t.Fatal(err)
   180  		}
   181  		assert.Equal(t, m.Path(), "example.com/example")
   182  	})
   183  }
   184  
   185  func Test_containsDiff(t *testing.T) {
   186  	tests := []struct {
   187  		name     string
   188  		filename string
   189  		path     string
   190  		diffs    []string
   191  		want     bool
   192  	}{
   193  		{
   194  			name:     "contains",
   195  			filename: "example.com/example/main.go",
   196  			path:     "example.com/example",
   197  			diffs:    []string{"a.go", "main.go"},
   198  			want:     true,
   199  		},
   200  		{
   201  			name:     "not contains",
   202  			filename: "example.com/example/main.go",
   203  			path:     "example.com/example",
   204  			diffs:    []string{"a.go", "b.go"},
   205  			want:     false,
   206  		},
   207  	}
   208  
   209  	for _, tt := range tests {
   210  		t.Run(tt.name, func(t *testing.T) {
   211  			got := containsDiff(tt.filename, tt.path, tt.diffs)
   212  			assert.Equal(t, tt.want, got)
   213  		})
   214  	}
   215  }