github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/packages/rubygems/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 rubygems
     7  
     8  import (
     9  	"archive/tar"
    10  	"bytes"
    11  	"encoding/base64"
    12  	"io"
    13  	"testing"
    14  
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func TestParsePackageMetaData(t *testing.T) {
    19  	createArchive := func(filename string, content []byte) io.Reader {
    20  		var buf bytes.Buffer
    21  		tw := tar.NewWriter(&buf)
    22  		hdr := &tar.Header{
    23  			Name: filename,
    24  			Mode: 0o600,
    25  			Size: int64(len(content)),
    26  		}
    27  		tw.WriteHeader(hdr)
    28  		tw.Write(content)
    29  		tw.Close()
    30  		return &buf
    31  	}
    32  
    33  	t.Run("MissingMetadataFile", func(t *testing.T) {
    34  		data := createArchive("dummy.txt", []byte{0})
    35  
    36  		rp, err := ParsePackageMetaData(data)
    37  		assert.ErrorIs(t, err, ErrMissingMetadataFile)
    38  		assert.Nil(t, rp)
    39  	})
    40  
    41  	t.Run("Valid", func(t *testing.T) {
    42  		content, _ := base64.StdEncoding.DecodeString("H4sICHC/I2EEAG1ldGFkYXRhAAEeAOH/bmFtZTogZwp2ZXJzaW9uOgogIHZlcnNpb246IDEKWw35Tx4AAAA=")
    43  		data := createArchive("metadata.gz", content)
    44  
    45  		rp, err := ParsePackageMetaData(data)
    46  		assert.NoError(t, err)
    47  		assert.NotNil(t, rp)
    48  	})
    49  }
    50  
    51  func TestParseMetadataFile(t *testing.T) {
    52  	content, _ := base64.StdEncoding.DecodeString(`H4sIAMe7I2ECA9VVTW/UMBC9+1eYXvaUbJpSQBZUHJAqDlwK4kCFIseZzZrGH9iTqisEv52Js9nd
    53  0KqggiqRXWnX45n3ZuZ5nCzL+JPQ15ulq7+AQnEORoj3HpReaSVRO8usNCB4qxEku4YQySbuCPo4
    54  bjHOd07HeZGfMt9JXLlgBB9imOxx7UIULOPnCZMMLsDXXgeiYbW2jQ6C0y9TELBSa6kJ6/IzaySS
    55  R1mUx1nxIitPeFGI9M2L6eGfWAMebANWaUgktzN9M3lsKNmxutBb1AYyCibbNhsDFu+q9GK/Tc4z
    56  d2IcLBl9js5eHaXFsLyvXeNz0LQyL/YoLx8EsiCMBZlx46k6sS2PDD5AgA5kJPNKdhH2elWzOv7n
    57  uv9Q9Aau/6ngP84elvNpXh5oRVlB5/yW7BH0+qu0G4gqaI/JdEHBFBS5l+pKtsARIjIwUnfj8Le0
    58  +TrdJLl2DG5A9SjrjgZ1mG+4QbAD+G4ZZBUap6qVnnzGf6Rwp+vliBRqtnYGPBEKvkb0USyXE8mS
    59  dVoR6hj07u0HZgAl3SRS8G/fmXcRK20jyq6rDMSYQFgidamqkXbbuspLXE/0k7GphtKqe67GuRC/
    60  yjAbmt9LsOMp8xMamFkSQ38fP5EFjdz8LA4do2C69VvqWXAJgrPbKZb58/xZXrKoW6ttW13Bhvzi
    61  4ftn7/yUxd4YGcglvTmmY8aGY3ZwRn4CqcWcidUGAAA=`)
    62  	rp, err := parseMetadataFile(bytes.NewReader(content))
    63  	assert.NoError(t, err)
    64  	assert.NotNil(t, rp)
    65  
    66  	assert.Equal(t, "gitbundle", rp.Name)
    67  	assert.Equal(t, "1.0.5", rp.Version)
    68  	assert.Equal(t, "ruby", rp.Metadata.Platform)
    69  	assert.Equal(t, "GitBundle package", rp.Metadata.Summary)
    70  	assert.Equal(t, "RubyGems package test", rp.Metadata.Description)
    71  	assert.Equal(t, []string{"GitBundle"}, rp.Metadata.Authors)
    72  	assert.Equal(t, "https://gitbundle.com/", rp.Metadata.ProjectURL)
    73  	assert.Equal(t, []string{"MIT"}, rp.Metadata.Licenses)
    74  	assert.Empty(t, rp.Metadata.RequiredRubygemsVersion)
    75  	assert.Len(t, rp.Metadata.RequiredRubyVersion, 1)
    76  	assert.Equal(t, ">=", rp.Metadata.RequiredRubyVersion[0].Restriction)
    77  	assert.Equal(t, "2.3.0", rp.Metadata.RequiredRubyVersion[0].Version)
    78  	assert.Len(t, rp.Metadata.RuntimeDependencies, 1)
    79  	assert.Equal(t, "runtime-dep", rp.Metadata.RuntimeDependencies[0].Name)
    80  	assert.Len(t, rp.Metadata.RuntimeDependencies[0].Version, 2)
    81  	assert.Equal(t, ">=", rp.Metadata.RuntimeDependencies[0].Version[0].Restriction)
    82  	assert.Equal(t, "1.2.0", rp.Metadata.RuntimeDependencies[0].Version[0].Version)
    83  	assert.Equal(t, "<", rp.Metadata.RuntimeDependencies[0].Version[1].Restriction)
    84  	assert.Equal(t, "2.0", rp.Metadata.RuntimeDependencies[0].Version[1].Version)
    85  	assert.Len(t, rp.Metadata.DevelopmentDependencies, 1)
    86  	assert.Equal(t, "dev-dep", rp.Metadata.DevelopmentDependencies[0].Name)
    87  	assert.Len(t, rp.Metadata.DevelopmentDependencies[0].Version, 1)
    88  	assert.Equal(t, "~>", rp.Metadata.DevelopmentDependencies[0].Version[0].Restriction)
    89  	assert.Equal(t, "5.2", rp.Metadata.DevelopmentDependencies[0].Version[0].Version)
    90  }