Skip to content

Commit

Permalink
Merge pull request #6 from ddddddO/fix_counter
Browse files Browse the repository at this point in the history
ディレクトリ数が合わないパターンがあったため修正
  • Loading branch information
owlinux1000 authored Sep 30, 2023
2 parents 31cdd17 + 74d3be0 commit e36f1bf
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 20 deletions.
6 changes: 5 additions & 1 deletion internal/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package internal
import (
"fmt"
"path/filepath"
"strings"
)

type counter struct {
Expand All @@ -20,7 +21,10 @@ func newCounter() *counter {
func (c *counter) count(path string) {
dir, file := filepath.Split(path)
if len(dir) > 0 {
c.dirs[dir] = struct{}{}
splited := strings.Split(dir, "/")
for i := range splited {
c.dirs[filepath.Join(splited[:i+1]...)] = struct{}{}
}
}
if len(file) > 0 {
c.files[path] = struct{}{}
Expand Down
63 changes: 44 additions & 19 deletions internal/counter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,51 @@ import (
)

func TestCounter_Summary(t *testing.T) {
paths := []string{
"empty_directory/",
"a.txt",
"a.tgz",
"source/a.tgz",
"source/b.tgz",
"source/c.tgz",
"tmp_directory/",
"tmp_directory/a.png",
"tmp_directory/empty_directory/",
"tmp_directory/source/a.tgz",
tests := []struct {
name string
paths []string
want string
}{
{
name: "pattern1",
paths: []string{
"empty_directory/",
"a.txt",
"a.tgz",
"source/a.tgz",
"source/b.tgz",
"source/c.tgz",
"tmp_directory/",
"tmp_directory/a.png",
"tmp_directory/empty_directory/",
"tmp_directory/source/a.tgz",
},
want: "5 directories, 7 files",
},
{
name: "pattern2",
paths: []string{
"skillset-visualizer/terraform/state/default.tfstate",
"standard/module/structure/default.tfstate",
},
want: "6 directories, 2 files",
},
}
want := "5 directories, 7 files"
c := newCounter()
for _, p := range paths {
c.count(p)
}
got := c.summary()

if got != want {
t.Errorf("\ngot: \n%s\nwant: \n%s", got, want)
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

c := newCounter()
for _, p := range tt.paths {
c.count(p)
}
got := c.summary()

if got != tt.want {
t.Errorf("\ngot: \n%s\nwant: \n%s", got, tt.want)
}
})
}
}

0 comments on commit e36f1bf

Please sign in to comment.