|
| 1 | +//go:build !go1.21 |
| 2 | + |
| 3 | +// Copyright 2023 The Go Authors. All rights reserved. |
| 4 | +// Use of this source code is governed by a BSD-style |
| 5 | +// license that can be found in the LICENSE file. |
| 6 | + |
| 7 | +// Backport fs.FormatDirEntry tests from go1.21. We don't test |
| 8 | +// the go1.21+ FormatDirEntry function since it just calls the |
| 9 | +// stdlib and we don't want changes in its output to break our |
| 10 | +// tests. |
| 11 | + |
| 12 | +package fmtdirent_test |
| 13 | + |
| 14 | +import ( |
| 15 | + . "io/fs" |
| 16 | + "testing" |
| 17 | + "time" |
| 18 | + |
| 19 | + "github.com/charlievieth/fastwalk/internal/fmtdirent" |
| 20 | +) |
| 21 | + |
| 22 | +// formatTest implements FileInfo to test FormatFileInfo, |
| 23 | +// and implements DirEntry to test FormatDirEntry. |
| 24 | +type formatTest struct { |
| 25 | + name string |
| 26 | + size int64 |
| 27 | + mode FileMode |
| 28 | + modTime time.Time |
| 29 | + isDir bool |
| 30 | +} |
| 31 | + |
| 32 | +func (fs *formatTest) Name() string { |
| 33 | + return fs.name |
| 34 | +} |
| 35 | + |
| 36 | +func (fs *formatTest) Size() int64 { |
| 37 | + return fs.size |
| 38 | +} |
| 39 | + |
| 40 | +func (fs *formatTest) Mode() FileMode { |
| 41 | + return fs.mode |
| 42 | +} |
| 43 | + |
| 44 | +func (fs *formatTest) ModTime() time.Time { |
| 45 | + return fs.modTime |
| 46 | +} |
| 47 | + |
| 48 | +func (fs *formatTest) IsDir() bool { |
| 49 | + return fs.isDir |
| 50 | +} |
| 51 | + |
| 52 | +func (fs *formatTest) Sys() any { |
| 53 | + return nil |
| 54 | +} |
| 55 | + |
| 56 | +func (fs *formatTest) Type() FileMode { |
| 57 | + return fs.mode.Type() |
| 58 | +} |
| 59 | + |
| 60 | +func (fs *formatTest) Info() (FileInfo, error) { |
| 61 | + return fs, nil |
| 62 | +} |
| 63 | + |
| 64 | +var formatTests = []struct { |
| 65 | + input formatTest |
| 66 | + wantDirEntry string |
| 67 | +}{ |
| 68 | + { |
| 69 | + formatTest{ |
| 70 | + name: "hello.go", |
| 71 | + size: 100, |
| 72 | + mode: 0o644, |
| 73 | + modTime: time.Date(1970, time.January, 1, 12, 0, 0, 0, time.UTC), |
| 74 | + isDir: false, |
| 75 | + }, |
| 76 | + "- hello.go", |
| 77 | + }, |
| 78 | + { |
| 79 | + formatTest{ |
| 80 | + name: "home/gopher", |
| 81 | + size: 0, |
| 82 | + mode: ModeDir | 0o755, |
| 83 | + modTime: time.Date(1970, time.January, 1, 12, 0, 0, 0, time.UTC), |
| 84 | + isDir: true, |
| 85 | + }, |
| 86 | + "d home/gopher/", |
| 87 | + }, |
| 88 | + { |
| 89 | + formatTest{ |
| 90 | + name: "big", |
| 91 | + size: 0x7fffffffffffffff, |
| 92 | + mode: ModeIrregular | 0o644, |
| 93 | + modTime: time.Date(1970, time.January, 1, 12, 0, 0, 0, time.UTC), |
| 94 | + isDir: false, |
| 95 | + }, |
| 96 | + "? big", |
| 97 | + }, |
| 98 | + { |
| 99 | + formatTest{ |
| 100 | + name: "small", |
| 101 | + size: -0x8000000000000000, |
| 102 | + mode: ModeSocket | ModeSetuid | 0o644, |
| 103 | + modTime: time.Date(1970, time.January, 1, 12, 0, 0, 0, time.UTC), |
| 104 | + isDir: false, |
| 105 | + }, |
| 106 | + "S small", |
| 107 | + }, |
| 108 | +} |
| 109 | + |
| 110 | +func TestFormatDirEntry(t *testing.T) { |
| 111 | + for i, test := range formatTests { |
| 112 | + got := fmtdirent.FormatDirEntry(&test.input) |
| 113 | + if got != test.wantDirEntry { |
| 114 | + t.Errorf("%d: FormatDirEntry(%#v) = %q, want %q", i, test.input, got, test.wantDirEntry) |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments