Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
mapfs_test.go126 linesDownload Raw Back to fstest
1// Copyright 2020 The Go Authors. All rights reserved.2// Use of this source code is governed by a BSD-style3// license that can be found in the LICENSE file.4 5package fstest6 7import (8	"fmt"9	"io/fs"10	"strings"11	"testing"12)13 14func TestMapFS(t *testing.T) {15	m := MapFS{16		"hello":             {Data: []byte("hello, world\n")},17		"fortune/k/ken.txt": {Data: []byte("If a program is too slow, it must have a loop.\n")},18	}19	if err := TestFS(m, "hello", "fortune", "fortune/k", "fortune/k/ken.txt"); err != nil {20		t.Fatal(err)21	}22}23 24func TestMapFSChmodDot(t *testing.T) {25	m := MapFS{26		"a/b.txt": &MapFile{Mode: 0666},27		".":       &MapFile{Mode: 0777 | fs.ModeDir},28	}29	buf := new(strings.Builder)30	fs.WalkDir(m, ".", func(path string, d fs.DirEntry, err error) error {31		fi, err := d.Info()32		if err != nil {33			return err34		}35		fmt.Fprintf(buf, "%s: %v\n", path, fi.Mode())36		return nil37	})38	want := `39.: drwxrwxrwx40a: dr-xr-xr-x41a/b.txt: -rw-rw-rw-42`[1:]43	got := buf.String()44	if want != got {45		t.Errorf("MapFS modes want:\n%s\ngot:\n%s\n", want, got)46	}47}48 49func TestMapFSFileInfoName(t *testing.T) {50	m := MapFS{51		"path/to/b.txt": &MapFile{},52	}53	info, _ := m.Stat("path/to/b.txt")54	want := "b.txt"55	got := info.Name()56	if want != got {57		t.Errorf("MapFS FileInfo.Name want:\n%s\ngot:\n%s\n", want, got)58	}59}60 61func TestMapFSSymlink(t *testing.T) {62	const fileContent = "If a program is too slow, it must have a loop.\n"63	m := MapFS{64		"fortune/k/ken.txt": {Data: []byte(fileContent)},65		"dirlink":           {Data: []byte("fortune/k"), Mode: fs.ModeSymlink},66		"linklink":          {Data: []byte("dirlink"), Mode: fs.ModeSymlink},67		"ken.txt":           {Data: []byte("dirlink/ken.txt"), Mode: fs.ModeSymlink},68	}69	if err := TestFS(m, "fortune/k/ken.txt", "dirlink", "ken.txt", "linklink"); err != nil {70		t.Error(err)71	}72 73	gotData, err := fs.ReadFile(m, "ken.txt")74	if string(gotData) != fileContent || err != nil {75		t.Errorf("fs.ReadFile(m, \"ken.txt\") = %q, %v; want %q, <nil>", gotData, err, fileContent)76	}77	gotLink, err := fs.ReadLink(m, "dirlink")78	if want := "fortune/k"; gotLink != want || err != nil {79		t.Errorf("fs.ReadLink(m, \"dirlink\") = %q, %v; want %q, <nil>", gotLink, err, fileContent)80	}81	gotInfo, err := fs.Lstat(m, "dirlink")82	if err != nil {83		t.Errorf("fs.Lstat(m, \"dirlink\") = _, %v; want _, <nil>", err)84	} else {85		if got, want := gotInfo.Name(), "dirlink"; got != want {86			t.Errorf("fs.Lstat(m, \"dirlink\").Name() = %q; want %q", got, want)87		}88		if got, want := gotInfo.Mode(), fs.ModeSymlink; got != want {89			t.Errorf("fs.Lstat(m, \"dirlink\").Mode() = %v; want %v", got, want)90		}91	}92	gotInfo, err = fs.Stat(m, "dirlink")93	if err != nil {94		t.Errorf("fs.Stat(m, \"dirlink\") = _, %v; want _, <nil>", err)95	} else {96		if got, want := gotInfo.Name(), "dirlink"; got != want {97			t.Errorf("fs.Stat(m, \"dirlink\").Name() = %q; want %q", got, want)98		}99		if got, want := gotInfo.Mode(), fs.ModeDir|0555; got != want {100			t.Errorf("fs.Stat(m, \"dirlink\").Mode() = %v; want %v", got, want)101		}102	}103	gotInfo, err = fs.Lstat(m, "linklink")104	if err != nil {105		t.Errorf("fs.Lstat(m, \"linklink\") = _, %v; want _, <nil>", err)106	} else {107		if got, want := gotInfo.Name(), "linklink"; got != want {108			t.Errorf("fs.Lstat(m, \"linklink\").Name() = %q; want %q", got, want)109		}110		if got, want := gotInfo.Mode(), fs.ModeSymlink; got != want {111			t.Errorf("fs.Lstat(m, \"linklink\").Mode() = %v; want %v", got, want)112		}113	}114	gotInfo, err = fs.Stat(m, "linklink")115	if err != nil {116		t.Errorf("fs.Stat(m, \"linklink\") = _, %v; want _, <nil>", err)117	} else {118		if got, want := gotInfo.Name(), "linklink"; got != want {119			t.Errorf("fs.Stat(m, \"linklink\").Name() = %q; want %q", got, want)120		}121		if got, want := gotInfo.Mode(), fs.ModeDir|0555; got != want {122			t.Errorf("fs.Stat(m, \"linklink\").Mode() = %v; want %v", got, want)123		}124	}125}126 
codekingpro/portable-devtools · Team Ai