@@ -20,7 +20,6 @@ package file
20
20
import (
21
21
"bytes"
22
22
"context"
23
- "io"
24
23
"os"
25
24
"path/filepath"
26
25
"testing"
@@ -46,8 +45,9 @@ func TestStore_Dir_ExtractSymlinkRel(t *testing.T) {
46
45
t .Fatal ("error calling WriteFile(), error =" , err )
47
46
}
48
47
// create symlink to a relative path
49
- symlink := filepath .Join (dirPath , "test_symlink" )
50
- if err := os .Symlink (fileName , symlink ); err != nil {
48
+ symlinkName := "test_symlink"
49
+ symlinkPath := filepath .Join (dirPath , symlinkName )
50
+ if err := os .Symlink (fileName , symlinkPath ); err != nil {
51
51
t .Fatal ("error calling Symlink(), error =" , err )
52
52
}
53
53
@@ -63,23 +63,6 @@ func TestStore_Dir_ExtractSymlinkRel(t *testing.T) {
63
63
if err != nil {
64
64
t .Fatal ("Store.Add() error =" , err )
65
65
}
66
- val , ok := src .digestToPath .Load (desc .Digest )
67
- if ! ok {
68
- t .Fatal ("failed to find internal gz" )
69
- }
70
- tmpPath := val .(string )
71
- zrc , err := os .Open (tmpPath )
72
- if err != nil {
73
- t .Fatal ("failed to open internal gz, error =" , err )
74
- }
75
- gotgz , err := io .ReadAll (zrc )
76
- if err != nil {
77
- t .Fatal ("failed to read internal gz, error =" , err )
78
- }
79
- if err := zrc .Close (); err != nil {
80
- t .Error ("failed to close internal gz, error =" , err )
81
- }
82
-
83
66
// pack a manifest
84
67
manifestDesc , err := oras .Pack (ctx , src , "dir" , []ocispec.Descriptor {desc }, oras.PackOptions {})
85
68
if err != nil {
@@ -97,21 +80,21 @@ func TestStore_Dir_ExtractSymlinkRel(t *testing.T) {
97
80
t .Fatal ("oras.CopyGraph() error =" , err )
98
81
}
99
82
100
- // compare content
101
- rc , err := dstAbs .Fetch (ctx , desc )
83
+ // verify extracted symlink
84
+ extractedSymlink := filepath .Join (tempDir , dirName , symlinkName )
85
+ symlinkDst , err := os .Readlink (extractedSymlink )
102
86
if err != nil {
103
- t .Fatal ("Store.Fetch() error =" , err )
87
+ t .Fatal ("failed to get symlink destination, error =" , err )
104
88
}
105
- got , err := io .ReadAll (rc )
106
- if err != nil {
107
- t .Fatal ("Store.Fetch().Read() error =" , err )
89
+ if want := fileName ; symlinkDst != want {
90
+ t .Errorf ("symlink destination = %v, want %v" , symlinkDst , want )
108
91
}
109
- err = rc . Close ( )
92
+ got , err := os . ReadFile ( extractedSymlink )
110
93
if err != nil {
111
- t .Error ( "Store.Fetch().Close() error =" , err )
94
+ t .Fatal ( "failed to read symlink file, error =" , err )
112
95
}
113
- if ! bytes .Equal (got , gotgz ) {
114
- t .Errorf ("Store.Fetch() = %v, want %v" , got , gotgz )
96
+ if ! bytes .Equal (got , content ) {
97
+ t .Errorf ("symlink content = %v, want %v" , got , content )
115
98
}
116
99
117
100
// copy to another file store created from a relative root, to trigger extracting directory
@@ -128,21 +111,21 @@ func TestStore_Dir_ExtractSymlinkRel(t *testing.T) {
128
111
t .Fatal ("oras.CopyGraph() error =" , err )
129
112
}
130
113
131
- // compare content
132
- rc , err = dstRel .Fetch (ctx , desc )
114
+ // verify extracted symlink
115
+ extractedSymlink = filepath .Join (tempDir , dirName , symlinkName )
116
+ symlinkDst , err = os .Readlink (extractedSymlink )
133
117
if err != nil {
134
- t .Fatal ("Store.Fetch() error =" , err )
118
+ t .Fatal ("failed to get symlink destination, error =" , err )
135
119
}
136
- got , err = io .ReadAll (rc )
137
- if err != nil {
138
- t .Fatal ("Store.Fetch().Read() error =" , err )
120
+ if want := fileName ; symlinkDst != want {
121
+ t .Errorf ("symlink destination = %v, want %v" , symlinkDst , want )
139
122
}
140
- err = rc . Close ( )
123
+ got , err = os . ReadFile ( extractedSymlink )
141
124
if err != nil {
142
- t .Error ( "Store.Fetch().Close() error =" , err )
125
+ t .Fatal ( "failed to read symlink file, error =" , err )
143
126
}
144
- if ! bytes .Equal (got , gotgz ) {
145
- t .Errorf ("Store.Fetch() = %v, want %v" , got , gotgz )
127
+ if ! bytes .Equal (got , content ) {
128
+ t .Errorf ("symlink content = %v, want %v" , got , content )
146
129
}
147
130
}
148
131
@@ -180,23 +163,6 @@ func TestStore_Dir_ExtractSymlinkAbs(t *testing.T) {
180
163
if err != nil {
181
164
t .Fatal ("Store.Add() error =" , err )
182
165
}
183
- val , ok := src .digestToPath .Load (desc .Digest )
184
- if ! ok {
185
- t .Fatal ("failed to find internal gz" )
186
- }
187
- tmpPath := val .(string )
188
- zrc , err := os .Open (tmpPath )
189
- if err != nil {
190
- t .Fatal ("failed to open internal gz, error =" , err )
191
- }
192
- gotgz , err := io .ReadAll (zrc )
193
- if err != nil {
194
- t .Fatal ("failed to read internal gz, error =" , err )
195
- }
196
- if err := zrc .Close (); err != nil {
197
- t .Error ("failed to close internal gz, error =" , err )
198
- }
199
-
200
166
// pack a manifest
201
167
manifestDesc , err := oras .Pack (ctx , src , "dir" , []ocispec.Descriptor {desc }, oras.PackOptions {})
202
168
if err != nil {
@@ -216,21 +182,20 @@ func TestStore_Dir_ExtractSymlinkAbs(t *testing.T) {
216
182
t .Fatal ("oras.CopyGraph() error =" , err )
217
183
}
218
184
219
- // compare content
220
- rc , err := dstAbs . Fetch ( ctx , desc )
185
+ // verify extracted symlink
186
+ symlinkDst , err := os . Readlink ( symlink )
221
187
if err != nil {
222
- t .Fatal ("Store.Fetch() error =" , err )
188
+ t .Fatal ("failed to get symlink destination, error =" , err )
223
189
}
224
- got , err := io .ReadAll (rc )
225
- if err != nil {
226
- t .Fatal ("Store.Fetch().Read() error =" , err )
190
+ if want := filePath ; symlinkDst != want {
191
+ t .Errorf ("symlink destination = %v, want %v" , symlinkDst , want )
227
192
}
228
- err = rc . Close ( )
193
+ got , err := os . ReadFile ( symlink )
229
194
if err != nil {
230
- t .Error ( "Store.Fetch().Close() error =" , err )
195
+ t .Fatal ( "failed to read symlink file, error =" , err )
231
196
}
232
- if ! bytes .Equal (got , gotgz ) {
233
- t .Errorf ("Store.Fetch() = %v, want %v" , got , gotgz )
197
+ if ! bytes .Equal (got , content ) {
198
+ t .Errorf ("symlink content = %v, want %v" , got , content )
234
199
}
235
200
236
201
// remove the original testing directory and create a new store using a relative path
@@ -248,21 +213,21 @@ func TestStore_Dir_ExtractSymlinkAbs(t *testing.T) {
248
213
if err := oras .CopyGraph (ctx , src , dstRel , manifestDesc , oras .DefaultCopyGraphOptions ); err != nil {
249
214
t .Fatal ("oras.CopyGraph() error =" , err )
250
215
}
251
- // compare content
252
- rc , err = dstRel .Fetch (ctx , desc )
216
+
217
+ // verify extracted symlink
218
+ symlinkDst , err = os .Readlink (symlink )
253
219
if err != nil {
254
- t .Fatal ("Store.Fetch() error =" , err )
220
+ t .Fatal ("failed to get symlink destination, error =" , err )
255
221
}
256
- got , err = io .ReadAll (rc )
257
- if err != nil {
258
- t .Fatal ("Store.Fetch().Read() error =" , err )
222
+ if want := filePath ; symlinkDst != want {
223
+ t .Errorf ("symlink destination = %v, want %v" , symlinkDst , want )
259
224
}
260
- err = rc . Close ( )
225
+ got , err = os . ReadFile ( symlink )
261
226
if err != nil {
262
- t .Error ( "Store.Fetch().Close() error =" , err )
227
+ t .Fatal ( "failed to read symlink file, error =" , err )
263
228
}
264
- if ! bytes .Equal (got , gotgz ) {
265
- t .Errorf ("Store.Fetch() = %v, want %v" , got , gotgz )
229
+ if ! bytes .Equal (got , content ) {
230
+ t .Errorf ("symlink content = %v, want %v" , got , content )
266
231
}
267
232
268
233
// copy to another file store created from an outside root, to trigger extracting directory
0 commit comments