@@ -17,13 +17,13 @@ import (
1717const (
1818 testOwnerArg = "owner=testowner"
1919 testRepoArg = "repo=testrepo"
20- testTemplateRepo = "bsv-blockchain /go-template"
20+ testTemplateRepo = "testowner /go-template"
2121 testMainGoFile = "main.go"
2222 testImagePngFile = "image.png"
2323 testOwnerRepoPath = "testowner/testrepo"
2424 testGoFile = "test.go"
2525 testGoTemplate = "go-template"
26- testBsvBlockchain = "bsv-blockchain "
26+ testOrg = "testowner "
2727)
2828
2929func TestValidatePath (t * testing.T ) {
@@ -262,11 +262,11 @@ func TestApplyReplacements(t *testing.T) {
262262 },
263263 {
264264 name : "multiple replacements" ,
265- content : testTemplateRepo + " and " + testGoTemplate + " by " + testBsvBlockchain ,
265+ content : testTemplateRepo + " and " + testGoTemplate + " by " + testOrg ,
266266 replacements : []struct { from , to string }{
267267 {testTemplateRepo , testOwnerRepoPath },
268268 {testGoTemplate , "testrepo" },
269- {testBsvBlockchain , "testowner" },
269+ {testOrg , "testowner" },
270270 },
271271 path : testGoFile ,
272272 wantContent : testOwnerRepoPath + " and testrepo by testowner" ,
@@ -303,16 +303,89 @@ func TestApplyReplacements(t *testing.T) {
303303 }
304304}
305305
306+ func TestGetTemplateInfo (t * testing.T ) {
307+ // This test runs from the magefiles directory, so we need to go up one level
308+ originalDir , err := os .Getwd ()
309+ require .NoError (t , err )
310+
311+ // Change to parent directory where go.mod exists
312+ err = os .Chdir (".." )
313+ require .NoError (t , err )
314+
315+ defer func () {
316+ // Restore original directory
317+ _ = os .Chdir (originalDir )
318+ }()
319+
320+ owner , repo , err := getTemplateInfo ()
321+ require .NoError (t , err )
322+ assert .NotEmpty (t , owner , "owner should not be empty" )
323+ 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" )
326+ }
327+
328+ func TestGetTemplateInfoError (t * testing.T ) {
329+ // Test error case when go.mod doesn't exist
330+ originalDir , err := os .Getwd ()
331+ require .NoError (t , err )
332+
333+ // Create a temporary directory without go.mod
334+ tmpDir := t .TempDir ()
335+ err = os .Chdir (tmpDir )
336+ require .NoError (t , err )
337+
338+ defer func () {
339+ // Restore original directory
340+ _ = os .Chdir (originalDir )
341+ }()
342+
343+ _ , _ , err = getTemplateInfo ()
344+ require .Error (t , err )
345+ assert .Contains (t , err .Error (), "failed to open go.mod" )
346+ }
347+
306348func TestCreateReplacements (t * testing.T ) {
307349 owner := "testowner"
308350 repo := "testrepo"
309351
352+ // Change to parent directory where go.mod exists
353+ originalDir , err := os .Getwd ()
354+ require .NoError (t , err )
355+
356+ err = os .Chdir (".." )
357+ require .NoError (t , err )
358+
359+ defer func () {
360+ // Restore original directory
361+ _ = os .Chdir (originalDir )
362+ }()
363+
310364 replacements := createReplacements (owner , repo )
311365
366+ // The replacements should use the values from go.mod
312367 expected := []struct { from , to string }{
313- {testTemplateRepo , testOwnerRepoPath },
368+ {"mrz1836/go-template" , testOwnerRepoPath },
369+ {testGoTemplate , "testrepo" },
370+ {"mrz1836" , "testowner" },
371+ }
372+
373+ assert .Equal (t , expected , replacements )
374+ }
375+
376+ func TestCreateReplacementsFallback (t * testing.T ) {
377+ owner := "testowner"
378+ repo := "testrepo"
379+
380+ // Test fallback behavior when go.mod is not accessible
381+ // Stay in magefiles directory where go.mod doesn't exist
382+ replacements := createReplacements (owner , repo )
383+
384+ // Should use fallback values
385+ expected := []struct { from , to string }{
386+ {"mrz1836/go-template" , testOwnerRepoPath },
314387 {testGoTemplate , "testrepo" },
315- {testBsvBlockchain , "testowner" },
388+ {"mrz1836" , "testowner" },
316389 }
317390
318391 assert .Equal (t , expected , replacements )
@@ -511,11 +584,11 @@ func BenchmarkIsBinaryFile(b *testing.B) {
511584}
512585
513586func BenchmarkApplyReplacements (b * testing.B ) {
514- content := strings .Repeat (testTemplateRepo + " is a template by " + testBsvBlockchain + " for " + testGoTemplate + " projects. " , 100 )
587+ content := strings .Repeat (testTemplateRepo + " is a template by " + testOrg + " for " + testGoTemplate + " projects. " , 100 )
515588 replacements := []struct { from , to string }{
516589 {testTemplateRepo , testOwnerRepoPath },
517590 {testGoTemplate , "testrepo" },
518- {testBsvBlockchain , "testowner" },
591+ {testOrg , "testowner" },
519592 }
520593
521594 b .ResetTimer ()
0 commit comments