-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdedup_test.go
More file actions
87 lines (80 loc) · 1.71 KB
/
Copy pathdedup_test.go
File metadata and controls
87 lines (80 loc) · 1.71 KB
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
package dedup
import (
"bufio"
"crypto/rand"
"fmt"
"io"
"log"
"os"
"testing"
uuid "github.com/satori/go.uuid"
"github.com/stretchr/testify/assert"
)
const testDirPath = "./testdir/"
func init() {
createTestFiles(250)
}
func createTestFiles(num int) {
for i := 0; i < num; i++ {
// Create New Filename using UUID-V4
filename := testDirPath + uuid.NewV4().String()
ext := ".txt"
f, err := os.Create(filename + ext)
if err != nil {
log.Fatal(err)
}
fbuf := bufio.NewWriter(f)
randBuf := make([]byte, 100000)
if _, err = rand.Read(randBuf); err != nil {
log.Fatal(err)
}
_, err = fbuf.Write(randBuf)
if err != nil {
log.Fatal(err)
}
f.Close()
// Duplicate even files
if i%2 == 0 {
i++
orig, err := os.Open(filename + ext)
defer orig.Close()
if err != nil {
log.Fatal(err)
}
dup, err := os.Create(filename + "_dup_" + ext)
defer dup.Close()
if err != nil {
log.Fatal(err)
}
_, err = io.Copy(dup, orig)
if err != nil {
log.Fatal(err)
}
}
}
}
func TestGetDirFiles(t *testing.T) {
result, err := getDirFiles(testDirPath)
if err != nil {
t.Log(err)
t.Fail()
}
for i := 0; i < 10; i++ {
fmt.Printf("Filename %v: %s\n", i, result[i])
}
}
func TestFindDuplicates(t *testing.T) {
results, err := FindDuplicates(testDirPath)
assert.Nil(t, err)
for i := 0; i < len(results); i++ {
fmt.Printf("File %v: %s Hash: %s Dupl: %v\n", i, results[i].Pathname, results[i].Blake2bHEX, results[i].DupOfPosition)
}
}
func TestDeduplicateToNew(t *testing.T) {
err := DeduplicateToNew(testDirPath, testDirPath+"Deduped")
assert.Nil(t, err)
}
func TestDeduplicateByDeletion(t *testing.T) {
err := DeduplicateByDeletion(testDirPath)
assert.Nil(t, err)
}