mirror of
https://github.com/go-gitea/gitea.git
synced 2026-10-26 06:16:56 +09:00
test: consolidate Go tests (#39962)
Consolidate all Go tests. Coverage is identical to before. Go tests are around 5% faster while being 7% smaller. No non-test code is touched.
This commit is contained in:
1 parent
ce7364e215
commit
8abda62312
388 files changed
+18410
-29561
No files matched your search
+94
-191
@@ -162,209 +162,112 @@ func lfsTestRoundtripHandler(req *http.Request) *http.Response {
|
||||
return &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(payload)}
|
||||
}
|
||||
|
||||
func TestHTTPClientDownload(t *testing.T) {
|
||||
p := Pointer{Oid: "fb8f7d8435968c4f82a726a92395be4d16f2f63116caf36c8ad35c60831ab041", Size: 6}
|
||||
|
||||
hc := &http.Client{Transport: RoundTripFunc(func(req *http.Request) *http.Response {
|
||||
assert.Equal(t, "POST", req.Method)
|
||||
assert.Equal(t, MediaType, req.Header.Get("Content-type"))
|
||||
assert.Equal(t, AcceptHeader, req.Header.Get("Accept"))
|
||||
|
||||
var batchRequest BatchRequest
|
||||
err := json.NewDecoder(req.Body).Decode(&batchRequest)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, "download", batchRequest.Operation)
|
||||
assert.Equal(t, &Reference{Name: "refs/heads/main"}, batchRequest.Ref)
|
||||
assert.Len(t, batchRequest.Objects, 1)
|
||||
assert.Equal(t, p.Oid, batchRequest.Objects[0].Oid)
|
||||
assert.Equal(t, p.Size, batchRequest.Objects[0].Size)
|
||||
|
||||
return lfsTestRoundtripHandler(req)
|
||||
})}
|
||||
dummy := &DummyTransferAdapter{}
|
||||
|
||||
cases := []struct {
|
||||
func TestHTTPClientTransfers(t *testing.T) {
|
||||
type clientCase struct {
|
||||
endpoint string
|
||||
expectedError string
|
||||
}
|
||||
for _, operation := range []struct {
|
||||
name string
|
||||
cases []clientCase
|
||||
}{
|
||||
{
|
||||
endpoint: "https://status-not-ok.io",
|
||||
expectedError: io.ErrUnexpectedEOF.Error(),
|
||||
name: "download",
|
||||
cases: []clientCase{
|
||||
{"https://status-not-ok.io", io.ErrUnexpectedEOF.Error()},
|
||||
{"https://invalid-json-response.io", "/(invalid json|invalid character)/"},
|
||||
{"https://valid-batch-request-download.io", ""},
|
||||
{"https://response-no-objects.io", ""},
|
||||
{"https://unknown-transfer-adapter.io", "TransferAdapter not found: "},
|
||||
{"https://error-in-response-objects.io", "Object not found"},
|
||||
{"https://empty-actions-map.io", "missing action 'download'"},
|
||||
{"https://download-actions-map.io", ""},
|
||||
{"https://upload-actions-map.io", "missing action 'download'"},
|
||||
{"https://verify-actions-map.io", "missing action 'download'"},
|
||||
{"https://unknown-actions-map.io", "missing action 'download'"},
|
||||
{"https://legacy-batch-request-download.io", ""},
|
||||
},
|
||||
},
|
||||
{
|
||||
endpoint: "https://invalid-json-response.io",
|
||||
expectedError: "/(invalid json|invalid character)/",
|
||||
name: "upload",
|
||||
cases: []clientCase{
|
||||
{"https://status-not-ok.io", io.ErrUnexpectedEOF.Error()},
|
||||
{"https://invalid-json-response.io", "/(invalid json|invalid character)/"},
|
||||
{"https://valid-batch-request-upload.io", ""},
|
||||
{"https://response-no-objects.io", ""},
|
||||
{"https://unknown-transfer-adapter.io", "TransferAdapter not found: "},
|
||||
{"https://error-in-response-objects.io", "Object not found"},
|
||||
{"https://empty-actions-map.io", ""},
|
||||
{"https://download-actions-map.io", "missing action 'upload'"},
|
||||
{"https://upload-actions-map.io", ""},
|
||||
{"https://verify-actions-map.io", "missing action 'upload'"},
|
||||
{"https://unknown-actions-map.io", "missing action 'upload'"},
|
||||
},
|
||||
},
|
||||
{
|
||||
endpoint: "https://valid-batch-request-download.io",
|
||||
expectedError: "",
|
||||
},
|
||||
{
|
||||
endpoint: "https://response-no-objects.io",
|
||||
expectedError: "",
|
||||
},
|
||||
{
|
||||
endpoint: "https://unknown-transfer-adapter.io",
|
||||
expectedError: "TransferAdapter not found: ",
|
||||
},
|
||||
{
|
||||
endpoint: "https://error-in-response-objects.io",
|
||||
expectedError: "Object not found",
|
||||
},
|
||||
{
|
||||
endpoint: "https://empty-actions-map.io",
|
||||
expectedError: "missing action 'download'",
|
||||
},
|
||||
{
|
||||
endpoint: "https://download-actions-map.io",
|
||||
expectedError: "",
|
||||
},
|
||||
{
|
||||
endpoint: "https://upload-actions-map.io",
|
||||
expectedError: "missing action 'download'",
|
||||
},
|
||||
{
|
||||
endpoint: "https://verify-actions-map.io",
|
||||
expectedError: "missing action 'download'",
|
||||
},
|
||||
{
|
||||
endpoint: "https://unknown-actions-map.io",
|
||||
expectedError: "missing action 'download'",
|
||||
},
|
||||
{
|
||||
endpoint: "https://legacy-batch-request-download.io",
|
||||
expectedError: "",
|
||||
},
|
||||
}
|
||||
} {
|
||||
t.Run(operation.name, func(t *testing.T) {
|
||||
p := Pointer{Oid: "fb8f7d8435968c4f82a726a92395be4d16f2f63116caf36c8ad35c60831ab041", Size: 6}
|
||||
|
||||
defer test.MockVariableValue(&setting.LFSClient.BatchOperationConcurrency, 8)()
|
||||
for _, c := range cases {
|
||||
t.Run(c.endpoint, func(t *testing.T) {
|
||||
client := &HTTPClient{
|
||||
client: hc,
|
||||
endpoint: c.endpoint,
|
||||
transfers: map[string]TransferAdapter{
|
||||
"dummy": dummy,
|
||||
},
|
||||
}
|
||||
hc := &http.Client{Transport: RoundTripFunc(func(req *http.Request) *http.Response {
|
||||
assert.Equal(t, "POST", req.Method)
|
||||
assert.Equal(t, MediaType, req.Header.Get("Content-type"))
|
||||
assert.Equal(t, AcceptHeader, req.Header.Get("Accept"))
|
||||
|
||||
err := client.Download(t.Context(), "refs/heads/main", []Pointer{p}, func(p Pointer, content io.ReadCloser, objectError error) error {
|
||||
if objectError != nil {
|
||||
return objectError
|
||||
}
|
||||
b, err := io.ReadAll(content)
|
||||
var batchRequest BatchRequest
|
||||
err := json.NewDecoder(req.Body).Decode(&batchRequest)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []byte("dummy"), b)
|
||||
return nil
|
||||
})
|
||||
if c.expectedError != "" {
|
||||
if strings.HasPrefix(c.expectedError, "/") && strings.HasSuffix(c.expectedError, "/") {
|
||||
assert.Regexp(t, strings.Trim(c.expectedError, "/"), err.Error())
|
||||
|
||||
assert.Equal(t, operation.name, batchRequest.Operation)
|
||||
if operation.name == "download" {
|
||||
assert.Equal(t, &Reference{Name: "refs/heads/main"}, batchRequest.Ref)
|
||||
} else {
|
||||
assert.ErrorContains(t, err, c.expectedError)
|
||||
assert.Nil(t, batchRequest.Ref)
|
||||
}
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHTTPClientUpload(t *testing.T) {
|
||||
p := Pointer{Oid: "fb8f7d8435968c4f82a726a92395be4d16f2f63116caf36c8ad35c60831ab041", Size: 6}
|
||||
|
||||
hc := &http.Client{Transport: RoundTripFunc(func(req *http.Request) *http.Response {
|
||||
assert.Equal(t, "POST", req.Method)
|
||||
assert.Equal(t, MediaType, req.Header.Get("Content-type"))
|
||||
assert.Equal(t, AcceptHeader, req.Header.Get("Accept"))
|
||||
|
||||
var batchRequest BatchRequest
|
||||
err := json.NewDecoder(req.Body).Decode(&batchRequest)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, "upload", batchRequest.Operation)
|
||||
assert.Nil(t, batchRequest.Ref)
|
||||
assert.Len(t, batchRequest.Objects, 1)
|
||||
assert.Equal(t, p.Oid, batchRequest.Objects[0].Oid)
|
||||
assert.Equal(t, p.Size, batchRequest.Objects[0].Size)
|
||||
|
||||
return lfsTestRoundtripHandler(req)
|
||||
})}
|
||||
dummy := &DummyTransferAdapter{}
|
||||
|
||||
cases := []struct {
|
||||
endpoint string
|
||||
expectedError string
|
||||
}{
|
||||
{
|
||||
endpoint: "https://status-not-ok.io",
|
||||
expectedError: io.ErrUnexpectedEOF.Error(),
|
||||
},
|
||||
{
|
||||
endpoint: "https://invalid-json-response.io",
|
||||
expectedError: "/(invalid json|invalid character)/",
|
||||
},
|
||||
{
|
||||
endpoint: "https://valid-batch-request-upload.io",
|
||||
expectedError: "",
|
||||
},
|
||||
{
|
||||
endpoint: "https://response-no-objects.io",
|
||||
expectedError: "",
|
||||
},
|
||||
{
|
||||
endpoint: "https://unknown-transfer-adapter.io",
|
||||
expectedError: "TransferAdapter not found: ",
|
||||
},
|
||||
{
|
||||
endpoint: "https://error-in-response-objects.io",
|
||||
expectedError: "Object not found",
|
||||
},
|
||||
{
|
||||
endpoint: "https://empty-actions-map.io",
|
||||
expectedError: "",
|
||||
},
|
||||
{
|
||||
endpoint: "https://download-actions-map.io",
|
||||
expectedError: "missing action 'upload'",
|
||||
},
|
||||
{
|
||||
endpoint: "https://upload-actions-map.io",
|
||||
expectedError: "",
|
||||
},
|
||||
{
|
||||
endpoint: "https://verify-actions-map.io",
|
||||
expectedError: "missing action 'upload'",
|
||||
},
|
||||
{
|
||||
endpoint: "https://unknown-actions-map.io",
|
||||
expectedError: "missing action 'upload'",
|
||||
},
|
||||
}
|
||||
|
||||
defer test.MockVariableValue(&setting.LFSClient.BatchOperationConcurrency, 8)()
|
||||
for _, c := range cases {
|
||||
t.Run(c.endpoint, func(t *testing.T) {
|
||||
client := &HTTPClient{
|
||||
client: hc,
|
||||
endpoint: c.endpoint,
|
||||
transfers: map[string]TransferAdapter{
|
||||
"dummy": dummy,
|
||||
},
|
||||
}
|
||||
|
||||
err := client.Upload(t.Context(), []Pointer{p}, func(p Pointer, objectError error) (io.ReadCloser, error) {
|
||||
return io.NopCloser(new(bytes.Buffer)), objectError
|
||||
})
|
||||
if c.expectedError != "" {
|
||||
if strings.HasPrefix(c.expectedError, "/") && strings.HasSuffix(c.expectedError, "/") {
|
||||
assert.Regexp(t, strings.Trim(c.expectedError, "/"), err.Error())
|
||||
} else {
|
||||
assert.ErrorContains(t, err, c.expectedError)
|
||||
}
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, batchRequest.Objects, 1)
|
||||
assert.Equal(t, p.Oid, batchRequest.Objects[0].Oid)
|
||||
assert.Equal(t, p.Size, batchRequest.Objects[0].Size)
|
||||
|
||||
return lfsTestRoundtripHandler(req)
|
||||
})}
|
||||
dummy := &DummyTransferAdapter{}
|
||||
|
||||
defer test.MockVariableValue(&setting.LFSClient.BatchOperationConcurrency, 8)()
|
||||
for _, testCase := range operation.cases {
|
||||
t.Run(testCase.endpoint, func(t *testing.T) {
|
||||
client := &HTTPClient{
|
||||
client: hc,
|
||||
endpoint: testCase.endpoint,
|
||||
transfers: map[string]TransferAdapter{
|
||||
"dummy": dummy,
|
||||
},
|
||||
}
|
||||
|
||||
var err error
|
||||
if operation.name == "download" {
|
||||
err = client.Download(t.Context(), "refs/heads/main", []Pointer{p}, func(p Pointer, content io.ReadCloser, objectError error) error {
|
||||
if objectError != nil {
|
||||
return objectError
|
||||
}
|
||||
b, err := io.ReadAll(content)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []byte("dummy"), b)
|
||||
return nil
|
||||
})
|
||||
} else {
|
||||
err = client.Upload(t.Context(), []Pointer{p}, func(p Pointer, objectError error) (io.ReadCloser, error) {
|
||||
return io.NopCloser(new(bytes.Buffer)), objectError
|
||||
})
|
||||
}
|
||||
if testCase.expectedError != "" {
|
||||
if strings.HasPrefix(testCase.expectedError, "/") && strings.HasSuffix(testCase.expectedError, "/") {
|
||||
assert.Regexp(t, strings.Trim(testCase.expectedError, "/"), err.Error())
|
||||
} else {
|
||||
assert.ErrorContains(t, err, testCase.expectedError)
|
||||
}
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ func TestBasicTransferAdapter(t *testing.T) {
|
||||
hc := &http.Client{Transport: RoundTripFunc(roundTripHandler)}
|
||||
a := &BasicTransferAdapter{hc}
|
||||
|
||||
t.Run("Download", func(t *testing.T) {
|
||||
testOperation := func(t *testing.T, endpoint string, transfer func(*Link) error) {
|
||||
cases := []struct {
|
||||
link *Link
|
||||
expectederror string
|
||||
@@ -77,7 +77,7 @@ func TestBasicTransferAdapter(t *testing.T) {
|
||||
// case 0
|
||||
{
|
||||
link: &Link{
|
||||
Href: "https://download-request.io",
|
||||
Href: endpoint,
|
||||
Header: map[string]string{"test-header": "test-value"},
|
||||
},
|
||||
expectederror: "",
|
||||
@@ -93,46 +93,26 @@ func TestBasicTransferAdapter(t *testing.T) {
|
||||
}
|
||||
|
||||
for n, c := range cases {
|
||||
_, err := a.Download(t.Context(), c.link)
|
||||
err := transfer(c.link)
|
||||
if len(c.expectederror) > 0 {
|
||||
assert.Contains(t, err.Error(), c.expectederror, "case %d: '%s' should contain '%s'", n, err.Error(), c.expectederror)
|
||||
} else {
|
||||
assert.NoError(t, err, "case %d", n)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("Download", func(t *testing.T) {
|
||||
testOperation(t, "https://download-request.io", func(link *Link) error {
|
||||
_, err := a.Download(t.Context(), link)
|
||||
return err
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("Upload", func(t *testing.T) {
|
||||
cases := []struct {
|
||||
link *Link
|
||||
expectederror string
|
||||
}{
|
||||
// case 0
|
||||
{
|
||||
link: &Link{
|
||||
Href: "https://upload-request.io",
|
||||
Header: map[string]string{"test-header": "test-value"},
|
||||
},
|
||||
expectederror: "",
|
||||
},
|
||||
// case 1
|
||||
{
|
||||
link: &Link{
|
||||
Href: "https://error-response.io",
|
||||
Header: map[string]string{"test-header": "test-value"},
|
||||
},
|
||||
expectederror: "Object not found",
|
||||
},
|
||||
}
|
||||
|
||||
for n, c := range cases {
|
||||
err := a.Upload(t.Context(), c.link, p, strings.NewReader("dummy"))
|
||||
if len(c.expectederror) > 0 {
|
||||
assert.Contains(t, err.Error(), c.expectederror, "case %d: '%s' should contain '%s'", n, err.Error(), c.expectederror)
|
||||
} else {
|
||||
assert.NoError(t, err, "case %d", n)
|
||||
}
|
||||
}
|
||||
testOperation(t, "https://upload-request.io", func(link *Link) error {
|
||||
return a.Upload(t.Context(), link, p, strings.NewReader("dummy"))
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("Upload created", func(t *testing.T) {
|
||||
@@ -145,35 +125,8 @@ func TestBasicTransferAdapter(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Verify", func(t *testing.T) {
|
||||
cases := []struct {
|
||||
link *Link
|
||||
expectederror string
|
||||
}{
|
||||
// case 0
|
||||
{
|
||||
link: &Link{
|
||||
Href: "https://verify-request.io",
|
||||
Header: map[string]string{"test-header": "test-value"},
|
||||
},
|
||||
expectederror: "",
|
||||
},
|
||||
// case 1
|
||||
{
|
||||
link: &Link{
|
||||
Href: "https://error-response.io",
|
||||
Header: map[string]string{"test-header": "test-value"},
|
||||
},
|
||||
expectederror: "Object not found",
|
||||
},
|
||||
}
|
||||
|
||||
for n, c := range cases {
|
||||
err := a.Verify(t.Context(), c.link, p)
|
||||
if len(c.expectederror) > 0 {
|
||||
assert.Contains(t, err.Error(), c.expectederror, "case %d: '%s' should contain '%s'", n, err.Error(), c.expectederror)
|
||||
} else {
|
||||
assert.NoError(t, err, "case %d", n)
|
||||
}
|
||||
}
|
||||
testOperation(t, "https://verify-request.io", func(link *Link) error {
|
||||
return a.Verify(t.Context(), link, p)
|
||||
})
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user