Skip to content

Commit

Permalink
Port tests to 'stretchr/testify' library
Browse files Browse the repository at this point in the history
  • Loading branch information
anatol committed Jul 22, 2023
1 parent 63f4d86 commit 0ddc400
Show file tree
Hide file tree
Showing 12 changed files with 477 additions and 1,137 deletions.
37 changes: 7 additions & 30 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import (
"path"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/require"
)

// test that `parseConfig()` can successfully load YAML config
Expand Down Expand Up @@ -56,14 +55,7 @@ repos:
DownloadTimeout: 200,
Prefetch: &RefreshPeriod{Cron: "0 0 3 * * * *", TTLUnaccessed: 5, TTLUnupdated: 200},
}
if !cmp.Equal(*got, *want, cmpopts.IgnoreFields(Config{}, "Prefetch"), cmpopts.IgnoreUnexported(Repo{})) {
t.Errorf("got %v, want %v", *got, *want)
}
gotR := *(*got).Prefetch
wantR := *(*want).Prefetch
if !cmp.Equal(gotR, wantR) {
t.Errorf("got %v, want %v", gotR, wantR)
}
require.Equal(t, want, got)
}

// test that `purgeFilesAfter` is being read correctly
Expand All @@ -88,9 +80,7 @@ repos:
Prefetch: nil,
}

if !cmp.Equal(*got, *want, cmpopts.IgnoreUnexported(Repo{})) {
t.Errorf("got %v, want %v", *got, *want)
}
require.Equal(t, want, got)
}

// test that config works without `purgeFilesAfter`
Expand All @@ -114,19 +104,15 @@ repos:
Prefetch: nil,
}

if !cmp.Equal(*got, *want, cmpopts.IgnoreUnexported(Repo{})) {
t.Errorf("got %v, want %v", *got, *want)
}
require.Equal(t, want, got)
}

func TestLoadConfigWithMirrorlist(t *testing.T) {
temp := t.TempDir()
tmpfile := path.Join(temp, "tmpMirrorFile")

f, err := os.Create(tmpfile)
if err != nil {
t.Error(err)
}
require.NoError(t, err)
f.Close()
got := parseConfig([]byte(`
cache_dir: ` + temp + `
Expand All @@ -153,14 +139,7 @@ repos:
DownloadTimeout: 200,
Prefetch: &RefreshPeriod{Cron: "0 0 3 * * * *", TTLUnaccessed: 5, TTLUnupdated: 200},
}
if !cmp.Equal(*got, *want, cmpopts.IgnoreFields(Config{}, "Prefetch"), cmpopts.IgnoreUnexported(Repo{})) {
t.Errorf("got %v, want %v", *got, *want)
}
gotR := *(*got).Prefetch
wantR := *(*want).Prefetch
if !cmp.Equal(gotR, wantR) {
t.Errorf("got %v, want %v", gotR, wantR)
}
require.Equal(t, want, got)
}

func TestLoadConfigWithMirrorlistTimestamps(t *testing.T) {
Expand All @@ -182,7 +161,5 @@ repos:
},
},
}
if !cmp.Equal(*got, *want, cmpopts.IgnoreUnexported(Repo{})) {
t.Errorf("got %v, want %v", *got, *want)
}
require.Equal(t, want, got)
}
25 changes: 9 additions & 16 deletions downloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"sync"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestParallelDownload(t *testing.T) {
Expand All @@ -30,17 +32,13 @@ func TestParallelDownload(t *testing.T) {

srv := &http.Server{Addr: ":0", Handler: mux}
ln, err := net.Listen("tcp", srv.Addr)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
go srv.Serve(ln)
defer srv.Shutdown(context.Background())

// setup a pacoloco proxy
testPacolocoDir, err := os.MkdirTemp(os.TempDir(), "*-pacoloco-repo")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer os.RemoveAll(testPacolocoDir)

repo := &Repo{
Expand Down Expand Up @@ -69,6 +67,8 @@ func TestParallelDownload(t *testing.T) {

for i := 0; i < num; i++ {
go func() {
defer counter.Done()

f := files[rand.Int()%len(files)]
content := "This is a sample content for /myrepo/" + f

Expand All @@ -84,19 +84,12 @@ func TestParallelDownload(t *testing.T) {
}

w := httptest.NewRecorder()
if err := handleRequest(w, req); err != nil {
t.Error(err)
}
require.NoError(t, handleRequest(w, req))
res := w.Result()
defer res.Body.Close()
data, err := io.ReadAll(res.Body)
if err != nil {
t.Errorf("expected error to be nil got %v", err)
}
if string(data) != content {
t.Errorf("expected '%s' got '%s'", content, string(data))
}
counter.Done()
require.NoError(t, err)
require.Equal(t, content, string(data))
}()
}

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75
github.com/klauspost/compress v1.16.7
github.com/prometheus/client_golang v1.16.0
github.com/stretchr/testify v1.8.4
golang.org/x/sys v0.10.0
gopkg.in/yaml.v3 v3.0.1
gorm.io/driver/sqlite v1.5.2
Expand All @@ -24,6 +25,7 @@ require (
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v1.16.0 h1:yk/hx9hDbrGHovbci4BY+pRMfSuuat626eFsHb7tmT8=
github.com/prometheus/client_golang v1.16.0/go.mod h1:Zsulrv/L9oM40tJ7T815tM89lFEugiJ9HzIqaAx4LKc=
github.com/prometheus/client_model v0.4.0 h1:5lQXD3cAg1OXBf4Wq03gTrXHeaV0TQvGfUooCfx1yqY=
Expand All @@ -38,6 +40,8 @@ github.com/prometheus/common v0.44.0/go.mod h1:ofAIvZbQ1e/nugmZGz4/qCb9Ap1VoSTIO
github.com/prometheus/procfs v0.11.0 h1:5EAgkfkMl659uZPbe9AS2N68a7Cc1TJbPEuGzFuRbyk=
github.com/prometheus/procfs v0.11.0/go.mod h1:nwNm2aOCAYw8uTR/9bWRREkZFxAUcWzPHWJq+XBB/FM=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
Loading

0 comments on commit 0ddc400

Please sign in to comment.