This repository has been archived by the owner on Jun 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
pkg/analysis/importalias: add tests #8
Open
ldez
wants to merge
1
commit into
projectcontour:main
Choose a base branch
from
ldez:feat/add-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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