1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
|
// Package vfst provides helper functions for testing code that uses
// github.com/twpayne/go-vfs.
package vfst
import (
"bytes"
"errors"
"fmt"
"io/fs"
"log"
"path/filepath"
"sort"
"strconv"
"testing"
vfs "github.com/twpayne/go-vfs/v4"
)
//nolint:gochecknoglobals
var umask fs.FileMode
// A Dir is a directory with a specified permissions and zero or more Entries.
type Dir struct {
Perm fs.FileMode
Entries map[string]interface{}
}
// A File is a file with a specified permissions and contents.
type File struct {
Perm fs.FileMode
Contents []byte
}
// A Symlink is a symbolic link with a specified target.
type Symlink struct {
Target string
}
// A Test is a test on an vfs.FS.
type Test func(*testing.T, vfs.FS)
// A PathTest is a test on a specified path in an vfs.FS.
type PathTest func(*testing.T, vfs.FS, string)
// A BuilderOption sets an option on a Builder.
type BuilderOption func(*Builder)
// A Builder populates an vfs.FS.
type Builder struct {
umask fs.FileMode
verbose bool
}
// BuilderUmask sets a builder's umask.
func BuilderUmask(umask fs.FileMode) BuilderOption {
return func(b *Builder) {
b.umask = umask
}
}
// BuilderVerbose sets a builder's verbose flag. When true, the builder will
// log all operations with the standard log package.
func BuilderVerbose(verbose bool) BuilderOption {
return func(b *Builder) {
b.verbose = verbose
}
}
// NewBuilder returns a new Builder with the given options set.
func NewBuilder(options ...BuilderOption) *Builder {
b := &Builder{
umask: umask,
verbose: false,
}
for _, option := range options {
option(b)
}
return b
}
// build is a recursive helper for Build.
func (b *Builder) build(fileSystem vfs.FS, path string, i interface{}) error {
switch i := i.(type) {
case []interface{}:
for _, element := range i {
if err := b.build(fileSystem, path, element); err != nil {
return err
}
}
return nil
case *Dir:
if parentDir := filepath.Dir(path); parentDir != "." {
if err := b.MkdirAll(fileSystem, parentDir, 0o777); err != nil {
return err
}
}
if err := b.Mkdir(fileSystem, path, i.Perm); err != nil {
return err
}
entryNames := make([]string, 0, len(i.Entries))
for entryName := range i.Entries {
entryNames = append(entryNames, entryName)
}
sort.Strings(entryNames)
for _, entryName := range entryNames {
if err := b.build(fileSystem, filepath.Join(path, entryName), i.Entries[entryName]); err != nil {
return err
}
}
return nil
case map[string]interface{}:
if err := b.MkdirAll(fileSystem, path, 0o777); err != nil {
return err
}
entryNames := make([]string, 0, len(i))
for entryName := range i {
entryNames = append(entryNames, entryName)
}
sort.Strings(entryNames)
for _, entryName := range entryNames {
if err := b.build(fileSystem, filepath.Join(path, entryName), i[entryName]); err != nil {
return err
}
}
return nil
case map[string]string:
if err := b.MkdirAll(fileSystem, path, 0o777); err != nil {
return err
}
entryNames := make([]string, 0, len(i))
for entryName := range i {
entryNames = append(entryNames, entryName)
}
sort.Strings(entryNames)
for _, entryName := range entryNames {
if err := b.WriteFile(fileSystem, filepath.Join(path, entryName), []byte(i[entryName]), 0o666); err != nil {
return err
}
}
return nil
case *File:
return b.WriteFile(fileSystem, path, i.Contents, i.Perm)
case string:
return b.WriteFile(fileSystem, path, []byte(i), 0o666)
case []byte:
return b.WriteFile(fileSystem, path, i, 0o666)
case *Symlink:
return b.Symlink(fileSystem, i.Target, path)
case nil:
return nil
default:
return fmt.Errorf("%s: unsupported type %T", path, i)
}
}
// Build populates fileSystem from root.
func (b *Builder) Build(fileSystem vfs.FS, root interface{}) error {
return b.build(fileSystem, "/", root)
}
// Mkdir creates directory path with permissions perm. It is idempotent and
// will not fail if path already exists, is a directory, and has permissions
// perm.
func (b *Builder) Mkdir(fileSystem vfs.FS, path string, perm fs.FileMode) error {
if info, err := fileSystem.Lstat(path); errors.Is(err, fs.ErrNotExist) {
if b.verbose {
log.Printf("mkdir -m 0%o %s", perm&^b.umask, path)
}
return fileSystem.Mkdir(path, perm&^b.umask)
} else if err != nil {
return err
} else if !info.IsDir() {
return fmt.Errorf("%s: not a directory", path)
} else if gotPerm, wantPerm := info.Mode()&fs.ModePerm, perm&^b.umask; !permEqual(gotPerm, wantPerm) {
return fmt.Errorf("%s has permissions 0%o, want 0%o", path, gotPerm, wantPerm)
}
return nil
}
// MkdirAll creates directory path and any missing parent directories with
// permissions perm. It is idempotent and will not file if path already exists
// and is a directory.
func (b *Builder) MkdirAll(fileSystem vfs.FS, path string, perm fs.FileMode) error {
// Check path.
info, err := fileSystem.Lstat(path)
switch {
case err != nil && errors.Is(err, fs.ErrNotExist):
// path does not exist, fallthrough to create.
case err == nil && info.IsDir():
// path already exists and is a directory.
return nil
case err == nil && !info.IsDir():
// path already exists, but is not a directory.
return err
default:
// Some other error.
return err
}
// Create path.
if b.verbose {
log.Printf("mkdir -p -m 0%o %s", perm&^b.umask, path)
}
return vfs.MkdirAll(fileSystem, path, perm&^b.umask)
}
// Symlink creates a symbolic link from newname to oldname. It will create any
// missing parent directories with default permissions. It is idempotent and
// will not fail if the symbolic link already exists and points to oldname.
func (b *Builder) Symlink(fileSystem vfs.FS, oldname, newname string) error {
// Check newname.
info, err := fileSystem.Lstat(newname)
switch {
case err == nil && info.Mode()&fs.ModeType != fs.ModeSymlink:
// newname exists, but it's not a symlink.
return fmt.Errorf("%s: not a symbolic link", newname)
case err == nil:
// newname exists, and it's a symlink. Check that it is a symlink to
// oldname.
gotTarget, err := fileSystem.Readlink(newname)
if err != nil {
return err
}
if gotTarget != oldname {
return fmt.Errorf("%s: has target %s, want %s", newname, gotTarget, oldname)
}
return nil
case errors.Is(err, fs.ErrNotExist):
// newname does not exist, fallthrough to create.
default:
// Some other error, return it.
return err
}
// Create newname.
if err := b.MkdirAll(fileSystem, filepath.Dir(newname), 0o777); err != nil {
return err
}
if b.verbose {
log.Printf("ln -s %s %s", oldname, newname)
}
return fileSystem.Symlink(oldname, newname)
}
// WriteFile writes file path withe contents contents and permissions perm. It
// will create any missing parent directories with default permissions. It is
// idempotent and will not fail if the file already exists, has contents
// contents, and permissions perm.
func (b *Builder) WriteFile(fileSystem vfs.FS, path string, contents []byte, perm fs.FileMode) error {
if info, err := fileSystem.Lstat(path); errors.Is(err, fs.ErrNotExist) {
// fallthrough to fileSystem.WriteFile
} else if err != nil {
return err
} else if !info.Mode().IsRegular() {
return fmt.Errorf("%s: not a regular file", path)
} else if gotPerm, wantPerm := info.Mode()&fs.ModePerm, perm&^b.umask; !permEqual(gotPerm, wantPerm) {
return fmt.Errorf("%s has permissions 0%o, want 0%o", path, gotPerm, wantPerm)
} else {
gotContents, err := fileSystem.ReadFile(path)
if err != nil {
return err
}
if !bytes.Equal(gotContents, contents) {
return fmt.Errorf("%s: has contents %v, want %v", path, gotContents, contents)
}
return nil
}
if err := b.MkdirAll(fileSystem, filepath.Dir(path), 0o777); err != nil {
return err
}
if b.verbose {
log.Printf("install -m 0%o /dev/null %s", perm&^b.umask, path)
}
return fileSystem.WriteFile(path, contents, perm&^b.umask)
}
// runTests recursively runs tests on fileSystem.
func runTests(t *testing.T, fileSystem vfs.FS, name string, test interface{}) {
t.Helper()
prefix := ""
if name != "" {
prefix = name + "_"
}
switch test := test.(type) {
case Test:
test(t, fileSystem)
case []Test:
for i, test := range test {
t.Run(prefix+strconv.Itoa(i), func(t *testing.T) {
//nolint:scopelint
test(t, fileSystem)
})
}
case map[string]Test:
testNames := make([]string, 0, len(test))
for testName := range test {
testNames = append(testNames, testName)
}
sort.Strings(testNames)
for _, testName := range testNames {
t.Run(prefix+testName, func(t *testing.T) {
//nolint:scopelint
test[testName](t, fileSystem)
})
}
case []interface{}:
for _, u := range test {
runTests(t, fileSystem, name, u)
}
case map[string]interface{}:
testNames := make([]string, 0, len(test))
for testName := range test {
testNames = append(testNames, testName)
}
sort.Strings(testNames)
for _, testName := range testNames {
runTests(t, fileSystem, prefix+testName, test[testName])
}
case nil:
default:
t.Fatalf("%s: unsupported type %T", name, test)
}
}
// RunTests recursively runs tests on fileSystem.
func RunTests(t *testing.T, fileSystem vfs.FS, name string, tests ...interface{}) {
t.Helper()
runTests(t, fileSystem, name, tests)
}
// TestContents returns a PathTest that verifies the contents of the file are
// equal to wantContents.
func TestContents(wantContents []byte) PathTest {
return func(t *testing.T, fileSystem vfs.FS, path string) {
t.Helper()
if gotContents, err := fileSystem.ReadFile(path); err != nil || !bytes.Equal(gotContents, wantContents) {
t.Errorf("fileSystem.ReadFile(%q) == %v, %v, want %v, <nil>", path, gotContents, err, wantContents)
}
}
}
// TestContentsString returns a PathTest that verifies the contetnts of the
// file are equal to wantContentsStr.
func TestContentsString(wantContentsStr string) PathTest {
return func(t *testing.T, fileSystem vfs.FS, path string) {
t.Helper()
if gotContents, err := fileSystem.ReadFile(path); err != nil || string(gotContents) != wantContentsStr {
t.Errorf("fileSystem.ReadFile(%q) == %q, %v, want %q, <nil>", path, gotContents, err, wantContentsStr)
}
}
}
// testDoesNotExist is a PathTest that verifies that a file or directory does
// not exist.
//nolint:gochecknoglobals
var testDoesNotExist = func(t *testing.T, fileSystem vfs.FS, path string) {
t.Helper()
_, err := fileSystem.Lstat(path)
if got, want := errors.Is(err, fs.ErrNotExist), true; got != want {
t.Errorf("_, err := fileSystem.Lstat(%q); errors.Is(err, fs.ErrNotExist) == %v, want %v", path, got, want)
}
}
// TestDoesNotExist is a PathTest that verifies that a file or directory does
// not exist.
//nolint:gochecknoglobals
var TestDoesNotExist PathTest = testDoesNotExist
// TestIsDir is a PathTest that verifies that the path is a directory.
//nolint:gochecknoglobals
var TestIsDir = TestModeType(fs.ModeDir)
// TestModePerm returns a PathTest that verifies that the path's permissions
// are equal to wantPerm.
func TestModePerm(wantPerm fs.FileMode) PathTest {
return func(t *testing.T, fileSystem vfs.FS, path string) {
t.Helper()
info, err := fileSystem.Lstat(path)
if err != nil {
t.Errorf("fileSystem.Lstat(%q) == %+v, %v, want !<nil>, <nil>", path, info, err)
return
}
if gotPerm := info.Mode() & fs.ModePerm; !permEqual(gotPerm, wantPerm) {
t.Errorf("fileSystem.Lstat(%q).Mode()&fs.ModePerm == 0%o, want 0%o", path, gotPerm, wantPerm)
}
}
}
// TestModeIsRegular is a PathTest that tests that the path is a regular file.
//nolint:gochecknoglobals
var TestModeIsRegular = TestModeType(0)
// TestModeType returns a PathTest that verifies that the path's mode type is
// equal to wantModeType.
func TestModeType(wantModeType fs.FileMode) PathTest {
return func(t *testing.T, fileSystem vfs.FS, path string) {
t.Helper()
info, err := fileSystem.Lstat(path)
if err != nil {
t.Errorf("fileSystem.Lstat(%q) == %+v, %v, want !<nil>, <nil>", path, info, err)
return
}
if gotModeType := info.Mode() & fs.ModeType; gotModeType != wantModeType {
t.Errorf("fileSystem.Lstat(%q).Mode()&fs.ModeType == %v, want %v", path, gotModeType, wantModeType)
}
}
}
// TestPath returns a Test that runs pathTests on path.
func TestPath(path string, pathTests ...PathTest) Test {
return func(t *testing.T, fileSystem vfs.FS) {
t.Helper()
for i, pathTest := range pathTests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
//nolint:scopelint
pathTest(t, fileSystem, path)
})
}
}
}
// TestSize returns a PathTest that tests that path's Size() is equal to
// wantSize.
func TestSize(wantSize int64) PathTest {
return func(t *testing.T, fileSystem vfs.FS, path string) {
t.Helper()
info, err := fileSystem.Lstat(path)
if err != nil {
t.Errorf("fileSystem.Lstat(%q) == %+v, %v, want !<nil>, <nil>", path, info, err)
return
}
if gotSize := info.Size(); gotSize != wantSize {
t.Errorf("fileSystem.Lstat(%q).Size() == %d, want %d", path, gotSize, wantSize)
}
}
}
// TestSymlinkTarget returns a PathTest that tests that path's target is wantTarget.
func TestSymlinkTarget(wantTarget string) PathTest {
return func(t *testing.T, fileSystem vfs.FS, path string) {
t.Helper()
if gotTarget, err := fileSystem.Readlink(path); err != nil || gotTarget != wantTarget {
t.Errorf("fileSystem.Readlink(%q) == %q, %v, want %q, <nil>", path, gotTarget, err, wantTarget)
return
}
}
}
// TestMinSize returns a PathTest that tests that path's Size() is at least
// wantMinSize.
func TestMinSize(wantMinSize int64) PathTest {
return func(t *testing.T, fileSystem vfs.FS, path string) {
t.Helper()
info, err := fileSystem.Lstat(path)
if err != nil {
t.Errorf("fileSystem.Lstat(%q) == %+v, %v, want !<nil>, <nil>", path, info, err)
return
}
if gotSize := info.Size(); gotSize < wantMinSize {
t.Errorf("fileSystem.Lstat(%q).Size() == %d, want >=%d", path, gotSize, wantMinSize)
}
}
}
|