Skip to content

Commit 7594d45

Browse files
committed
refactor(tests): replace hardcoded values with constants in test cases
1 parent 38e74bd commit 7594d45

File tree

2 files changed

+34
-25
lines changed

2 files changed

+34
-25
lines changed

magefiles/magefile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ func createReplacements(owner, repo string) []struct{ from, to string } {
172172
}{
173173
{"bsv-blockchain/go-template", fmt.Sprintf("%s/%s", owner, repo)},
174174
{"go-template", repo},
175-
{"mrz1836", owner},
175+
{"bsv-blockchain", owner},
176176
}
177177
}
178178

magefiles/magefile_test.go

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ import (
1313
"github.com/stretchr/testify/require"
1414
)
1515

16+
// Test constants to avoid code duplication
17+
const (
18+
testOwnerArg = "owner=testowner"
19+
testRepoArg = "repo=testrepo"
20+
testTemplateRepo = "bsv-blockchain/go-template"
21+
testMainGoFile = "main.go"
22+
testImagePngFile = "image.png"
23+
)
24+
1625
func TestValidatePath(t *testing.T) {
1726
tests := []struct {
1827
name string
@@ -76,12 +85,12 @@ func TestParseArgument(t *testing.T) {
7685
}{
7786
{
7887
name: "parse owner argument",
79-
arg: "owner=testowner",
88+
arg: testOwnerArg,
8089
expectedOwner: "testowner",
8190
},
8291
{
8392
name: "parse repo argument",
84-
arg: "repo=testrepo",
93+
arg: testRepoArg,
8594
expectedRepo: "testrepo",
8695
},
8796
{
@@ -179,12 +188,12 @@ func TestIsBinaryFile(t *testing.T) {
179188
}{
180189
{
181190
name: "text file",
182-
path: "main.go",
191+
path: testMainGoFile,
183192
expected: false,
184193
},
185194
{
186195
name: "png image",
187-
path: "image.png",
196+
path: testImagePngFile,
188197
expected: true,
189198
},
190199
{
@@ -204,7 +213,7 @@ func TestIsBinaryFile(t *testing.T) {
204213
},
205214
{
206215
name: "case insensitive",
207-
path: "IMAGE.PNG",
216+
path: "IMAGE.PNG", // Keep uppercase to test case insensitivity
208217
expected: true,
209218
},
210219
{
@@ -239,21 +248,21 @@ func TestApplyReplacements(t *testing.T) {
239248
}{
240249
{
241250
name: "single replacement",
242-
content: "package bsv-blockchain/go-template",
251+
content: "package " + testTemplateRepo,
243252
replacements: []struct{ from, to string }{
244-
{"bsv-blockchain/go-template", "testowner/testrepo"},
253+
{testTemplateRepo, "testowner/testrepo"},
245254
},
246255
path: "test.go",
247256
wantContent: "package testowner/testrepo",
248257
wantModified: true,
249258
},
250259
{
251260
name: "multiple replacements",
252-
content: "bsv-blockchain/go-template and go-template by mrz1836",
261+
content: testTemplateRepo + " and go-template by bsv-blockchain",
253262
replacements: []struct{ from, to string }{
254-
{"bsv-blockchain/go-template", "testowner/testrepo"},
263+
{testTemplateRepo, "testowner/testrepo"},
255264
{"go-template", "testrepo"},
256-
{"mrz1836", "testowner"},
265+
{"bsv-blockchain", "testowner"},
257266
},
258267
path: "test.go",
259268
wantContent: "testowner/testrepo and testrepo by testowner",
@@ -297,9 +306,9 @@ func TestCreateReplacements(t *testing.T) {
297306
replacements := createReplacements(owner, repo)
298307

299308
expected := []struct{ from, to string }{
300-
{"bsv-blockchain/go-template", "testowner/testrepo"},
309+
{testTemplateRepo, "testowner/testrepo"},
301310
{"go-template", "testrepo"},
302-
{"mrz1836", "testowner"},
311+
{"bsv-blockchain", "testowner"},
303312
}
304313

305314
assert.Equal(t, expected, replacements)
@@ -314,7 +323,7 @@ func TestShouldSkipFile(t *testing.T) {
314323
}{
315324
{
316325
name: "regular go file",
317-
path: "main.go",
326+
path: testMainGoFile,
318327
isDir: false,
319328
expected: false,
320329
},
@@ -338,7 +347,7 @@ func TestShouldSkipFile(t *testing.T) {
338347
},
339348
{
340349
name: "binary file",
341-
path: "image.png",
350+
path: testImagePngFile,
342351
isDir: false,
343352
expected: true,
344353
},
@@ -366,7 +375,7 @@ func TestShouldSkipFile(t *testing.T) {
366375
}
367376
}
368377

369-
func TestProcessFile_PathValidation(t *testing.T) {
378+
func TestProcessFilePathValidation(t *testing.T) {
370379
// Test that processFile properly validates paths
371380
replacements := []struct{ from, to string }{
372381
{"test", "replacement"},
@@ -407,25 +416,25 @@ func TestParseInstallArgsWithMockedArgs(t *testing.T) {
407416
}{
408417
{
409418
name: "valid arguments",
410-
args: []string{"cmd", "function", "owner=testowner", "repo=testrepo"},
419+
args: []string{"cmd", "function", testOwnerArg, testRepoArg},
411420
expectedOwner: "testowner",
412421
expectedRepo: "testrepo",
413422
},
414423
{
415424
name: "missing owner",
416-
args: []string{"cmd", "function", "repo=testrepo"},
425+
args: []string{"cmd", "function", testRepoArg},
417426
expectError: true,
418427
errorType: errMissingOwner,
419428
},
420429
{
421430
name: "missing repo",
422-
args: []string{"cmd", "function", "owner=testowner"},
431+
args: []string{"cmd", "function", testOwnerArg},
423432
expectError: true,
424433
errorType: errMissingRepo,
425434
},
426435
{
427436
name: "with flags",
428-
args: []string{"cmd", "function", "owner=testowner", "repo=testrepo", "dryrun=true", "verbose=true", "cleanup=true"},
437+
args: []string{"cmd", "function", testOwnerArg, testRepoArg, "dryrun=true", "verbose=true", "cleanup=true"},
429438
expectedOwner: "testowner",
430439
expectedRepo: "testrepo",
431440
expectedDry: true,
@@ -481,8 +490,8 @@ func BenchmarkValidatePath(b *testing.B) {
481490

482491
func BenchmarkIsBinaryFile(b *testing.B) {
483492
files := []string{
484-
"main.go",
485-
"image.png",
493+
testMainGoFile,
494+
testImagePngFile,
486495
"document.pdf",
487496
"library.so",
488497
"README.md",
@@ -498,11 +507,11 @@ func BenchmarkIsBinaryFile(b *testing.B) {
498507
}
499508

500509
func BenchmarkApplyReplacements(b *testing.B) {
501-
content := strings.Repeat("bsv-blockchain/go-template is a template by mrz1836 for go-template projects. ", 100)
510+
content := strings.Repeat(testTemplateRepo+" is a template by bsv-blockchain for go-template projects. ", 100)
502511
replacements := []struct{ from, to string }{
503-
{"bsv-blockchain/go-template", "testowner/testrepo"},
512+
{testTemplateRepo, "testowner/testrepo"},
504513
{"go-template", "testrepo"},
505-
{"mrz1836", "testowner"},
514+
{"bsv-blockchain", "testowner"},
506515
}
507516

508517
b.ResetTimer()

0 commit comments

Comments
 (0)