diff --git a/article_test.go b/article_test.go index 2cee68d..b1299cc 100644 --- a/article_test.go +++ b/article_test.go @@ -117,6 +117,35 @@ Private: true }, wantErr: false, }, + { + name: "invalid_yaml", + inputData: `--- +ID: 1234567890abcdefghij +Title: テストTitle +Tags: Test:v0.0.1 +Author: d-tsuji +Private: true + +# はじめに + +はじめてのQiitaです +`, + args: args{filepath.Join("temp", "test.md")}, + wantErr: true, + }, + { + name: "invalid_yaml_format", + inputData: `--- +- ID: 1234567890abcdefghij +--- + +# はじめに + +はじめてのQiitaです +`, + args: args{filepath.Join("temp", "test.md")}, + wantErr: true, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/broker.go b/broker.go index fa99166..c226d2d 100644 --- a/broker.go +++ b/broker.go @@ -240,19 +240,13 @@ func (b *Broker) store(path string, article *Article) error { if err != nil { return err } + defer f.Close() fullContext, err := article.fullContent() if err != nil { return err } - _, err = f.WriteString(fullContext) - if err != nil { - return err - } - - if err := f.Close(); err != nil { - return err - } + f.WriteString(fullContext) return os.Chtimes(path, article.Item.UpdatedAt, article.Item.UpdatedAt) } diff --git a/broker_test.go b/broker_test.go index 3c24cf8..2b8ae9d 100644 --- a/broker_test.go +++ b/broker_test.go @@ -1044,6 +1044,42 @@ func TestStoreFilename(t *testing.T) { } } +func Test_dirwalk(t *testing.T) { + baseDir := filepath.Join("testdata", "walk") + os.MkdirAll(baseDir, 0755) + + tempDir, err := ioutil.TempDir(baseDir, "temp") + if err != nil { + t.Errorf("create tempDir: %v", err) + } + t.Cleanup(func() { + if err := os.RemoveAll(tempDir); err != nil { + t.Errorf("remove tempDir: %v", err) + } + }) + + os.MkdirAll(filepath.Join(tempDir, "dir_b"), 0777) + os.MkdirAll(filepath.Join(tempDir, "dir_c"), 0777) + + f, _ := os.Create(filepath.Join(tempDir, "file_a")) + f.Close() + f, _ = os.Create(filepath.Join(tempDir, "dir_b", "file_b")) + f.Close() + f, _ = os.Create(filepath.Join(tempDir, "dir_c", "file_c")) + f.Close() + + got := dirwalk(baseDir) + want := []string{ + filepath.Join(tempDir, "dir_b", "file_b"), + filepath.Join(tempDir, "dir_c", "file_c"), + filepath.Join(tempDir, "file_a"), + } + + if diff := cmp.Diff(want, got); diff != "" { + t.Errorf("dirwalk(%s) mismatch (-want +got):\n%s", tempDir, diff) + } +} + func setup() (broker *Broker, mux *http.ServeMux, serverURL string, teardown func()) { mux = http.NewServeMux() diff --git a/config_test.go b/config_test.go index 860af09..4581fd5 100644 --- a/config_test.go +++ b/config_test.go @@ -115,6 +115,13 @@ filename_mode = "title"`), }, wantErr: false, }, + { + name: "invalid_linux_relative_title", + args: args{ + r: strings.NewReader(`invalid`), + }, + wantErr: true, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {