github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/packages/nuget/metadata_test.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package nuget
     7  
     8  import (
     9  	"archive/zip"
    10  	"bytes"
    11  	"strings"
    12  	"testing"
    13  
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  const (
    18  	id                = "System.GitBundle"
    19  	semver            = "1.0.1"
    20  	authors           = "GitBundle Authors"
    21  	projectURL        = "https://gitbundle.com"
    22  	description       = "Package Description"
    23  	releaseNotes      = "Package Release Notes"
    24  	repositoryURL     = "https://gitbundle.com/gitbundle/gitbundle"
    25  	targetFramework   = ".NETStandard2.1"
    26  	dependencyID      = "System.Text.Json"
    27  	dependencyVersion = "5.0.0"
    28  )
    29  
    30  const nuspecContent = `<?xml version="1.0" encoding="utf-8"?>
    31  <package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
    32    <metadata>
    33      <id>` + id + `</id>
    34      <version>` + semver + `</version>
    35      <authors>` + authors + `</authors>
    36      <requireLicenseAcceptance>true</requireLicenseAcceptance>
    37      <projectUrl>` + projectURL + `</projectUrl>
    38      <description>` + description + `</description>
    39      <releaseNotes>` + releaseNotes + `</releaseNotes>
    40      <repository url="` + repositoryURL + `" />
    41      <dependencies>
    42        <group targetFramework="` + targetFramework + `">
    43          <dependency id="` + dependencyID + `" version="` + dependencyVersion + `" exclude="Build,Analyzers" />
    44        </group>
    45      </dependencies>
    46    </metadata>
    47  </package>`
    48  
    49  const symbolsNuspecContent = `<?xml version="1.0" encoding="utf-8"?>
    50  <package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
    51    <metadata>
    52      <id>` + id + `</id>
    53      <version>` + semver + `</version>
    54      <description>` + description + `</description>
    55      <packageTypes>
    56        <packageType name="SymbolsPackage" />
    57      </packageTypes>
    58      <dependencies>
    59        <group targetFramework="` + targetFramework + `" />
    60      </dependencies>
    61    </metadata>
    62  </package>`
    63  
    64  func TestParsePackageMetaData(t *testing.T) {
    65  	createArchive := func(name, content string) []byte {
    66  		var buf bytes.Buffer
    67  		archive := zip.NewWriter(&buf)
    68  		w, _ := archive.Create(name)
    69  		w.Write([]byte(content))
    70  		archive.Close()
    71  		return buf.Bytes()
    72  	}
    73  
    74  	t.Run("MissingNuspecFile", func(t *testing.T) {
    75  		data := createArchive("dummy.txt", "")
    76  
    77  		np, err := ParsePackageMetaData(bytes.NewReader(data), int64(len(data)))
    78  		assert.Nil(t, np)
    79  		assert.ErrorIs(t, err, ErrMissingNuspecFile)
    80  	})
    81  
    82  	t.Run("MissingNuspecFileInRoot", func(t *testing.T) {
    83  		data := createArchive("sub/package.nuspec", "")
    84  
    85  		np, err := ParsePackageMetaData(bytes.NewReader(data), int64(len(data)))
    86  		assert.Nil(t, np)
    87  		assert.ErrorIs(t, err, ErrMissingNuspecFile)
    88  	})
    89  
    90  	t.Run("InvalidNuspecFile", func(t *testing.T) {
    91  		data := createArchive("package.nuspec", "")
    92  
    93  		np, err := ParsePackageMetaData(bytes.NewReader(data), int64(len(data)))
    94  		assert.Nil(t, np)
    95  		assert.Error(t, err)
    96  	})
    97  
    98  	t.Run("InvalidPackageId", func(t *testing.T) {
    99  		data := createArchive("package.nuspec", `<?xml version="1.0" encoding="utf-8"?>
   100  		<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
   101  		  <metadata></metadata>
   102  		</package>`)
   103  
   104  		np, err := ParsePackageMetaData(bytes.NewReader(data), int64(len(data)))
   105  		assert.Nil(t, np)
   106  		assert.ErrorIs(t, err, ErrNuspecInvalidID)
   107  	})
   108  
   109  	t.Run("InvalidPackageVersion", func(t *testing.T) {
   110  		data := createArchive("package.nuspec", `<?xml version="1.0" encoding="utf-8"?>
   111  		<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
   112  		  <metadata>
   113  			<id>`+id+`</id>
   114  		  </metadata>
   115  		</package>`)
   116  
   117  		np, err := ParsePackageMetaData(bytes.NewReader(data), int64(len(data)))
   118  		assert.Nil(t, np)
   119  		assert.ErrorIs(t, err, ErrNuspecInvalidVersion)
   120  	})
   121  
   122  	t.Run("Valid", func(t *testing.T) {
   123  		data := createArchive("package.nuspec", nuspecContent)
   124  
   125  		np, err := ParsePackageMetaData(bytes.NewReader(data), int64(len(data)))
   126  		assert.NoError(t, err)
   127  		assert.NotNil(t, np)
   128  	})
   129  }
   130  
   131  func TestParseNuspecMetaData(t *testing.T) {
   132  	t.Run("Dependency Package", func(t *testing.T) {
   133  		np, err := ParseNuspecMetaData(strings.NewReader(nuspecContent))
   134  		assert.NoError(t, err)
   135  		assert.NotNil(t, np)
   136  		assert.Equal(t, DependencyPackage, np.PackageType)
   137  
   138  		assert.Equal(t, id, np.ID)
   139  		assert.Equal(t, semver, np.Version)
   140  		assert.Equal(t, authors, np.Metadata.Authors)
   141  		assert.Equal(t, projectURL, np.Metadata.ProjectURL)
   142  		assert.Equal(t, description, np.Metadata.Description)
   143  		assert.Equal(t, releaseNotes, np.Metadata.ReleaseNotes)
   144  		assert.Equal(t, repositoryURL, np.Metadata.RepositoryURL)
   145  		assert.Len(t, np.Metadata.Dependencies, 1)
   146  		assert.Contains(t, np.Metadata.Dependencies, targetFramework)
   147  		deps := np.Metadata.Dependencies[targetFramework]
   148  		assert.Len(t, deps, 1)
   149  		assert.Equal(t, dependencyID, deps[0].ID)
   150  		assert.Equal(t, dependencyVersion, deps[0].Version)
   151  	})
   152  
   153  	t.Run("Symbols Package", func(t *testing.T) {
   154  		np, err := ParseNuspecMetaData(strings.NewReader(symbolsNuspecContent))
   155  		assert.NoError(t, err)
   156  		assert.NotNil(t, np)
   157  		assert.Equal(t, SymbolsPackage, np.PackageType)
   158  
   159  		assert.Equal(t, id, np.ID)
   160  		assert.Equal(t, semver, np.Version)
   161  		assert.Equal(t, description, np.Metadata.Description)
   162  		assert.Empty(t, np.Metadata.Dependencies)
   163  	})
   164  }