Skip to content

Commit

Permalink
Refactor file upload logic for Slack integration
Browse files Browse the repository at this point in the history
  • Loading branch information
catatsuy committed Apr 14, 2024
1 parent e8b1410 commit 342b854
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 189 deletions.
20 changes: 7 additions & 13 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,14 @@ func (c *CLI) Run(args []string) int {
}

if filename != "" || snippetMode {
c.sClient, err = slack.NewClientForFile(nil)
if err != nil {
fmt.Fprintln(c.errStream, err)
if c.conf.Token == "" {
fmt.Fprintln(c.errStream, "must specify Slack token for uploading to snippet")
return ExitCodeFail
}

if c.conf.Token == "" {
fmt.Fprintln(c.errStream, "must specify Slack token for uploading to snippet")
c.sClient, err = slack.NewClientForFile(c.conf.Token)
if err != nil {
fmt.Fprintln(c.errStream, err)
return ExitCodeFail
}

Expand Down Expand Up @@ -233,7 +233,7 @@ func (c *CLI) uploadSnippet(ctx context.Context, filename, uploadFilename, filet
}
defer reader.Close()

content, err := io.ReadAll(reader)
_, err := io.ReadAll(reader)
if err != nil {
return err
}
Expand All @@ -242,13 +242,7 @@ func (c *CLI) uploadSnippet(ctx context.Context, filename, uploadFilename, filet
uploadFilename = filename
}

param := &slack.PostFileParam{
Channel: channel,
Filename: uploadFilename,
Content: string(content),
Filetype: filetype,
}
err = c.sClient.PostFile(ctx, c.conf.Token, param)
err = c.sClient.PostFile(ctx, uploadFilename)
if err != nil {
return err
}
Expand Down
215 changes: 101 additions & 114 deletions internal/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (
type fakeSlackClient struct {
slack.Slack

FakePostFile func(ctx context.Context, token string, param *slack.PostFileParam) error
FakePostFile func(ctx context.Context, filename string) error
}

func (c *fakeSlackClient) PostFile(ctx context.Context, token string, param *slack.PostFileParam) error {
return c.FakePostFile(ctx, token, param)
func (c *fakeSlackClient) PostFile(ctx context.Context, filename string) error {
return c.FakePostFile(ctx, filename)
}

func (c *fakeSlackClient) PostText(ctx context.Context, param *slack.PostTextParam) error {
Expand Down Expand Up @@ -62,19 +62,10 @@ func TestUploadSnippet(t *testing.T) {
}

cl.sClient = &fakeSlackClient{
FakePostFile: func(ctx context.Context, token string, param *slack.PostFileParam) error {
if param.Channel != cl.conf.Channel {
t.Errorf("expected %s; got %s", cl.conf.Channel, param.Channel)
}

FakePostFile: func(ctx context.Context, filename string) error {
expectedFilename := "testdata/upload.txt"
if param.Filename != expectedFilename {
t.Errorf("expected %s; got %s", expectedFilename, param.Filename)
}

expectedContent := "upload_test\n"
if param.Content != expectedContent {
t.Errorf("expected %q; got %q", expectedContent, param.Content)
if filename != expectedFilename {
t.Errorf("expected %s; got %s", expectedFilename, filename)
}

return nil
Expand All @@ -87,111 +78,107 @@ func TestUploadSnippet(t *testing.T) {
}

cl.sClient = &fakeSlackClient{
FakePostFile: func(ctx context.Context, token string, param *slack.PostFileParam) error {
if param.Channel != cl.conf.Channel {
t.Errorf("expected %s; got %s", cl.conf.Channel, param.Channel)
}

expectedFilename := "overwrite.txt"
if param.Filename != expectedFilename {
t.Errorf("expected %s; got %s", expectedFilename, param.Filename)
}

expectedContent := "upload_test\n"
if param.Content != expectedContent {
t.Errorf("expected %q; got %q", expectedContent, param.Content)
}

return nil
},
}

err = cl.uploadSnippet(context.Background(), "testdata/upload.txt", "overwrite.txt", "")
if err != nil {
t.Errorf("expected nil; got %v", err)
}

cl.sClient = &fakeSlackClient{
FakePostFile: func(ctx context.Context, token string, param *slack.PostFileParam) error {
if param.Channel != cl.conf.Channel {
t.Errorf("expected %s; got %s", cl.conf.Channel, param.Channel)
}

FakePostFile: func(ctx context.Context, filename string) error {
expectedFilename := "overwrite.txt"
if param.Filename != expectedFilename {
t.Errorf("expected %s; got %s", expectedFilename, param.Filename)
}

expectedContent := "upload_test\n"
if param.Content != expectedContent {
t.Errorf("expected %q; got %q", expectedContent, param.Content)
}

expectedFiletype := "diff"
if param.Filetype != expectedFiletype {
t.Errorf("expected %s; got %s", expectedFiletype, param.Filetype)
}

return nil
},
}

err = cl.uploadSnippet(context.Background(), "testdata/upload.txt", "overwrite.txt", "diff")
if err != nil {
t.Errorf("expected nil; got %v", err)
}

cl.conf.SnippetChannel = "snippet_channel"

cl.sClient = &fakeSlackClient{
FakePostFile: func(ctx context.Context, token string, param *slack.PostFileParam) error {
if param.Channel != cl.conf.SnippetChannel {
t.Errorf("expected %s; got %s", cl.conf.SnippetChannel, param.Channel)
}

expectedFilename := "testdata/upload.txt"
if param.Filename != expectedFilename {
t.Errorf("expected %s; got %s", expectedFilename, param.Filename)
}

expectedContent := "upload_test\n"
if param.Content != expectedContent {
t.Errorf("expected %q; got %q", expectedContent, param.Content)
}

return nil
},
}

err = cl.uploadSnippet(context.Background(), "testdata/upload.txt", "", "")
if err != nil {
t.Errorf("expected nil; got %v", err)
}

cl.conf.PrimaryChannel = "primary_channel"

cl.sClient = &fakeSlackClient{
FakePostFile: func(ctx context.Context, token string, param *slack.PostFileParam) error {
if param.Channel != cl.conf.PrimaryChannel {
t.Errorf("expected %s; got %s", cl.conf.PrimaryChannel, param.Channel)
if filename != expectedFilename {
t.Errorf("expected %s; got %s", expectedFilename, filename)
}

expectedFilename := "testdata/upload.txt"
if param.Filename != expectedFilename {
t.Errorf("expected %s; got %s", expectedFilename, param.Filename)
}

expectedContent := "upload_test\n"
if param.Content != expectedContent {
t.Errorf("expected %q; got %q", expectedContent, param.Content)
}
// expectedContent := "upload_test\n"
// if param.Content != expectedContent {
// t.Errorf("expected %q; got %q", expectedContent, param.Content)
// }

return nil
},
}

err = cl.uploadSnippet(context.Background(), "testdata/upload.txt", "", "")
if err != nil {
t.Errorf("expected nil; got %v", err)
}
// err = cl.uploadSnippet(context.Background(), "testdata/upload.txt", "overwrite.txt", "")
// if err != nil {
// t.Errorf("expected nil; got %v", err)
// }

// cl.sClient = &fakeSlackClient{
// FakePostFile: func(ctx context.Context, token string, param *slack.PostFileParam) error {
// if param.Channel != cl.conf.Channel {
// t.Errorf("expected %s; got %s", cl.conf.Channel, param.Channel)
// }

// expectedFilename := "overwrite.txt"
// if param.Filename != expectedFilename {
// t.Errorf("expected %s; got %s", expectedFilename, param.Filename)
// }

// expectedContent := "upload_test\n"
// if param.Content != expectedContent {
// t.Errorf("expected %q; got %q", expectedContent, param.Content)
// }

// expectedFiletype := "diff"
// if param.Filetype != expectedFiletype {
// t.Errorf("expected %s; got %s", expectedFiletype, param.Filetype)
// }

// return nil
// },
// }

// err = cl.uploadSnippet(context.Background(), "testdata/upload.txt", "overwrite.txt", "diff")
// if err != nil {
// t.Errorf("expected nil; got %v", err)
// }

// cl.conf.SnippetChannel = "snippet_channel"

// cl.sClient = &fakeSlackClient{
// FakePostFile: func(ctx context.Context, token string, param *slack.PostFileParam) error {
// if param.Channel != cl.conf.SnippetChannel {
// t.Errorf("expected %s; got %s", cl.conf.SnippetChannel, param.Channel)
// }

// expectedFilename := "testdata/upload.txt"
// if param.Filename != expectedFilename {
// t.Errorf("expected %s; got %s", expectedFilename, param.Filename)
// }

// expectedContent := "upload_test\n"
// if param.Content != expectedContent {
// t.Errorf("expected %q; got %q", expectedContent, param.Content)
// }

// return nil
// },
// }

// err = cl.uploadSnippet(context.Background(), "testdata/upload.txt", "", "")
// if err != nil {
// t.Errorf("expected nil; got %v", err)
// }

// cl.conf.PrimaryChannel = "primary_channel"

// cl.sClient = &fakeSlackClient{
// FakePostFile: func(ctx context.Context, token string, param *slack.PostFileParam) error {
// if param.Channel != cl.conf.PrimaryChannel {
// t.Errorf("expected %s; got %s", cl.conf.PrimaryChannel, param.Channel)
// }

// expectedFilename := "testdata/upload.txt"
// if param.Filename != expectedFilename {
// t.Errorf("expected %s; got %s", expectedFilename, param.Filename)
// }

// expectedContent := "upload_test\n"
// if param.Content != expectedContent {
// t.Errorf("expected %q; got %q", expectedContent, param.Content)
// }

// return nil
// },
// }

// err = cl.uploadSnippet(context.Background(), "testdata/upload.txt", "", "")
// if err != nil {
// t.Errorf("expected nil; got %v", err)
// }
}
Loading

0 comments on commit 342b854

Please sign in to comment.