...

Source file src/k8s.io/kubernetes/test/typecheck/main_test.go

Documentation: k8s.io/kubernetes/test/typecheck

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"flag"
    21  	"os"
    22  	"path/filepath"
    23  	"testing"
    24  
    25  	"golang.org/x/tools/go/packages"
    26  )
    27  
    28  // This exists because `go` is not always in the PATH when running CI.
    29  var goBinary = flag.String("go", "", "path to a `go` binary")
    30  
    31  func TestVerify(t *testing.T) {
    32  	// x/tools/packages is going to literally exec `go`, so it needs some
    33  	// setup.
    34  	setEnvVars(t)
    35  
    36  	tcs := []struct {
    37  		path   string
    38  		expect int
    39  	}{
    40  		{"./testdata/good", 0},
    41  		{"./testdata/bad", 18},
    42  	}
    43  
    44  	for _, tc := range tcs {
    45  		errs, err := verify("linux/amd64", []string{tc.path}, nil)
    46  		if err != nil {
    47  			t.Errorf("unexpected error: %v", err)
    48  		} else if len(errs) != tc.expect {
    49  			t.Errorf("Expected %d errors, got %d: %v", tc.expect, len(errs), errs)
    50  		}
    51  	}
    52  }
    53  
    54  func setEnvVars(t testing.TB) {
    55  	t.Helper()
    56  	if *goBinary != "" {
    57  		newPath := filepath.Dir(*goBinary)
    58  		curPath := os.Getenv("PATH")
    59  		if curPath != "" {
    60  			newPath = newPath + ":" + curPath
    61  		}
    62  		t.Setenv("PATH", newPath)
    63  	}
    64  	if os.Getenv("HOME") == "" {
    65  		t.Setenv("HOME", "/tmp")
    66  	}
    67  }
    68  
    69  func TestDedup(t *testing.T) {
    70  	testcases := []struct {
    71  		input    []packages.Error
    72  		expected int
    73  	}{{
    74  		input:    nil,
    75  		expected: 0,
    76  	}, {
    77  		input: []packages.Error{
    78  			{Pos: "file:7", Msg: "message", Kind: packages.ParseError},
    79  		},
    80  		expected: 1,
    81  	}, {
    82  		input: []packages.Error{
    83  			{Pos: "file:7", Msg: "message1", Kind: packages.ParseError},
    84  			{Pos: "file:8", Msg: "message2", Kind: packages.ParseError},
    85  		},
    86  		expected: 2,
    87  	}, {
    88  		input: []packages.Error{
    89  			{Pos: "file:7", Msg: "message1", Kind: packages.ParseError},
    90  			{Pos: "file:8", Msg: "message2", Kind: packages.ParseError},
    91  			{Pos: "file:7", Msg: "message1", Kind: packages.ParseError},
    92  		},
    93  		expected: 2,
    94  	}}
    95  
    96  	for i, tc := range testcases {
    97  		out := dedup(tc.input)
    98  		if len(out) != tc.expected {
    99  			t.Errorf("[%d] dedup(%v) = '%v', expected %d",
   100  				i, tc.input, out, tc.expected)
   101  		}
   102  	}
   103  }
   104  

View as plain text