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

Commit f26be93

Browse files
committed
pkg/analysis/importalias: add tests
Signed-off-by: Fernandez Ludovic <[email protected]>
1 parent 00ba86c commit f26be93

File tree

7 files changed

+2023
-0
lines changed

7 files changed

+2023
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright Project Contour Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package importalias
15+
16+
import (
17+
"os"
18+
"os/exec"
19+
"path/filepath"
20+
"testing"
21+
22+
"golang.org/x/tools/go/analysis/analysistest"
23+
)
24+
25+
func TestAnalyzer(t *testing.T) {
26+
testdata := analysistest.TestData()
27+
28+
testCases := []struct {
29+
desc string
30+
pkg string
31+
}{
32+
{
33+
desc: "Valid imports",
34+
pkg: "a",
35+
},
36+
{
37+
desc: "Invalid imports",
38+
pkg: "b",
39+
},
40+
}
41+
42+
for _, test := range testCases {
43+
test := test
44+
t.Run(test.desc, func(t *testing.T) {
45+
t.Parallel()
46+
47+
dir := filepath.Join(testdata, "src", test.pkg)
48+
49+
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
50+
cmd := exec.Command("go", "mod", "vendor")
51+
cmd.Dir = dir
52+
53+
t.Cleanup(func() {
54+
_ = os.RemoveAll(filepath.Join(testdata, "src", test.pkg, "vendor"))
55+
})
56+
57+
if output, err := cmd.CombinedOutput(); err != nil {
58+
t.Fatal(err, string(output))
59+
}
60+
}
61+
62+
analysistest.Run(t, testdata, Analyzer, test.pkg)
63+
})
64+
}
65+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright Project Contour Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package a
15+
16+
import (
17+
// envoy_api_v2_auth "github.com/envoyproxy/go-control-plane/envoy/api/v2/auth"
18+
envoy_config_filter_http_ext_authz_v2 "github.com/envoyproxy/go-control-plane/envoy/config/filter/http/ext_authz/v2"
19+
contour_api_v1 "github.com/projectcontour/contour/apis/projectcontour/v1"
20+
contour_api_v1alpha1 "github.com/projectcontour/contour/apis/projectcontour/v1alpha1"
21+
kingpin_v2 "gopkg.in/alecthomas/kingpin.v2"
22+
api_meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+
api_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
gateway_v1alpha1 "sigs.k8s.io/gateway-api/apis/v1alpha1"
26+
gatewayapi_v1alpha1 "sigs.k8s.io/gateway-api/apis/v1alpha1"
27+
)
28+
29+
func foo() {
30+
meta_v1.Now()
31+
api_meta_v1.Now()
32+
api_v1.Now()
33+
// _ = envoy_api_v2_auth.CertificateValidationContext_ACCEPT_UNTRUSTED
34+
_ = envoy_config_filter_http_ext_authz_v2.AuthorizationRequest{}
35+
contour_api_v1.AddKnownTypes(nil)
36+
_ = contour_api_v1alpha1.GroupVersion
37+
kingpin_v2.Parse()
38+
_ = gatewayapi_v1alpha1.GroupVersion
39+
_ = gateway_v1alpha1.GroupVersion
40+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module github.com/projectcontour/lint/pkg/analysis/importalias/testdata/src/src/a
2+
3+
go 1.16
4+
5+
require (
6+
github.com/envoyproxy/go-control-plane v0.9.8
7+
github.com/projectcontour/contour v1.13.0
8+
gopkg.in/alecthomas/kingpin.v2 v2.2.6
9+
k8s.io/apimachinery v0.20.4
10+
sigs.k8s.io/gateway-api v0.2.0
11+
)

0 commit comments

Comments
 (0)