github.com/galaxyobe/gen@v0.0.0-20220910125335-392fa8f0990f/third_party/gengo/parser/local_parse_test.go (about)

     1  /*
     2  Copyright 2017 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 parser
    18  
    19  import (
    20  	"os"
    21  	"testing"
    22  )
    23  
    24  func TestImportBuildPackage(t *testing.T) {
    25  	// get our original dir to restore
    26  	dir, err := os.Getwd()
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	defer func() {
    31  		os.Chdir(dir)
    32  	}()
    33  	// switch into the fake dependent module which knows where the fake module is
    34  	os.Chdir("../testdata/dependent")
    35  
    36  	b := New()
    37  	if _, err := b.importBuildPackage("fake/dep"); err != nil {
    38  		t.Fatal(err)
    39  	}
    40  	if _, ok := b.buildPackages["fake/dep"]; !ok {
    41  		t.Errorf("missing expected, but got %v", b.buildPackages)
    42  	}
    43  
    44  	if len(b.buildPackages) > 1 {
    45  		// this would happen if the canonicalization failed to normalize the path
    46  		// you'd get a k8s.io/gengo/vendor/fake/dep key too
    47  		t.Errorf("missing one, but got %v", b.buildPackages)
    48  	}
    49  }
    50  
    51  func TestIsErrPackageNotFound(t *testing.T) {
    52  	b := New()
    53  	if _, err := b.importBuildPackage("fake/empty"); !isErrPackageNotFound(err) {
    54  		t.Errorf("expected error like %s, but got %v", regexErrPackageNotFound.String(), err)
    55  	}
    56  }
    57  
    58  func TestCanonicalizeImportPath(t *testing.T) {
    59  	tcs := []struct {
    60  		name   string
    61  		input  string
    62  		output string
    63  	}{
    64  		{
    65  			name:   "passthrough",
    66  			input:  "github.com/foo/bar",
    67  			output: "github.com/foo/bar",
    68  		},
    69  		{
    70  			name:   "simple",
    71  			input:  "github.com/foo/vendor/k8s.io/kubernetes/pkg/api",
    72  			output: "k8s.io/kubernetes/pkg/api",
    73  		},
    74  		{
    75  			name:   "deeper",
    76  			input:  "github.com/foo/bar/vendor/k8s.io/kubernetes/pkg/api",
    77  			output: "k8s.io/kubernetes/pkg/api",
    78  		},
    79  	}
    80  
    81  	for _, tc := range tcs {
    82  		actual := canonicalizeImportPath(tc.input)
    83  		if string(actual) != tc.output {
    84  			t.Errorf("%v: expected %q got %q", tc.name, tc.output, actual)
    85  		}
    86  	}
    87  }