Skip to content

Commit

Permalink
modify: refactoring dirwalk implementaion
Browse files Browse the repository at this point in the history
  • Loading branch information
d-tsuji committed Apr 25, 2020
1 parent 4253057 commit 96d252e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@
# Executable Binary
qiisync
qiisync.exe

# Go test coverage
c.out
coverage.html
26 changes: 12 additions & 14 deletions broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,10 @@ func (b *Broker) fetchRemoteItemsPerPage(page int) ([]*Article, bool, error) {
// FetchLocalArticles searches base_dir of local filesystem and extracts articles.
func (b *Broker) FetchLocalArticles() (articles map[string]*Article, err error) {
articles = make(map[string]*Article)
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("recoverd when dirwalk(%s): %v", b.baseDir(), r)
}
}()
fnameList := dirwalk(b.baseDir())
fnameList, err := dirwalk(b.baseDir())
if err != nil {
return nil, fmt.Errorf("dirwalk %s: %w", fnameList, err)
}
for i := range fnameList {
a, err := ArticleFromFile(fnameList[i])
if err != nil {
Expand All @@ -144,26 +142,26 @@ func (b *Broker) FetchLocalArticles() (articles map[string]*Article, err error)
return articles, nil
}

func dirwalk(dir string) []string {
err := os.MkdirAll(dir, 0755)
if err != nil {
panic(err)
}
func dirwalk(dir string) ([]string, error) {
files, err := ioutil.ReadDir(dir)
if err != nil {
panic(err)
return nil, fmt.Errorf("read dir: %w", err)
}

var paths []string
for _, file := range files {
if file.IsDir() {
paths = append(paths, dirwalk(filepath.Join(dir, file.Name()))...)
p, err := dirwalk(filepath.Join(dir, file.Name()))
if err != nil {
return nil, fmt.Errorf("dirwalk %s: %w", filepath.Join(dir, file.Name()), err)
}
paths = append(paths, p...)
continue
}
paths = append(paths, filepath.Join(dir, file.Name()))
}

return paths
return paths, nil
}

// NewRequest is a testable NewRequest that wraps http.NewRequest.
Expand Down
18 changes: 17 additions & 1 deletion broker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,12 @@ func Test_fetchLocalArticles(t *testing.T) {
Local: localConfig{Dir: filepath.Join("testdata", "duplicate")}}},
wantErr: true,
},
{
name: "dummy_dir",
fields: fields{config: &Config{
Local: localConfig{Dir: "dummy"}}},
wantErr: true,
},
}

if err := os.Chtimes(filepath.Join("testdata", "article", "20_test_article_posted.md"), updateAt, updateAt); err != nil {
Expand Down Expand Up @@ -1092,7 +1098,10 @@ func Test_dirwalk(t *testing.T) {
f, _ = os.Create(filepath.Join(tempDir, "dir_c", "file_c"))
f.Close()

got := dirwalk(baseDir)
got, err := dirwalk(baseDir)
if err != nil {
t.Errorf("dirwalk: %v", err)
}
want := []string{
filepath.Join(tempDir, "dir_b", "file_b"),
filepath.Join(tempDir, "dir_c", "file_c"),
Expand All @@ -1104,6 +1113,13 @@ func Test_dirwalk(t *testing.T) {
}
}

func Test_dirwalk_dummyDir(t *testing.T) {
_, err := dirwalk("dummy")
if err == nil {
t.Errorf("expect to occure error but nothing: %v", err)
}
}

func setup() (broker *Broker, mux *http.ServeMux, serverURL string, teardown func()) {
mux = http.NewServeMux()

Expand Down

0 comments on commit 96d252e

Please sign in to comment.