github.com/koderover/helm@v2.17.0+incompatible/pkg/sympath/walk_test.go (about)

     1  /*
     2  Copyright (c) for portions of walk_test.go are held by The Go Authors, 2009 and are provided under
     3  the BSD license.
     4  
     5  https://github.com/golang/go/blob/master/LICENSE
     6  
     7  Copyright The Helm Authors.
     8  Licensed under the Apache License, Version 2.0 (the "License");
     9  you may not use this file except in compliance with the License.
    10  You may obtain a copy of the License at
    11  
    12  http://www.apache.org/licenses/LICENSE-2.0
    13  
    14  Unless required by applicable law or agreed to in writing, software
    15  distributed under the License is distributed on an "AS IS" BASIS,
    16  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    17  See the License for the specific language governing permissions and
    18  limitations under the License.
    19  */
    20  
    21  package sympath
    22  
    23  import (
    24  	"os"
    25  	"path/filepath"
    26  	"testing"
    27  )
    28  
    29  type Node struct {
    30  	name          string
    31  	entries       []*Node // nil if the entry is a file
    32  	marks         int
    33  	expectedMarks int
    34  	symLinkedTo   string
    35  }
    36  
    37  var tree = &Node{
    38  	"testdata",
    39  	[]*Node{
    40  		{"a", nil, 0, 1, ""},
    41  		{"b", []*Node{}, 0, 1, ""},
    42  		{"c", nil, 0, 2, ""},
    43  		{"d", nil, 0, 0, "c"},
    44  		{
    45  			"e",
    46  			[]*Node{
    47  				{"x", nil, 0, 1, ""},
    48  				{"y", []*Node{}, 0, 1, ""},
    49  				{
    50  					"z",
    51  					[]*Node{
    52  						{"u", nil, 0, 1, ""},
    53  						{"v", nil, 0, 1, ""},
    54  						{"w", nil, 0, 1, ""},
    55  					},
    56  					0,
    57  					1,
    58  					"",
    59  				},
    60  			},
    61  			0,
    62  			1,
    63  			"",
    64  		},
    65  	},
    66  	0,
    67  	1,
    68  	"",
    69  }
    70  
    71  func walkTree(n *Node, path string, f func(path string, n *Node)) {
    72  	f(path, n)
    73  	for _, e := range n.entries {
    74  		walkTree(e, filepath.Join(path, e.name), f)
    75  	}
    76  }
    77  
    78  func makeTree(t *testing.T) {
    79  	walkTree(tree, tree.name, func(path string, n *Node) {
    80  		if n.entries == nil {
    81  			if n.symLinkedTo != "" {
    82  				if err := os.Symlink(n.symLinkedTo, path); err != nil {
    83  					t.Fatalf("makeTree: %v", err)
    84  				}
    85  			} else {
    86  				fd, err := os.Create(path)
    87  				if err != nil {
    88  					t.Fatalf("makeTree: %v", err)
    89  					return
    90  				}
    91  				fd.Close()
    92  			}
    93  		} else {
    94  			if err := os.Mkdir(path, 0770); err != nil {
    95  				t.Fatalf("makeTree: %v", err)
    96  			}
    97  		}
    98  	})
    99  }
   100  
   101  func checkMarks(t *testing.T, report bool) {
   102  	walkTree(tree, tree.name, func(path string, n *Node) {
   103  		if n.marks != n.expectedMarks && report {
   104  			t.Errorf("node %s mark = %d; expected %d", path, n.marks, n.expectedMarks)
   105  		}
   106  		n.marks = 0
   107  	})
   108  }
   109  
   110  // Assumes that each node name is unique. Good enough for a test.
   111  // If clear is true, any incoming error is cleared before return. The errors
   112  // are always accumulated, though.
   113  func mark(info os.FileInfo, err error, errors *[]error, clear bool) error {
   114  	if err != nil {
   115  		*errors = append(*errors, err)
   116  		if clear {
   117  			return nil
   118  		}
   119  		return err
   120  	}
   121  	name := info.Name()
   122  	walkTree(tree, tree.name, func(path string, n *Node) {
   123  		if n.name == name {
   124  			n.marks++
   125  		}
   126  	})
   127  	return nil
   128  }
   129  
   130  func TestWalk(t *testing.T) {
   131  	makeTree(t)
   132  	errors := make([]error, 0, 10)
   133  	clear := true
   134  	markFn := func(path string, info os.FileInfo, err error) error {
   135  		return mark(info, err, &errors, clear)
   136  	}
   137  	// Expect no errors.
   138  	err := Walk(tree.name, markFn)
   139  	if err != nil {
   140  		t.Fatalf("no error expected, found: %s", err)
   141  	}
   142  	if len(errors) != 0 {
   143  		t.Fatalf("unexpected errors: %s", errors)
   144  	}
   145  	checkMarks(t, true)
   146  
   147  	// cleanup
   148  	if err := os.RemoveAll(tree.name); err != nil {
   149  		t.Errorf("removeTree: %v", err)
   150  	}
   151  }