diff --git a/internal/app/app.go b/internal/app/app.go index da097231..2f5836a6 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -47,17 +47,18 @@ func NewClient( } // UpdateDefaultProjectFiles should update any project specific files if any -func UpdateDefaultProjectFiles(fs afero.Fs, dirPath string, appDirName string) error { +func UpdateDefaultProjectFiles(fs afero.Fs, dirPath string, appDirName string, displayName string) error { // Files and their corresponding app name replacement functions projectFiles := []struct { filename string replacer func([]byte, string) []byte + name string }{ - {"manifest.json", regexReplaceAppNameInManifest}, - {"manifest.js", regexReplaceAppNameInManifest}, - {"manifest.ts", regexReplaceAppNameInManifest}, - {"package.json", regexReplaceAppNameInPackageJSON}, - {"pyproject.toml", regexReplaceAppNameInPyprojectToml}, + {"manifest.json", regexReplaceAppNameInManifest, displayName}, + {"manifest.js", regexReplaceAppNameInManifest, displayName}, + {"manifest.ts", regexReplaceAppNameInManifest, displayName}, + {"package.json", regexReplaceAppNameInPackageJSON, appDirName}, + {"pyproject.toml", regexReplaceAppNameInPyprojectToml, appDirName}, } for _, pf := range projectFiles { @@ -67,7 +68,7 @@ func UpdateDefaultProjectFiles(fs afero.Fs, dirPath string, appDirName string) e continue } - fileData = pf.replacer(fileData, appDirName) + fileData = pf.replacer(fileData, pf.name) if err := afero.WriteFile(fs, filePath, fileData, 0644); err != nil { return err } diff --git a/internal/app/app_test.go b/internal/app/app_test.go index c4720846..b14af730 100644 --- a/internal/app/app_test.go +++ b/internal/app/app_test.go @@ -16,24 +16,28 @@ package app import ( "fmt" + "os" "path/filepath" "testing" "github.com/slackapi/slack-cli/internal/slackdeps" "github.com/slackapi/slack-cli/test/testdata" "github.com/spf13/afero" + "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" ) func Test_App_UpdateDefaultProjectFiles(t *testing.T) { tests := map[string]struct { appDirName string + displayName string existingFiles map[string]string expectedFiles map[string]string expectedErrorType error }{ "manifest.json file exists": { - appDirName: "vibrant-butterfly-1234", + appDirName: "vibrant-butterfly-1234", + displayName: "Vibrant butterfly - 1234", existingFiles: map[string]string{ "manifest.json": string(testdata.ManifestJSON), }, @@ -43,7 +47,8 @@ func Test_App_UpdateDefaultProjectFiles(t *testing.T) { expectedErrorType: nil, }, "manifest.js file exists": { - appDirName: "vibrant-butterfly-1234", + appDirName: "vibrant-butterfly-1234", + displayName: "Vibrant butterfly - 1234", existingFiles: map[string]string{ "manifest.js": string(testdata.ManifestJS), }, @@ -53,7 +58,8 @@ func Test_App_UpdateDefaultProjectFiles(t *testing.T) { expectedErrorType: nil, }, "manifest.ts file exists": { - appDirName: "vibrant-butterfly-1234", + appDirName: "vibrant-butterfly-1234", + displayName: "Vibrant butterfly - 1234", existingFiles: map[string]string{ "manifest.ts": string(testdata.ManifestTS), }, @@ -63,7 +69,8 @@ func Test_App_UpdateDefaultProjectFiles(t *testing.T) { expectedErrorType: nil, }, "Multiple manifest files exist": { - appDirName: "vibrant-butterfly-1234", + appDirName: "vibrant-butterfly-1234", + displayName: "Vibrant butterfly - 1234", existingFiles: map[string]string{ "manifest.json": string(testdata.ManifestJSON), "manifest.ts": string(testdata.ManifestTS), @@ -75,7 +82,8 @@ func Test_App_UpdateDefaultProjectFiles(t *testing.T) { expectedErrorType: nil, }, "package.json file exists": { - appDirName: "vibrant-butterfly-1234", + appDirName: "vibrant-butterfly-1234", + displayName: "Vibrant butterfly - 1234", existingFiles: map[string]string{ "package.json": string(testdata.PackageJSON), }, @@ -85,7 +93,8 @@ func Test_App_UpdateDefaultProjectFiles(t *testing.T) { expectedErrorType: nil, }, "pyproject.toml file exists": { - appDirName: "vibrant-butterfly-1234", + appDirName: "vibrant-butterfly-1234", + displayName: "Vibrant butterfly - 1234", existingFiles: map[string]string{ "pyproject.toml": string(testdata.PyprojectTOML), }, @@ -95,7 +104,8 @@ func Test_App_UpdateDefaultProjectFiles(t *testing.T) { expectedErrorType: nil, }, "Multiple project files exist": { - appDirName: "vibrant-butterfly-1234", + appDirName: "vibrant-butterfly-1234", + displayName: "Vibrant butterfly - 1234", existingFiles: map[string]string{ "manifest.json": string(testdata.ManifestJSON), "package.json": string(testdata.PackageJSON), @@ -110,20 +120,11 @@ func Test_App_UpdateDefaultProjectFiles(t *testing.T) { }, "No manifest files exist": { appDirName: "vibrant-butterfly-1234", + displayName: "Vibrant Butterfly 1234", existingFiles: map[string]string{}, expectedFiles: map[string]string{}, expectedErrorType: nil, }, - "WriteFile error": { - appDirName: "vibrant-butterfly-1234", - existingFiles: map[string]string{ - "manifest.json": string(testdata.ManifestJSON), - }, - expectedFiles: map[string]string{ - "manifest.json": string(testdata.ManifestJSONAppName), - }, - expectedErrorType: nil, - }, } for name, tc := range tests { @@ -146,7 +147,7 @@ func Test_App_UpdateDefaultProjectFiles(t *testing.T) { } // Run the tests - err := UpdateDefaultProjectFiles(fs, projectDirPath, tc.appDirName) + err := UpdateDefaultProjectFiles(fs, projectDirPath, tc.appDirName, tc.displayName) // Assertions require.IsType(t, err, tc.expectedErrorType) @@ -161,6 +162,21 @@ func Test_App_UpdateDefaultProjectFiles(t *testing.T) { } } +func Test_App_UpdateDefaultProjectFiles_WriteFileError(t *testing.T) { + fs := slackdeps.NewFsMock() + projectDirPath := "/path/to/project-name" + + err := fs.MkdirAll(projectDirPath, 0755) + require.NoError(t, err) + err = afero.WriteFile(fs, filepath.Join(projectDirPath, "manifest.json"), testdata.ManifestJSON, 0644) + require.NoError(t, err) + + fs.On("OpenFile", mock.Anything, mock.Anything, mock.Anything).Return((*os.File)(nil), os.ErrPermission) + + err = UpdateDefaultProjectFiles(fs, projectDirPath, "vibrant-butterfly-1234", "Vibrant butterfly - 1234") + require.ErrorIs(t, err, os.ErrPermission) +} + func Test_RegexReplaceAppNameInManifest(t *testing.T) { tests := map[string]struct { src []byte @@ -169,22 +185,22 @@ func Test_RegexReplaceAppNameInManifest(t *testing.T) { }{ "manifest.json is validate": { src: testdata.ManifestJSON, - appName: "vibrant-butterfly-1234", + appName: "Vibrant butterfly - 1234", expectedSrc: testdata.ManifestJSONAppName, }, "manifest.js is validate": { src: testdata.ManifestJS, - appName: "vibrant-butterfly-1234", + appName: "Vibrant butterfly - 1234", expectedSrc: testdata.ManifestJSAppName, }, "manifest.ts is validate": { src: testdata.ManifestTS, - appName: "vibrant-butterfly-1234", + appName: "Vibrant butterfly - 1234", expectedSrc: testdata.ManifestTSAppName, }, "manifest.ts with sdk is validate": { src: testdata.ManifestSDKTS, - appName: "vibrant-butterfly-1234", + appName: "Vibrant butterfly - 1234", expectedSrc: testdata.ManifestSDKTSAppName, }, } diff --git a/internal/pkg/create/create.go b/internal/pkg/create/create.go index f10e07ec..357785c3 100644 --- a/internal/pkg/create/create.go +++ b/internal/pkg/create/create.go @@ -150,7 +150,7 @@ func Create(ctx context.Context, clients *shared.ClientFactory, createArgs Creat }() // Update default project files' app name, bot name, etc - if err := app.UpdateDefaultProjectFiles(clients.Fs, projectDirPath, appDirName); err != nil { + if err := app.UpdateDefaultProjectFiles(clients.Fs, projectDirPath, appDirName, createArgs.AppName); err != nil { return "", slackerror.Wrap(err, slackerror.ErrProjectFileUpdate) } diff --git a/test/testdata/manifest-app-name.js b/test/testdata/manifest-app-name.js index 6cd7e6c6..5f3914e3 100644 --- a/test/testdata/manifest-app-name.js +++ b/test/testdata/manifest-app-name.js @@ -3,7 +3,7 @@ export default { major_version: 2 }, display_information: { - name: 'vibrant-butterfly-1234' + name: 'Vibrant butterfly - 1234' }, // This is a comment features: { @@ -13,7 +13,7 @@ export default { messages_tab_read_only_enabled: false }, bot_user: { - display_name: 'vibrant-butterfly-1234' + display_name: 'Vibrant butterfly - 1234' } }, functions: { diff --git a/test/testdata/manifest-app-name.json b/test/testdata/manifest-app-name.json index f9b9e097..fbc49b43 100644 --- a/test/testdata/manifest-app-name.json +++ b/test/testdata/manifest-app-name.json @@ -3,7 +3,7 @@ "major_version": 2 }, "display_information": { - "name": "vibrant-butterfly-1234" + "name": "Vibrant butterfly - 1234" }, "features": { "app_home": { @@ -12,7 +12,7 @@ "messages_tab_read_only_enabled": false }, "bot_user": { - "display_name": "vibrant-butterfly-1234" + "display_name": "Vibrant butterfly - 1234" } }, "functions": { diff --git a/test/testdata/manifest-app-name.ts b/test/testdata/manifest-app-name.ts index ca28ee87..429ea553 100644 --- a/test/testdata/manifest-app-name.ts +++ b/test/testdata/manifest-app-name.ts @@ -12,7 +12,7 @@ export default { "major_version": 2 }, "display_information": { - "name": "vibrant-butterfly-1234" + "name": "Vibrant butterfly - 1234" }, // This is a comment "features": { @@ -22,7 +22,7 @@ export default { "messages_tab_read_only_enabled": false }, "bot_user": { - "display_name": "vibrant-butterfly-1234" + "display_name": "Vibrant butterfly - 1234" } }, "functions": { diff --git a/test/testdata/manifest-sdk-app-name.ts b/test/testdata/manifest-sdk-app-name.ts index 1818b2ab..a8d9584a 100644 --- a/test/testdata/manifest-sdk-app-name.ts +++ b/test/testdata/manifest-sdk-app-name.ts @@ -11,7 +11,7 @@ const obj = { }; export default Manifest({ - "name": "vibrant-butterfly-1234", + "name": "Vibrant butterfly - 1234", "description": "Reverse a string", // "runtime_environment": "slack", "runtime": "deno1.x",