1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package vagrant
import (
"archive/tar"
"bytes"
"compress/gzip"
"io"
"testing"
"code.gitea.io/gitea/modules/json"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
author = "gitea"
description = "Package Description"
projectURL = "https://gitea.io"
repositoryURL = "https://gitea.io/gitea/gitea"
)
func TestParseMetadataFromBox(t *testing.T) {
createArchive := func(files map[string][]byte) io.Reader {
var buf bytes.Buffer
zw := gzip.NewWriter(&buf)
tw := tar.NewWriter(zw)
for filename, content := range files {
hdr := &tar.Header{
Name: filename,
Mode: 0o600,
Size: int64(len(content)),
}
tw.WriteHeader(hdr)
tw.Write(content)
}
tw.Close()
zw.Close()
return &buf
}
t.Run("MissingInfoFile", func(t *testing.T) {
data := createArchive(map[string][]byte{"dummy.txt": {}})
metadata, err := ParseMetadataFromBox(data)
assert.NotNil(t, metadata)
require.NoError(t, err)
})
t.Run("Valid", func(t *testing.T) {
content, err := json.Marshal(map[string]string{
"description": description,
"author": author,
"website": projectURL,
"repository": repositoryURL,
})
require.NoError(t, err)
data := createArchive(map[string][]byte{"info.json": content})
metadata, err := ParseMetadataFromBox(data)
assert.NotNil(t, metadata)
require.NoError(t, err)
assert.Equal(t, author, metadata.Author)
assert.Equal(t, description, metadata.Description)
assert.Equal(t, projectURL, metadata.ProjectURL)
assert.Equal(t, repositoryURL, metadata.RepositoryURL)
})
}
func TestParseInfoFile(t *testing.T) {
t.Run("UnknownKeys", func(t *testing.T) {
content, err := json.Marshal(map[string]string{
"package": "",
"dummy": "",
})
require.NoError(t, err)
metadata, err := ParseInfoFile(bytes.NewReader(content))
assert.NotNil(t, metadata)
require.NoError(t, err)
assert.Empty(t, metadata.Author)
assert.Empty(t, metadata.Description)
assert.Empty(t, metadata.ProjectURL)
assert.Empty(t, metadata.RepositoryURL)
})
t.Run("Valid", func(t *testing.T) {
content, err := json.Marshal(map[string]string{
"description": description,
"author": author,
"website": projectURL,
"repository": repositoryURL,
})
require.NoError(t, err)
metadata, err := ParseInfoFile(bytes.NewReader(content))
assert.NotNil(t, metadata)
require.NoError(t, err)
assert.Equal(t, author, metadata.Author)
assert.Equal(t, description, metadata.Description)
assert.Equal(t, projectURL, metadata.ProjectURL)
assert.Equal(t, repositoryURL, metadata.RepositoryURL)
})
}
|