From cd9f1427a7325d0bdcf9a9ec877dfb3e4a2b1116 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Mon, 6 Jan 2020 17:14:01 +0800 Subject: [PATCH] test: add git reset for refactor test --- _fixtures/refactor/rename.config | 2 +- cocatest/git_reset.go | 18 +++++++++ .../refactor/rename/rename_method_test.go | 38 +++++-------------- .../unused/remove_unused_import_test.go | 13 ++++++- 4 files changed, 40 insertions(+), 31 deletions(-) create mode 100644 cocatest/git_reset.go diff --git a/_fixtures/refactor/rename.config b/_fixtures/refactor/rename.config index 23a675c..1d470d0 100644 --- a/_fixtures/refactor/rename.config +++ b/_fixtures/refactor/rename.config @@ -1 +1 @@ -polymorphism.Overload.demo -> polymorphism.Overload.demoA +polymorphism.Overload.demoA -> polymorphism.Overload.demo diff --git a/cocatest/git_reset.go b/cocatest/git_reset.go new file mode 100644 index 0000000..9919b55 --- /dev/null +++ b/cocatest/git_reset.go @@ -0,0 +1,18 @@ +package cocatest + +import ( + "fmt" + "log" + "os/exec" +) + +func ResetGitDir(codePath string) { + cmd := exec.Command("git", "checkout", "--ignore-skip-worktree-bits", "--", codePath) + + out, err := cmd.CombinedOutput() + if err != nil { + fmt.Println(string(out)) + log.Fatalf("cmd.Run() failed with %s\n", err) + } + +} diff --git a/core/context/refactor/rename/rename_method_test.go b/core/context/refactor/rename/rename_method_test.go index e57fc42..701a322 100644 --- a/core/context/refactor/rename/rename_method_test.go +++ b/core/context/refactor/rename/rename_method_test.go @@ -1,18 +1,18 @@ package unused import ( + "fmt" . "github.com/onsi/gomega" + "github.com/phodal/coca/cmd/cmd_util" + "github.com/phodal/coca/cocatest" "github.com/phodal/coca/core/context/analysis" "path/filepath" - "sync" "testing" ) func TestRenameMethodApp(t *testing.T) { g := NewGomegaWithT(t) - var wg sync.WaitGroup - codePath := "../../../../_fixtures/refactor/unused" configPath := "../../../../_fixtures/refactor/rename.config" codePath = filepath.FromSlash(codePath) @@ -25,35 +25,15 @@ func TestRenameMethodApp(t *testing.T) { classes = append(classes, node.Package+"."+node.ClassName) } - wg.Add(1) callApp := analysis.NewJavaFullApp() callNodes := callApp.AnalysisPath(codePath, classes, identifiers) - wg.Add(1) - go func() { - - RenameMethodApp(callNodes).Refactoring("") - defer wg.Done() - - newnodes := callApp.AnalysisPath(codePath, classes, identifiers) - g.Expect(newnodes[0].Methods[0].Name).To(Equal("demoA")) - - }() - - wg.Add(1) - go func() { - wg.Wait() - - configPath2 := "../../../../_fixtures/refactor/rename_back.config" - configPath2 = filepath.FromSlash(configPath2) - - RenameMethodApp(callNodes).Refactoring("") - defer wg.Done() - - renameBackNodes := callApp.AnalysisPath(codePath, classes, identifiers) - g.Expect(renameBackNodes[0].Methods[0].Name).To(Equal("demo")) + fmt.Println(callNodes) + configBytes := cmd_util.ReadFile(configPath) + RenameMethodApp(callNodes).Refactoring(string(configBytes)) - wg.Wait() - }() + newnodes := callApp.AnalysisPath(codePath, classes, identifiers) + g.Expect(newnodes[0].Methods[0].Name).To(Equal("demo")) + cocatest.ResetGitDir(codePath) } diff --git a/core/context/refactor/unused/remove_unused_import_test.go b/core/context/refactor/unused/remove_unused_import_test.go index 0d70be2..fd40eff 100644 --- a/core/context/refactor/unused/remove_unused_import_test.go +++ b/core/context/refactor/unused/remove_unused_import_test.go @@ -2,6 +2,7 @@ package unused import ( . "github.com/onsi/gomega" + "github.com/phodal/coca/cocatest" "path/filepath" "testing" ) @@ -9,9 +10,13 @@ import ( func TestRemoveUnusedImportApp_Analysis(t *testing.T) { g := NewGomegaWithT(t) - codePath := "../../../../_fixtures/refactor/unused" codePath = filepath.FromSlash(codePath) + cocatest.ResetGitDir(codePath) + + deps1, _, _ := cocatest.BuildAnalysisDeps(codePath) + g.Expect(len(deps1[0].Imports)).To(Equal(3)) + app := NewRemoveUnusedImportApp(codePath) results := app.Analysis() @@ -19,4 +24,10 @@ func TestRemoveUnusedImportApp_Analysis(t *testing.T) { errorLines := BuildErrorLines(results[0]) g.Expect(errorLines).To(Equal([]int{3,4,5})) + + app.Refactoring(results) + + deps, _, _ := cocatest.BuildAnalysisDeps(codePath) + g.Expect(len(deps[0].Imports)).To(Equal(0)) + cocatest.ResetGitDir(codePath) } -- GitLab