Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

define func newApp() for internal testability #133

Merged
merged 2 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ type broker struct {
writer io.Writer
}

func newBroker(bc *blogConfig) *broker {
func newBroker(bc *blogConfig, w io.Writer) *broker {
if w == nil {
w = os.Stdout
}
return &broker{
Client: &atom.Client{
Client: &http.Client{
Expand All @@ -28,7 +31,7 @@ func newBroker(bc *blogConfig) *broker {
},
},
blogConfig: bc,
writer: os.Stdout,
writer: w,
}
}

Expand Down
2 changes: 1 addition & 1 deletion broker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestOriginalPath(t *testing.T) {
BlogID: "example1.hatenablog.com",
Username: "sample1",
}
broker := newBroker(&config)
broker := newBroker(&config, nil)

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (bc *blogConfig) fetchRootURL() string {
if bc.rootURL != "" {
return bc.rootURL
}
b := newBroker(bc)
b := newBroker(bc, nil)
u := entryEndPointUrl(bc)
feed, err := b.Client.GetFeed(u)
if err != nil {
Expand Down
21 changes: 12 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

var errCommandHelp = fmt.Errorf("command help shown")

func main() {
func newApp() *cli.App {
app := cli.NewApp()
app.Commands = []*cli.Command{
commandPull,
Expand All @@ -23,8 +23,11 @@ func main() {
commandRemove,
}
app.Version = fmt.Sprintf("%s (%s)", version, revision)
err := app.Run(os.Args)
if err != nil {
return app
}

func main() {
if err := newApp().Run(os.Args); err != nil {
if err != errCommandHelp {
logf("error", "%s", err)
}
Expand Down Expand Up @@ -122,7 +125,7 @@ var commandPull = &cli.Command{
return fmt.Errorf("blog not found: %s", blog)
}

b := newBroker(blogConfig)
b := newBroker(blogConfig, c.App.Writer)
remoteEntries, err := b.FetchRemoteEntries(
!c.Bool("only-drafts"), !c.Bool("no-drafts"))
if err != nil {
Expand Down Expand Up @@ -174,7 +177,7 @@ var commandFetch = &cli.Command{
if bc == nil {
return fmt.Errorf("cannot find blog for %s", path)
}
b := newBroker(bc)
b := newBroker(bc, c.App.Writer)
if _, err := b.StoreFresh(e, path); err != nil {
return err
}
Expand Down Expand Up @@ -233,7 +236,7 @@ var commandPush = &cli.Command{
return fmt.Errorf("%q is not a blog entry", path)
}
entry.CustomPath = strings.TrimSuffix(stuffs[1], entryExt)
b := newBroker(bc)
b := newBroker(bc, c.App.Writer)
err = b.PostEntry(entry, false)
if err != nil {
return err
Expand All @@ -250,7 +253,7 @@ var commandPush = &cli.Command{
return fmt.Errorf("cannot find blog for %s", path)
}

_, err = newBroker(bc).UploadFresh(entry)
_, err = newBroker(bc, c.App.Writer).UploadFresh(entry)
if err != nil {
return err
}
Expand Down Expand Up @@ -301,7 +304,7 @@ var commandPost = &cli.Command{
entry.Title = title
}

b := newBroker(blogConfig)
b := newBroker(blogConfig, c.App.Writer)
err = b.PostEntry(entry, c.Bool("page"))
if err != nil {
return err
Expand Down Expand Up @@ -386,7 +389,7 @@ var commandRemove = &cli.Command{
return fmt.Errorf("cannot find blog for %s", path)
}

err = newBroker(bc).RemoveEntry(entry, path)
err = newBroker(bc, c.App.Writer).RemoveEntry(entry, path)
if err != nil {
return err
}
Expand Down
Loading