11package chunk
22
33import (
4- "bytes"
5- "compress/gzip"
6- "encoding/json"
4+ "errors"
75 "io/fs"
8- "os "
6+ "strings "
97 "testing"
108 "testing/fstest"
119
1210 "github.com/rusq/slack"
11+ "github.com/stretchr/testify/assert"
1312
1413 "github.com/rusq/slackdump/v3/internal/fixtures"
14+ "github.com/rusq/slackdump/v3/internal/testutil"
1515)
1616
1717// assortment of channel info chunks
7070 }
7171)
7272
73- func TestOpenDir (t * testing.T ) {
74- }
75-
7673func 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-
74+ fixtures .SkipOnWindows (t ) // TODO: fix this test on Windows
11375 testChannels := fixtures.Load [[]slack.Channel ](fixtures .TestChannelsJSON )
11476 channelInfos := make ([]Chunk , len (testChannels ))
11577 for _ , ch := range testChannels {
@@ -123,43 +85,81 @@ func TestDirectory_Walk(t *testing.T) {
12385 t .Fatal ("fixture has no channels" )
12486 }
12587
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 ])),
88+ tests := []struct {
89+ name string
90+ fsys fs.FS
91+ want []string
92+ wantErr bool
93+ }{
94+ {
95+ name : "invalid json in root" ,
96+ fsys : fstest.MapFS {
97+ "C123VALID.json.gz" : & fstest.MapFile {
98+ Data : testutil .GZCompress (t , testutil .MarshalJSON (t , channelInfos [0 ])),
99+ },
100+ "C123INVALID.json.gz" : & fstest.MapFile {
101+ Data : testutil .GZCompress (t , []byte ("invalid json" )),
102+ },
103+ "C123VALID2.json.gz" : & fstest.MapFile {
104+ Data : testutil .GZCompress (t , testutil .MarshalJSON (t , channelInfos [1 ])),
105+ },
130106 },
131- "C123INVALID.json.gz" : & fstest.MapFile {
132- Data : compress (t , []byte ("invalid json" )),
107+ want : []string {
108+ "C123VALID.json.gz" ,
109+ "C123VALID2.json.gz" ,
133110 },
134- "C123VALID2.json.gz" : & fstest.MapFile {
135- Data : compress (t , marshal (t , channelInfos [1 ])),
111+ },
112+ {
113+ name : "should scan only top level dir" ,
114+ fsys : fstest.MapFS {
115+ "__uploads/CINVALID.json.gz" : & fstest.MapFile {
116+ Data : testutil .GZCompress (t , []byte ("NaN" )),
117+ },
118+ "__uploads/CVALID.json.gz" : & fstest.MapFile {
119+ Data : testutil .GZCompress (t , testutil .MarshalJSON (t , channelInfos [1 ])),
120+ },
121+ "__avatars/CVALID.json.gz" : & fstest.MapFile {
122+ Data : testutil .GZCompress (t , testutil .MarshalJSON (t , channelInfos [2 ])),
123+ },
124+ "somedir/CVALID.json.gz" : & fstest.MapFile {
125+ Data : testutil .GZCompress (t , testutil .MarshalJSON (t , channelInfos [3 ])),
126+ },
127+ "CVALID.json.gz" : & fstest.MapFile {
128+ Data : testutil .GZCompress (t , testutil .MarshalJSON (t , channelInfos [0 ])),
129+ },
130+ "CANOTHER.json.gz" : & fstest.MapFile {
131+ Data : testutil .GZCompress (t , testutil .MarshalJSON (t , channelInfos [1 ])),
132+ },
136133 },
137- }
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 {
134+ want : [] string {
135+ "CVALID.json.gz" ,
136+ "CANOTHER.json.gz" ,
137+ },
138+ },
139+ }
140+ for _ , tt := range tests {
141+ t . Run ( tt . name , func ( t * testing. T ) {
142+ dir := testutil . PrepareTestDirectory ( t , tt . fsys )
143+ d , err := OpenDir ( dir )
147144 if err != nil {
148- t .Fatalf ("Walk () error = %v" , err )
145+ t .Fatalf ("OpenDir () error = %v" , err )
149146 }
150- if name == "C123INVALID.json.gz" {
151- t .Fatal ("should not be called for invalid json" )
147+ defer d .Close ()
148+ var seen []string
149+ if err := d .Walk (func (name string , f * File , err error ) error {
150+ if err != nil {
151+ return err
152+ }
153+ if f == nil {
154+ return errors .New ("file is nil" )
155+ }
156+ t .Logf ("name: %q, trimmed: %q" , name , strings .TrimLeft (name , dir ))
157+ seen = append (seen , strings .TrimLeft (name , dir ))
158+ return nil
159+ }); (err != nil ) != tt .wantErr {
160+ t .Fatalf ("Walk() wantErr: %v, got error = %v" , tt .wantErr , err )
152161 }
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- })
162+ assert .ElementsMatch (t , tt .want , seen )
163+ })
164+ }
165165}
0 commit comments