This is an automated email from the ASF dual-hosted git repository.

pabloem pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/beam.git


The following commit(s) were added to refs/heads/master by this push:
     new 65a67a7  Add fmt preparator for go sdk
     new b2c67c6  Merge pull request #16088 from [Beam-13153] [Playground] Add 
validators and preparators for go code
65a67a7 is described below

commit 65a67a75d48cac757f688541e9f8c6c7233beff0
Author: Pavel Avilov <[email protected]>
AuthorDate: Tue Nov 30 19:39:56 2021 +0300

    Add fmt preparator for go sdk
---
 .../backend/internal/preparators/go_preparators.go |  28 +++-
 .../internal/preparators/go_preparators_test.go    | 148 +++++++++++++++++++++
 .../internal/preparators/python_preparators.go     |   2 -
 .../backend/internal/validators/go_validators.go   |   1 -
 4 files changed, 175 insertions(+), 4 deletions(-)

diff --git a/playground/backend/internal/preparators/go_preparators.go 
b/playground/backend/internal/preparators/go_preparators.go
index 87ee4cf..2cedc2f 100644
--- a/playground/backend/internal/preparators/go_preparators.go
+++ b/playground/backend/internal/preparators/go_preparators.go
@@ -15,7 +15,33 @@
 
 package preparators
 
+import (
+       "errors"
+       "os/exec"
+       "path/filepath"
+)
+
+const (
+       nameBinGo = "go"
+       fmtArgs   = "fmt"
+)
+
 // GetGoPreparators returns reparation methods that should be applied to Go 
code
 func GetGoPreparators(filePath string) *[]Preparator {
-       return &[]Preparator{}
+       preparatorArgs := make([]interface{}, 1)
+       preparatorArgs[0] = filePath
+       formatCodePreparator := Preparator{Prepare: formatCode, Args: 
preparatorArgs}
+       return &[]Preparator{formatCodePreparator}
+}
+
+// formatCode formats go code
+func formatCode(args ...interface{}) error {
+       filePath := args[0].(string)
+       cmd := exec.Command(nameBinGo, fmtArgs, filepath.Base(filePath))
+       cmd.Dir = filepath.Dir(filePath)
+       stdout, err := cmd.CombinedOutput()
+       if err != nil {
+               return errors.New(string(stdout))
+       }
+       return nil
 }
diff --git a/playground/backend/internal/preparators/go_preparators_test.go 
b/playground/backend/internal/preparators/go_preparators_test.go
new file mode 100644
index 0000000..e72a7c1
--- /dev/null
+++ b/playground/backend/internal/preparators/go_preparators_test.go
@@ -0,0 +1,148 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package preparators
+
+import (
+       "beam.apache.org/playground/backend/internal/logger"
+       "fmt"
+       "os"
+       "reflect"
+       "testing"
+)
+
+const (
+       correctCode   = "package main\n\nimport \"fmt\"\n\nfunc main() 
{\n\tfmt.Println(\"hello world\")\n\n\n}\n"
+       correctFile   = "correct.go"
+       incorrectCode = "package main\n\nimport \"fmt\"\n\nfunc main() 
{\n\tfmt.Println(\"hello world\"\n\n}\n"
+       incorrectFile = "incorrect.go"
+)
+
+func TestMain(m *testing.M) {
+       err := setupPreparedFiles()
+       if err != nil {
+               logger.Fatal(err)
+       }
+       defer teardown()
+       m.Run()
+}
+
+// setupPreparedFiles creates 2 go programs:
+// correctFile - program without errors
+// incorrectFile - program with errors
+func setupPreparedFiles() error {
+       err := createFile(correctFile, correctCode)
+       if err != nil {
+               return err
+       }
+       err = createFile(incorrectFile, incorrectCode)
+       if err != nil {
+               return err
+       }
+       return nil
+}
+
+//createFile create file with fileName and write text to it
+func createFile(fileName, text string) error {
+       f, err := os.Create(fileName)
+       if err != nil {
+               return err
+       }
+       defer f.Close()
+       _, err = f.Write([]byte(text))
+       if err != nil {
+               return err
+       }
+       return nil
+}
+
+func teardown() {
+       err := os.Remove(correctFile)
+       if err != nil {
+               logger.Fatal(err)
+       }
+       err = os.Remove(incorrectFile)
+       if err != nil {
+               logger.Fatal(err)
+       }
+}
+
+// getPreparedArgs returns array of received arguments
+func getPreparedArgs(args ...interface{}) []interface{} {
+       preparedArgs := make([]interface{}, len(args))
+       for i := range preparedArgs {
+               preparedArgs[i] = args[i]
+       }
+       return preparedArgs
+}
+
+func TestGetGoPreparators(t *testing.T) {
+       expectedPreparator := Preparator{Prepare: formatCode, Args: nil}
+       type args struct {
+               filePath string
+       }
+       tests := []struct {
+               name string
+               args args
+               want *[]Preparator
+       }{
+               {
+                       // getting the expected preparator
+                       name: "get expected preparator",
+                       args: args{filePath: ""},
+                       want: &[]Preparator{expectedPreparator},
+               },
+       }
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       if got := GetGoPreparators(tt.args.filePath); 
!reflect.DeepEqual(fmt.Sprint(got), fmt.Sprint(tt.want)) {
+                               t.Errorf("GetGoPreparators() = %v, want %v", 
got, tt.want)
+                       }
+               })
+       }
+}
+
+func Test_formatCode(t *testing.T) {
+       preparedArgs1 := getPreparedArgs(correctFile)
+       preparedArgs2 := getPreparedArgs(incorrectFile)
+       type args struct {
+               args []interface{}
+       }
+       tests := []struct {
+               name    string
+               args    args
+               wantErr bool
+       }{
+               {
+                       // formatting code that does not contain errors
+                       name:    "file without errors",
+                       args:    args{preparedArgs1},
+                       wantErr: false,
+               },
+               {
+                       // formatting code that contain errors
+                       name:    "file with errors",
+                       args:    args{preparedArgs2},
+                       wantErr: true,
+               },
+       }
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       if err := formatCode(tt.args.args...); (err != nil) != 
tt.wantErr {
+                               t.Errorf("formatCode() error = %v, wantErr %v", 
err, tt.wantErr)
+                       }
+               })
+       }
+}
diff --git a/playground/backend/internal/preparators/python_preparators.go 
b/playground/backend/internal/preparators/python_preparators.go
index d2ff963..c758ed1 100644
--- a/playground/backend/internal/preparators/python_preparators.go
+++ b/playground/backend/internal/preparators/python_preparators.go
@@ -15,8 +15,6 @@
 
 package preparators
 
-const ()
-
 // GetPythonPreparators returns preparation methods that should be applied to 
Python code
 func GetPythonPreparators(filePath string) *[]Preparator {
        return &[]Preparator{}
diff --git a/playground/backend/internal/validators/go_validators.go 
b/playground/backend/internal/validators/go_validators.go
index 651cb2d..4258971 100644
--- a/playground/backend/internal/validators/go_validators.go
+++ b/playground/backend/internal/validators/go_validators.go
@@ -17,6 +17,5 @@ package validators
 
 // GetGoValidators return validators methods that should be applied to Go code
 func GetGoValidators() *[]Validator {
-       //TODO: Will be added in task [BEAM-13153]
        return &[]Validator{}
 }

Reply via email to