77 "fmt"
88 "io"
99 "os"
10+ "path/filepath"
11+ "strings"
1012 "testing"
1113
1214 "github.com/rs/zerolog"
@@ -117,7 +119,7 @@ func TestGetFilename(t *testing.T) {
117119 // Call GetAppPackTomlFilename and check the default value
118120
119121 filename := GetAppPackTomlFilename ()
120- if filename != "apppack.toml" {
122+ if filename != DefaultAppPackTomlFilename {
121123 t .Errorf ("expected apppack.toml, got %s" , filename )
122124 }
123125
@@ -130,6 +132,91 @@ func TestGetFilename(t *testing.T) {
130132 }
131133}
132134
135+ func TestCopyAppPackTomlToDefault (t * testing.T ) {
136+ tests := []struct {
137+ name string
138+ envValue string
139+ setupFiles map [string ]string
140+ expectCopy bool
141+ expectError bool
142+ errorContains string
143+ }{
144+ {
145+ name : "no copy needed when using default location" ,
146+ envValue : "" ,
147+ expectCopy : false ,
148+ },
149+ {
150+ name : "copies from custom location" ,
151+ envValue : "config/custom.toml" ,
152+ setupFiles : map [string ]string {
153+ "config/custom.toml" : "[build]\n test = true\n " ,
154+ },
155+ expectCopy : true ,
156+ },
157+ {
158+ name : "error when custom file doesn't exist" ,
159+ envValue : "nonexistent.toml" ,
160+ expectCopy : false ,
161+ expectError : true ,
162+ errorContains : "failed to copy nonexistent.toml" ,
163+ },
164+ }
165+
166+ for _ , tt := range tests {
167+ t .Run (tt .name , func (t * testing.T ) {
168+ // Set up environment
169+ if tt .envValue != "" {
170+ os .Setenv ("APPPACK_TOML" , tt .envValue )
171+ defer os .Unsetenv ("APPPACK_TOML" )
172+ }
173+
174+ // Create temp directory for test
175+ tempDir := t .TempDir ()
176+ originalDir , _ := os .Getwd ()
177+ os .Chdir (tempDir )
178+ defer os .Chdir (originalDir )
179+
180+ // Set up test files
181+ for path , content := range tt .setupFiles {
182+ dir := filepath .Dir (path )
183+ if dir != "." {
184+ os .MkdirAll (dir , 0o755 )
185+ }
186+ os .WriteFile (path , []byte (content ), 0o644 )
187+ }
188+
189+ // Run the function
190+ err := CopyAppPackTomlToDefault ()
191+
192+ // Check error
193+ if tt .expectError {
194+ if err == nil {
195+ t .Errorf ("expected error, got nil" )
196+ } else if tt .errorContains != "" && ! strings .Contains (err .Error (), tt .errorContains ) {
197+ t .Errorf ("expected error to contain %q, got %q" , tt .errorContains , err .Error ())
198+ }
199+ } else if err != nil {
200+ t .Errorf ("unexpected error: %v" , err )
201+ }
202+
203+ // Check if file was copied
204+ if tt .expectCopy {
205+ if _ , err := os .Stat (DefaultAppPackTomlFilename ); os .IsNotExist (err ) {
206+ t .Errorf ("expected %s to exist after copy" , DefaultAppPackTomlFilename )
207+ } else {
208+ // Verify content matches
209+ expected := tt .setupFiles [tt .envValue ]
210+ actual , _ := os .ReadFile (DefaultAppPackTomlFilename )
211+ if string (actual ) != expected {
212+ t .Errorf ("copied content mismatch: got %q, want %q" , actual , expected )
213+ }
214+ }
215+ }
216+ })
217+ }
218+ }
219+
133220func dummyTarBuffer () (* io.Reader , error ) {
134221 var buf bytes.Buffer
135222 tw := tar .NewWriter (& buf )
0 commit comments