diff options
author | Jacob Vosmaer <jacob@gitlab.com> | 2020-08-04 18:10:36 +0300 |
---|---|---|
committer | Jacob Vosmaer <jacob@gitlab.com> | 2020-08-05 13:23:21 +0300 |
commit | 33801fe9ba05c4bd304ebd8e40eb1337309f6b48 (patch) | |
tree | b87bdce7099c8f8c11ba9ecfe0ba81470ce38258 /internal | |
parent | b2922c74ae775f4a56c784572c6672fa2c7332fa (diff) |
Fix tests that use illegal ../ paths
Diffstat (limited to 'internal')
-rw-r--r-- | internal/domain/domain_test.go | 35 | ||||
-rw-r--r-- | internal/serving/disk/serving_test.go | 12 | ||||
-rw-r--r-- | internal/source/disk/domain_test.go | 27 | ||||
-rw-r--r-- | internal/source/disk/map_test.go | 6 | ||||
-rw-r--r-- | internal/testhelpers/chdir.go | 29 |
5 files changed, 55 insertions, 54 deletions
diff --git a/internal/domain/domain_test.go b/internal/domain/domain_test.go index fc5611ba..26a7735c 100644 --- a/internal/domain/domain_test.go +++ b/internal/domain/domain_test.go @@ -5,7 +5,6 @@ import ( "io/ioutil" "net/http" "net/http/httptest" - "os" "testing" "github.com/stretchr/testify/require" @@ -132,32 +131,14 @@ func BenchmarkEnsureCertificate(b *testing.B) { var chdirSet = false -func setUpTests(t require.TestingT) func() { - return chdirInPath(t, "../../shared/pages") -} - -func chdirInPath(t require.TestingT, path string) func() { - noOp := func() {} - if chdirSet { - return noOp - } - - cwd, err := os.Getwd() - require.NoError(t, err, "Cannot Getwd") - - err = os.Chdir(path) - require.NoError(t, err, "Cannot Chdir") - - chdirSet = true - return func() { - err := os.Chdir(cwd) - require.NoError(t, err, "Cannot Chdir in cleanup") - - chdirSet = false - } +func setUpTests(t testing.TB) func() { + t.Helper() + return testhelpers.ChdirInPath(t, "../../shared/pages", &chdirSet) } func TestServeNamespaceNotFound(t *testing.T) { + defer setUpTests(t)() + tests := []struct { name string domain string @@ -171,7 +152,7 @@ func TestServeNamespaceNotFound(t *testing.T) { path: "/unknown", resolver: &stubbedResolver{ project: &serving.LookupPath{ - Path: "../../shared/pages/group.404/group.404.gitlab-example.com/public", + Path: "group.404/group.404.gitlab-example.com/public", IsNamespaceProject: true, }, subpath: "/unknown", @@ -184,7 +165,7 @@ func TestServeNamespaceNotFound(t *testing.T) { path: "/private_project/unknown", resolver: &stubbedResolver{ project: &serving.LookupPath{ - Path: "../../shared/pages/group.404/group.404.gitlab-example.com/public", + Path: "group.404/group.404.gitlab-example.com/public", IsNamespaceProject: true, HasAccessControl: false, }, @@ -198,7 +179,7 @@ func TestServeNamespaceNotFound(t *testing.T) { path: "/unknown", resolver: &stubbedResolver{ project: &serving.LookupPath{ - Path: "../../shared/pages/group.404/group.404.gitlab-example.com/public", + Path: "group.404/group.404.gitlab-example.com/public", IsNamespaceProject: true, HasAccessControl: true, }, diff --git a/internal/serving/disk/serving_test.go b/internal/serving/disk/serving_test.go index 6398700d..02b7fac7 100644 --- a/internal/serving/disk/serving_test.go +++ b/internal/serving/disk/serving_test.go @@ -9,9 +9,12 @@ import ( "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitlab-pages/internal/serving" + "gitlab.com/gitlab-org/gitlab-pages/internal/testhelpers" ) func TestDisk_ServeFileHTTP(t *testing.T) { + defer setUpTests(t)() + s := New() w := httptest.NewRecorder() r := httptest.NewRequest("GET", "http://group.gitlab-example.com/serving/index.html", nil) @@ -20,7 +23,7 @@ func TestDisk_ServeFileHTTP(t *testing.T) { Request: r, LookupPath: &serving.LookupPath{ Prefix: "/serving", - Path: "../../../shared/pages/group/serving/public", + Path: "group/serving/public", }, SubPath: "/index.html", } @@ -36,3 +39,10 @@ func TestDisk_ServeFileHTTP(t *testing.T) { require.Contains(t, string(body), "HTML Document") } + +var chdirSet = false + +func setUpTests(t testing.TB) func() { + t.Helper() + return testhelpers.ChdirInPath(t, "../../../shared/pages", &chdirSet) +} diff --git a/internal/source/disk/domain_test.go b/internal/source/disk/domain_test.go index efe45043..5c7bdbea 100644 --- a/internal/source/disk/domain_test.go +++ b/internal/source/disk/domain_test.go @@ -6,7 +6,6 @@ import ( "net/http" "net/http/httptest" "net/url" - "os" "strconv" "testing" "time" @@ -398,27 +397,7 @@ func TestCacheControlHeaders(t *testing.T) { var chdirSet = false -func setUpTests(t require.TestingT) func() { - return chdirInPath(t, "../../../shared/pages") -} - -func chdirInPath(t require.TestingT, path string) func() { - noOp := func() {} - if chdirSet { - return noOp - } - - cwd, err := os.Getwd() - require.NoError(t, err, "Cannot Getwd") - - err = os.Chdir(path) - require.NoError(t, err, "Cannot Chdir") - - chdirSet = true - return func() { - err := os.Chdir(cwd) - require.NoError(t, err, "Cannot Chdir in cleanup") - - chdirSet = false - } +func setUpTests(t *testing.T) func() { + t.Helper() + return testhelpers.ChdirInPath(t, "../../../shared/pages", &chdirSet) } diff --git a/internal/source/disk/map_test.go b/internal/source/disk/map_test.go index d2162883..4b9d6a23 100644 --- a/internal/source/disk/map_test.go +++ b/internal/source/disk/map_test.go @@ -11,6 +11,8 @@ import ( "github.com/karrick/godirwalk" "github.com/stretchr/testify/require" + + "gitlab.com/gitlab-org/gitlab-pages/internal/testhelpers" ) func getEntries(t require.TestingT) godirwalk.Dirents { @@ -179,7 +181,7 @@ func recvTimeout(t *testing.T, ch <-chan Map) Map { } } -func buildFakeDomainsDirectory(t require.TestingT, nGroups, levels int) func() { +func buildFakeDomainsDirectory(t testing.TB, nGroups, levels int) func() { testRoot, err := ioutil.TempDir("", "gitlab-pages-test") require.NoError(t, err) @@ -197,7 +199,7 @@ func buildFakeDomainsDirectory(t require.TestingT, nGroups, levels int) func() { } } - cleanup := chdirInPath(t, testRoot) + cleanup := testhelpers.ChdirInPath(t, testRoot, &chdirSet) return func() { defer cleanup() diff --git a/internal/testhelpers/chdir.go b/internal/testhelpers/chdir.go new file mode 100644 index 00000000..4015d888 --- /dev/null +++ b/internal/testhelpers/chdir.go @@ -0,0 +1,29 @@ +package testhelpers + +import ( + "os" + "testing" + + "github.com/stretchr/testify/require" +) + +func ChdirInPath(t testing.TB, path string, chdirSet *bool) func() { + t.Helper() + + if *chdirSet { + return func() {} + } + + cwd, err := os.Getwd() + require.NoError(t, err, "Cannot Getwd") + + require.NoError(t, os.Chdir(path), "Cannot Chdir") + + *chdirSet = true + return func() { + err := os.Chdir(cwd) + require.NoError(t, err, "Cannot Chdir in cleanup") + + *chdirSet = false + } +} |