-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_test.go
More file actions
147 lines (132 loc) · 4.07 KB
/
delete_test.go
File metadata and controls
147 lines (132 loc) · 4.07 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
package main
import (
"bytes"
"os"
"strings"
"testing"
)
func TestDelete(t *testing.T) {
t.Run("Testing delete directory function", func(t *testing.T) {
output := bytes.Buffer{}
dir := "/Users/numeezbaloch17/Documents/fs-cli/test_delete_dir"
file := dir + "/" + "check.txt"
err := os.WriteFile(file, []byte("This is delete test"), os.ModePerm)
if err != nil {
t.Error(err)
}
input := "/Users/numeezbaloch17/Documents/fs-cli/test_delete_dir/check.txt"
deleteAllContentsOfDirectory(input, &output)
_, err = os.Open(file)
if err == nil {
t.Error("Expecting an error but did not get it")
}
})
t.Run("Deleting a directory", func(t *testing.T) {
output := bytes.Buffer{}
dir := "/Users/numeezbaloch17/Documents/fs-cli/test_delete_directory"
files := []string{dir + "/" + "check.txt", dir + "/" + "file1.txt", dir + "/" + "file2.txt", dir + "/" + "file3.txt"}
err := os.Mkdir(dir, os.ModePerm)
if err != nil {
t.Error(err)
}
for _, file := range files {
err = os.WriteFile(file, []byte("This is delete test"), os.ModePerm)
if err != nil {
t.Error(err)
}
}
deleteAllContentsOfDirectory(dir, &output)
_, err = os.Stat(dir)
isNotExist := os.IsNotExist(err)
if !isNotExist {
t.Errorf("Expected value %t Got : %t", true, isNotExist)
}
})
t.Run("Check whether the directory is empty or not",func(t *testing.T){
cases:=[]struct{
dir string
result bool
}{
{"/Users/numeezbaloch17/Documents/fs-cli/bin",false},
{"/Users/numeezbaloch17/Documents/fs-cli/copy_test_dir",false},
{"/Users/numeezbaloch17/Documents/fs-cli/empty_dir",true},
{"/Users/numeezbaloch17/Documents/fs-cli/test_dir",false},
}
for _,c:=range cases{
actual,err:=IsDirectoryEmpty(c.dir)
if err!=nil{
t.Error(err)
}
if actual!=c.result{
t.Errorf("Actual : %v Expected : %v",c.result,actual)
}
}
})
}
func TestDeleteDirectory(t *testing.T) {
t.Run("Checking Delete Directory function",func(t *testing.T) {
dir:="/Users/numeezbaloch17/Documents/fs-cli/helper.txt"
os.WriteFile(dir,[]byte("This is the test file for delete functionality"),os.ModePerm)
input:="delete /Users/numeezbaloch17/Documents/fs-cli/helper.txt"
output:=bytes.Buffer{}
DeleteDirectory(input,delete,&output)
_,err:=os.Open(dir)
if err==nil{
t.Error("Expecting an error but did not get it")
}
})
t.Run("Passing invalid directory",func(t *testing.T){
input:="delete /Users/numeezbaloch17/Documents/fs-cli/helper.txt .md"
output:=bytes.Buffer{}
DeleteDirectory(input,delete,&output)
expected:=`Invalid number of arguments
`
if expected!=output.String(){
t.Errorf("Expected : %s Actual : %s",expected,output.String())
}
})
}
func TestDeleteForMove(t *testing.T) {
t.Run("Checking the function",func(t *testing.T){
dir:="/Users/numeezbaloch17/Documents/fs-cli/helper.txt"
os.WriteFile(dir,[]byte("This is the test file for delete functionality"),os.ModePerm)
output:=bytes.Buffer{}
DeleteForMove(dir,&output)
_,err:=os.Open(dir)
if err==nil{
t.Error("Expecting an error but did not get it")
}
})
}
func TestDeleteFunction(t *testing.T) {
basePath:="/Users/numeezbaloch17/Documents/fs-cli/test_delete_dir"
data:="This is "
files:=[]string{"file1.txt","file2.txt","file3.txt","file4.txt","file5.txt","file6.txt","file7.txt"}
for _,file:= range files{
err:=os.WriteFile(basePath+"/"+file,[]byte(data+file),os.ModePerm)
if err!=nil{
t.Error(err)
}
}
cases:=[]struct{
dir string
input string
output string
}{
{basePath+"/"+"file1.txt","y\n",""},
{basePath+"/"+"file2.txt","y\n",""},
{basePath+"/"+"file3.txt","y\n",""},
{basePath+"/"+"file4.txt","y\n",""},
{basePath+"/"+"file5.txt","n\n","Delete operation cancelled"},
{basePath+"/"+"file6.txt","n\n","Delete operation cancelled"},
{basePath+"/"+"file7.txt","x\n","Invalid input please enter Y or N according to your needs"},
}
for _,c:=range cases{
output:=bytes.Buffer{}
reader:=strings.NewReader(c.input)
Delete("delete "+c.dir,delete,reader,&output)
if output.String()!=c.output{
t.Errorf("Expected : %s Actual : %s",c.output,output.String())
}
}
}