kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/platform/vfs/vfs_test.go (about)

     1  /*
     2   * Copyright 2020 The Kythe Authors. All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *   http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package vfs
    18  
    19  import (
    20  	"context"
    21  	"io/ioutil"
    22  	"os"
    23  	"path/filepath"
    24  	"testing"
    25  
    26  	"kythe.io/kythe/go/test/testutil"
    27  )
    28  
    29  type walkResult struct {
    30  	Path string
    31  	Info string
    32  }
    33  
    34  const testDataDir = "testdata"
    35  
    36  var files = []string{
    37  	"a/b/*/c/empty.txt",
    38  	"a/b/c/d/empty.txt",
    39  }
    40  
    41  func tempDir() string {
    42  	root := os.Getenv("TEST_TMPDIR")
    43  	if len(root) > 0 {
    44  		return root
    45  	}
    46  	return os.TempDir()
    47  }
    48  
    49  func populateFiles(t *testing.T) (string, func()) {
    50  	t.Helper()
    51  	root := filepath.Join(tempDir(), testDataDir)
    52  	for _, file := range files {
    53  		path := filepath.Join(root, filepath.FromSlash(file))
    54  		testutil.Fatalf(t, "unable to create directory", os.MkdirAll(filepath.Dir(path), 0777))
    55  		testutil.Fatalf(t, "unable to write file", ioutil.WriteFile(path, nil, 0777))
    56  	}
    57  	return root, func() {
    58  		testutil.Fatalf(t, "failed to remove test data", os.RemoveAll(root))
    59  	}
    60  }
    61  
    62  func TestGlobWalker(t *testing.T) {
    63  	root, cleanup := populateFiles(t)
    64  	defer cleanup()
    65  
    66  	byglob, err := collectFiles(&globWalker{LocalFS{}}, root)
    67  	if len(byglob) == 0 {
    68  		t.Fatal("no files found")
    69  	}
    70  	testutil.Fatalf(t, "unable to collect files from glob", err)
    71  	bylocal, err := collectFiles(LocalFS{}, root)
    72  	testutil.Fatalf(t, "unable to collect local files", err)
    73  	testutil.Fatalf(t, "local and glob walks differ", testutil.DeepEqual(bylocal, byglob))
    74  }
    75  
    76  func collectFiles(walk Walker, root string) ([]walkResult, error) {
    77  	var results []walkResult
    78  	return results, walk.Walk(context.Background(), root, func(path string, info os.FileInfo, err error) error {
    79  		if err != nil {
    80  			return err
    81  		}
    82  		results = append(results, walkResult{path, info.Name()})
    83  		return nil
    84  	})
    85  }