-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
93 lines (76 loc) · 1.98 KB
/
main_test.go
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
90
91
92
93
package main
import (
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"testing"
)
// Mock of os.DirEntry for testing purposes
type mockDirEntry struct {
name string
}
func (m *mockDirEntry) Name() string {
return m.name
}
func (m *mockDirEntry) IsDir() bool {
return false
}
func (m *mockDirEntry) Type() fs.FileMode {
return 0
}
func (m *mockDirEntry) Info() (fs.FileInfo, error) {
return nil, nil
}
// Testing ensureJPEGDirectoryExists function
func TestEnsureJPEGDirectoryExists(t *testing.T) {
dir := os.TempDir()
_ = ensureJPEGDirectoryExists(dir)
jpegDir := filepath.Join(dir, "jpegs")
if _, err := os.Stat(jpegDir); os.IsNotExist(err) {
t.Fatalf("Directory jpegs was not created")
}
}
// Testing getFilesInDirectory function
func TestGetFilesInDirectory(t *testing.T) {
dir := os.TempDir()
_, err := getFilesInDirectory(dir)
if err != nil {
t.Fatalf("Failed to read directory: %v", err)
}
}
// Testing processFile function for non-HEIC files
func TestProcessFileNonHEIC(t *testing.T) {
entry := &mockDirEntry{name: "test.txt"}
currentDir := os.TempDir()
jpegDir := filepath.Join(currentDir, "jpegs")
logs := processFile(entry, currentDir, jpegDir)
if _, exists := logs["test.txt"]; exists {
t.Fatalf("Non-HEIC file should not be processed")
}
}
func setupTestDir() (string, error) {
tmpDir, err := ioutil.TempDir("", "testdir")
if err != nil {
return "", err
}
// Create a mock .heic file
err = ioutil.WriteFile(tmpDir+"/test.heic", []byte("mock content"), 0644)
return tmpDir, err
}
func TestProcessFiles(t *testing.T) {
currentDir, err := setupTestDir()
if err != nil {
t.Fatalf("Failed to setup test directory: %v", err)
}
defer os.RemoveAll(currentDir)
jpegDir := currentDir + "/jpegs"
entries, err := os.ReadDir(currentDir)
if err != nil {
t.Fatalf("Failed to read directory: %v", err)
}
logs := processFiles(currentDir, jpegDir, entries)
if _, ok := logs["test.heic"]; !ok {
t.Errorf("Expected log entry for test.heic but didn't find one")
}
}