Skip to content

Commit b019d90

Browse files
committed
Move VSIX helpers to testutil
This is an attempt to reduce clutter in the storage tests.
1 parent c189bd4 commit b019d90

File tree

2 files changed

+52
-53
lines changed

2 files changed

+52
-53
lines changed

storage/storage_test.go

Lines changed: 11 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package storage_test
22

33
import (
4-
"archive/zip"
5-
"bytes"
64
"context"
7-
"encoding/xml"
85
"errors"
96
"fmt"
107
"io"
@@ -172,45 +169,6 @@ func TestWalkExtensions(t *testing.T) {
172169
})
173170
}
174171

175-
type file struct {
176-
name string
177-
body []byte
178-
}
179-
180-
// createVSIX returns the bytes for a VSIX file containing the provided raw
181-
// manifest bytes (if not nil) and an icon.
182-
func createVSIX(t *testing.T, manifestBytes []byte) []byte {
183-
files := []file{{"icon.png", []byte("fake icon")}}
184-
if manifestBytes != nil {
185-
files = append(files, file{"extension.vsixmanifest", manifestBytes})
186-
}
187-
buf := bytes.NewBuffer(nil)
188-
zw := zip.NewWriter(buf)
189-
for _, file := range files {
190-
fw, err := zw.Create(file.name)
191-
require.NoError(t, err)
192-
_, err = fw.Write([]byte(file.body))
193-
require.NoError(t, err)
194-
}
195-
err := zw.Close()
196-
require.NoError(t, err)
197-
return buf.Bytes()
198-
}
199-
200-
// createVSIXFromManifest returns the bytes for a VSIX file containing the
201-
// provided manifest and an icon.
202-
func createVSIXFromManifest(t *testing.T, manifest *storage.VSIXManifest) []byte {
203-
manifestBytes, err := xml.Marshal(manifest)
204-
require.NoError(t, err)
205-
return createVSIX(t, manifestBytes)
206-
}
207-
208-
// createVSIXFromExtension returns the bytes for a VSIX file containing the
209-
// manifest for the provided test extension and an icon.
210-
func createVSIXFromExtension(t *testing.T, ext testutil.Extension) []byte {
211-
return createVSIXFromManifest(t, testutil.ConvertExtensionToManifest(ext, ext.LatestVersion))
212-
}
213-
214172
func TestReadVSIX(t *testing.T) {
215173
t.Parallel()
216174

@@ -257,7 +215,7 @@ func TestReadVSIX(t *testing.T) {
257215
handler := test.handler
258216
if handler == nil {
259217
handler = func(rw http.ResponseWriter, r *http.Request) {
260-
vsix := createVSIXFromExtension(t, test.expected)
218+
vsix := testutil.CreateVSIXFromExtension(t, test.expected)
261219
_, err := rw.Write(vsix)
262220
require.NoError(t, err)
263221
}
@@ -271,7 +229,7 @@ func TestReadVSIX(t *testing.T) {
271229
require.Error(t, err)
272230
require.Regexp(t, test.error, err.Error())
273231
} else {
274-
require.Equal(t, createVSIXFromExtension(t, test.expected), got)
232+
require.Equal(t, testutil.CreateVSIXFromExtension(t, test.expected), got)
275233
}
276234
})
277235
}
@@ -299,7 +257,7 @@ func TestReadVSIX(t *testing.T) {
299257
name: "OK",
300258
expected: testutil.Extensions[0],
301259
source: func(t *testing.T, extdir string) (string, error) {
302-
vsix := createVSIXFromExtension(t, testutil.Extensions[0])
260+
vsix := testutil.CreateVSIXFromExtension(t, testutil.Extensions[0])
303261
vsixPath := filepath.Join(extdir, "extension.vsix")
304262
return vsixPath, os.WriteFile(vsixPath, vsix, 0o644)
305263
},
@@ -341,7 +299,7 @@ func TestReadVSIX(t *testing.T) {
341299
require.Error(t, err)
342300
require.True(t, errors.Is(err, test.error))
343301
} else {
344-
require.Equal(t, createVSIXFromExtension(t, test.expected), got)
302+
require.Equal(t, testutil.CreateVSIXFromExtension(t, test.expected), got)
345303
}
346304
})
347305
}
@@ -379,27 +337,27 @@ func TestReadVSIXManifest(t *testing.T) {
379337
{
380338
name: "MissingManifest",
381339
error: "not found",
382-
vsix: createVSIX(t, nil),
340+
vsix: testutil.CreateVSIX(t, nil),
383341
},
384342
{
385343
name: "EmptyManifest",
386344
error: "EOF",
387-
vsix: createVSIX(t, []byte("")),
345+
vsix: testutil.CreateVSIX(t, []byte("")),
388346
},
389347
{
390348
name: "TextFileManifest",
391349
error: "EOF",
392-
vsix: createVSIX(t, []byte("just some random text")),
350+
vsix: testutil.CreateVSIX(t, []byte("just some random text")),
393351
},
394352
{
395353
name: "ManifestSyntaxError",
396354
error: "XML syntax error",
397-
vsix: createVSIX(t, []byte("<PackageManifest/PackageManifest>")),
355+
vsix: testutil.CreateVSIX(t, []byte("<PackageManifest/PackageManifest>")),
398356
},
399357
{
400358
name: "ManifestMissingPublisher",
401359
error: "publisher",
402-
vsix: createVSIXFromManifest(t, &storage.VSIXManifest{}),
360+
vsix: testutil.CreateVSIXFromManifest(t, &storage.VSIXManifest{}),
403361
},
404362
{
405363
name: "ManifestMissingID",
@@ -432,7 +390,7 @@ func TestReadVSIXManifest(t *testing.T) {
432390
t.Parallel()
433391
vsix := test.vsix
434392
if vsix == nil {
435-
vsix = createVSIXFromManifest(t, test.manifest)
393+
vsix = testutil.CreateVSIXFromManifest(t, test.manifest)
436394
}
437395
manifest, err := storage.ReadVSIXManifest(vsix)
438396
if test.error != "" {
@@ -529,7 +487,7 @@ func TestAddExtension(t *testing.T) {
529487
vsix := test.vsix
530488
if vsix == nil {
531489
manifest = testutil.ConvertExtensionToManifest(test.extension, test.extension.LatestVersion)
532-
vsix = createVSIXFromManifest(t, manifest)
490+
vsix = testutil.CreateVSIXFromManifest(t, manifest)
533491
}
534492
location, err := s.AddExtension(context.Background(), manifest, vsix)
535493
if test.error != "" {

testutil/extensions.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package testutil
22

33
import (
4+
"archive/zip"
5+
"bytes"
46
"encoding/xml"
57
"fmt"
68
"os"
@@ -144,3 +146,42 @@ func AddExtension(t *testing.T, ext Extension, extdir, version string) *storage.
144146

145147
return manifest
146148
}
149+
150+
type file struct {
151+
name string
152+
body []byte
153+
}
154+
155+
// createVSIX returns the bytes for a VSIX file containing the provided raw
156+
// manifest bytes (if not nil) and an icon.
157+
func CreateVSIX(t *testing.T, manifestBytes []byte) []byte {
158+
files := []file{{"icon.png", []byte("fake icon")}}
159+
if manifestBytes != nil {
160+
files = append(files, file{"extension.vsixmanifest", manifestBytes})
161+
}
162+
buf := bytes.NewBuffer(nil)
163+
zw := zip.NewWriter(buf)
164+
for _, file := range files {
165+
fw, err := zw.Create(file.name)
166+
require.NoError(t, err)
167+
_, err = fw.Write([]byte(file.body))
168+
require.NoError(t, err)
169+
}
170+
err := zw.Close()
171+
require.NoError(t, err)
172+
return buf.Bytes()
173+
}
174+
175+
// CreateVSIXFromManifest returns the bytes for a VSIX file containing the
176+
// provided manifest and an icon.
177+
func CreateVSIXFromManifest(t *testing.T, manifest *storage.VSIXManifest) []byte {
178+
manifestBytes, err := xml.Marshal(manifest)
179+
require.NoError(t, err)
180+
return CreateVSIX(t, manifestBytes)
181+
}
182+
183+
// CreateVSIXFromExtension returns the bytes for a VSIX file containing the
184+
// manifest for the provided test extension and an icon.
185+
func CreateVSIXFromExtension(t *testing.T, ext Extension) []byte {
186+
return CreateVSIXFromManifest(t, ConvertExtensionToManifest(ext, ext.LatestVersion))
187+
}

0 commit comments

Comments
 (0)