@@ -19,9 +19,13 @@ package test
19
19
import (
20
20
"crypto/sha256"
21
21
"fmt"
22
+ "os"
23
+ "path/filepath"
22
24
"regexp"
23
25
"strings"
24
- "testing"
26
+
27
+ "github.com/containerd/nerdctl/mod/tigron/internal/assertive"
28
+ "github.com/containerd/nerdctl/mod/tigron/tig"
25
29
)
26
30
27
31
const (
@@ -38,26 +42,49 @@ func WithData(key, value string) Data {
38
42
return dat
39
43
}
40
44
41
- // Contains the implementation of the Data interface.
42
- func configureData (t * testing.T , seedData , parent Data ) Data {
45
+ // Contains the implementation of the Data interface
46
+ //
47
+ //nolint:varnamelen
48
+ func configureData (t tig.T , seedData , parent Data ) Data {
43
49
t .Helper ()
44
50
51
+ silentT := assertive .WithSilentSuccess (t )
52
+
45
53
if seedData == nil {
46
54
seedData = & data {}
47
55
}
48
56
49
- //nolint:forcetypeassert
57
+ var labels map [string ]string
58
+ if castData , ok := seedData .(* data ); ok {
59
+ labels = castData .labels
60
+ }
61
+
50
62
dat := & data {
51
63
// Note: implementation dependent
52
- labels : seedData .(* data ).labels ,
53
- tempDir : t .TempDir (),
64
+ labels : labels ,
54
65
testID : func (suffix ... string ) string {
55
66
suffix = append ([]string {t .Name ()}, suffix ... )
56
67
57
68
return defaultIdentifierHashing (suffix ... )
58
69
},
70
+ t : silentT ,
59
71
}
60
72
73
+ // NOTE: certain systems will use the path dirname to decide how they name resources.
74
+ // t.TempDir() will always return /tmp/TestTempDir2153252249/001, meaning these systems will all
75
+ // use the identical 001 part. This is true for compose specifically.
76
+ // Appending the base test identifier here would guarantee better unicity.
77
+ // Note though that Windows will barf if >256 characters, so, hashing...
78
+ // Small caveat: identically named tests in different modules WILL still end-up with the same last segment.
79
+ tempDir := filepath .Join (
80
+ t .TempDir (),
81
+ fmt .Sprintf ("%x" , sha256 .Sum256 ([]byte (t .Name ())))[0 :identifierSignatureLength ],
82
+ )
83
+
84
+ assertive .ErrorIsNil (silentT , os .MkdirAll (tempDir , DirPermissionsDefault ))
85
+
86
+ dat .tempDir = tempDir
87
+
61
88
if parent != nil {
62
89
dat .adopt (parent )
63
90
}
@@ -69,6 +96,7 @@ type data struct {
69
96
labels map [string ]string
70
97
testID func (suffix ... string ) string
71
98
tempDir string
99
+ t tig.T
72
100
}
73
101
74
102
func (dt * data ) Get (key string ) string {
@@ -85,6 +113,31 @@ func (dt *data) Set(key, value string) Data {
85
113
return dt
86
114
}
87
115
116
+ func (dt * data ) AssetLoad (key string ) string {
117
+ //nolint:gosec
118
+ content , err := os .ReadFile (filepath .Join (dt .tempDir , key ))
119
+
120
+ assertive .ErrorIsNil (dt .t , err )
121
+
122
+ return string (content )
123
+ }
124
+
125
+ func (dt * data ) AssetSave (key , value string ) Data {
126
+ err := os .WriteFile (
127
+ filepath .Join (dt .tempDir , key ),
128
+ []byte (value ),
129
+ FilePermissionsDefault ,
130
+ )
131
+
132
+ assertive .ErrorIsNil (dt .t , err )
133
+
134
+ return dt
135
+ }
136
+
137
+ func (dt * data ) AssetPath (key string ) string {
138
+ return filepath .Join (dt .tempDir , key )
139
+ }
140
+
88
141
func (dt * data ) Identifier (suffix ... string ) string {
89
142
return dt .testID (suffix ... )
90
143
}
0 commit comments