gitee.com/mirrors_u-root/u-root@v7.0.0+incompatible/pkg/find/find_test.go (about)

     1  // Copyright 2015-2017 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package find
     6  
     7  import (
     8  	"context"
     9  	"io/ioutil"
    10  	"os"
    11  	"path/filepath"
    12  	"reflect"
    13  	"strings"
    14  	"syscall"
    15  	"testing"
    16  )
    17  
    18  func TestSimple(t *testing.T) {
    19  	type tests struct {
    20  		name  string
    21  		opts  Set
    22  		names []string
    23  	}
    24  
    25  	var testCases = []tests{
    26  		{
    27  			name: "basic find",
    28  			opts: nil,
    29  			names: []string{
    30  				"",
    31  				"/root",
    32  				"/root/xyz",
    33  				"/root/xyz/0777",
    34  				"/root/xyz/file",
    35  			},
    36  		},
    37  		{
    38  			name: "just a dir",
    39  			opts: WithModeMatch(os.ModeDir, os.ModeDir),
    40  			names: []string{
    41  				"",
    42  				"/root",
    43  				"/root/xyz",
    44  			},
    45  		},
    46  		{
    47  			name: "just a file",
    48  			opts: WithModeMatch(0, os.ModeType),
    49  			names: []string{
    50  				"/root/xyz/0777",
    51  				"/root/xyz/file",
    52  			},
    53  		},
    54  		{
    55  			name:  "file by mode",
    56  			opts:  WithModeMatch(0444, os.ModePerm),
    57  			names: []string{"/root/xyz/0777"},
    58  		},
    59  		{
    60  			name:  "file by name",
    61  			opts:  WithFilenameMatch("*file"),
    62  			names: []string{"/root/xyz/file"},
    63  		},
    64  	}
    65  	d, err := ioutil.TempDir(os.TempDir(), "u-root.cmds.find")
    66  	if err != nil {
    67  		t.Fatal(err)
    68  	}
    69  	defer os.RemoveAll(d)
    70  
    71  	// Make sure files are actually created with the permissions we ask for.
    72  	syscall.Umask(0)
    73  	if err := os.MkdirAll(filepath.Join(d, "root/xyz"), 0775); err != nil {
    74  		t.Fatal(err)
    75  	}
    76  	if err := ioutil.WriteFile(filepath.Join(d, "root/xyz/file"), nil, 0664); err != nil {
    77  		t.Fatal(err)
    78  	}
    79  	if err := ioutil.WriteFile(filepath.Join(d, "root/xyz/0777"), nil, 0444); err != nil {
    80  		t.Fatal(err)
    81  	}
    82  
    83  	for _, tc := range testCases {
    84  		t.Run(tc.name, func(t *testing.T) {
    85  			ctx := context.Background()
    86  			files := Find(ctx, WithRoot(d), tc.opts)
    87  
    88  			var names []string
    89  			for o := range files {
    90  				if o.Err != nil {
    91  					t.Errorf("%v: got %v, want nil", o.Name, o.Err)
    92  				}
    93  				names = append(names, strings.TrimPrefix(o.Name, d))
    94  			}
    95  
    96  			if len(names) != len(tc.names) {
    97  				t.Errorf("Find output: got %d bytes, want %d bytes", len(names), len(tc.names))
    98  			}
    99  			if !reflect.DeepEqual(names, tc.names) {
   100  				t.Errorf("Find output: got %v, want %v", names, tc.names)
   101  			}
   102  		})
   103  	}
   104  }