Skip to content

Commit

Permalink
Merge pull request #195 from catatsuy/feature-use-t-context
Browse files Browse the repository at this point in the history
use t.Context() instead of context.Background() for test
  • Loading branch information
catatsuy authored Mar 2, 2025
2 parents b1aca90 + a52a777 commit a4389fe
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go: ["1.23.x"]
go: ["1.24.x"]
steps:
- name: Set up Go
uses: actions/setup-go@v5
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/catatsuy/notify_slack

go 1.23.0
go 1.24.0

require (
github.com/google/go-cmp v0.7.0
Expand Down
8 changes: 4 additions & 4 deletions internal/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestUploadSnippet(t *testing.T) {
}

cl.conf.ChannelID = "C12345678"
err := cl.uploadSnippet(context.Background(), "testdata/nofile.txt", "", "")
err := cl.uploadSnippet(t.Context(), "testdata/nofile.txt", "", "")
want := "no such file or directory"
if err == nil || !strings.Contains(err.Error(), want) {
t.Errorf("error = %v; want %q", err, want)
Expand All @@ -72,7 +72,7 @@ func TestUploadSnippet(t *testing.T) {
},
}

err = cl.uploadSnippet(context.Background(), "testdata/upload.txt", "", "")
err = cl.uploadSnippet(t.Context(), "testdata/upload.txt", "", "")
if err != nil {
t.Errorf("expected nil; got %v", err)
}
Expand All @@ -93,7 +93,7 @@ func TestUploadSnippet(t *testing.T) {
},
}

err = cl.uploadSnippet(context.Background(), "testdata/upload.txt", "overwrite.txt", "")
err = cl.uploadSnippet(t.Context(), "testdata/upload.txt", "overwrite.txt", "")
if err != nil {
t.Errorf("expected nil; got %v", err)
}
Expand Down Expand Up @@ -123,7 +123,7 @@ func TestUploadSnippet(t *testing.T) {
},
}

err = cl.uploadSnippet(context.Background(), "testdata/upload.txt", "overwrite.txt", "diff")
err = cl.uploadSnippet(t.Context(), "testdata/upload.txt", "overwrite.txt", "diff")
if err != nil {
t.Errorf("expected nil; got %v", err)
}
Expand Down
25 changes: 12 additions & 13 deletions internal/slack/client_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package slack_test

import (
"context"
"encoding/json"
"io"
"log/slog"
Expand Down Expand Up @@ -89,7 +88,7 @@ func TestPostText_Success(t *testing.T) {
t.Fatal(err)
}

err = c.PostText(context.Background(), param)
err = c.PostText(t.Context(), param)

if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -123,7 +122,7 @@ func TestPostText_Fail(t *testing.T) {
t.Fatal(err)
}

err = c.PostText(context.Background(), param)
err = c.PostText(t.Context(), param)

if err == nil {
t.Fatal("expected error, but nothing was returned")
Expand Down Expand Up @@ -189,7 +188,7 @@ func TestPostFile_Success(t *testing.T) {
t.Fatal(err)
}

uploadURL, fileID, err := c.GetUploadURLExternalURL(context.Background(), param)
uploadURL, fileID, err := c.GetUploadURLExternalURL(t.Context(), param)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -231,23 +230,23 @@ func TestPostFile_FailCallFunc(t *testing.T) {
t.Fatal(err)
}

_, _, err = c.GetUploadURLExternalURL(context.Background(), nil)
_, _, err = c.GetUploadURLExternalURL(t.Context(), nil)
expectedErrorPart = "provide filename and length"
if err == nil {
t.Fatal("expected error, but nothing was returned")
} else if !strings.Contains(err.Error(), expectedErrorPart) {
t.Fatalf("expected %q to contain %q", err.Error(), expectedErrorPart)
}

_, _, err = c.GetUploadURLExternalURL(context.Background(), &GetUploadURLExternalResParam{})
_, _, err = c.GetUploadURLExternalURL(t.Context(), &GetUploadURLExternalResParam{})
expectedErrorPart = "provide filename"
if err == nil {
t.Fatal("expected error, but nothing was returned")
} else if !strings.Contains(err.Error(), expectedErrorPart) {
t.Fatalf("expected %q to contain %q", err.Error(), expectedErrorPart)
}

_, _, err = c.GetUploadURLExternalURL(context.Background(), &GetUploadURLExternalResParam{Filename: "test.txt"})
_, _, err = c.GetUploadURLExternalURL(t.Context(), &GetUploadURLExternalResParam{Filename: "test.txt"})
expectedErrorPart = "provide length"
if err == nil {
t.Fatal("expected error, but nothing was returned")
Expand Down Expand Up @@ -318,7 +317,7 @@ func TestPostFile_FailAPINotOK(t *testing.T) {
t.Fatal(err)
}

_, _, err = c.GetUploadURLExternalURL(context.Background(), param)
_, _, err = c.GetUploadURLExternalURL(t.Context(), param)

if err == nil {
t.Fatal("expected error, but nothing was returned")
Expand Down Expand Up @@ -396,7 +395,7 @@ func TestPostFile_FailAPIStatusOK(t *testing.T) {
t.Fatal(err)
}

_, _, err = c.GetUploadURLExternalURL(context.Background(), param)
_, _, err = c.GetUploadURLExternalURL(t.Context(), param)

if err == nil {
t.Fatal("expected error, but nothing was returned")
Expand Down Expand Up @@ -469,7 +468,7 @@ func TestPostFile_FailBrokenJSON(t *testing.T) {
t.Fatal(err)
}

_, _, err = c.GetUploadURLExternalURL(context.Background(), param)
_, _, err = c.GetUploadURLExternalURL(t.Context(), param)

if err == nil {
t.Fatal("expected error, but nothing was returned")
Expand Down Expand Up @@ -528,7 +527,7 @@ func TestUploadToURL_success(t *testing.T) {
t.Fatal(err)
}

err = c.UploadToURL(context.Background(), "testdata/upload.txt", testAPIServer.URL, b)
err = c.UploadToURL(t.Context(), "testdata/upload.txt", testAPIServer.URL, b)
if err != nil {
t.Fatal(err)
}
Expand All @@ -553,7 +552,7 @@ func TestUploadToURL_fail(t *testing.T) {
t.Fatal(err)
}

err = c.UploadToURL(context.Background(), "upload.txt", testAPIServer.URL, b)
err = c.UploadToURL(t.Context(), "upload.txt", testAPIServer.URL, b)
if err == nil {
t.Fatal("expected error, but nothing was returned")
}
Expand Down Expand Up @@ -618,7 +617,7 @@ func TestCompleteUploadExternal_Success(t *testing.T) {
Title: "file-title",
ChannelID: "C0NF841BK",
}
err = c.CompleteUploadExternal(context.Background(), param)
err = c.CompleteUploadExternal(t.Context(), param)
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/throttle/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestRun_pipeClose(t *testing.T) {

ex := NewExec(pr)

ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(t.Context())
testC := make(chan time.Time)
count := 0
fc := make(chan struct{})
Expand Down Expand Up @@ -110,7 +110,7 @@ func TestRun_contextDone(t *testing.T) {

ex := NewExec(pr)

ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(t.Context())
testC := make(chan time.Time)
count := 0
fc := make(chan struct{})
Expand Down

0 comments on commit a4389fe

Please sign in to comment.