...

Source file src/github.com/shurcooL/httpfs/filter/filter_test.go

Documentation: github.com/shurcooL/httpfs/filter

     1  package filter_test
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"log"
     7  	"net/http"
     8  	"os"
     9  	pathpkg "path"
    10  	"strings"
    11  
    12  	"github.com/shurcooL/httpfs/filter"
    13  	"github.com/shurcooL/httpfs/vfsutil"
    14  	"golang.org/x/tools/godoc/vfs/httpfs"
    15  	"golang.org/x/tools/godoc/vfs/mapfs"
    16  )
    17  
    18  func ExampleKeep() {
    19  	var srcFS http.FileSystem
    20  
    21  	// Keep only "/target/dir" and its contents.
    22  	fs := filter.Keep(srcFS, func(path string, fi os.FileInfo) bool {
    23  		return path == "/" ||
    24  			path == "/target" ||
    25  			path == "/target/dir" ||
    26  			strings.HasPrefix(path, "/target/dir/")
    27  	})
    28  
    29  	_ = fs
    30  }
    31  
    32  func ExampleSkip() {
    33  	var srcFS http.FileSystem
    34  
    35  	// Skip all files named ".DS_Store".
    36  	fs := filter.Skip(srcFS, func(path string, fi os.FileInfo) bool {
    37  		return !fi.IsDir() && fi.Name() == ".DS_Store"
    38  	})
    39  
    40  	_ = fs
    41  }
    42  
    43  func Example_detailed() {
    44  	srcFS := httpfs.New(mapfs.New(map[string]string{
    45  		"zzz-last-file.txt":                "It should be visited last.",
    46  		"a-file.txt":                       "It has stuff.",
    47  		"another-file.txt":                 "Also stuff.",
    48  		"some-file.html":                   "<html>and stuff</html>",
    49  		"folderA/entry-A.txt":              "Alpha.",
    50  		"folderA/entry-B.txt":              "Beta.",
    51  		"folderA/main.go":                  "package main\n",
    52  		"folderA/folder-to-skip/many.txt":  "Entire folder can be skipped.",
    53  		"folderA/folder-to-skip/files.txt": "Entire folder can be skipped.",
    54  		"folder-to-skip":                   "This is a file, not a folder, and shouldn't be skipped.",
    55  	}))
    56  
    57  	// Skip files with .go and .html extensions, and directories named "folder-to-skip" (but
    58  	// not files named "folder-to-skip").
    59  	fs := filter.Skip(srcFS, func(path string, fi os.FileInfo) bool {
    60  		return pathpkg.Ext(fi.Name()) == ".go" || pathpkg.Ext(fi.Name()) == ".html" ||
    61  			(fi.IsDir() && fi.Name() == "folder-to-skip")
    62  	})
    63  
    64  	err := vfsutil.Walk(fs, "/", func(path string, fi os.FileInfo, err error) error {
    65  		if err != nil {
    66  			log.Printf("can't stat file %s: %v\n", path, err)
    67  			return nil
    68  		}
    69  		fmt.Println(path)
    70  		return nil
    71  	})
    72  	if err != nil {
    73  		panic(err)
    74  	}
    75  
    76  	fmt.Println()
    77  
    78  	// This file should be filtered out, even if accessed directly.
    79  	_, err = fs.Open("/folderA/main.go")
    80  	fmt.Println("os.IsNotExist(err):", os.IsNotExist(err))
    81  	fmt.Println(err)
    82  
    83  	fmt.Println()
    84  
    85  	// This folder should be filtered out, even if accessed directly.
    86  	_, err = fs.Open("/folderA/folder-to-skip")
    87  	fmt.Println("os.IsNotExist(err):", os.IsNotExist(err))
    88  	fmt.Println(err)
    89  
    90  	fmt.Println()
    91  
    92  	// This file should not be filtered out.
    93  	f, err := fs.Open("/folder-to-skip")
    94  	if err != nil {
    95  		panic(err)
    96  	}
    97  	io.Copy(os.Stdout, f)
    98  	f.Close()
    99  
   100  	// Output:
   101  	// /
   102  	// /a-file.txt
   103  	// /another-file.txt
   104  	// /folder-to-skip
   105  	// /folderA
   106  	// /folderA/entry-A.txt
   107  	// /folderA/entry-B.txt
   108  	// /zzz-last-file.txt
   109  	//
   110  	// os.IsNotExist(err): true
   111  	// open /folderA/main.go: file does not exist
   112  	//
   113  	// os.IsNotExist(err): true
   114  	// open /folderA/folder-to-skip: file does not exist
   115  	//
   116  	// This is a file, not a folder, and shouldn't be skipped.
   117  }
   118  

View as plain text