diff options
author | Nick Thomas <nick@gitlab.com> | 2019-09-11 16:46:05 +0300 |
---|---|---|
committer | Nick Thomas <nick@gitlab.com> | 2019-09-12 14:18:00 +0300 |
commit | 26d8e2d1197e0ffb9c0fcd47bb61ef35a901f67b (patch) | |
tree | 0829e6f01c15972e1780ed9fc1f42d993350c830 /internal/domain | |
parent | dd802698dba879ef5eeeb7b4f4abd03364f3788f (diff) |
Remove github.com/stretchr/testify/assert
The "assert" form is difficult to use correctly and complicates test
understanding. I think we should insist on using require *only* in
our tests across GitLab.
Diffstat (limited to 'internal/domain')
-rw-r--r-- | internal/domain/domain_config_test.go | 21 | ||||
-rw-r--r-- | internal/domain/domain_test.go | 89 | ||||
-rw-r--r-- | internal/domain/group_test.go | 8 | ||||
-rw-r--r-- | internal/domain/map_test.go | 19 |
4 files changed, 67 insertions, 70 deletions
diff --git a/internal/domain/domain_config_test.go b/internal/domain/domain_config_test.go index a89ead35..8cdcdeaa 100644 --- a/internal/domain/domain_config_test.go +++ b/internal/domain/domain_config_test.go @@ -6,7 +6,6 @@ import ( "path/filepath" "testing" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -16,25 +15,25 @@ const validConfig = `{"Domains":[{"Domain":"test"}]}` func TestDomainConfigValidness(t *testing.T) { d := domainConfig{} - assert.False(t, d.Valid("gitlab.io")) + require.False(t, d.Valid("gitlab.io")) d = domainConfig{Domain: "test"} - assert.True(t, d.Valid("gitlab.io")) + require.True(t, d.Valid("gitlab.io")) d = domainConfig{Domain: "test"} - assert.True(t, d.Valid("gitlab.io")) + require.True(t, d.Valid("gitlab.io")) d = domainConfig{Domain: "test.gitlab.io"} - assert.False(t, d.Valid("gitlab.io")) + require.False(t, d.Valid("gitlab.io")) d = domainConfig{Domain: "test.test.gitlab.io"} - assert.False(t, d.Valid("gitlab.io")) + require.False(t, d.Valid("gitlab.io")) d = domainConfig{Domain: "test.testgitlab.io"} - assert.True(t, d.Valid("gitlab.io")) + require.True(t, d.Valid("gitlab.io")) d = domainConfig{Domain: "test.GitLab.Io"} - assert.False(t, d.Valid("gitlab.io")) + require.False(t, d.Valid("gitlab.io")) } func TestDomainConfigRead(t *testing.T) { @@ -43,20 +42,20 @@ func TestDomainConfigRead(t *testing.T) { d := domainsConfig{} err := d.Read("test-group", "test-project") - assert.Error(t, err) + require.Error(t, err) os.MkdirAll(filepath.Dir(configFile), 0700) defer os.RemoveAll("test-group") d = domainsConfig{} err = d.Read("test-group", "test-project") - assert.Error(t, err) + require.Error(t, err) err = ioutil.WriteFile(configFile, []byte(invalidConfig), 0600) require.NoError(t, err) d = domainsConfig{} err = d.Read("test-group", "test-project") - assert.Error(t, err) + require.Error(t, err) err = ioutil.WriteFile(configFile, []byte(validConfig), 0600) require.NoError(t, err) diff --git a/internal/domain/domain_test.go b/internal/domain/domain_test.go index 8b2dae1c..499510a8 100644 --- a/internal/domain/domain_test.go +++ b/internal/domain/domain_test.go @@ -10,7 +10,6 @@ import ( "testing" "time" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitlab-pages/internal/fixture" @@ -45,27 +44,27 @@ func testGroupServeHTTPHost(t *testing.T, host string) { serve := serveFileOrNotFound(testGroup) - assert.HTTPBodyContains(t, serve, "GET", makeURL("/"), nil, "main-dir") - assert.HTTPBodyContains(t, serve, "GET", makeURL("/index"), nil, "main-dir") - assert.HTTPBodyContains(t, serve, "GET", makeURL("/index.html"), nil, "main-dir") + require.HTTPBodyContains(t, serve, "GET", makeURL("/"), nil, "main-dir") + require.HTTPBodyContains(t, serve, "GET", makeURL("/index"), nil, "main-dir") + require.HTTPBodyContains(t, serve, "GET", makeURL("/index.html"), nil, "main-dir") testhelpers.AssertRedirectTo(t, serve, "GET", makeURL("/project"), nil, "//"+host+"/project/") - assert.HTTPBodyContains(t, serve, "GET", makeURL("/project/"), nil, "project-subdir") - assert.HTTPBodyContains(t, serve, "GET", makeURL("/project/index"), nil, "project-subdir") - assert.HTTPBodyContains(t, serve, "GET", makeURL("/project/index/"), nil, "project-subdir") - assert.HTTPBodyContains(t, serve, "GET", makeURL("/project/index.html"), nil, "project-subdir") + require.HTTPBodyContains(t, serve, "GET", makeURL("/project/"), nil, "project-subdir") + require.HTTPBodyContains(t, serve, "GET", makeURL("/project/index"), nil, "project-subdir") + require.HTTPBodyContains(t, serve, "GET", makeURL("/project/index/"), nil, "project-subdir") + require.HTTPBodyContains(t, serve, "GET", makeURL("/project/index.html"), nil, "project-subdir") testhelpers.AssertRedirectTo(t, serve, "GET", makeURL("/project/subdir"), nil, "//"+host+"/project/subdir/") - assert.HTTPBodyContains(t, serve, "GET", makeURL("/project/subdir/"), nil, "project-subsubdir") - assert.HTTPBodyContains(t, serve, "GET", makeURL("/project2/"), nil, "project2-main") - assert.HTTPBodyContains(t, serve, "GET", makeURL("/project2/index"), nil, "project2-main") - assert.HTTPBodyContains(t, serve, "GET", makeURL("/project2/index.html"), nil, "project2-main") - assert.HTTPError(t, serve, "GET", makeURL("/private.project/"), nil) - assert.HTTPError(t, serve, "GET", makeURL("//about.gitlab.com/%2e%2e"), nil) - assert.HTTPError(t, serve, "GET", makeURL("/symlink"), nil) - assert.HTTPError(t, serve, "GET", makeURL("/symlink/index.html"), nil) - assert.HTTPError(t, serve, "GET", makeURL("/symlink/subdir/"), nil) - assert.HTTPError(t, serve, "GET", makeURL("/project/fifo"), nil) - assert.HTTPError(t, serve, "GET", makeURL("/not-existing-file"), nil) - assert.HTTPRedirect(t, serve, "GET", makeURL("/project//about.gitlab.com/%2e%2e"), nil) + require.HTTPBodyContains(t, serve, "GET", makeURL("/project/subdir/"), nil, "project-subsubdir") + require.HTTPBodyContains(t, serve, "GET", makeURL("/project2/"), nil, "project2-main") + require.HTTPBodyContains(t, serve, "GET", makeURL("/project2/index"), nil, "project2-main") + require.HTTPBodyContains(t, serve, "GET", makeURL("/project2/index.html"), nil, "project2-main") + require.HTTPError(t, serve, "GET", makeURL("/private.project/"), nil) + require.HTTPError(t, serve, "GET", makeURL("//about.gitlab.com/%2e%2e"), nil) + require.HTTPError(t, serve, "GET", makeURL("/symlink"), nil) + require.HTTPError(t, serve, "GET", makeURL("/symlink/index.html"), nil) + require.HTTPError(t, serve, "GET", makeURL("/symlink/subdir/"), nil) + require.HTTPError(t, serve, "GET", makeURL("/project/fifo"), nil) + require.HTTPError(t, serve, "GET", makeURL("/not-existing-file"), nil) + require.HTTPRedirect(t, serve, "GET", makeURL("/project//about.gitlab.com/%2e%2e"), nil) } func TestGroupServeHTTP(t *testing.T) { @@ -88,15 +87,15 @@ func TestDomainServeHTTP(t *testing.T) { }, } - assert.HTTPBodyContains(t, serveFileOrNotFound(testDomain), "GET", "/", nil, "project2-main") - assert.HTTPBodyContains(t, serveFileOrNotFound(testDomain), "GET", "/index.html", nil, "project2-main") - assert.HTTPRedirect(t, serveFileOrNotFound(testDomain), "GET", "/subdir", nil) - assert.HTTPBodyContains(t, serveFileOrNotFound(testDomain), "GET", "/subdir", nil, + require.HTTPBodyContains(t, serveFileOrNotFound(testDomain), "GET", "/", nil, "project2-main") + require.HTTPBodyContains(t, serveFileOrNotFound(testDomain), "GET", "/index.html", nil, "project2-main") + require.HTTPRedirect(t, serveFileOrNotFound(testDomain), "GET", "/subdir", nil) + require.HTTPBodyContains(t, serveFileOrNotFound(testDomain), "GET", "/subdir", nil, `<a href="/subdir/">Found</a>`) - assert.HTTPBodyContains(t, serveFileOrNotFound(testDomain), "GET", "/subdir/", nil, "project2-subdir") - assert.HTTPBodyContains(t, serveFileOrNotFound(testDomain), "GET", "/subdir/index.html", nil, "project2-subdir") - assert.HTTPError(t, serveFileOrNotFound(testDomain), "GET", "//about.gitlab.com/%2e%2e", nil) - assert.HTTPError(t, serveFileOrNotFound(testDomain), "GET", "/not-existing-file", nil) + require.HTTPBodyContains(t, serveFileOrNotFound(testDomain), "GET", "/subdir/", nil, "project2-subdir") + require.HTTPBodyContains(t, serveFileOrNotFound(testDomain), "GET", "/subdir/index.html", nil, "project2-subdir") + require.HTTPError(t, serveFileOrNotFound(testDomain), "GET", "//about.gitlab.com/%2e%2e", nil) + require.HTTPError(t, serveFileOrNotFound(testDomain), "GET", "/not-existing-file", nil) } func TestIsHTTPSOnly(t *testing.T) { @@ -200,7 +199,7 @@ func TestIsHTTPSOnly(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { req, _ := http.NewRequest(http.MethodGet, test.url, nil) - assert.Equal(t, test.expected, test.domain.IsHTTPSOnly(req)) + require.Equal(t, test.expected, test.domain.IsHTTPSOnly(req)) }) } } @@ -264,7 +263,7 @@ func TestHasAcmeChallenge(t *testing.T) { } for _, test := range tests { t.Run(test.name, func(t *testing.T) { - assert.Equal(t, test.expected, test.domain.HasAcmeChallenge(test.token)) + require.Equal(t, test.expected, test.domain.HasAcmeChallenge(test.token)) }) } } @@ -284,16 +283,16 @@ func testHTTPGzip(t *testing.T, handler http.HandlerFunc, mode, url string, valu defer reader.Close() contentEncoding := w.Header().Get("Content-Encoding") - assert.Equal(t, "gzip", contentEncoding, "Content-Encoding") + require.Equal(t, "gzip", contentEncoding, "Content-Encoding") bytes, err := ioutil.ReadAll(reader) require.NoError(t, err) - assert.Contains(t, string(bytes), str) + require.Contains(t, string(bytes), str) } else { - assert.Contains(t, w.Body.String(), str) + require.Contains(t, w.Body.String(), str) } - assert.Equal(t, contentType, w.Header().Get("Content-Type")) + require.Equal(t, contentType, w.Header().Get("Content-Type")) } func TestGroupServeHTTPGzip(t *testing.T) { @@ -386,7 +385,7 @@ func TestGroup404ServeHTTP(t *testing.T) { testhelpers.AssertHTTP404(t, serveFileOrNotFound(testGroup), "GET", "http://group.404.test.io/not/existing-file", nil, "Custom 404 group page") testhelpers.AssertHTTP404(t, serveFileOrNotFound(testGroup), "GET", "http://group.404.test.io/not-existing-file", nil, "Custom 404 group page") testhelpers.AssertHTTP404(t, serveFileOrNotFound(testGroup), "GET", "http://group.404.test.io/", nil, "Custom 404 group page") - assert.HTTPBodyNotContains(t, serveFileOrNotFound(testGroup), "GET", "http://group.404.test.io/project.404.symlink/not/existing-file", nil, "Custom 404 project page") + require.HTTPBodyNotContains(t, serveFileOrNotFound(testGroup), "GET", "http://group.404.test.io/project.404.symlink/not/existing-file", nil, "Custom 404 project page") // Ensure the namespace project's custom 404.html is not used by projects testhelpers.AssertHTTP404(t, serveFileOrNotFound(testGroup), "GET", "http://group.404.test.io/project.no.404/not/existing-file", nil, "The page you're looking for could not be found.") @@ -426,8 +425,8 @@ func TestGroupCertificate(t *testing.T) { } tls, err := testGroup.EnsureCertificate() - assert.Nil(t, tls) - assert.Error(t, err) + require.Nil(t, tls) + require.Error(t, err) } func TestDomainNoCertificate(t *testing.T) { @@ -440,12 +439,12 @@ func TestDomainNoCertificate(t *testing.T) { } tls, err := testDomain.EnsureCertificate() - assert.Nil(t, tls) - assert.Error(t, err) + require.Nil(t, tls) + require.Error(t, err) _, err2 := testDomain.EnsureCertificate() - assert.Error(t, err) - assert.Equal(t, err, err2) + require.Error(t, err) + require.Equal(t, err, err2) } func TestDomainCertificate(t *testing.T) { @@ -460,7 +459,7 @@ func TestDomainCertificate(t *testing.T) { } tls, err := testDomain.EnsureCertificate() - assert.NotNil(t, tls) + require.NotNil(t, tls) require.NoError(t, err) } @@ -483,8 +482,8 @@ func TestCacheControlHeaders(t *testing.T) { now := time.Now() serveFileOrNotFound(testGroup)(w, req) - assert.Equal(t, http.StatusOK, w.Code) - assert.Equal(t, "max-age=600", w.Header().Get("Cache-Control")) + require.Equal(t, http.StatusOK, w.Code) + require.Equal(t, "max-age=600", w.Header().Get("Cache-Control")) expires := w.Header().Get("Expires") require.NotEmpty(t, expires) @@ -492,7 +491,7 @@ func TestCacheControlHeaders(t *testing.T) { expiresTime, err := time.Parse(time.RFC1123, expires) require.NoError(t, err) - assert.WithinDuration(t, now.UTC().Add(10*time.Minute), expiresTime.UTC(), time.Minute) + require.WithinDuration(t, now.UTC().Add(10*time.Minute), expiresTime.UTC(), time.Minute) } func TestOpenNoFollow(t *testing.T) { diff --git a/internal/domain/group_test.go b/internal/domain/group_test.go index 2e41ef53..8f75fe96 100644 --- a/internal/domain/group_test.go +++ b/internal/domain/group_test.go @@ -4,7 +4,7 @@ import ( "strings" "testing" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestGroupDig(t *testing.T) { @@ -89,9 +89,9 @@ func TestGroupDig(t *testing.T) { t.Run(test.name, func(t *testing.T) { project, projectPath, urlPath := test.g.digProjectWithSubpath("", strings.Split(test.path, "/")) - assert.Equal(t, test.expectedProject, project) - assert.Equal(t, test.expectedProjectPath, projectPath) - assert.Equal(t, test.expectedPath, urlPath) + require.Equal(t, test.expectedProject, project) + require.Equal(t, test.expectedProjectPath, projectPath) + require.Equal(t, test.expectedPath, urlPath) }) } } diff --git a/internal/domain/map_test.go b/internal/domain/map_test.go index 156b039f..de5e4955 100644 --- a/internal/domain/map_test.go +++ b/internal/domain/map_test.go @@ -10,7 +10,6 @@ import ( "time" "github.com/karrick/godirwalk" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -61,19 +60,19 @@ func TestReadProjects(t *testing.T) { } for _, expected := range domains { - assert.Contains(t, domains, expected) + require.Contains(t, domains, expected) } for _, actual := range domains { - assert.Contains(t, expectedDomains, actual) + require.Contains(t, expectedDomains, actual) } // Check that multiple domains in the same project are recorded faithfully exp1 := &domainConfig{Domain: "test.domain.com"} - assert.Equal(t, exp1, dm["test.domain.com"].config) + require.Equal(t, exp1, dm["test.domain.com"].config) exp2 := &domainConfig{Domain: "other.domain.com", Certificate: "test", Key: "key"} - assert.Equal(t, exp2, dm["other.domain.com"].config) + require.Equal(t, exp2, dm["other.domain.com"].config) // check subgroups domain, ok := dm["group.test.io"] @@ -105,7 +104,7 @@ func TestReadProjectsMaxDepth(t *testing.T) { } for _, expected := range domains { - assert.Contains(t, domains, expected) + require.Contains(t, domains, expected) } for _, actual := range domains { @@ -113,7 +112,7 @@ func TestReadProjectsMaxDepth(t *testing.T) { if !strings.HasSuffix(actual, defaultDomain) { continue } - assert.Contains(t, expectedDomains, actual) + require.Contains(t, expectedDomains, actual) } // check subgroups @@ -165,15 +164,15 @@ func TestWatch(t *testing.T) { defer os.Remove(updateFile) domains := recvTimeout(t, update) - assert.NotNil(t, domains, "if the domains are fetched on start") + require.NotNil(t, domains, "if the domains are fetched on start") writeRandomTimestamp(t) domains = recvTimeout(t, update) - assert.NotNil(t, domains, "if the domains are updated after the creation") + require.NotNil(t, domains, "if the domains are updated after the creation") writeRandomTimestamp(t) domains = recvTimeout(t, update) - assert.NotNil(t, domains, "if the domains are updated after the timestamp change") + require.NotNil(t, domains, "if the domains are updated after the timestamp change") } func recvTimeout(t *testing.T, ch <-chan Map) Map { |