...

Source file src/sigs.k8s.io/kustomize/kyaml/filesys/confirmeddir_test.go

Documentation: sigs.k8s.io/kustomize/kyaml/filesys

     1  // Copyright 2019 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  //go:build !windows
     5  // +build !windows
     6  
     7  package filesys
     8  
     9  import (
    10  	"os"
    11  	"path/filepath"
    12  	"testing"
    13  )
    14  
    15  func TestJoin(t *testing.T) {
    16  	fSys := MakeFsInMemory()
    17  	if err := fSys.Mkdir("/foo"); err != nil {
    18  		t.Fatalf("unexpected err: %v", err)
    19  	}
    20  	d, f, err := fSys.CleanedAbs("/foo")
    21  	if err != nil {
    22  		t.Fatalf("unexpected err: %v", err)
    23  	}
    24  	if f != "" {
    25  		t.Fatalf("unexpected file: %v", f)
    26  	}
    27  	if d.Join("bar") != "/foo/bar" {
    28  		t.Fatalf("expected join %s", d.Join("bar"))
    29  	}
    30  }
    31  
    32  func TestHasPrefix_Slash(t *testing.T) {
    33  	fSys := MakeFsInMemory()
    34  	d, f, err := fSys.CleanedAbs("/")
    35  	if err != nil {
    36  		t.Fatalf("unexpected err: %v", err)
    37  	}
    38  	if f != "" {
    39  		t.Fatalf("unexpected file: %v", f)
    40  	}
    41  	if d.HasPrefix("/hey") {
    42  		t.Fatalf("should be false")
    43  	}
    44  	if !d.HasPrefix("/") {
    45  		t.Fatalf("/ should have the prefix /")
    46  	}
    47  }
    48  
    49  func TestHasPrefix_SlashFoo(t *testing.T) {
    50  	fSys := MakeFsInMemory()
    51  	err := fSys.Mkdir("/foo")
    52  	if err != nil {
    53  		t.Fatalf("unexpected err: %v", err)
    54  	}
    55  	d, _, err := fSys.CleanedAbs("/foo")
    56  	if err != nil {
    57  		t.Fatalf("unexpected err: %v", err)
    58  	}
    59  	if d.HasPrefix("/fo") {
    60  		t.Fatalf("/foo does not have path prefix /fo")
    61  	}
    62  	if d.HasPrefix("/fod") {
    63  		t.Fatalf("/foo does not have path prefix /fod")
    64  	}
    65  	if !d.HasPrefix("/foo") {
    66  		t.Fatalf("/foo should have prefix /foo")
    67  	}
    68  }
    69  
    70  func TestHasPrefix_SlashFooBar(t *testing.T) {
    71  	fSys := MakeFsInMemory()
    72  	err := fSys.MkdirAll("/foo/bar")
    73  	if err != nil {
    74  		t.Fatalf("unexpected err: %v", err)
    75  	}
    76  	d, _, err := fSys.CleanedAbs("/foo/bar")
    77  	if err != nil {
    78  		t.Fatalf("unexpected err: %v", err)
    79  	}
    80  	if d.HasPrefix("/fo") {
    81  		t.Fatalf("/foo/bar does not have path prefix /fo")
    82  	}
    83  	if d.HasPrefix("/foobar") {
    84  		t.Fatalf("/foo/bar does not have path prefix /foobar")
    85  	}
    86  	if !d.HasPrefix("/foo/bar") {
    87  		t.Fatalf("/foo/bar should have prefix /foo/bar")
    88  	}
    89  	if !d.HasPrefix("/foo") {
    90  		t.Fatalf("/foo/bar should have prefix /foo")
    91  	}
    92  	if !d.HasPrefix("/") {
    93  		t.Fatalf("/foo/bar should have prefix /")
    94  	}
    95  }
    96  
    97  func TestNewTempConfirmDir(t *testing.T) {
    98  	tmp, err := NewTmpConfirmedDir()
    99  	if err != nil {
   100  		t.Fatalf("unexpected error: %v", err)
   101  	}
   102  	defer os.RemoveAll(string(tmp))
   103  
   104  	delinked, err := filepath.EvalSymlinks(string(tmp))
   105  	if err != nil {
   106  		t.Fatalf("unexpected error: %v", err)
   107  	}
   108  	if string(tmp) != delinked {
   109  		t.Fatalf("unexpected path containing symlinks")
   110  	}
   111  }
   112  

View as plain text