1+ // Copyright 2026 Chainguard, Inc.
2+ //
3+ // Licensed under the Apache License, Version 2.0 (the "License");
4+ // you may not use this file except in compliance with the License.
5+ // You may obtain a copy of the License at
6+ //
7+ // http://www.apache.org/licenses/LICENSE-2.0
8+ //
9+ // Unless required by applicable law or agreed to in writing, software
10+ // distributed under the License is distributed on an "AS IS" BASIS,
11+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ // See the License for the specific language governing permissions and
13+ // limitations under the License.
14+
115package expandapk
216
317import (
@@ -12,14 +26,7 @@ import (
1226)
1327
1428func TestPkgInfo (t * testing.T ) {
15- tests := []struct {
16- name string
17- content string
18- want * types.PackageInfo
19- }{{
20- // See example at https://wiki.alpinelinux.org/wiki/Apk_spec
21- name : "example" ,
22- content : `
29+ samplePackageInfo := `
2330# Generated by abuild 3.9.0-r2
2431# using fakeroot version 1.25.3
2532# Wed Jul 6 19:09:49 UTC 2022
@@ -44,7 +51,19 @@ provides = cmd:busybox=1.35.0-r18
4451provides = cmd:sh=1.35.0-r18
4552depend = so:libc.musl-x86_64.so.1
4653datahash = 7d3351ac6c3ebaf18182efb5390061f50d077ce5ade60a15909d91278f70ada7
47- ` ,
54+ `
55+
56+ tests := []struct {
57+ name string
58+ files map [string ]string
59+ want * types.PackageInfo
60+ wantErr bool
61+ }{{
62+ // See example at https://wiki.alpinelinux.org/wiki/Apk_spec
63+ name : "example" ,
64+ files : map [string ]string {
65+ ".PKGINFO" : samplePackageInfo ,
66+ },
4867 want : & types.PackageInfo {
4968 Name : "busybox" ,
5069 Version : "1.35.0-r18" ,
@@ -65,20 +84,31 @@ datahash = 7d3351ac6c3ebaf18182efb5390061f50d077ce5ade60a15909d91278f70ada7
6584 DataHash : "7d3351ac6c3ebaf18182efb5390061f50d077ce5ade60a15909d91278f70ada7" ,
6685 Triggers : []string {"/bin /usr/bin /sbin /usr/sbin /lib/modules/*" },
6786 },
87+ }, {
88+ name : "empty" ,
89+ files : map [string ]string {
90+ ".PKGINFO" : "" ,
91+ },
92+ want : & types.PackageInfo {},
93+ }, {
94+ name : "no PKGINFO" ,
95+ wantErr : true ,
6896 }}
6997
7098 for _ , tt := range tests {
7199 t .Run (tt .name , func (t * testing.T ) {
72100 var buf bytes.Buffer
73101 tw := tar .NewWriter (& buf )
74- err := tw .WriteHeader (& tar.Header {
75- Name : ".PKGINFO" ,
76- Mode : 0o644 ,
77- Size : int64 (len ([]byte (tt .content ))),
78- })
79- require .NoError (t , err )
80- _ , err = tw .Write ([]byte (tt .content ))
81- require .NoError (t , err )
102+ for name , content := range tt .files {
103+ err := tw .WriteHeader (& tar.Header {
104+ Name : name ,
105+ Mode : 0o644 ,
106+ Size : int64 (len (content )),
107+ })
108+ require .NoError (t , err )
109+ _ , err = tw .Write ([]byte (content ))
110+ require .NoError (t , err )
111+ }
82112 require .NoError (t , tw .Close ())
83113
84114 controlFs , err := tarfs .New (bytes .NewReader (buf .Bytes ()), int64 (buf .Len ()))
@@ -87,6 +117,10 @@ datahash = 7d3351ac6c3ebaf18182efb5390061f50d077ce5ade60a15909d91278f70ada7
87117 exp := & APKExpanded {ControlFS : controlFs }
88118
89119 got , err := exp .PkgInfo ()
120+ if tt .wantErr {
121+ require .Error (t , err )
122+ return
123+ }
90124 require .NoError (t , err )
91125 require .Equal (t , tt .want , got )
92126 })
0 commit comments