aboutsummaryrefslogtreecommitdiff
path: root/internal/scanner/scanner_test.go
blob: 1fe6d3e017beacf5fc8c757cc7072ed5f6401588 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package scanner

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

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

	s := New(Config{
		Roots: []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{}{
			"image.jpg":  {},
			"image.png":  {},
			"image.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("skips hidden directories", func(t *testing.T) {
		for _, file := range files {
			if filepath.Base(file.Path) == ".hidden.jpg" {
				t.Fatal("hidden file entered the scanner")
			}
		}
	})

	t.Run("skips hidden files", func(t *testing.T) {
		for _, file := range files {
			if filepath.Base(file.Path) == ".secret.jpg" {
				t.Fatal("scanner returned hidden file")
			}
		}
	})

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

		s := New(Config{
			Roots: []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))
		}
	})

	t.Run("ignores symbolic links", func(t *testing.T) {
		for _, file := range files {
			if filepath.Base(file.Path) == "link.jpg" {
				t.Fatal("scanner followed symbolic link")
			}
		}
	})
}