github.com/shivakar/gdupes@v0.0.0-20180726052558-d5c070c306d0/gdupes/gdupes_test.go (about) 1 package gdupes_test 2 3 import ( 4 "bytes" 5 "fmt" 6 "os" 7 "sort" 8 "strings" 9 "testing" 10 11 "github.com/shivakar/gdupes/gdupes" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func isStringSliceEqual(e []string, a []string) bool { 16 if len(e) != len(a) { 17 return false 18 } 19 for i, v := range e { 20 if v != a[i] { 21 return false 22 } 23 } 24 return true 25 } 26 27 func isStringSSEqual(e [][]string, a [][]string) bool { 28 for _, av := range a { 29 found := false 30 sort.Strings(av) 31 for _, ev := range e { 32 sort.Strings(ev) 33 if isStringSliceEqual(ev, av) { 34 found = true 35 break 36 } 37 } 38 if !found { 39 return false 40 } 41 } 42 return true 43 } 44 45 func TestGdupes(t *testing.T) { 46 c := &gdupes.Config{NumWorkers: 2} 47 dirs := []string{"testdata"} 48 buf := &bytes.Buffer{} 49 if err := os.Chdir(".."); err != nil { 50 panic(err) 51 } 52 53 c.Writer = buf 54 t.Run("default", func(t *testing.T) { 55 expected := [][]string{ 56 {"testdata/zero.txt", 57 "testdata/.hidden.txt", 58 }, { 59 "testdata/b_hardlink.txt", 60 "testdata/b_copy.txt", 61 }, { 62 "testdata/a.txt", 63 "testdata/a_copy.txt", 64 "testdata/a_copy_copy.txt", 65 }, 66 } 67 68 assert := assert.New(t) 69 dupfiles, err := gdupes.Run(c, dirs) 70 assert.Nil(err) 71 assert.True(isStringSSEqual(expected, dupfiles), 72 "expected: %v,\ngot: %v\n", expected, dupfiles) 73 }) 74 75 c.PrintVersion = true 76 buf.Reset() 77 t.Run("--version", func(t *testing.T) { 78 assert := assert.New(t) 79 expected := fmt.Sprintf("gdupes v%s\n", gdupes.VERSION) 80 gdupes.Run(c, dirs) 81 assert.Equal(expected, buf.String()) 82 }) 83 c.PrintVersion = false 84 85 c.Summarize = true 86 buf.Reset() 87 t.Run("--summarize", func(t *testing.T) { 88 assert := assert.New(t) 89 expected := "4 duplicate files (in 3 sets), occupying 12 B.\nTotal time for processing: " 90 gdupes.Run(c, dirs) 91 assert.Equal(expected, buf.String()[:len(expected)]) 92 }) 93 c.Summarize = false 94 95 c.Recurse = true 96 buf.Reset() 97 t.Run("--recurse", func(t *testing.T) { 98 expected := [][]string{ 99 {"testdata/dir1/zero_copy.txt", 100 "testdata/zero.txt", 101 "testdata/dir1/.hidden_copy.txt", 102 "testdata/.hidden.txt", 103 "testdata/dir1/e.txt", 104 "testdata/dir2/dir3/zero.txt", 105 "testdata/dir4/.zerohidden.txt"}, 106 {"testdata/d.txt", 107 "testdata/dir2/dir3/d_hardlink.txt"}, 108 {"testdata/b_hardlink.txt", 109 "testdata/b_copy.txt"}, 110 {"testdata/a.txt", 111 "testdata/a_copy.txt", 112 "testdata/a_copy_copy.txt", 113 "testdata/dir1/a.txt"}, 114 } 115 assert := assert.New(t) 116 dupfiles, err := gdupes.Run(c, dirs) 117 assert.Nil(err) 118 assert.True(isStringSSEqual(expected, dupfiles), 119 "expected: %v,\ngot %v\n", expected, dupfiles) 120 }) 121 c.Recurse = false 122 123 c.Recurse = true 124 c.Summarize = true 125 buf.Reset() 126 t.Run("--recurse --summarize", func(t *testing.T) { 127 expected := "10 duplicate files (in 3 sets), occupying 16 B.\nTotal time for processing: " 128 gdupes.Run(c, dirs) 129 assert := assert.New(t) 130 assert.Equal(expected, buf.String()[:len(expected)]) 131 }) 132 c.Recurse = false 133 c.Summarize = false 134 135 c.Sameline = true 136 buf.Reset() 137 t.Run("--sameline", func(t *testing.T) { 138 expected := "testdata/zero.txt testdata/.hidden.txt \n" + 139 "testdata/b_hardlink.txt testdata/b_copy.txt \n" + 140 "testdata/a.txt testdata/a_copy.txt testdata/a_copy_copy.txt \n" 141 gdupes.Run(c, dirs) 142 assert := assert.New(t) 143 144 actual := buf.String() 145 assert.Equal(len(expected), len(actual)) 146 assert.Equal(len(strings.Split(expected, "\n")), len(strings.Split(actual, "\n"))) 147 148 splitStrOutput := func(s string) [][]string { 149 out := [][]string{} 150 for _, v := range strings.Split(s, "\n") { 151 temp := strings.Split(v, " ") 152 sort.Strings(temp) 153 out = append(out, temp) 154 } 155 return out 156 } 157 158 eSlices := splitStrOutput(expected) 159 aSlices := splitStrOutput(actual) 160 161 assert.True(isStringSSEqual(eSlices, aSlices), 162 "expected: %v,\ngot: %v\n", expected, actual) 163 }) 164 c.Sameline = false 165 166 c.NoEmpty = true 167 t.Run("--noempty", func(t *testing.T) { 168 expected := [][]string{ 169 {"testdata/b_hardlink.txt", "testdata/b_copy.txt"}, 170 {"testdata/a.txt", "testdata/a_copy.txt", "testdata/a_copy_copy.txt"}, 171 } 172 173 actual, err := gdupes.Run(c, dirs) 174 assert := assert.New(t) 175 assert.Nil(err) 176 assert.True(isStringSSEqual(expected, actual), 177 "expected: %v,\ngot: %v\n", expected, actual) 178 }) 179 c.NoEmpty = false 180 181 c.NoHidden = true 182 t.Run("--nohidden", func(t *testing.T) { 183 expected := [][]string{ 184 {"testdata/b_copy.txt", "testdata/b_hardlink.txt"}, 185 {"testdata/a.txt", "testdata/a_copy.txt", 186 "testdata/a_copy_copy.txt"}, 187 } 188 actual, err := gdupes.Run(c, dirs) 189 assert := assert.New(t) 190 assert.Nil(err) 191 assert.True(isStringSSEqual(expected, actual), 192 "expected: %v,\ngot: %v\n", expected, actual) 193 }) 194 c.NoHidden = false 195 196 c.Hardlinks = true 197 t.Run("--hardlinks", func(t *testing.T) { 198 expected := [][]string{ 199 {"testdata/.hidden.txt", 200 "testdata/zero.txt"}, 201 {"testdata/b.txt", 202 "testdata/b_copy.txt", 203 "testdata/b_hardlink.txt"}, 204 {"testdata/a.txt", 205 "testdata/a_copy.txt", 206 "testdata/a_copy_copy.txt"}, 207 } 208 actual, err := gdupes.Run(c, dirs) 209 assert := assert.New(t) 210 assert.Nil(err) 211 assert.True(isStringSSEqual(expected, actual), 212 "expected: %v,\ngot: %v\n", expected, actual) 213 }) 214 c.Hardlinks = false 215 }