@@ -19,9 +19,13 @@ package test
1919import (
2020 "crypto/sha256"
2121 "fmt"
22+ "os"
23+ "path/filepath"
2224 "regexp"
2325 "strings"
24- "testing"
26+
27+ "github.com/containerd/nerdctl/mod/tigron/internal/assertive"
28+ "github.com/containerd/nerdctl/mod/tigron/tig"
2529)
2630
2731const (
@@ -38,26 +42,49 @@ func WithData(key, value string) Data {
3842 return dat
3943}
4044
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 {
4349 t .Helper ()
4450
51+ silentT := assertive .WithSilentSuccess (t )
52+
4553 if seedData == nil {
4654 seedData = & data {}
4755 }
4856
49- //nolint:forcetypeassert
57+ var labels map [string ]string
58+ if castData , ok := seedData .(* data ); ok {
59+ labels = castData .labels
60+ }
61+
5062 dat := & data {
5163 // Note: implementation dependent
52- labels : seedData .(* data ).labels ,
53- tempDir : t .TempDir (),
64+ labels : labels ,
5465 testID : func (suffix ... string ) string {
5566 suffix = append ([]string {t .Name ()}, suffix ... )
5667
5768 return defaultIdentifierHashing (suffix ... )
5869 },
70+ t : silentT ,
5971 }
6072
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+
6188 if parent != nil {
6289 dat .adopt (parent )
6390 }
@@ -69,6 +96,7 @@ type data struct {
6996 labels map [string ]string
7097 testID func (suffix ... string ) string
7198 tempDir string
99+ t tig.T
72100}
73101
74102func (dt * data ) Get (key string ) string {
@@ -85,6 +113,31 @@ func (dt *data) Set(key, value string) Data {
85113 return dt
86114}
87115
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+
88141func (dt * data ) Identifier (suffix ... string ) string {
89142 return dt .testID (suffix ... )
90143}
0 commit comments