2022-03-30 10:42:47 +02:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-03-30 10:42:47 +02:00
|
|
|
|
|
|
|
package maven
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 21:42:06 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-07-14 11:39:15 +02:00
|
|
|
"golang.org/x/text/encoding/charmap"
|
2022-03-30 10:42:47 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
2024-07-30 21:42:06 +02:00
|
|
|
require.Error(t, err)
|
2022-03-30 10:42:47 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Valid", func(t *testing.T) {
|
|
|
|
m, err := ParsePackageMetaData(strings.NewReader(pomContent))
|
2024-07-30 21:42:06 +02:00
|
|
|
require.NoError(t, err)
|
2022-03-30 10:42:47 +02:00
|
|
|
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)
|
|
|
|
})
|
2023-07-14 11:39:15 +02:00
|
|
|
|
|
|
|
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"?>`,
|
|
|
|
),
|
|
|
|
)
|
2024-07-30 21:42:06 +02:00
|
|
|
require.NoError(t, err)
|
2023-07-14 11:39:15 +02:00
|
|
|
|
|
|
|
m, err := ParsePackageMetaData(strings.NewReader(pomContent8859_1))
|
2024-07-30 21:42:06 +02:00
|
|
|
require.NoError(t, err)
|
2023-07-14 11:39:15 +02:00
|
|
|
assert.NotNil(t, m)
|
|
|
|
})
|
2022-03-30 10:42:47 +02:00
|
|
|
}
|