Skip to content
This repository has been archived by the owner on Jun 28, 2022. It is now read-only.

Commit

Permalink
pkg/analysis/importalias: add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Fernandez Ludovic <[email protected]>
  • Loading branch information
ldez committed Feb 28, 2021
1 parent 00ba86c commit f26be93
Show file tree
Hide file tree
Showing 7 changed files with 2,023 additions and 0 deletions.
65 changes: 65 additions & 0 deletions pkg/analysis/importalias/analyzer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright Project Contour Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package importalias

import (
"os"
"os/exec"
"path/filepath"
"testing"

"golang.org/x/tools/go/analysis/analysistest"
)

func TestAnalyzer(t *testing.T) {
testdata := analysistest.TestData()

testCases := []struct {
desc string
pkg string
}{
{
desc: "Valid imports",
pkg: "a",
},
{
desc: "Invalid imports",
pkg: "b",
},
}

for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()

dir := filepath.Join(testdata, "src", test.pkg)

if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
cmd := exec.Command("go", "mod", "vendor")
cmd.Dir = dir

t.Cleanup(func() {
_ = os.RemoveAll(filepath.Join(testdata, "src", test.pkg, "vendor"))
})

if output, err := cmd.CombinedOutput(); err != nil {
t.Fatal(err, string(output))
}
}

analysistest.Run(t, testdata, Analyzer, test.pkg)
})
}
}
40 changes: 40 additions & 0 deletions pkg/analysis/importalias/testdata/src/a/a.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright Project Contour Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package a

import (
// envoy_api_v2_auth "github.com/envoyproxy/go-control-plane/envoy/api/v2/auth"
envoy_config_filter_http_ext_authz_v2 "github.com/envoyproxy/go-control-plane/envoy/config/filter/http/ext_authz/v2"
contour_api_v1 "github.com/projectcontour/contour/apis/projectcontour/v1"
contour_api_v1alpha1 "github.com/projectcontour/contour/apis/projectcontour/v1alpha1"
kingpin_v2 "gopkg.in/alecthomas/kingpin.v2"
api_meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
api_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
gateway_v1alpha1 "sigs.k8s.io/gateway-api/apis/v1alpha1"
gatewayapi_v1alpha1 "sigs.k8s.io/gateway-api/apis/v1alpha1"
)

func foo() {
meta_v1.Now()
api_meta_v1.Now()
api_v1.Now()
// _ = envoy_api_v2_auth.CertificateValidationContext_ACCEPT_UNTRUSTED
_ = envoy_config_filter_http_ext_authz_v2.AuthorizationRequest{}
contour_api_v1.AddKnownTypes(nil)
_ = contour_api_v1alpha1.GroupVersion
kingpin_v2.Parse()
_ = gatewayapi_v1alpha1.GroupVersion
_ = gateway_v1alpha1.GroupVersion
}
11 changes: 11 additions & 0 deletions pkg/analysis/importalias/testdata/src/a/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/projectcontour/lint/pkg/analysis/importalias/testdata/src/src/a

go 1.16

require (
github.com/envoyproxy/go-control-plane v0.9.8
github.com/projectcontour/contour v1.13.0
gopkg.in/alecthomas/kingpin.v2 v2.2.6
k8s.io/apimachinery v0.20.4
sigs.k8s.io/gateway-api v0.2.0
)
Loading

0 comments on commit f26be93

Please sign in to comment.