This repository was archived by the owner on Aug 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrecyclebin_test.go
More file actions
164 lines (150 loc) Β· 4.58 KB
/
Copy pathrecyclebin_test.go
File metadata and controls
164 lines (150 loc) Β· 4.58 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2018 Nikola Trubitsyn. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package recyclebin
import (
"github.com/spf13/afero"
"net/url"
"os"
"testing"
)
func TestMain(m *testing.M) {
setup()
ret := m.Run()
teardown()
os.Exit(ret)
}
func setup() {
fs = afero.NewMemMapFs()
}
func teardown() {
os.Unsetenv("XDG_DATA_HOME")
}
func TestMoveToTrash(t *testing.T) {
trashPath := ".local/share/Trash"
bin := NewRecycleBin(trashPath)
filename := "file"
f, err := fs.Create(filename)
if err != nil {
t.Error("unable to create test file for removal.")
}
if err := f.Close(); err != nil {
t.Error("unable to close test file.")
}
trashedFilename := getTrashedFilename(trashPath, filename)
if err = bin.Recycle(filename); err != nil {
t.Error("unable to recycle test file.")
}
fileExists, err := afero.Exists(fs, filename)
if err != nil {
t.Error("unable to check if file is still not deleted")
}
if fileExists {
t.Error("file has not been moved to trash")
}
if !existsTrashFile(trashPath, trashedFilename) {
t.Error("trash file '" + trashedFilename + "' has not been created")
}
if !existsTrashInfo(trashPath, trashedFilename) {
t.Error("trash info '" + trashedFilename + ".trashinfo' has not been created")
}
trashInfo, err := readTrashInfo(buildTrashInfoPath(trashPath, trashedFilename))
if err != nil {
t.Error("trash info ''" + trashedFilename + "'.trashinfo cannot be read")
}
escapedRealPath := url.PathEscape(filename)
if escapedRealPath != trashInfo.Path {
t.Error("trash info '" + trashedFilename + "'.trashinfo has invalid Path value")
}
}
func TestDeleteFromTrash(t *testing.T) {
trashPath := ".local/share/Trash"
bin := NewRecycleBin(trashPath)
filename := "file"
if err := createTrashFiles(trashPath, filename); err != nil {
t.Error("unable to create test trash files")
}
if err := bin.Remove(filename); err != nil {
t.Error("unable to remove file")
}
if existsTrashFile(trashPath, filename) {
t.Error("trash file '" + filename + "' has not been removed")
}
if existsTrashInfo(trashPath, filename) {
t.Error("trash info '" + filename + ".trashinfo' has not been removed")
}
}
func TestRestoreFromTrash(t *testing.T) {
trashPath := ".local/share/Trash"
bin := NewRecycleBin(trashPath)
trashFilename := "file"
if err := createTrashFiles(trashPath, trashFilename); err != nil {
t.Error("unable to create test trash files")
}
if err := bin.Restore(trashFilename); err != nil {
t.Error("unable to restore file '" + trashFilename + "'")
}
if exists, _ := afero.Exists(fs, buildTrashFilePath(trashPath, trashFilename)); exists {
t.Error("trash still contains the file")
}
if exists, _ := afero.Exists(fs, buildTrashInfoPath(trashPath, trashFilename)); exists {
t.Error("trash still contains trashinfo file")
}
if exists, _ := afero.Exists(fs, trashFilename); !exists {
t.Error("file has not been restored")
}
}
func TestEmptyTrash(t *testing.T) {
trashPath := ".local/share/Trash"
bin := NewRecycleBin(trashPath)
if err := createTrashFiles(trashPath, "script.sh"); err != nil {
t.Error("unable to create test trash files")
}
if err := createTrashFiles(trashPath, "lib.so"); err != nil {
t.Error("unable to create test trash files")
}
if err := bin.Empty(); err != nil {
t.Error("unable to empty the trash")
}
if existsTrashFile(trashPath, "script.sh") || existsTrashFile(trashPath, "lib.so") {
t.Error("trash files were not deleted")
}
}
func createTrashFiles(trashPath string, filename string) error {
if err := fs.MkdirAll(trashPath+"/files", os.ModeDir); err != nil {
return err
}
f, err := fs.Create(buildTrashFilePath(trashPath, filename))
if err != nil {
return err
}
_ = f.Close()
if err := fs.MkdirAll(trashPath+"/info", os.ModeDir); err != nil {
return err
}
f, err = fs.Create(buildTrashInfoPath(trashPath, filename))
if err != nil {
return err
}
defer f.Close()
content := "[Trash Info]\n" + "Path=" + filename + "\n" + "DeletionDate=2018-10-11\n"
if _, err := f.WriteString(content); err != nil {
return err
}
return nil
}
func existsTrashFile(trashPath string, filename string) bool {
hasFile, _ := afero.Exists(fs, buildTrashFilePath(trashPath, filename))
return hasFile
}
func existsTrashInfo(trashPath string, filename string) bool {
hasTrashInfo, _ := afero.Exists(fs, buildTrashInfoPath(trashPath, filename))
return hasTrashInfo
}
func initHomeEnvironment() {
os.Setenv("XDG_DATA_HOME", "/home/user")
fs.MkdirAll("/home/user/.local/share/Trash", os.ModeDir)
}
func deinitHomeEnvironment() {
os.Unsetenv("XDG_DATA_HOME")
}