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

pkg/analysis/importalias: add tests #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.RunWithSuggestedFixes(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"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The readme suggests that is a valid import but it doesn't work.

https://github.com/projectcontour/lint/blob/main/README.md#linters

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
)
931 changes: 931 additions & 0 deletions pkg/analysis/importalias/testdata/src/a/go.sum

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions pkg/analysis/importalias/testdata/src/b/b.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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 b

import (
envoy_config "github.com/envoyproxy/go-control-plane/envoy/config/filter/http/ext_authz/v2" // want `version "v2" not specified in alias "envoy_config" for import path "github/com/envoyproxy/gocontrolplane/envoy/config/filter/http/ext/authz/v2"`
contour "github.com/projectcontour/contour/apis/projectcontour/v1" // want `"v1" not specified in alias "contour" for import path "github/com/projectcontour/contour/apis/projectcontour/v1"`
contour_api_v1alpha1 "github.com/projectcontour/contour/apis/projectcontour/v1alpha1"
v2 "gopkg.in/alecthomas/kingpin.v2" // want `alias "v2" uses words that are not in path "in/alecthomas/kingpin/v2"`
meta "k8s.io/apimachinery/pkg/apis/meta/v1" // want `version "v1" not specified in alias "meta" for import path "k8s/io/apimachinery/pkg/apis/meta/v1"`
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" // want `alias "v1" uses words that are not in path "io/apimachinery/pkg/apis/meta/v1"`
gateway_v1alpha1 "sigs.k8s.io/gateway-api/apis/v1alpha1"
)

func foo() {
meta.Now()
v1.Now()
_ = envoy_config.AuthorizationRequest{}
contour.AddKnownTypes(nil)
_ = contour_api_v1alpha1.GroupVersion
v2.Parse()
_ = gateway_v1alpha1.GroupVersion
}
34 changes: 34 additions & 0 deletions pkg/analysis/importalias/testdata/src/b/b.go.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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 b

import (
authz_v2 "github.com/envoyproxy/go-control-plane/envoy/config/filter/http/ext_authz/v2" // want `version "v2" not specified in alias "envoy_config" for import path "github/com/envoyproxy/gocontrolplane/envoy/config/filter/http/ext/authz/v2"`
projectcontour_v1 "github.com/projectcontour/contour/apis/projectcontour/v1" // want `"v1" not specified in alias "contour" for import path "github/com/projectcontour/contour/apis/projectcontour/v1"`
contour_api_v1alpha1 "github.com/projectcontour/contour/apis/projectcontour/v1alpha1"
kingpin_v2 "gopkg.in/alecthomas/kingpin.v2" // want `alias "v2" uses words that are not in path "in/alecthomas/kingpin/v2"`
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" // want `alias "v1" uses words that are not in path "io/apimachinery/pkg/apis/meta/v1"`
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" // want `version "v1" not specified in alias "meta" for import path "k8s/io/apimachinery/pkg/apis/meta/v1"`
Comment on lines +21 to +22
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the suggestions can produce some duplicates

gateway_v1alpha1 "sigs.k8s.io/gateway-api/apis/v1alpha1"
)

func foo() {
meta.Now()
v1.Now()
_ = envoy_config.AuthorizationRequest{}
contour.AddKnownTypes(nil)
_ = contour_api_v1alpha1.GroupVersion
v2.Parse()
_ = gateway_v1alpha1.GroupVersion
}
11 changes: 11 additions & 0 deletions pkg/analysis/importalias/testdata/src/b/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/b

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