github.com/adharshmk96/stk@v1.2.3/pkg/project/name_test.go (about) 1 package project_test 2 3 import ( 4 "os" 5 "testing" 6 7 "github.com/adharshmk96/stk/pkg/commands" 8 "github.com/adharshmk96/stk/pkg/project" 9 "github.com/adharshmk96/stk/testutils" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestGetPackageName(t *testing.T) { 14 gitCmd := commands.NewGitCmd() 15 goCmd := commands.NewGoCmd() 16 t.Run("gets package name from git repo", func(t *testing.T) { 17 tempDir, removeDir := testutils.CreateTempDirectory(t) 18 19 defer removeDir() 20 21 os.Chdir(tempDir) 22 23 gitCmd.Init() 24 gitCmd.AddRemote("origin", "https://github.com/user/package.git") 25 goCmd.ModInit("github.com/user/package") 26 27 packageName := project.GetPackageName([]string{"some-package"}) 28 29 assert.Equal(t, "github.com/user/package", packageName) 30 }) 31 32 t.Run("gets package name from go.mod", func(t *testing.T) { 33 tempDir, removeDir := testutils.CreateTempDirectory(t) 34 35 defer removeDir() 36 37 os.Chdir(tempDir) 38 39 gitCmd.Init() 40 goCmd.ModInit("github.com/user/package") 41 42 packageName := project.GetPackageName([]string{"some-package"}) 43 assert.Equal(t, "github.com/user/package", packageName) 44 }) 45 46 t.Run("gets package name from first arg", func(t *testing.T) { 47 packageName := project.GetPackageName([]string{"some-package-name"}) 48 assert.Equal(t, "some-package-name", packageName) 49 }) 50 51 t.Run("assign random name", func(t *testing.T) { 52 packageName := project.GetPackageName([]string{}) 53 assert.NotEmpty(t, packageName) 54 }) 55 } 56 57 func TestGetAppNameFromPkgName(t *testing.T) { 58 tc := []struct { 59 pkgName string 60 appName string 61 }{ 62 {"stk", "stk"}, 63 {"github.com/adharshmk96/stk", "stk"}, 64 {"github.com/adharshmk96/stk-cli", "stkCli"}, 65 {"github.com/adharshmk96/stk-cli-go", "stkCliGo"}, 66 {"github.com/adharshmk96/stk_cli-go", "stkCliGo"}, 67 } 68 69 for _, c := range tc { 70 t.Run(c.pkgName, func(t *testing.T) { 71 appName := project.GetAppNameFromPkgName(c.pkgName) 72 assert.Equal(t, c.appName, appName) 73 }) 74 } 75 }