Skip to content

Commit ba70710

Browse files
committed
feat(magefiles): implement fallback for template info extraction
Added getFallbackTemplateInfo function to dynamically retrieve template info from multiple locations, improving resilience in various directory structures. Updated tests to reflect these changes and ensure correctness.
1 parent a59842f commit ba70710

File tree

2 files changed

+62
-15
lines changed

2 files changed

+62
-15
lines changed

magefiles/magefile.go

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,15 +203,50 @@ func getTemplateInfo() (owner, repo string, err error) {
203203
return "", "", errModuleNotFound
204204
}
205205

206+
// getFallbackTemplateInfo returns template info by trying multiple locations
207+
// This is extracted to make it testable and avoid hardcoding values in multiple places
208+
func getFallbackTemplateInfo() (owner, repo string, err error) {
209+
// Try to extract from go.mod in current directory
210+
owner, repo, err = getTemplateInfo()
211+
if err == nil {
212+
return owner, repo, nil
213+
}
214+
215+
// If not found, try parent directory (magefiles subdirectory case)
216+
originalDir, dirErr := os.Getwd()
217+
if dirErr != nil {
218+
return "", "", fmt.Errorf("failed to get working directory: %w", dirErr)
219+
}
220+
221+
if chErr := os.Chdir(".."); chErr != nil {
222+
return "", "", fmt.Errorf("failed to change to parent directory: %w", chErr)
223+
}
224+
225+
defer func() {
226+
_ = os.Chdir(originalDir)
227+
}()
228+
229+
owner, repo, err = getTemplateInfo()
230+
if err != nil {
231+
return "", "", fmt.Errorf("failed to read template info from go.mod: %w", err)
232+
}
233+
234+
return owner, repo, nil
235+
}
236+
206237
// createReplacements creates the list of string replacements needed
207238
func createReplacements(owner, repo string) []struct{ from, to string } {
208239
// Get current template owner and repo from go.mod
209240
templateOwner, templateRepo, err := getTemplateInfo()
210241
if err != nil {
211-
// Fallback to default values if go.mod cannot be read
212-
// This allows the function to work in test environments
213-
templateOwner = "mrz1836"
214-
templateRepo = "go-template"
242+
// Fallback: try multiple locations (current dir and parent dir)
243+
// This allows the function to work when called from subdirectories
244+
templateOwner, templateRepo, err = getFallbackTemplateInfo()
245+
if err != nil {
246+
// If we still can't read go.mod, log error and continue with empty values
247+
// This shouldn't happen in normal usage, but makes the code more resilient
248+
logMessage("Warning: Could not read template info from go.mod: %v\n", err)
249+
}
215250
}
216251

217252
return []struct {

magefiles/magefile_test.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package main
44

55
import (
6+
"fmt"
67
"os"
78
"path/filepath"
89
"strings"
@@ -321,8 +322,11 @@ func TestGetTemplateInfo(t *testing.T) {
321322
require.NoError(t, err)
322323
assert.NotEmpty(t, owner, "owner should not be empty")
323324
assert.NotEmpty(t, repo, "repo should not be empty")
324-
assert.Equal(t, "mrz1836", owner, "owner should match go.mod")
325-
assert.Equal(t, "go-template", repo, "repo should match go.mod")
325+
326+
// Verify that the values extracted match the module path format
327+
// The owner and repo should be the last two parts of the module path
328+
assert.NotContains(t, owner, "/", "owner should not contain slashes")
329+
assert.NotContains(t, repo, "/", "repo should not contain slashes")
326330
}
327331

328332
func TestGetTemplateInfoError(t *testing.T) {
@@ -361,13 +365,17 @@ func TestCreateReplacements(t *testing.T) {
361365
_ = os.Chdir(originalDir)
362366
}()
363367

368+
// Get the current template info dynamically from go.mod
369+
templateOwner, templateRepo, err := getTemplateInfo()
370+
require.NoError(t, err)
371+
364372
replacements := createReplacements(owner, repo)
365373

366-
// The replacements should use the values from go.mod
374+
// The replacements should use the values from go.mod (dynamic)
367375
expected := []struct{ from, to string }{
368-
{"mrz1836/go-template", testOwnerRepoPath},
369-
{testGoTemplate, "testrepo"},
370-
{"mrz1836", "testowner"},
376+
{fmt.Sprintf("%s/%s", templateOwner, templateRepo), testOwnerRepoPath},
377+
{templateRepo, "testrepo"},
378+
{templateOwner, "testowner"},
371379
}
372380

373381
assert.Equal(t, expected, replacements)
@@ -378,14 +386,18 @@ func TestCreateReplacementsFallback(t *testing.T) {
378386
repo := "testrepo"
379387

380388
// Test fallback behavior when go.mod is not accessible
381-
// Stay in magefiles directory where go.mod doesn't exist
389+
// Stay in magefiles directory where go.mod doesn't exist in current dir
382390
replacements := createReplacements(owner, repo)
383391

384-
// Should use fallback values
392+
// Get the fallback values dynamically
393+
fallbackOwner, fallbackRepo, err := getFallbackTemplateInfo()
394+
require.NoError(t, err, "getFallbackTemplateInfo should succeed from magefiles directory")
395+
396+
// Should use fallback values (dynamic based on current implementation)
385397
expected := []struct{ from, to string }{
386-
{"mrz1836/go-template", testOwnerRepoPath},
387-
{testGoTemplate, "testrepo"},
388-
{"mrz1836", "testowner"},
398+
{fmt.Sprintf("%s/%s", fallbackOwner, fallbackRepo), testOwnerRepoPath},
399+
{fallbackRepo, "testrepo"},
400+
{fallbackOwner, "testowner"},
389401
}
390402

391403
assert.Equal(t, expected, replacements)

0 commit comments

Comments
 (0)