diff --git a/openapi2beans/pkg/generator/yaml2java.go b/openapi2beans/pkg/generator/yaml2java.go index 80caca6..a1d9afa 100644 --- a/openapi2beans/pkg/generator/yaml2java.go +++ b/openapi2beans/pkg/generator/yaml2java.go @@ -14,6 +14,7 @@ import ( ) var filepathSeparator = "/" +const JAVA_FILE_EXTENSION_LENGTH = 5 func GenerateFiles(fs files.FileSystem, projectFilePath string, apiFilePath string, packageName string, force bool) error { var fatalErr error @@ -49,11 +50,14 @@ func generateDirectories(fs files.FileSystem, storeFilepath string, force bool) exists, err := fs.DirExists(storeFilepath) if err == nil { if exists { - if !force { - err = openapi2beans_errors.NewError("The tool is unable to create files in folder %s because files in that folder already exist. Generating files is a destructive operation, removing all Java files in that folder prior to new files being created.\nIf you wish to proceed, delete the files manually, or re-run the tool using the --force option", storeFilepath) - } - if err == nil { - err = deleteAllJavaFiles(fs, storeFilepath) + var javaFilepaths []string + javaFilepaths, err = retrieveAllJavaFiles(fs, storeFilepath) + if err == nil && len(javaFilepaths) > 0 { + if !force { + err = openapi2beans_errors.NewError("The tool is unable to create files in folder %s because files in that folder already exist. Generating files is a destructive operation, removing all Java files in that folder prior to new files being created.\nIf you wish to proceed, delete the files manually, or re-run the tool using the --force option", storeFilepath) + } else { + deleteAllJavaFiles(fs, javaFilepaths) + } } } else { log.Printf("Creating output directory: %s\n", storeFilepath) @@ -83,15 +87,22 @@ func generateStoreFilepath(outputFilepath string, packageName string) string { return outputFilepath + packageFilepath } -func deleteAllJavaFiles(fs files.FileSystem, storeFilepath string) error { +func deleteAllJavaFiles(fs files.FileSystem, javaFilepaths []string) { + for _, filepath := range javaFilepaths { + fs.DeleteFile(filepath) + } +} + +func retrieveAllJavaFiles(fs files.FileSystem, storeFilepath string) ([]string, error) { + var javaFilepaths []string filepaths, err := fs.GetAllFilePaths(storeFilepath) for _, filepath := range filepaths { - relativePath := filepath[len(storeFilepath)+1:] - if len(relativePath) - 5 > 0 { - if relativePath[len(relativePath) - 5 :] == ".java" && !strings.Contains(relativePath, filepathSeparator){ - fs.DeleteFile(filepath) + filename := filepath[len(storeFilepath)+1:] + if len(filename) - JAVA_FILE_EXTENSION_LENGTH > 0 { // makes sure file name is longer than just the .java extension + if filename[len(filename) - JAVA_FILE_EXTENSION_LENGTH:] == ".java" && !strings.Contains(filename, filepathSeparator) { + javaFilepaths = append(javaFilepaths, filepath) } } } - return err + return javaFilepaths, err } diff --git a/openapi2beans/pkg/generator/yaml2java_test.go b/openapi2beans/pkg/generator/yaml2java_test.go index 9eb1eb8..fe27cc2 100644 --- a/openapi2beans/pkg/generator/yaml2java_test.go +++ b/openapi2beans/pkg/generator/yaml2java_test.go @@ -1154,6 +1154,22 @@ func TestGenerateDirectoriesCleansExistingJavaFilesFromFolder(t *testing.T) { assert.False(t, fileExists) } +func TestGenerateDirectoriesErrorsWhenForceIsFalseAndThereIsAJavaFile(t *testing.T) { + // Given... + mfs := files.NewMockFileSystem() + storeFilepath := "openapi2beans.dev/src/main/java/this/package/hallo" + randomFilepath := "openapi2beans.dev/src/main/java/this/package/hallo/smthn.java" + mfs.MkdirAll(storeFilepath) + mfs.WriteTextFile(randomFilepath, "public class emptyClass{}") + + // When... + err := generateDirectories(mfs, storeFilepath, false) + + // Then... + assert.NotNil(t, err) + assert.Contains(t, err.Error(), "The tool is unable to create files in folder openapi2beans.dev/src/main/java/this/package/hallo because files in that folder already exist. Generating files is a destructive operation, removing all Java files in that folder prior to new files being created.\nIf you wish to proceed, delete the files manually, or re-run the tool using the --force option") +} + func TestGenerateDirectoriesDoesntRemoveNonJavaFiles(t *testing.T) { // Given... mfs := files.NewMockFileSystem() @@ -1232,4 +1248,98 @@ func TestGenerateDirectoriesCleansExistingJavaFilesFromFolderButNotSubFolder(t * fileExists, err = mfs.Exists(deepRandomFilepath) assert.Nil(t, err) assert.True(t, fileExists) +} + +func TestRetrieveAllJavaFilesWithNoJavaFiles(t *testing.T) { + // Given... + mfs := files.NewMockFileSystem() + storeFilepath := "openapi2beans.dev/src/main/java/this/package/hallo" + mfs.MkdirAll(storeFilepath) + + // When... + javafilepaths, err := retrieveAllJavaFiles(mfs, storeFilepath) + + // Then... + assert.Nil(t, err) + assert.Empty(t, javafilepaths) +} + +func TestRetrieveAllJavaFilesWithOneJavaFile(t *testing.T) { + // Given... + mfs := files.NewMockFileSystem() + storeFilepath := "openapi2beans.dev/src/main/java/this/package/hallo" + randomFilepath := "openapi2beans.dev/src/main/java/this/package/hallo/smthn.java" + mfs.MkdirAll(storeFilepath) + mfs.WriteTextFile(randomFilepath, "public class emptyClass{}") + + // When... + javafilepaths, err := retrieveAllJavaFiles(mfs, storeFilepath) + + // Then... + assert.Nil(t, err) + assert.NotEmpty(t, javafilepaths) + assert.Equal(t, randomFilepath, javafilepaths[0]) +} + +func TestRetrieveAllJavaFilesWithMultipleJavaFiles(t *testing.T) { + // Given... + mfs := files.NewMockFileSystem() + storeFilepath := "openapi2beans.dev/src/main/java/this/package/hallo" + randomFilepath := "openapi2beans.dev/src/main/java/this/package/hallo/smthn.java" + randomFilepath2 := "openapi2beans.dev/src/main/java/this/package/hallo/bonsai.java" + mfs.MkdirAll(storeFilepath) + mfs.WriteTextFile(randomFilepath, "public class emptyClass{}") + mfs.WriteTextFile(randomFilepath2, "public class Bonsai{private int leaves;}") + + // When... + javafilepaths, err := retrieveAllJavaFiles(mfs, storeFilepath) + + // Then... + assert.Nil(t, err) + assert.NotEmpty(t, javafilepaths) + assert.Equal(t, 2, len(javafilepaths)) + assert.Contains(t, javafilepaths, randomFilepath) + assert.Contains(t, javafilepaths, randomFilepath2) +} + +func TestRetrieveAllJavaFilesDoesntPickUpNonJavaFile(t *testing.T) { + // Given... + mfs := files.NewMockFileSystem() + storeFilepath := "openapi2beans.dev/src/main/java/this/package/hallo" + randomFilepath := "openapi2beans.dev/src/main/java/this/package/hallo/smthn.java" + randomFilepath2 := "openapi2beans.dev/src/main/java/this/package/hallo/bonsai.txt" + mfs.MkdirAll(storeFilepath) + mfs.WriteTextFile(randomFilepath, "public class emptyClass{}") + mfs.WriteTextFile(randomFilepath2, "I am a bonsai, short n stout") + + // When... + javafilepaths, err := retrieveAllJavaFiles(mfs, storeFilepath) + + // Then... + assert.Nil(t, err) + assert.NotEmpty(t, javafilepaths) + assert.Equal(t, 1, len(javafilepaths)) + assert.Contains(t, javafilepaths, randomFilepath) + assert.NotContains(t, javafilepaths, randomFilepath2) +} + +func TestRetrieveAllJavaFilesDoesntPickUpDeepJavaFile(t *testing.T) { + // Given... + mfs := files.NewMockFileSystem() + storeFilepath := "openapi2beans.dev/src/main/java/this/package/hallo" + randomFilepath := "openapi2beans.dev/src/main/java/this/package/hallo/smthn.java" + randomFilepath2 := "openapi2beans.dev/src/main/java/this/package/hallo/tree/bonsai.java" + mfs.MkdirAll(storeFilepath) + mfs.WriteTextFile(randomFilepath, "public class emptyClass{}") + mfs.WriteTextFile(randomFilepath2, "public class Bonsai{private int branches}") + + // When... + javafilepaths, err := retrieveAllJavaFiles(mfs, storeFilepath) + + // Then... + assert.Nil(t, err) + assert.NotEmpty(t, javafilepaths) + assert.Equal(t, 1, len(javafilepaths)) + assert.Contains(t, javafilepaths, randomFilepath) + assert.NotContains(t, javafilepaths, randomFilepath2) } \ No newline at end of file