diff --git a/.gitignore b/.gitignore index 0c6b2e9..7fbf926 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,7 @@ # Executable Binary qiisync qiisync.exe + +# Go test coverage +c.out +coverage.html diff --git a/broker.go b/broker.go index 761fd0a..2f9674e 100644 --- a/broker.go +++ b/broker.go @@ -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 { @@ -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. diff --git a/broker_test.go b/broker_test.go index c73d7a8..c11ee76 100644 --- a/broker_test.go +++ b/broker_test.go @@ -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 { @@ -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"), @@ -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 occur error but nothing: %v", err) + } +} + func setup() (broker *Broker, mux *http.ServeMux, serverURL string, teardown func()) { mux = http.NewServeMux()