github.com/verrazzano/verrazzano@v1.7.0/tools/eventually-checker/check_eventually_test.go (about)

     1  // Copyright (c) 2021, 2022, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  package main
     4  
     5  import (
     6  	"bytes"
     7  	"flag"
     8  	"go/token"
     9  	"os"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  // TestAnalyzePackages tests analyzing packages and finds bad calls inside gomega.Eventually.
    17  // GIVEN test source code that calls gomega.Eventually and ginkgo.Fail and gomega.Expect
    18  // WHEN the packages are analyzed
    19  // THEN the checker correctly identifies Fail and Expect calls that are called by Eventually (directly or indirectly)
    20  func TestAnalyzePackages(t *testing.T) {
    21  	assert := assert.New(t)
    22  
    23  	// load the packages from the unit test data directory
    24  	fset, pkgs, err := loadPackages("./testdata")
    25  	if err != nil {
    26  		assert.NoError(err)
    27  	}
    28  
    29  	// should be two packages
    30  	assert.Len(pkgs, 2)
    31  
    32  	// analyze the packages
    33  	for _, pkg := range pkgs {
    34  		analyze(pkg.Syntax)
    35  	}
    36  
    37  	// check for bad calls, we should get 2
    38  	results := checkForBadCalls()
    39  	assert.Len(results, 8)
    40  
    41  	for key, val := range results {
    42  		// convert the failed call position to a string of the form "filename:row:column"
    43  		failedCallPos := fset.PositionFor(key, true).String()
    44  		if strings.HasSuffix(failedCallPos, "/internal/helper.go:12:2") {
    45  			// expect this bad call from Eventually in main.go
    46  			assert.Len(val, 1)
    47  			eventuallyPos := fset.PositionFor(val[0], true).String()
    48  			assert.True(strings.HasSuffix(eventuallyPos, "/main.go:32:3"))
    49  		} else if strings.HasSuffix(failedCallPos, "/main.go:62:2") {
    50  			// expect this bad call from Eventually in main.go
    51  			assert.Len(val, 1)
    52  			eventuallyPos := fset.PositionFor(val[0], true).String()
    53  			assert.True(strings.HasSuffix(eventuallyPos, "/main.go:41:3"))
    54  		} else if strings.HasSuffix(failedCallPos, "/main.go:17:2") {
    55  			// expect this bad call from Eventually in main.go
    56  			assert.Len(val, 1)
    57  			eventuallyPos := fset.PositionFor(val[0], true).String()
    58  			assert.True(strings.HasSuffix(eventuallyPos, "/main.go:49:3"))
    59  		} else if strings.HasSuffix(failedCallPos, "/main.go:22:2") {
    60  			// expect this bad call from Eventually in main.go
    61  			assert.Len(val, 1)
    62  			eventuallyPos := fset.PositionFor(val[0], true).String()
    63  			assert.True(strings.HasSuffix(eventuallyPos, "/main.go:55:3"))
    64  		} else if strings.HasSuffix(failedCallPos, "/main.go:91:4") {
    65  			// expect this bad call from Eventually in main.go
    66  			assert.Len(val, 1)
    67  			eventuallyPos := fset.PositionFor(val[0], true).String()
    68  			assert.True(strings.HasSuffix(eventuallyPos, "/main.go:89:3"))
    69  		} else if strings.HasSuffix(failedCallPos, "/main.go:99:4") {
    70  			// expect this bad call from Eventually in main.go
    71  			assert.Len(val, 1)
    72  			eventuallyPos := fset.PositionFor(val[0], true).String()
    73  			assert.True(strings.HasSuffix(eventuallyPos, "/main.go:97:3"))
    74  		} else if strings.HasSuffix(failedCallPos, "/helper.go:21:9") {
    75  			// expect this bad call from Eventually in main.go
    76  			assert.Len(val, 1)
    77  			eventuallyPos := fset.PositionFor(val[0], true).String()
    78  			assert.True(strings.HasSuffix(eventuallyPos, "/main.go:105:3"))
    79  		} else if strings.HasSuffix(failedCallPos, "/helper.go:26:3") {
    80  			// expect this bad call from Eventually in main.go
    81  			assert.Len(val, 1)
    82  			eventuallyPos := fset.PositionFor(val[0], true).String()
    83  			assert.True(strings.HasSuffix(eventuallyPos, "/helper.go:25:2"))
    84  		} else {
    85  			t.Errorf("Found unexpected Fail/Expect call at: %s", failedCallPos)
    86  		}
    87  	}
    88  }
    89  
    90  // TestParseFlags tests command line flag parsing
    91  // GIVEN command line arguments
    92  // WHEN the command line arguments are parsed
    93  // THEN validate that the flags are set correctly and positional arguments are correct
    94  func TestParseFlags(t *testing.T) {
    95  	assert := assert.New(t)
    96  	path := "/unit/test/path"
    97  
    98  	os.Args = []string{"cmd_exe", "-report", path}
    99  
   100  	parseFlags()
   101  	assert.True(reportOnly)
   102  	assert.Equal(path, flag.Args()[0])
   103  
   104  	// reset flag parsing
   105  	flag.CommandLine = flag.NewFlagSet("", flag.ExitOnError)
   106  
   107  	os.Args = []string{"cmd_exe", path}
   108  
   109  	parseFlags()
   110  	assert.False(reportOnly)
   111  	assert.Equal(path, flag.Args()[0])
   112  }
   113  
   114  // TestDisplayResults tests output of checker results
   115  // GIVEN test source code that calls gomega.Eventually and ginkgo.Fail and gomega.Expect
   116  // WHEN the packages are analyzed
   117  // AND results are displayed
   118  // THEN the output contains the files and positions of the bad calls
   119  func TestDisplayResults(t *testing.T) {
   120  	assert := assert.New(t)
   121  
   122  	// clear the maps from previous analysis run
   123  	funcMap = make(map[string][]funcCall)
   124  	eventuallyMap = make(map[token.Pos][]funcCall)
   125  
   126  	fset, pkgs, err := loadPackages("./testdata")
   127  	if err != nil {
   128  		assert.NoError(err)
   129  	}
   130  
   131  	for _, pkg := range pkgs {
   132  		analyze(pkg.Syntax)
   133  	}
   134  
   135  	results := checkForBadCalls()
   136  
   137  	var b bytes.Buffer
   138  	displayResults(results, fset, &b)
   139  	assert.Contains(b.String(), "helper.go:12:2")
   140  	assert.Contains(b.String(), "main.go:32:3")
   141  	assert.Contains(b.String(), "main.go:62:2")
   142  	assert.Contains(b.String(), "main.go:41:3")
   143  	assert.Contains(b.String(), "main.go:17:2")
   144  	assert.Contains(b.String(), "main.go:49:3")
   145  	assert.Contains(b.String(), "main.go:22:2")
   146  	assert.Contains(b.String(), "main.go:55:3")
   147  }