github.com/haalcala/mattermost-server-change-repo@v0.0.0-20210713015153-16753fbeee5f/plugin/checker/check_api_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package main
     5  
     6  import (
     7  	"os"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestCheckAPIVersionComments(t *testing.T) {
    14  	testCases := []struct {
    15  		name, pkgPath, err string
    16  		expected           result
    17  	}{
    18  		{
    19  			name:    "valid comments",
    20  			pkgPath: "github.com/mattermost/mattermost-server/v5/plugin/checker/internal/test/valid",
    21  			err:     "",
    22  		},
    23  		{
    24  			name:    "invalid comments",
    25  			pkgPath: "github.com/mattermost/mattermost-server/v5/plugin/checker/internal/test/invalid",
    26  			expected: result{
    27  				Errors: []string{"internal/test/invalid/invalid.go:15:2: missing a minimum server version comment on method InvalidMethod"},
    28  			},
    29  		},
    30  		{
    31  			name:    "missing API interface",
    32  			pkgPath: "github.com/mattermost/mattermost-server/v5/plugin/checker/internal/test/missing",
    33  			err:     "could not find API interface",
    34  		},
    35  		{
    36  			name:    "non-existent package path",
    37  			pkgPath: "github.com/mattermost/mattermost-server/v5/plugin/checker/internal/test/does_not_exist",
    38  			err:     "could not find API interface",
    39  		},
    40  	}
    41  
    42  	// Enable debug flag to have packagesdriver/sizes.go print stderr of `go list` command.
    43  	// We want to surface any error text that may exist in stderr of this command.
    44  	prevEnvValue := os.Getenv("GOPACKAGESPRINTGOLISTERRORS")
    45  	os.Setenv("GOPACKAGESPRINTGOLISTERRORS", "true")
    46  
    47  	for _, tc := range testCases {
    48  		t.Run(tc.name, func(t *testing.T) {
    49  			res, err := checkAPIVersionComments(tc.pkgPath)
    50  			assert.Equal(t, res, tc.expected)
    51  
    52  			if tc.err != "" {
    53  				assert.EqualError(t, err, tc.err)
    54  			} else {
    55  				assert.NoError(t, err)
    56  			}
    57  		})
    58  	}
    59  	os.Setenv("GOPACKAGESPRINTGOLISTERRORS", prevEnvValue)
    60  }