From 9d46ca1616d556c7d882a336bb2bfc0dccfc961c Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Sun, 29 Dec 2019 15:45:26 +0800 Subject: [PATCH] fix: fix move file test --- .gitignore | 1 + _fixtures/refactor/move.config | 2 +- _fixtures/refactor/move/a/ImportForB.java | 5 +++ .../refactor/move_class/move_class_app.go | 34 +++++++++++++++++-- .../move_class/move_class_app_test.go | 8 +++-- core/support/file_helper.go | 26 -------------- 6 files changed, 44 insertions(+), 32 deletions(-) diff --git a/.gitignore b/.gitignore index 45203ea5..37e9842c 100644 --- a/.gitignore +++ b/.gitignore @@ -67,3 +67,4 @@ coca_reporter *.coverprofile bug _fixtures/tbs/tbs +_fixtures/refactor/move/b/ImportForB.java diff --git a/_fixtures/refactor/move.config b/_fixtures/refactor/move.config index 0fa97e81..4292fb20 100644 --- a/_fixtures/refactor/move.config +++ b/_fixtures/refactor/move.config @@ -1 +1 @@ -move.a.ImportForB -> move.a.ImportForB +move.a.ImportForB -> move.b.ImportForB diff --git a/_fixtures/refactor/move/a/ImportForB.java b/_fixtures/refactor/move/a/ImportForB.java index e69de29b..1440de6d 100644 --- a/_fixtures/refactor/move/a/ImportForB.java +++ b/_fixtures/refactor/move/a/ImportForB.java @@ -0,0 +1,5 @@ +package move.a; + +public class ImportForB { + +} diff --git a/core/domain/refactor/move_class/move_class_app.go b/core/domain/refactor/move_class/move_class_app.go index 7d49713c..14816ba3 100644 --- a/core/domain/refactor/move_class/move_class_app.go +++ b/core/domain/refactor/move_class/move_class_app.go @@ -7,6 +7,7 @@ import ( base2 "github.com/phodal/coca/core/domain/refactor/base" models2 "github.com/phodal/coca/core/domain/refactor/base/models" utils2 "github.com/phodal/coca/core/support" + "io" "io/ioutil" "log" "os" @@ -32,7 +33,7 @@ func NewMoveClassApp(config string, pPath string) *MoveClassApp { return &MoveClassApp{} } -func (j *MoveClassApp) Analysis() { +func (j *MoveClassApp) Analysis() []models2.JMoveStruct { // TODO: 使用 Deps.json 来移动包 files := utils2.GetJavaFiles(configPath) for index := range files { @@ -49,11 +50,13 @@ func (j *MoveClassApp) Analysis() { antlr.NewParseTreeWalker().Walk(listener, context) + node = listener.GetNodeInfo() moveStruct := &models2.JMoveStruct{node, currentFile, node.GetImports()} nodes = append(nodes, *moveStruct) } parseRename() + return nodes } func parseRename() { @@ -95,6 +98,7 @@ func updatePackageInfo(originImport string, newImport string) { if originNode.Name == "" { return } + path := buildJavaPath(configPath + newImport) split := strings.Split(newImport, ".") pkg := strings.Join(split[:len(split)-1], ".") @@ -142,8 +146,7 @@ func copyClass(originFile string, newFile string) { newFile = strings.ReplaceAll(newFile, ".", "/") + ".java" } - fmt.Println(originFile, newFile) - _, err := utils2.CopyFile(originFile, newFile) + _, err := CopyFile(originFile, newFile) if err != nil { panic(err) } @@ -160,3 +163,28 @@ func buildJavaPath(originFile string) string { } return str } + +func CopyFile(src, dst string) (int64, error) { + sourceFileStat, err := os.Stat(src) + if err != nil { + return 0, err + } + + if !sourceFileStat.Mode().IsRegular() { + return 0, fmt.Errorf("%s is not a regular file", src) + } + + source, err := os.Open(src) + if err != nil { + return 0, err + } + defer source.Close() + + destination, err := os.Create(dst) + if err != nil { + return 0, err + } + defer destination.Close() + nBytes, err := io.Copy(destination, source) + return nBytes, err +} diff --git a/core/domain/refactor/move_class/move_class_app_test.go b/core/domain/refactor/move_class/move_class_app_test.go index a428c015..f131e77a 100644 --- a/core/domain/refactor/move_class/move_class_app_test.go +++ b/core/domain/refactor/move_class/move_class_app_test.go @@ -2,6 +2,8 @@ package move_class import ( . "github.com/onsi/gomega" + "os" + "path/filepath" "testing" ) @@ -10,8 +12,10 @@ func TestNewMoveClassApp(t *testing.T) { config := "../../../../_fixtures/refactor/move.config" path := "../../../../_fixtures/refactor/" - app := NewMoveClassApp(config, path) + abs_path, _ := filepath.Abs(path) + app := NewMoveClassApp(config, abs_path + "/") app.Analysis() - g.Expect(true).To(Equal(true)) + stat, _ := os.Stat(path + "/move/b/ImportForB.java") + g.Expect(stat.Name()).To(Equal("ImportForB.java")) } \ No newline at end of file diff --git a/core/support/file_helper.go b/core/support/file_helper.go index 39c5e7b8..3674ecfc 100644 --- a/core/support/file_helper.go +++ b/core/support/file_helper.go @@ -3,7 +3,6 @@ package support import ( "fmt" "github.com/phodal/coca/config" - "io" "io/ioutil" "os" ) @@ -33,28 +32,3 @@ func ReadFile(fileName string) []byte { } return contents } - -func CopyFile(src, dst string) (int64, error) { - sourceFileStat, err := os.Stat(src) - if err != nil { - return 0, err - } - - if !sourceFileStat.Mode().IsRegular() { - return 0, fmt.Errorf("%s is not a regular file", src) - } - - source, err := os.Open(src) - if err != nil { - return 0, err - } - defer source.Close() - - destination, err := os.Create(dst) - if err != nil { - return 0, err - } - defer destination.Close() - nBytes, err := io.Copy(destination, source) - return nBytes, err -}