-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Daniel Milde
committed
Dec 30, 2020
1 parent
97f5c24
commit bd1ffe1
Showing
16 changed files
with
276 additions
and
267 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package analyze | ||
|
||
// Device struct | ||
type Device struct { | ||
Name string | ||
MountPoint string | ||
Size int64 | ||
Free int64 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package analyze | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestProcessDir(t *testing.T) { | ||
fin := CreateTestDir() | ||
defer fin() | ||
|
||
dir := ProcessDir("test_dir", &CurrentProgress{Mutex: &sync.Mutex{}}, func(_ string) bool { return false }) | ||
|
||
// test dir info | ||
assert.Equal(t, "test_dir", dir.Name) | ||
assert.Equal(t, int64(7), dir.Size) | ||
assert.Equal(t, 5, dir.ItemCount) | ||
assert.True(t, dir.IsDir) | ||
|
||
// test dir tree | ||
assert.Equal(t, "nested", dir.Files[0].Name) | ||
assert.Equal(t, "subnested", dir.Files[0].Files[1].Name) | ||
|
||
// test file | ||
assert.Equal(t, "file2", dir.Files[0].Files[0].Name) | ||
assert.Equal(t, int64(2), dir.Files[0].Files[0].Size) | ||
|
||
assert.Equal(t, "file", dir.Files[0].Files[1].Files[0].Name) | ||
assert.Equal(t, int64(5), dir.Files[0].Files[1].Files[0].Size) | ||
|
||
// test parent link | ||
assert.Equal(t, "test_dir", dir.Files[0].Files[1].Files[0].Parent.Parent.Parent.Name) | ||
} | ||
|
||
func TestIgnoreDir(t *testing.T) { | ||
dir := ProcessDir("/proc", &CurrentProgress{Mutex: &sync.Mutex{}}, func(_ string) bool { return true }) | ||
|
||
assert.Equal(t, "proc", dir.Name) | ||
assert.Equal(t, 1, dir.ItemCount) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package analyze | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFind(t *testing.T) { | ||
dir := File{ | ||
Name: "xxx", | ||
Size: 5, | ||
ItemCount: 2, | ||
} | ||
|
||
file := &File{ | ||
Name: "yyy", | ||
Size: 2, | ||
ItemCount: 1, | ||
Parent: &dir, | ||
} | ||
file2 := &File{ | ||
Name: "zzz", | ||
Size: 3, | ||
ItemCount: 1, | ||
Parent: &dir, | ||
} | ||
dir.Files = []*File{file, file2} | ||
|
||
assert.Equal(t, 0, dir.Files.Find(file)) | ||
assert.Equal(t, 1, dir.Files.Find(file2)) | ||
} | ||
|
||
func TestRemove(t *testing.T) { | ||
dir := File{ | ||
Name: "xxx", | ||
Size: 5, | ||
ItemCount: 2, | ||
} | ||
|
||
file := &File{ | ||
Name: "yyy", | ||
Size: 2, | ||
ItemCount: 1, | ||
Parent: &dir, | ||
} | ||
file2 := &File{ | ||
Name: "zzz", | ||
Size: 3, | ||
ItemCount: 1, | ||
Parent: &dir, | ||
} | ||
dir.Files = []*File{file, file2} | ||
|
||
dir.Files = dir.Files.Remove(file) | ||
|
||
assert.Equal(t, 1, len(dir.Files)) | ||
assert.Equal(t, file2, dir.Files[0]) | ||
} | ||
|
||
func TestRemoveFile(t *testing.T) { | ||
dir := &File{ | ||
Name: "xxx", | ||
Size: 5, | ||
ItemCount: 3, | ||
} | ||
|
||
subdir := &File{ | ||
Name: "yyy", | ||
Size: 4, | ||
ItemCount: 2, | ||
Parent: dir, | ||
} | ||
file := &File{ | ||
Name: "zzz", | ||
Size: 3, | ||
ItemCount: 1, | ||
Parent: subdir, | ||
} | ||
dir.Files = []*File{subdir} | ||
subdir.Files = []*File{file} | ||
|
||
subdir.RemoveFile(file) | ||
|
||
assert.Equal(t, 0, len(subdir.Files)) | ||
assert.Equal(t, 1, subdir.ItemCount) | ||
assert.Equal(t, int64(1), subdir.Size) | ||
assert.Equal(t, 1, len(dir.Files)) | ||
assert.Equal(t, 2, dir.ItemCount) | ||
assert.Equal(t, int64(2), dir.Size) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package analyze | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
) | ||
|
||
// CreateTestDir creates test dir structure | ||
func CreateTestDir() func() { | ||
os.MkdirAll("test_dir/nested/subnested", os.ModePerm) | ||
ioutil.WriteFile("test_dir/nested/subnested/file", []byte("hello"), 0644) | ||
ioutil.WriteFile("test_dir/nested/file2", []byte("go"), 0644) | ||
return func() { | ||
os.RemoveAll("test_dir") | ||
} | ||
} |
Oops, something went wrong.