Skip to content

Commit

Permalink
add: test
Browse files Browse the repository at this point in the history
  • Loading branch information
d-tsuji committed Apr 24, 2020
1 parent 8b5b578 commit a9a63bb
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 8 deletions.
29 changes: 29 additions & 0 deletions article_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 2 additions & 8 deletions broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
36 changes: 36 additions & 0 deletions broker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
7 changes: 7 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit a9a63bb

Please sign in to comment.