github.com/x-helm/helm@v3.0.0-beta.3+incompatible/internal/sympath/walk_test.go (about)

     1  /*
     2  Copyright (c) for portions of walk_test.go are held by The Go Authors, 2009 and are
     3  provided under 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  	mark    int
    33  }
    34  
    35  var tree = &Node{
    36  	"testdata",
    37  	[]*Node{
    38  		{"a", nil, 0},
    39  		{"b", []*Node{}, 0},
    40  		{"c", nil, 0},
    41  		{
    42  			"d",
    43  			[]*Node{
    44  				{"x", nil, 0},
    45  				{"y", []*Node{}, 0},
    46  				{
    47  					"z",
    48  					[]*Node{
    49  						{"u", nil, 0},
    50  						{"v", nil, 0},
    51  						{"w", nil, 0},
    52  					},
    53  					0,
    54  				},
    55  			},
    56  			0,
    57  		},
    58  	},
    59  	0,
    60  }
    61  
    62  func walkTree(n *Node, path string, f func(path string, n *Node)) {
    63  	f(path, n)
    64  	for _, e := range n.entries {
    65  		walkTree(e, filepath.Join(path, e.name), f)
    66  	}
    67  }
    68  
    69  func makeTree(t *testing.T) {
    70  	walkTree(tree, tree.name, func(path string, n *Node) {
    71  		if n.entries == nil {
    72  			fd, err := os.Create(path)
    73  			if err != nil {
    74  				t.Errorf("makeTree: %v", err)
    75  				return
    76  			}
    77  			fd.Close()
    78  		} else {
    79  			os.Mkdir(path, 0770)
    80  		}
    81  	})
    82  }
    83  
    84  func checkMarks(t *testing.T, report bool) {
    85  	walkTree(tree, tree.name, func(path string, n *Node) {
    86  		if n.mark != 1 && report {
    87  			t.Errorf("node %s mark = %d; expected 1", path, n.mark)
    88  		}
    89  		n.mark = 0
    90  	})
    91  }
    92  
    93  // Assumes that each node name is unique. Good enough for a test.
    94  // If clear is true, any incoming error is cleared before return. The errors
    95  // are always accumulated, though.
    96  func mark(info os.FileInfo, err error, errors *[]error, clear bool) error {
    97  	if err != nil {
    98  		*errors = append(*errors, err)
    99  		if clear {
   100  			return nil
   101  		}
   102  		return err
   103  	}
   104  	name := info.Name()
   105  	walkTree(tree, tree.name, func(path string, n *Node) {
   106  		if n.name == name {
   107  			n.mark++
   108  		}
   109  	})
   110  	return nil
   111  }
   112  
   113  func TestWalk(t *testing.T) {
   114  	makeTree(t)
   115  	errors := make([]error, 0, 10)
   116  	clear := true
   117  	markFn := func(path string, info os.FileInfo, err error) error {
   118  		return mark(info, err, &errors, clear)
   119  	}
   120  	// Expect no errors.
   121  	err := Walk(tree.name, markFn)
   122  	if err != nil {
   123  		t.Fatalf("no error expected, found: %s", err)
   124  	}
   125  	if len(errors) != 0 {
   126  		t.Fatalf("unexpected errors: %s", errors)
   127  	}
   128  	checkMarks(t, true)
   129  
   130  	// cleanup
   131  	if err := os.RemoveAll(tree.name); err != nil {
   132  		t.Errorf("removeTree: %v", err)
   133  	}
   134  }