@@ -356,17 +356,97 @@ func Test_Migrate_WithHash(t *testing.T) {
356356 defer func () { require .NoError (t , os .Chdir (cwd )) }()
357357
358358 hash := "abcdef1234567890abcdef1234567890abcdef12"
359+ short := hash [:7 ]
359360 httpmock .Activate ()
360361 defer httpmock .DeactivateAndReset ()
361- commitURL := "https://api.github.com/repos/gofiber/fiber/commits/" + hash
362- httpmock .RegisterResponder (http .MethodGet , commitURL , httpmock .NewBytesResponder (200 , []byte (`{"commit":{"committer":{"date":"2020-01-02T03:04:05Z"}}}` )))
362+ commitURL := "https://api.github.com/repos/gofiber/fiber/commits/" + short
363+ httpmock .RegisterResponder (http .MethodGet , commitURL , httpmock .NewBytesResponder (200 , []byte (`{"sha":"` + hash + `"," commit":{"committer":{"date":"2020-01-02T03:04:05Z"}}}` )))
363364
364365 cmd := newMigrateCmd ()
365366 setupCmd ()
366367 defer teardownCmd ()
367- _ , err = runCobraCmd (cmd , "-t=3.0.0" , "--hash=" + hash )
368+ _ , err = runCobraCmd (cmd , "-t=3.0.0" , "--hash=" + short )
368369 require .NoError (t , err )
369370
370371 gm := readFileTB (t , filepath .Join (dir , "go.mod" ))
371372 assert .Contains (t , gm , "github.com/gofiber/fiber/v3 v3.0.1-0.20200102030405-abcdef123456" )
372373}
374+
375+ func Test_Migrate_WithHash_UpdatePseudoVersion (t * testing.T ) {
376+ dir , err := os .MkdirTemp ("" , "migrate_hash_update" )
377+ require .NoError (t , err )
378+ defer func () { require .NoError (t , os .RemoveAll (dir )) }()
379+
380+ goMod := "module example\n \n " +
381+ "go 1.20\n \n " +
382+ "require github.com/gofiber/fiber/v3\t v3.0.1-0.20200102030405-abcdef123456\n "
383+ require .NoError (t , os .WriteFile (filepath .Join (dir , "go.mod" ), []byte (goMod ), 0o600 ))
384+ require .NoError (t , os .WriteFile (filepath .Join (dir , "main.go" ), []byte ("package main" ), 0o600 ))
385+
386+ cwd , err := os .Getwd ()
387+ require .NoError (t , err )
388+ require .NoError (t , os .Chdir (dir ))
389+ defer func () { require .NoError (t , os .Chdir (cwd )) }()
390+
391+ hash := "fedcba654321fedcba654321fedcba654321fedc"
392+ short := hash [:7 ]
393+ httpmock .Activate ()
394+ defer httpmock .DeactivateAndReset ()
395+ commitURL := "https://api.github.com/repos/gofiber/fiber/commits/" + short
396+ httpmock .RegisterResponder (http .MethodGet , commitURL , httpmock .NewBytesResponder (200 , []byte (`{"sha":"` + hash + `","commit":{"committer":{"date":"2020-01-03T03:04:05Z"}}}` )))
397+
398+ cmd := newMigrateCmd ()
399+ setupCmd ()
400+ defer teardownCmd ()
401+ _ , err = runCobraCmd (cmd , "-t=3.0.0" , "--hash=" + short , "-f" )
402+ require .NoError (t , err )
403+
404+ gm := readFileTB (t , filepath .Join (dir , "go.mod" ))
405+ assert .Contains (t , gm , "v3.0.1-0.20200103030405-fedcba654321" )
406+ assert .NotContains (t , gm , "abcdef123456" )
407+ }
408+
409+ func Test_Migrate_WithHash_DowngradeWithForce (t * testing.T ) {
410+ dir , err := os .MkdirTemp ("" , "migrate_hash_downgrade" )
411+ require .NoError (t , err )
412+ defer func () { require .NoError (t , os .RemoveAll (dir )) }()
413+
414+ goMod := "module example\n \n " +
415+ "go 1.20\n \n " +
416+ "require github.com/gofiber/fiber/v3\t v3.0.1-0.20200103030405-fedcba654321\n "
417+ require .NoError (t , os .WriteFile (filepath .Join (dir , "go.mod" ), []byte (goMod ), 0o600 ))
418+ require .NoError (t , os .WriteFile (filepath .Join (dir , "main.go" ), []byte ("package main" ), 0o600 ))
419+
420+ cwd , err := os .Getwd ()
421+ require .NoError (t , err )
422+ require .NoError (t , os .Chdir (dir ))
423+ defer func () { require .NoError (t , os .Chdir (cwd )) }()
424+
425+ hash := "abcdef1234567890abcdef1234567890abcdef12"
426+ short := hash [:7 ]
427+ httpmock .Activate ()
428+ defer httpmock .DeactivateAndReset ()
429+ commitURL := "https://api.github.com/repos/gofiber/fiber/commits/" + short
430+ httpmock .RegisterResponder (http .MethodGet , commitURL , httpmock .NewBytesResponder (200 , []byte (`{"sha":"` + hash + `","commit":{"committer":{"date":"2020-01-02T03:04:05Z"}}}` )))
431+
432+ t .Run ("without force" , func (t * testing.T ) {
433+ cmd := newMigrateCmd ()
434+ setupCmd ()
435+ defer teardownCmd ()
436+ out , err := runCobraCmd (cmd , "-t=3.0.0" , "--hash=" + short )
437+ require .Error (t , err )
438+ assert .Contains (t , out , "not greater" )
439+ })
440+
441+ t .Run ("force" , func (t * testing.T ) {
442+ cmd := newMigrateCmd ()
443+ setupCmd ()
444+ defer teardownCmd ()
445+ _ , err := runCobraCmd (cmd , "-t=3.0.0" , "--hash=" + short , "-f" )
446+ require .NoError (t , err )
447+
448+ gm := readFileTB (t , filepath .Join (dir , "go.mod" ))
449+ assert .Contains (t , gm , "v3.0.1-0.20200102030405-abcdef123456" )
450+ assert .NotContains (t , gm , "fedcba654321" )
451+ })
452+ }
0 commit comments