Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

removed function requesting user input to confirm deleting files in directory… #50

Merged
merged 3 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 19 additions & 27 deletions openapi2beans/pkg/generator/yaml2java.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package generator

import (
"fmt"
"log"
"strings"

Expand All @@ -15,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
Expand Down Expand Up @@ -50,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 = requestDeletionAffirmation(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)
Expand Down Expand Up @@ -84,33 +87,22 @@ func generateStoreFilepath(outputFilepath string, packageName string) string {
return outputFilepath + packageFilepath
}

func requestDeletionAffirmation(storeFilepath string) error {
var err error
var userSure string

fmt.Printf(`Directory already exists.
Do you wish to continue and delete already existing files in location: %s?
`, storeFilepath)
for strings.ToLower(userSure) != "y" && strings.ToLower(userSure) != "n" {
fmt.Print("(y/n): ")
fmt.Scan(&userSure)
func deleteAllJavaFiles(fs files.FileSystem, javaFilepaths []string) {
for _, filepath := range javaFilepaths {
fs.DeleteFile(filepath)
}
if userSure == "n" {
err = openapi2beans_errors.NewError("generateDirectories: permission not given to delete java files in %s", storeFilepath)
}

return err
}

func deleteAllJavaFiles(fs files.FileSystem, storeFilepath string) error {
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
}
110 changes: 110 additions & 0 deletions openapi2beans/pkg/generator/yaml2java_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
}