KhaninArtur commented on a change in pull request #16273:
URL: https://github.com/apache/beam/pull/16273#discussion_r771476785
##########
File path: playground/backend/internal/preparators/java_preparators_test.go
##########
@@ -108,3 +110,40 @@ func TestGetJavaPreparators(t *testing.T) {
})
}
}
+
+func Test_changeJavaTestFileName(t *testing.T) {
+ codeWithPublicClass := "package org.apache.beam.sdk.transforms; \n
public class Class {\n public static void main(String[] args) {\n
System.out.println(\"Hello World!\");\n }\n}"
+ path, err := os.Getwd()
+ if err != nil {
+ panic(err)
+ }
+ lc, _ := fs_tool.NewLifeCycle(pb.Sdk_SDK_JAVA, uuid.New(),
filepath.Join(path, "temp"))
+ _ = lc.CreateFolders()
+ defer os.RemoveAll(filepath.Join(path, "temp"))
+ _, _ = lc.CreateSourceCodeFile(codeWithPublicClass)
+ validationResults := sync.Map{}
+ validationResults.Store(validators.UnitTestValidatorName, true)
+
+ type args struct {
+ args []interface{}
+ }
+ tests := []struct {
+ name string
+ args args
+ wantErr bool
+ }{
+ {
+ // Test that file where package is used changes to
import all dependencies from this package
+ name: "original file with package",
+ args:
args{[]interface{}{lc.GetAbsoluteSourceFilePath(), &validationResults}},
+ wantErr: false,
Review comment:
Let's also check that the code is changed as we expected
##########
File path: playground/backend/internal/preparators/java_preparators.go
##########
@@ -137,3 +157,38 @@ func addNewLine(newLine bool, file *os.File) error {
}
return nil
}
+
+func changeJavaTestFileName(args ...interface{}) error {
+ filePath := args[0].(string)
+ validationResults := args[1].(*sync.Map)
+ isUnitTest, ok :=
validationResults.Load(validators.UnitTestValidatorName)
+ if ok && isUnitTest.(bool) {
+ err, className := getPublicClassName(filePath)
+ if err != nil {
+ return err
+ }
+ err = renameJavaFile(filePath, className)
+ if err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
+func renameJavaFile(filePath string, className string) error {
+ currentFileName := filepath.Base(filePath)
+ newFilePath := strings.Replace(filePath, currentFileName,
fmt.Sprintf("%s%s", className, fs_tool.JavaSourceFileExtension), 1)
+ err := os.Rename(filePath, newFilePath)
+ return err
+}
+
+func getPublicClassName(filePath string) (error, string) {
+ code, err := ioutil.ReadFile(filePath)
+ if err != nil {
+ logger.Errorf("Preparer: Error during open file: %s, err:
%s\n", filePath, err.Error())
Review comment:
Let's keep the consistent naming before the refactoring we planned
```suggestion
logger.Errorf("Preparator: Error during open file: %s, err:
%s\n", filePath, err.Error())
```
##########
File path: playground/backend/internal/preparators/java_preparators_test.go
##########
@@ -108,3 +110,40 @@ func TestGetJavaPreparators(t *testing.T) {
})
}
}
+
+func Test_changeJavaTestFileName(t *testing.T) {
+ codeWithPublicClass := "package org.apache.beam.sdk.transforms; \n
public class Class {\n public static void main(String[] args) {\n
System.out.println(\"Hello World!\");\n }\n}"
+ path, err := os.Getwd()
+ if err != nil {
+ panic(err)
+ }
+ lc, _ := fs_tool.NewLifeCycle(pb.Sdk_SDK_JAVA, uuid.New(),
filepath.Join(path, "temp"))
+ _ = lc.CreateFolders()
+ defer os.RemoveAll(filepath.Join(path, "temp"))
+ _, _ = lc.CreateSourceCodeFile(codeWithPublicClass)
+ validationResults := sync.Map{}
+ validationResults.Store(validators.UnitTestValidatorName, true)
+
+ type args struct {
+ args []interface{}
+ }
+ tests := []struct {
+ name string
+ args args
+ wantErr bool
+ }{
+ {
+ // Test that file where package is used changes to
import all dependencies from this package
+ name: "original file with package",
+ args:
args{[]interface{}{lc.GetAbsoluteSourceFilePath(), &validationResults}},
+ wantErr: false,
Review comment:
And add test case when there is no `package`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]