Skip to content
This repository has been archived by the owner on Mar 29, 2023. It is now read-only.

skip ignored files when calculating size #30

Merged
merged 2 commits into from
Mar 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions serialfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,18 @@ func (f *serialFile) Size() (int64, error) {

var du int64
err := filepath.Walk(f.path, func(p string, fi os.FileInfo, err error) error {
if err != nil {
if err != nil || fi == nil {
return err
}

if fi != nil && fi.Mode().IsRegular() {
if f.filter.ShouldExclude(fi) {
if fi.Mode().IsDir() {
return filepath.SkipDir
}
} else if fi.Mode().IsRegular() {
du += fi.Size()
}

return nil
})

Expand Down
81 changes: 51 additions & 30 deletions serialfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,32 @@ func testSerialFile(t *testing.T, hidden, withIgnoreRules bool) {
t.Fatal(err)
}
}
expectedHiddenPaths := make([]string, 0, 4)
expectedRegularPaths := make([]string, 0, 6)
expectedPaths := make([]string, 0, 4)
expectedSize := int64(0)

testInputs:
for p := range testInputs {
path := filepath.Join(tmppath, p)
stat, err := os.Stat(path)
if err != nil {
t.Fatal(err)
}
if !fileFilter.ShouldExclude(stat) {
if isFullPathHidden(path) {
expectedHiddenPaths = append(expectedHiddenPaths, p)
} else {
expectedRegularPaths = append(expectedRegularPaths, p)
components := strings.Split(p, "/")
var stat os.FileInfo
for i := range components {
stat, err = os.Stat(filepath.Join(
append([]string{tmppath}, components[:i+1]...)...,
))
if err != nil {
t.Fatal(err)
}
if fileFilter.ShouldExclude(stat) {
continue testInputs
}
}
expectedPaths = append(expectedPaths, p)
if stat.Mode().IsRegular() {
expectedSize += stat.Size()
}
}

sort.Strings(expectedPaths)

stat, err := os.Stat(tmppath)
if err != nil {
t.Fatal(err)
Expand All @@ -102,9 +111,14 @@ func testSerialFile(t *testing.T, hidden, withIgnoreRules bool) {
}
defer sf.Close()

if size, err := sf.Size(); err != nil {
t.Fatalf("failed to determine size: %s", err)
} else if size != expectedSize {
t.Fatalf("expected size %d, got size %d", expectedSize, size)
}

rootFound := false
actualRegularPaths := make([]string, 0, len(expectedRegularPaths))
actualHiddenPaths := make([]string, 0, len(expectedHiddenPaths))
actualPaths := make([]string, 0, len(expectedPaths))
err = Walk(sf, func(path string, nd Node) error {
defer nd.Close()

Expand All @@ -119,16 +133,15 @@ func testSerialFile(t *testing.T, hidden, withIgnoreRules bool) {
rootFound = true
return nil
}
if isFullPathHidden(path) {
actualHiddenPaths = append(actualHiddenPaths, path)
} else {
actualRegularPaths = append(actualRegularPaths, path)
}
actualPaths = append(actualPaths, path)
if !hidden && isFullPathHidden(path) {
return fmt.Errorf("found a hidden file")
}
if fileFilter.Rules.MatchesPath(path) {
return fmt.Errorf("found a file that should be excluded")
components := filepath.SplitList(path)
for i := range components {
if fileFilter.Rules.MatchesPath(filepath.Join(components[:i+1]...)) {
return fmt.Errorf("found a file that should be excluded")
}
}

data, ok := testInputs[path]
Expand All @@ -155,19 +168,27 @@ func testSerialFile(t *testing.T, hidden, withIgnoreRules bool) {
}
return nil
})
if err != nil {
t.Fatal(err)
}
if !rootFound {
t.Fatal("didn't find the root")
}
for _, regular := range expectedRegularPaths {
if idx := sort.SearchStrings(actualRegularPaths, regular); idx < 0 {
t.Errorf("missed regular path %q", regular)
}

if len(expectedPaths) != len(actualPaths) {
t.Fatalf("expected %d paths, found %d",
len(expectedPaths),
len(actualPaths),
)
}
if hidden && len(actualHiddenPaths) != len(expectedHiddenPaths) {
for _, missing := range expectedHiddenPaths {
if idx := sort.SearchStrings(actualHiddenPaths, missing); idx < 0 {
t.Errorf("missed hidden path %q", missing)
}

for i := range expectedPaths {
if expectedPaths[i] != actualPaths[i] {
t.Errorf(
"expected path %q does not match actual %q",
expectedPaths[i],
actualPaths[i],
)
}
}
}