aboutsummaryrefslogtreecommitdiff
path: root/internal/scanner/scanner_test.go
blob: 4027d95bbd752aaeceaa1262f897d1301dcce260 (plain)
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
package scanner

import (
	"context"
	"path/filepath"
	"testing"
)

func TestScan(t *testing.T) {
	root := filepath.Join("testdata", "gallery")

	s := New([]string{root})

	files, err := s.Scan(context.Background())
	if err != nil {
		t.Fatalf("Scan failed: %v", err)
	}

	t.Run("Find supported images", func(t *testing.T) {
		expected := map[string]struct{}{
			"cat.jpg":    {},
			"dog.png":    {},
			"beach.webp": {},
		}

		for _, file := range files {
			delete(expected, filepath.Base(file.Path))
		}

		if len(expected) != 0 {
			t.Fatalf("Missing files: %v", expected)
		}
	})

	t.Run("Ignore unsupported files", func(t *testing.T) {
		for _, file := range files {
			switch filepath.Base(file.Path) {
			case "readme.txt", "jeffsfiles.pdf":
				t.Fatalf("Scanner returned unsupported files %q", file.Path)
			}
		}
	})

	t.Run("Skip hidden directories", func(t *testing.T) {
		for _, file := range files {
			if filepath.Base(file.Path) == "nuclearlaunchcode.txt" {
				t.Fatal("Hidden file entered the scanner")
			}
		}
	})

	t.Run("Deduplicates overlapping roots", func(t *testing.T) {
		root := filepath.Join("testdata", "duplicate")

		s := New([]string{
			root,
			root,
		})

		files, err := s.Scan(context.Background())
		if err != nil {
			t.Fatalf("Scan failed: %v", err)
		}

		if len(files) != 2 {
			t.Fatalf("Expected 2 unique files, got %d", len(files))
		}
	})
}