...

Source file src/github.com/shurcooL/httpfs/union/union_test.go

Documentation: github.com/shurcooL/httpfs/union

     1  package union_test
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"net/http"
     7  	"os"
     8  
     9  	"github.com/shurcooL/httpfs/union"
    10  	"github.com/shurcooL/httpfs/vfsutil"
    11  	"golang.org/x/tools/godoc/vfs/httpfs"
    12  	"golang.org/x/tools/godoc/vfs/mapfs"
    13  )
    14  
    15  func walk(path string, fi os.FileInfo, err error) error {
    16  	if err != nil {
    17  		log.Printf("can't stat file %s: %v\n", path, err)
    18  		return nil
    19  	}
    20  	fmt.Println(path)
    21  	return nil
    22  }
    23  
    24  func Example() {
    25  	fs0 := httpfs.New(mapfs.New(map[string]string{
    26  		"zzz-last-file.txt":   "It should be visited last.",
    27  		"a-file.txt":          "It has stuff.",
    28  		"another-file.txt":    "Also stuff.",
    29  		"folderA/entry-A.txt": "Alpha.",
    30  		"folderA/entry-B.txt": "Beta.",
    31  	}))
    32  	fs1 := httpfs.New(mapfs.New(map[string]string{
    33  		"sample-file.txt":                "This file compresses well. Blaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah!",
    34  		"not-worth-compressing-file.txt": "Its normal contents are here.",
    35  		"folderA/file1.txt":              "Stuff 1.",
    36  		"folderA/file2.txt":              "Stuff 2.",
    37  		"folderB/folderC/file3.txt":      "Stuff C-3.",
    38  	}))
    39  
    40  	fs := union.New(map[string]http.FileSystem{
    41  		"/fs0": fs0,
    42  		"/fs1": fs1,
    43  	})
    44  
    45  	err := vfsutil.Walk(fs, "/", walk)
    46  	if err != nil {
    47  		panic(err)
    48  	}
    49  
    50  	// Output:
    51  	// /
    52  	// /fs0
    53  	// /fs0/a-file.txt
    54  	// /fs0/another-file.txt
    55  	// /fs0/folderA
    56  	// /fs0/folderA/entry-A.txt
    57  	// /fs0/folderA/entry-B.txt
    58  	// /fs0/zzz-last-file.txt
    59  	// /fs1
    60  	// /fs1/folderA
    61  	// /fs1/folderA/file1.txt
    62  	// /fs1/folderA/file2.txt
    63  	// /fs1/folderB
    64  	// /fs1/folderB/folderC
    65  	// /fs1/folderB/folderC/file3.txt
    66  	// /fs1/not-worth-compressing-file.txt
    67  	// /fs1/sample-file.txt
    68  }
    69  
    70  func Example_empty() {
    71  	empty := union.New(nil)
    72  
    73  	err := vfsutil.Walk(empty, "/", walk)
    74  	if err != nil {
    75  		panic(err)
    76  	}
    77  
    78  	// Output:
    79  	// /
    80  }
    81  
    82  func Example_notExist() {
    83  	empty := union.New(nil)
    84  
    85  	_, err := empty.Open("/does-not-exist")
    86  	fmt.Println("os.IsNotExist:", os.IsNotExist(err))
    87  	fmt.Println(err)
    88  
    89  	// Output:
    90  	// os.IsNotExist: true
    91  	// open /does-not-exist: file does not exist
    92  }
    93  

View as plain text