mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-10 04:05:42 +01:00
4de909747b
Some checks are pending
/ release (push) Waiting to run
testing / test-remote-cacher (map[image:redis:7.2 port:6379]) (push) Blocked by required conditions
testing / test-remote-cacher (map[image:registry.redict.io/redict:7.3.0-scratch port:6379]) (push) Blocked by required conditions
testing / test-mysql (push) Blocked by required conditions
testing / test-pgsql (push) Blocked by required conditions
testing / backend-checks (push) Waiting to run
testing / frontend-checks (push) Waiting to run
testing / test-unit (push) Blocked by required conditions
testing / test-remote-cacher (map[image:docker.io/valkey/valkey:7.2.5-alpine3.19 port:6379]) (push) Blocked by required conditions
testing / test-remote-cacher (map[image:ghcr.io/microsoft/garnet-alpine:1.0.14 port:6379]) (push) Blocked by required conditions
testing / test-sqlite (push) Blocked by required conditions
testing / security-check (push) Blocked by required conditions
go-require lint is ignored for now Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4535 Reviewed-by: Gusted <gusted@noreply.codeberg.org> Co-authored-by: TheFox0x7 <thefox0x7@gmail.com> Co-committed-by: TheFox0x7 <thefox0x7@gmail.com>
90 lines
2.6 KiB
Go
90 lines
2.6 KiB
Go
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package maven
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"golang.org/x/text/encoding/charmap"
|
|
)
|
|
|
|
const (
|
|
groupID = "org.gitea"
|
|
artifactID = "my-project"
|
|
version = "1.0.1"
|
|
name = "My Gitea Project"
|
|
description = "Package Description"
|
|
projectURL = "https://gitea.io"
|
|
license = "MIT"
|
|
dependencyGroupID = "org.gitea.core"
|
|
dependencyArtifactID = "git"
|
|
dependencyVersion = "5.0.0"
|
|
)
|
|
|
|
const pomContent = `<?xml version="1.0"?>
|
|
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
<groupId>` + groupID + `</groupId>
|
|
<artifactId>` + artifactID + `</artifactId>
|
|
<version>` + version + `</version>
|
|
<name>` + name + `</name>
|
|
<description>` + description + `</description>
|
|
<url>` + projectURL + `</url>
|
|
<licenses>
|
|
<license>
|
|
<name>` + license + `</name>
|
|
</license>
|
|
</licenses>
|
|
<dependencies>
|
|
<dependency>
|
|
<groupId>` + dependencyGroupID + `</groupId>
|
|
<artifactId>` + dependencyArtifactID + `</artifactId>
|
|
<version>` + dependencyVersion + `</version>
|
|
</dependency>
|
|
</dependencies>
|
|
</project>`
|
|
|
|
func TestParsePackageMetaData(t *testing.T) {
|
|
t.Run("InvalidFile", func(t *testing.T) {
|
|
m, err := ParsePackageMetaData(strings.NewReader(""))
|
|
assert.Nil(t, m)
|
|
require.Error(t, err)
|
|
})
|
|
|
|
t.Run("Valid", func(t *testing.T) {
|
|
m, err := ParsePackageMetaData(strings.NewReader(pomContent))
|
|
require.NoError(t, err)
|
|
assert.NotNil(t, m)
|
|
|
|
assert.Equal(t, groupID, m.GroupID)
|
|
assert.Equal(t, artifactID, m.ArtifactID)
|
|
assert.Equal(t, name, m.Name)
|
|
assert.Equal(t, description, m.Description)
|
|
assert.Equal(t, projectURL, m.ProjectURL)
|
|
assert.Len(t, m.Licenses, 1)
|
|
assert.Equal(t, license, m.Licenses[0])
|
|
assert.Len(t, m.Dependencies, 1)
|
|
assert.Equal(t, dependencyGroupID, m.Dependencies[0].GroupID)
|
|
assert.Equal(t, dependencyArtifactID, m.Dependencies[0].ArtifactID)
|
|
assert.Equal(t, dependencyVersion, m.Dependencies[0].Version)
|
|
})
|
|
|
|
t.Run("Encoding", func(t *testing.T) {
|
|
// UTF-8 is default but the metadata could be encoded differently
|
|
pomContent8859_1, err := charmap.ISO8859_1.NewEncoder().String(
|
|
strings.ReplaceAll(
|
|
pomContent,
|
|
`<?xml version="1.0"?>`,
|
|
`<?xml version="1.0" encoding="ISO-8859-1"?>`,
|
|
),
|
|
)
|
|
require.NoError(t, err)
|
|
|
|
m, err := ParsePackageMetaData(strings.NewReader(pomContent8859_1))
|
|
require.NoError(t, err)
|
|
assert.NotNil(t, m)
|
|
})
|
|
}
|