Skip to content

Commit

Permalink
status: display count of directories
Browse files Browse the repository at this point in the history
fixes #112
  • Loading branch information
kevin-hanselman committed Aug 18, 2022
1 parent e98de8f commit 4ead549
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 16 deletions.
39 changes: 25 additions & 14 deletions src/artifact/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,26 +85,41 @@ type Status struct {
ChildrenStatus map[string]*Status
}

func (stat Status) childStatusCounts(out map[string]int) {
func (stat Status) dirStatusCounts(counts map[string]int) {
// len(nil map) returns 0
if len(stat.ChildrenStatus) == 0 {
counts["empty directory"]++
} else {
counts["directory"]++
}
for _, childStatus := range stat.ChildrenStatus {
if childStatus.IsDir {
childStatus.childStatusCounts(out)
childStatus.dirStatusCounts(counts)
} else {
out[childStatus.String()]++
counts[childStatus.String()]++
}
}
}

func sortByValue(counts map[string]int) []string {
func sortCounts(counts map[string]int) []string {
keys := make([]string, len(counts))
i := 0
for key := range counts {
keys[i] = key
i++
}
sort.Slice(keys, func(i, j int) bool {
// Sort into descending order.
return counts[keys[i]] > counts[keys[j]]
sort.Slice(keys, func(a, b int) bool {
aKey := keys[a]
aVal := counts[aKey]
bKey := keys[b]
bVal := counts[keys[b]]
// If the values are equal, fallback to lexicographic order of the
// string keys. This is primarily for writing deterministic tests.
if aVal == bVal {
return aKey < bKey
}
// Sort by highest value first.
return aVal > bVal
})
return keys
}
Expand Down Expand Up @@ -166,15 +181,11 @@ func (stat Status) String() string {
}

if stat.IsDir {
// len(nil map) returns 0
if len(stat.ChildrenStatus) == 0 {
return "empty directory"
}
counts := make(map[string]int)
stat.childStatusCounts(counts)
stat.dirStatusCounts(counts)
countStrings := make([]string, len(counts))
for i, status := range sortByValue(counts) {
countStrings[i] = fmt.Sprintf("x%d %s", counts[status], status)
for i, status := range sortCounts(counts) {
countStrings[i] = fmt.Sprintf("%dx %s", counts[status], status)
}
return strings.Join(countStrings, ", ")
}
Expand Down
31 changes: 29 additions & 2 deletions src/artifact/artifact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package artifact
import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/kevin-hanselman/dud/src/fsutil"
)

Expand Down Expand Up @@ -127,7 +128,7 @@ func TestArtifactStatusString(t *testing.T) {
},
}

want := "x3 up-to-date, x1 not committed"
want := "3x up-to-date, 2x directory, 1x not committed"

got := status.String()
if got != want {
Expand All @@ -144,14 +145,40 @@ func TestArtifactStatusString(t *testing.T) {
ContentsMatch: false,
}

want := "empty directory"
want := "1x empty directory"

got := status.String()
if got != want {
t.Fatalf("Status.String() got %#v, want %#v", got, want)
}
})

t.Run("empty sub-directory", func(t *testing.T) {
status := Status{
Artifact: Artifact{IsDir: true},
WorkspaceFileStatus: fsutil.StatusDirectory,
HasChecksum: true,
ChecksumInCache: true,
ContentsMatch: false,
ChildrenStatus: map[string]*Status{
"c": {
Artifact: Artifact{IsDir: true},
WorkspaceFileStatus: fsutil.StatusDirectory,
HasChecksum: true,
ChecksumInCache: true,
ContentsMatch: false,
},
},
}

want := "1x directory, 1x empty directory"

got := status.String()
if diff := cmp.Diff(want, got); diff != "" {
t.Fatalf("Status.String() -want +got:\n%s", diff)
}
})

t.Run("directory but IsDir false", func(t *testing.T) {
status := Status{
Artifact: Artifact{IsDir: false},
Expand Down

0 comments on commit 4ead549

Please sign in to comment.