k8s.io/registry.k8s.io@v0.3.1/internal/integration/paths_test.go (about)

     1  /*
     2  Copyright 2022 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 integration
    18  
    19  import (
    20  	"errors"
    21  	"testing"
    22  )
    23  
    24  func TestModuleRootDir(t *testing.T) {
    25  	root, err := ModuleRootDir()
    26  	if err != nil {
    27  		t.Fatalf("unexpected error getting root dir: %v", err)
    28  	} else if root == "" {
    29  		t.Fatal("expected root dir to be non-empty string")
    30  	}
    31  
    32  	// we reasonably assume the filesystem root is not a module
    33  	wdAlwaysRoot := func() (string, error) { return "/", nil }
    34  	root, err = moduleRootDir(wdAlwaysRoot)
    35  	if err == nil {
    36  		t.Fatal("expected error getting moduleRootDir for /")
    37  	} else if root != "" {
    38  		t.Fatal("did not expect non-empty string getting moduleRootDir for /")
    39  	}
    40  
    41  	// test error handling for os.Getwd
    42  	expectErr := errors.New("err")
    43  	wdAlwaysError := func() (string, error) { return "", expectErr }
    44  	root, err = moduleRootDir(wdAlwaysError)
    45  	if err == nil {
    46  		t.Fatal("expected error getting moduleRootDir with erroring getWD")
    47  	} else if root != "" {
    48  		t.Fatal("did not expect non-empty string getting moduleRootDir for erroring getWD")
    49  	}
    50  }