Skip to content

Commit

Permalink
fix: fix move file test
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 29, 2019
1 parent 453ea3c commit 9d46ca1
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -67,3 +67,4 @@ coca_reporter
*.coverprofile
bug
_fixtures/tbs/tbs
_fixtures/refactor/move/b/ImportForB.java
2 changes: 1 addition & 1 deletion _fixtures/refactor/move.config
@@ -1 +1 @@
move.a.ImportForB -> move.a.ImportForB
move.a.ImportForB -> move.b.ImportForB
5 changes: 5 additions & 0 deletions _fixtures/refactor/move/a/ImportForB.java
@@ -0,0 +1,5 @@
package move.a;

public class ImportForB {

}
34 changes: 31 additions & 3 deletions core/domain/refactor/move_class/move_class_app.go
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -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() {
Expand Down Expand Up @@ -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], ".")
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
}
8 changes: 6 additions & 2 deletions core/domain/refactor/move_class/move_class_app_test.go
Expand Up @@ -2,6 +2,8 @@ package move_class

import (
. "github.com/onsi/gomega"
"os"
"path/filepath"
"testing"
)

Expand All @@ -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"))
}
26 changes: 0 additions & 26 deletions core/support/file_helper.go
Expand Up @@ -3,7 +3,6 @@ package support
import (
"fmt"
"github.com/phodal/coca/config"
"io"
"io/ioutil"
"os"
)
Expand Down Expand Up @@ -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
}

0 comments on commit 9d46ca1

Please sign in to comment.