...

Source file src/go.etcd.io/etcd/client/pkg/v3/fileutil/read_dir_test.go

Documentation: go.etcd.io/etcd/client/pkg/v3/fileutil

     1  // Copyright 2018 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package fileutil
    16  
    17  import (
    18  	"io/ioutil"
    19  	"os"
    20  	"path/filepath"
    21  	"reflect"
    22  	"testing"
    23  )
    24  
    25  func TestReadDir(t *testing.T) {
    26  	tmpdir, err := ioutil.TempDir("", "")
    27  	defer os.RemoveAll(tmpdir)
    28  	if err != nil {
    29  		t.Fatalf("unexpected ioutil.TempDir error: %v", err)
    30  	}
    31  
    32  	files := []string{"def", "abc", "xyz", "ghi"}
    33  	for _, f := range files {
    34  		writeFunc(t, filepath.Join(tmpdir, f))
    35  	}
    36  	fs, err := ReadDir(tmpdir)
    37  	if err != nil {
    38  		t.Fatalf("error calling ReadDir: %v", err)
    39  	}
    40  	wfs := []string{"abc", "def", "ghi", "xyz"}
    41  	if !reflect.DeepEqual(fs, wfs) {
    42  		t.Fatalf("ReadDir: got %v, want %v", fs, wfs)
    43  	}
    44  
    45  	files = []string{"def.wal", "abc.wal", "xyz.wal", "ghi.wal"}
    46  	for _, f := range files {
    47  		writeFunc(t, filepath.Join(tmpdir, f))
    48  	}
    49  	fs, err = ReadDir(tmpdir, WithExt(".wal"))
    50  	if err != nil {
    51  		t.Fatalf("error calling ReadDir: %v", err)
    52  	}
    53  	wfs = []string{"abc.wal", "def.wal", "ghi.wal", "xyz.wal"}
    54  	if !reflect.DeepEqual(fs, wfs) {
    55  		t.Fatalf("ReadDir: got %v, want %v", fs, wfs)
    56  	}
    57  }
    58  
    59  func writeFunc(t *testing.T, path string) {
    60  	fh, err := os.Create(path)
    61  	if err != nil {
    62  		t.Fatalf("error creating file: %v", err)
    63  	}
    64  	if err = fh.Close(); err != nil {
    65  		t.Fatalf("error closing file: %v", err)
    66  	}
    67  }
    68  

View as plain text