github.com/vanstinator/golangci-lint@v0.0.0-20240223191551-cc572f00d9d1/test/testshared/runner_test.go (about)

     1  package testshared
     2  
     3  import (
     4  	"path/filepath"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/vanstinator/golangci-lint/pkg/exitcodes"
    10  )
    11  
    12  func TestRunnerBuilder_Runner(t *testing.T) {
    13  	testCases := []struct {
    14  		desc     string
    15  		builder  *RunnerBuilder
    16  		expected *Runner
    17  	}{
    18  		{
    19  			desc:    "default",
    20  			builder: NewRunnerBuilder(t),
    21  			expected: &Runner{
    22  				env:     []string(nil),
    23  				command: "run",
    24  				args: []string{
    25  					"--internal-cmd-test",
    26  					"--allow-parallel-runners",
    27  				},
    28  			},
    29  		},
    30  		{
    31  			desc:    "with command",
    32  			builder: NewRunnerBuilder(t).WithCommand("example"),
    33  			expected: &Runner{
    34  				env:     []string(nil),
    35  				command: "example",
    36  				args: []string{
    37  					"--internal-cmd-test",
    38  					"--allow-parallel-runners",
    39  				},
    40  			},
    41  		},
    42  		{
    43  			desc:    "with no-config",
    44  			builder: NewRunnerBuilder(t).WithNoConfig(),
    45  			expected: &Runner{
    46  				env:     []string(nil),
    47  				command: "run",
    48  				args: []string{
    49  					"--internal-cmd-test",
    50  					"--allow-parallel-runners",
    51  					"--no-config",
    52  				},
    53  			},
    54  		},
    55  		{
    56  			desc:    "with config file",
    57  			builder: NewRunnerBuilder(t).WithConfigFile("./testdata/example.yml"),
    58  			expected: &Runner{
    59  				env:     []string(nil),
    60  				command: "run",
    61  				args: []string{
    62  					"--internal-cmd-test",
    63  					"--allow-parallel-runners",
    64  					"-c",
    65  					filepath.FromSlash("./testdata/example.yml"),
    66  				},
    67  			},
    68  		},
    69  		{
    70  			desc:    "with directives",
    71  			builder: NewRunnerBuilder(t).WithDirectives("./testdata/all.go"),
    72  			expected: &Runner{
    73  				env:     []string(nil),
    74  				command: "run",
    75  				args: []string{
    76  					"--internal-cmd-test",
    77  					"--allow-parallel-runners",
    78  					"-c",
    79  					filepath.FromSlash("testdata/example.yml"),
    80  					"-Efoo",
    81  					"--simple",
    82  					"--hello=world",
    83  				},
    84  			},
    85  		},
    86  		{
    87  			desc:    "with environ",
    88  			builder: NewRunnerBuilder(t).WithEnviron("FOO=BAR", "FII=BIR"),
    89  			expected: &Runner{
    90  				env:     []string{"FOO=BAR", "FII=BIR"},
    91  				command: "run",
    92  				args: []string{
    93  					"--internal-cmd-test",
    94  					"--allow-parallel-runners",
    95  				},
    96  			},
    97  		},
    98  		{
    99  			desc:    "with no parallel runners",
   100  			builder: NewRunnerBuilder(t).WithNoParallelRunners(),
   101  			expected: &Runner{
   102  				env:     []string(nil),
   103  				command: "run",
   104  				args: []string{
   105  					"--internal-cmd-test",
   106  				},
   107  			},
   108  		},
   109  		{
   110  			desc:    "with args",
   111  			builder: NewRunnerBuilder(t).WithArgs("-Efoo", "--simple", "--hello=world"),
   112  			expected: &Runner{
   113  				env:     []string(nil),
   114  				command: "run",
   115  				args: []string{
   116  					"--internal-cmd-test",
   117  					"--allow-parallel-runners",
   118  					"-Efoo",
   119  					"--simple",
   120  					"--hello=world",
   121  				},
   122  			},
   123  		},
   124  		{
   125  			desc:    "with target path",
   126  			builder: NewRunnerBuilder(t).WithTargetPath("./testdata/all.go"),
   127  			expected: &Runner{
   128  				env:     []string(nil),
   129  				command: "run",
   130  				args: []string{
   131  					"--internal-cmd-test",
   132  					"--allow-parallel-runners",
   133  					filepath.FromSlash("testdata/all.go"),
   134  				},
   135  			},
   136  		},
   137  		{
   138  			desc: "with RunContext (directives)",
   139  			builder: NewRunnerBuilder(t).
   140  				WithRunContext(&RunContext{
   141  					Args:           []string{"-Efoo", "--simple", "--hello=world"},
   142  					ConfigPath:     filepath.FromSlash("testdata/example.yml"),
   143  					ExpectedLinter: "test",
   144  				}),
   145  			expected: &Runner{
   146  				env:     []string(nil),
   147  				command: "run",
   148  				args: []string{
   149  					"--internal-cmd-test",
   150  					"--allow-parallel-runners",
   151  					"-c",
   152  					filepath.FromSlash("testdata/example.yml"),
   153  					"-Efoo",
   154  					"--simple",
   155  					"--hello=world",
   156  				},
   157  			},
   158  		},
   159  	}
   160  
   161  	for _, test := range testCases {
   162  		test := test
   163  		t.Run(test.desc, func(t *testing.T) {
   164  			t.Parallel()
   165  
   166  			runner := test.builder.Runner()
   167  
   168  			assert.NotNil(t, runner.log)
   169  			assert.NotNil(t, runner.tb)
   170  			assert.Equal(t, test.expected.env, runner.env)
   171  			assert.Equal(t, test.expected.command, runner.command)
   172  			assert.Equal(t, test.expected.args, runner.args)
   173  		})
   174  	}
   175  }
   176  
   177  func TestRunnerResult_ExpectExitCode(t *testing.T) {
   178  	r := &RunnerResult{tb: t, exitCode: exitcodes.Success}
   179  	r.ExpectExitCode(exitcodes.Failure, exitcodes.Success)
   180  }
   181  
   182  func TestRunnerResult_ExpectNoIssues(t *testing.T) {
   183  	r := &RunnerResult{tb: t}
   184  	r.ExpectNoIssues()
   185  }
   186  
   187  func TestRunnerResult_ExpectOutputContains(t *testing.T) {
   188  	r := &RunnerResult{tb: t, output: "this is an output"}
   189  	r.ExpectOutputContains("an")
   190  }
   191  
   192  func TestRunnerResult_ExpectHasIssue(t *testing.T) {
   193  	r := &RunnerResult{tb: t, exitCode: exitcodes.IssuesFound, output: "this is an output"}
   194  	r.ExpectHasIssue("an")
   195  }
   196  
   197  func TestRunnerResult_ExpectOutputEq(t *testing.T) {
   198  	r := &RunnerResult{tb: t, output: "this is an output"}
   199  	r.ExpectOutputEq("this is an output")
   200  }
   201  
   202  func TestRunnerResult_ExpectOutputNotContains(t *testing.T) {
   203  	r := &RunnerResult{tb: t, output: "this is an output"}
   204  	r.ExpectOutputNotContains("one")
   205  }
   206  
   207  func TestRunnerResult_ExpectOutputRegexp(t *testing.T) {
   208  	r := &RunnerResult{tb: t, output: "this is an output"}
   209  	r.ExpectOutputRegexp(`an.+`)
   210  	r.ExpectOutputRegexp("an")
   211  }