github.com/abemedia/appcast@v0.4.0/integrations/yum/types_test.go (about)

     1  package yum_test
     2  
     3  import (
     4  	"encoding/xml"
     5  	"testing"
     6  
     7  	"github.com/abemedia/appcast/integrations/yum"
     8  	"github.com/google/go-cmp/cmp"
     9  	"github.com/google/go-cmp/cmp/cmpopts"
    10  )
    11  
    12  func TestRepoMDMarshalUnmarshal(t *testing.T) {
    13  	in := &yum.RepoMD{
    14  		Data: []yum.Data{
    15  			{
    16  				Type: "primary",
    17  				Location: yum.Location{
    18  					HREF: "repodata/primary.xml.gz",
    19  				},
    20  				Checksum: yum.Checksum{
    21  					Type:  "sha256",
    22  					Value: "abc",
    23  				},
    24  				OpenChecksum: yum.Checksum{
    25  					Type:  "sha256",
    26  					Value: "abc",
    27  				},
    28  				Timestamp: 1192033773,
    29  			},
    30  			{
    31  				Type: "filelists",
    32  				Location: yum.Location{
    33  					HREF: "repodata/filelists.xml.gz",
    34  				},
    35  				Checksum: yum.Checksum{
    36  					Type:  "sha256",
    37  					Value: "abc",
    38  				},
    39  				OpenChecksum: yum.Checksum{
    40  					Type:  "sha256",
    41  					Value: "abc",
    42  				},
    43  				Timestamp: 1192033773,
    44  			},
    45  			{
    46  				Type: "other",
    47  				Location: yum.Location{
    48  					HREF: "repodata/other.xml.gz",
    49  				},
    50  				Checksum: yum.Checksum{
    51  					Type:  "sha256",
    52  					Value: "abc",
    53  				},
    54  				OpenChecksum: yum.Checksum{
    55  					Type:  "sha256",
    56  					Value: "abc",
    57  				},
    58  				Timestamp: 1192033773,
    59  			},
    60  		},
    61  	}
    62  
    63  	want := `<repomd xmlns="http://linux.duke.edu/metadata/repo">
    64  	<data type="primary">
    65  		<checksum type="sha256">abc</checksum>
    66  		<open-checksum type="sha256">abc</open-checksum>
    67  		<location href="repodata/primary.xml.gz"></location>
    68  		<timestamp>1192033773</timestamp>
    69  	</data>
    70  	<data type="filelists">
    71  		<checksum type="sha256">abc</checksum>
    72  		<open-checksum type="sha256">abc</open-checksum>
    73  		<location href="repodata/filelists.xml.gz"></location>
    74  		<timestamp>1192033773</timestamp>
    75  	</data>
    76  	<data type="other">
    77  		<checksum type="sha256">abc</checksum>
    78  		<open-checksum type="sha256">abc</open-checksum>
    79  		<location href="repodata/other.xml.gz"></location>
    80  		<timestamp>1192033773</timestamp>
    81  	</data>
    82  </repomd>`
    83  
    84  	testMarshalUnmarshal(t, in, want)
    85  }
    86  
    87  func TestMetaDataMarshalUnmarshal(t *testing.T) {
    88  	in := &yum.MetaData{
    89  		Package: []yum.Package{{
    90  			Type: "rpm",
    91  			Name: "Title",
    92  			Arch: "x86_64",
    93  			Version: yum.Version{
    94  				Ver:   "1.0",
    95  				Rel:   "1",
    96  				Epoch: "0",
    97  			},
    98  			Checksum: yum.Checksum{
    99  				Type:  "sha256",
   100  				Value: "abc",
   101  			},
   102  			Summary:     "Summary",
   103  			Description: "Description",
   104  			Time: yum.Time{
   105  				File:  1192033774,
   106  				Build: 1192033773,
   107  			},
   108  			Size: yum.Size{
   109  				Package:   1000,
   110  				Installed: 2000,
   111  				Archive:   2000,
   112  			},
   113  			Location: yum.Location{
   114  				HREF: "Packages/test.x86_64.rpm",
   115  			},
   116  			Format: yum.Format{
   117  				License: "MIT",
   118  				HeaderRange: yum.HeaderRange{
   119  					Start: 5,
   120  					End:   10,
   121  				},
   122  			},
   123  		}},
   124  	}
   125  
   126  	want := `<metadata xmlns="http://linux.duke.edu/metadata/common" xmlns:rpm="http://linux.duke.edu/metadata/rpm" packages="1">
   127  	<package type="rpm">
   128  		<name>Title</name>
   129  		<arch>x86_64</arch>
   130  		<version ver="1.0" rel="1" epoch="0"></version>
   131  		<checksum type="sha256">abc</checksum>
   132  		<summary>Summary</summary>
   133  		<description>Description</description>
   134  		<time file="1192033774" build="1192033773"></time>
   135  		<size package="1000" installed="2000" archive="2000"></size>
   136  		<location href="Packages/test.x86_64.rpm"></location>
   137  		<format>
   138  			<rpm:license>MIT</rpm:license>
   139  			<rpm:header-range start="5" end="10"></rpm:header-range>
   140  		</format>
   141  	</package>
   142  </metadata>`
   143  
   144  	testMarshalUnmarshal(t, in, want)
   145  }
   146  
   147  func testMarshalUnmarshal[T any](t *testing.T, in *T, want string) {
   148  	t.Helper()
   149  
   150  	b, err := xml.MarshalIndent(in, "", "\t")
   151  	if err != nil {
   152  		t.Fatal(err)
   153  	}
   154  
   155  	if diff := cmp.Diff(want, string(b)); diff != "" {
   156  		t.Fatal(diff)
   157  	}
   158  
   159  	got := new(T)
   160  	if err = xml.Unmarshal(b, got); err != nil {
   161  		t.Fatal(err)
   162  	}
   163  
   164  	if diff := cmp.Diff(in, got, cmpopts.IgnoreTypes(xml.Name{})); diff != "" {
   165  		t.Fatal(diff)
   166  	}
   167  }