|
1 | 1 | package chunk |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
| 5 | + "compress/gzip" |
| 6 | + "encoding/json" |
| 7 | + "io/fs" |
| 8 | + "os" |
4 | 9 | "testing" |
| 10 | + "testing/fstest" |
5 | 11 |
|
6 | 12 | "github.com/rusq/slack" |
| 13 | + |
7 | 14 | "github.com/rusq/slackdump/v3/internal/fixtures" |
8 | 15 | ) |
9 | 16 |
|
|
64 | 71 | ) |
65 | 72 |
|
66 | 73 | func TestOpenDir(t *testing.T) { |
| 74 | +} |
| 75 | + |
| 76 | +func TestDirectory_Walk(t *testing.T) { |
| 77 | + var ( |
| 78 | + // prepDir prepares a temporary directory for testing and populates it with |
| 79 | + // files from fsys. It returns the path to the directory. |
| 80 | + prepDir = func(t *testing.T, fsys fs.FS) string { |
| 81 | + t.Helper() |
| 82 | + dir := t.TempDir() |
| 83 | + if err := os.CopyFS(dir, fsys); err != nil { |
| 84 | + t.Fatal(err) |
| 85 | + } |
| 86 | + return dir |
| 87 | + } |
| 88 | + |
| 89 | + compress = func(t *testing.T, data []byte) []byte { |
| 90 | + t.Helper() |
| 91 | + var buf bytes.Buffer |
| 92 | + gz := gzip.NewWriter(&buf) |
| 93 | + defer gz.Close() |
| 94 | + if _, err := gz.Write(data); err != nil { |
| 95 | + t.Fatal(err) |
| 96 | + } |
| 97 | + if err := gz.Close(); err != nil { |
| 98 | + t.Fatal(err) |
| 99 | + } |
| 100 | + return buf.Bytes() |
| 101 | + } |
| 102 | + |
| 103 | + marshal = func(t *testing.T, v any) []byte { |
| 104 | + t.Helper() |
| 105 | + data, err := json.Marshal(v) |
| 106 | + if err != nil { |
| 107 | + t.Fatal(err) |
| 108 | + } |
| 109 | + return data |
| 110 | + } |
| 111 | + ) |
| 112 | + |
| 113 | + testChannels := fixtures.Load[[]slack.Channel](fixtures.TestChannelsJSON) |
| 114 | + channelInfos := make([]Chunk, len(testChannels)) |
| 115 | + for _, ch := range testChannels { |
| 116 | + channelInfos = append(channelInfos, Chunk{ |
| 117 | + Type: CChannelInfo, |
| 118 | + ChannelID: ch.ID, |
| 119 | + Channel: &ch, |
| 120 | + }) |
| 121 | + } |
| 122 | + if len(channelInfos) == 0 { |
| 123 | + t.Fatal("fixture has no channels") |
| 124 | + } |
| 125 | + |
| 126 | + t.Run("doesn't fail on invalid json", func(t *testing.T) { |
| 127 | + testdir := fstest.MapFS{ |
| 128 | + "C123VALID.json.gz": &fstest.MapFile{ |
| 129 | + Data: compress(t, marshal(t, channelInfos[0])), |
| 130 | + }, |
| 131 | + "C123INVALID.json.gz": &fstest.MapFile{ |
| 132 | + Data: compress(t, []byte("invalid json")), |
| 133 | + }, |
| 134 | + "C123VALID2.json.gz": &fstest.MapFile{ |
| 135 | + Data: compress(t, marshal(t, channelInfos[1])), |
| 136 | + }, |
| 137 | + } |
67 | 138 |
|
| 139 | + dir := prepDir(t, testdir) |
| 140 | + d, err := OpenDir(dir) |
| 141 | + if err != nil { |
| 142 | + t.Fatalf("OpenDir() error = %v", err) |
| 143 | + } |
| 144 | + defer d.Close() |
| 145 | + var seen []string |
| 146 | + if err := d.Walk(func(name string, f *File, err error) error { |
| 147 | + if err != nil { |
| 148 | + t.Fatalf("Walk() error = %v", err) |
| 149 | + } |
| 150 | + if name == "C123INVALID.json.gz" { |
| 151 | + t.Fatal("should not be called for invalid json") |
| 152 | + } |
| 153 | + if f == nil { |
| 154 | + t.Fatalf("Walk() file is nil") |
| 155 | + } |
| 156 | + seen = append(seen, name) |
| 157 | + return nil |
| 158 | + }); err != nil { |
| 159 | + t.Fatalf("Walk() error = %v", err) |
| 160 | + } |
| 161 | + if len(seen) != 2 { |
| 162 | + t.Fatalf("Walk() = %v, want 2", len(seen)) |
| 163 | + } |
| 164 | + }) |
68 | 165 | } |
0 commit comments