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

Commit

Permalink
program more sensitive to the filepath being stored in having java fi…
Browse files Browse the repository at this point in the history
…les (and not regular text files as the java files are the only ones going to be deleted)

Signed-off-by: Holly Hunt <[email protected]>
  • Loading branch information
Wyvinar committed Jun 7, 2024
1 parent ae270c5 commit 49ce8b8
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 11 deletions.
33 changes: 22 additions & 11 deletions openapi2beans/pkg/generator/yaml2java.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
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)
}

0 comments on commit 49ce8b8

Please sign in to comment.