Skip to content

Commit

Permalink
Update xdry-php-plugin and fix expectations
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoVie committed Sep 19, 2022
1 parent 25f5d39 commit 6a66862
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 78 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ _testdata/generated/reports/*
_testdata/xdry.log
xdry-cache_*.json
/xdry.json
/xdry.log
/xdry.log
_testdata/cache/*
!/_testdata/cache/.gitkeep
Empty file added _testdata/cache/.gitkeep
Empty file.
72 changes: 10 additions & 62 deletions _testdata/expected_xdry_report.json
Original file line number Diff line number Diff line change
Expand Up @@ -192,39 +192,13 @@
"CloneType": 2,
"AggregatedClones": [
{
"Content": " ) ;",
"Language": "php",
"Instances": [
{
"Path": "%PROJECT_PATH%/_testdata/php/bar.php",
"Language": "php",
"Index": 31
},
{
"Path": "%PROJECT_PATH%/_testdata/php/bla.php",
"Language": "php",
"Index": 21
},
{
"Path": "%PROJECT_PATH%/_testdata/php/bum.php",
"Language": "php",
"Index": 31
},
{
"Path": "%PROJECT_PATH%/_testdata/php/foo.php",
"Language": "php",
"Index": 31
}
]
},
{
"Content": "$x0 = 1 ; ",
"Content": "$x = 1 ; print ( $x ) ;",
"Language": "php",
"Instances": [
{
"Path": "%PROJECT_PATH%/_testdata/php/bar.php",
"Language": "php",
"Index": 0
"Index": 9
},
{
"Path": "%PROJECT_PATH%/_testdata/php/bla.php",
Expand All @@ -234,38 +208,12 @@
{
"Path": "%PROJECT_PATH%/_testdata/php/bum.php",
"Language": "php",
"Index": 0
},
{
"Path": "%PROJECT_PATH%/_testdata/php/foo.php",
"Language": "php",
"Index": 0
}
]
},
{
"Content": "print ( $x",
"Language": "php",
"Instances": [
{
"Path": "%PROJECT_PATH%/_testdata/php/bar.php",
"Language": "php",
"Index": 20
},
{
"Path": "%PROJECT_PATH%/_testdata/php/bla.php",
"Language": "php",
"Index": 10
},
{
"Path": "%PROJECT_PATH%/_testdata/php/bum.php",
"Language": "php",
"Index": 20
"Index": 9
},
{
"Path": "%PROJECT_PATH%/_testdata/php/foo.php",
"Language": "php",
"Index": 20
"Index": 9
}
]
},
Expand All @@ -291,7 +239,7 @@
]
},
{
"Content": "$x0 = 1 ; $x1 = 1 ; print ( $x1 ) ;",
"Content": "$x = 1 ; $x = 1 ; print ( $x ) ;",
"Language": "php",
"Instances": [
{
Expand Down Expand Up @@ -333,13 +281,13 @@
"CloneType": 3,
"AggregatedClones": [
{
"Content": "$x0 = 1 ; print ( $x ) ;",
"Content": "$x = 1 ; print ( $x ) ;",
"Language": "php",
"Instances": [
{
"Path": "%PROJECT_PATH%/_testdata/php/bar.php",
"Language": "php",
"Index": 0
"Index": 9
},
{
"Path": "%PROJECT_PATH%/_testdata/php/bla.php",
Expand All @@ -349,12 +297,12 @@
{
"Path": "%PROJECT_PATH%/_testdata/php/bum.php",
"Language": "php",
"Index": 0
"Index": 9
},
{
"Path": "%PROJECT_PATH%/_testdata/php/foo.php",
"Language": "php",
"Index": 0
"Index": 9
}
]
},
Expand All @@ -380,7 +328,7 @@
]
},
{
"Content": "$x0 = 1 ; $x1 = 1 ; print ( $x1 ) ;",
"Content": "$x = 1 ; $x = 1 ; print ( $x ) ;",
"Language": "php",
"Instances": [
{
Expand Down
7 changes: 4 additions & 3 deletions _testdata/xdry_2.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"level-1": 1,
"level-2": 1,
"level-3": 1
}
},
"cacheDirectory": "cache"
},
"reports": [
{
Expand All @@ -31,7 +32,7 @@
"--rm",
"-v",
"%FILEPATH%:/file.php",
"leovie/x-dry-php-plugin:latest",
"leovie/xdry-php-plugin:0.1.0",
"normalize:level-1",
"/file.php"
]
Expand All @@ -46,7 +47,7 @@
"--rm",
"-v",
"%FILEPATH%:/file.php",
"leovie/x-dry-php-plugin:latest",
"leovie/xdry-php-plugin:0.1.0",
"normalize:level-2",
"/file.php"
]
Expand Down
52 changes: 43 additions & 9 deletions src/cmd/xdry_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package main

import (
"fmt"
. "github.com/benjamintf1/unmarshalledmatchers"
. "github.com/onsi/gomega"
"io"
"os"
"path"
"path/filepath"
"strings"
"testing"
"x-dry-go/src/internal/service"
Expand All @@ -17,30 +17,64 @@ func TestRun(t *testing.T) {

cwd, _ := os.Getwd()
projectPath := path.Join(cwd, "..", "..")
testdataPath := path.Join(projectPath, "_testdata")
reportsDir := path.Join(testdataPath, "generated", "reports")
cacheDir := path.Join(testdataPath, "cache")

generatedReportFile := path.Join(projectPath, "_testdata", "generated", "reports", "xdry_report.json")
deletedReports := clearReportsDir(reportsDir)
deletedCaches := clearCacheDir(cacheDir)

fmt.Println(generatedReportFile)
for _, deletedReport := range deletedReports {
g.Expect(deletedReport).ShouldNot(BeAnExistingFile())
}
for _, deletedCache := range deletedCaches {
g.Expect(deletedCache).ShouldNot(BeAnExistingFile())
}

os.Remove(generatedReportFile)

g.Expect(generatedReportFile).ShouldNot(BeAnExistingFile())

wantFile := path.Join(projectPath, "_testdata", "expected_xdry_report.json")
wantFile := path.Join(testdataPath, "expected_xdry_report.json")
wantBytes, _ := os.ReadFile(wantFile)
want := string(wantBytes)

want = strings.ReplaceAll(want, "%PROJECT_PATH%", projectPath)

configPath := path.Join(projectPath, "_testdata", "xdry_2.json")
configPath := path.Join(testdataPath, "xdry_2.json")

var out io.Writer = os.Stdout

service.Analyze(out, configPath)

generatedReportFile := path.Join(reportsDir, "xdry_report.json")
actualBytes, _ := os.ReadFile(generatedReportFile)
actual := string(actualBytes)

g.Expect(actual).Should(
MatchUnorderedJSON(want))
}

func clearReportsDir(reportsDir string) []string {
files, err := filepath.Glob(path.Join(reportsDir, "xdry_report.*"))
if err != nil {
panic(err)
}
for _, f := range files {
if err := os.Remove(f); err != nil {
panic(err)
}
}

return files
}

func clearCacheDir(cacheDir string) []string {
files, err := filepath.Glob(path.Join(cacheDir, "xdry-cache_*.json"))
if err != nil {
panic(err)
}
for _, f := range files {
if err := os.Remove(f); err != nil {
panic(err)
}
}

return files
}
13 changes: 11 additions & 2 deletions src/internal/clone_detect/clone_detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/schollz/progressbar/v3"
"log"
"os"
"path"
"path/filepath"
"strconv"
"sync"
Expand Down Expand Up @@ -43,7 +44,12 @@ func (pair filePair) hash() string {
return pair.AFile.Path + "_" + pair.BFile.Path
}

func DetectInDirectory(directory string, cloneType int, levelNormalizers map[int][]config.Normalizer) (error, []Clone) {
func DetectInDirectory(
directory string,
cloneType int,
levelNormalizers map[int][]config.Normalizer,
configuration config.Config,
) (error, []Clone) {
filepaths, err := findFilesInDir(directory)
if err != nil {
return err, []Clone{}
Expand All @@ -53,6 +59,7 @@ func DetectInDirectory(directory string, cloneType int, levelNormalizers map[int
cloneTypeToNormalizeLevel(cloneType),
levelNormalizers,
filepaths,
configuration,
)

err, compareFunc := getCompareFuncForLevel(cloneType)
Expand Down Expand Up @@ -189,8 +196,10 @@ func normalizeFiles(
level int,
levelNormalizers map[int][]config.Normalizer,
filepaths []string,
configuration config.Config,
) map[string]structs.File {
fileCache, err := cache.InitOrReadCache("xdry-cache_level_" + strconv.Itoa(level) + ".json")
cachePath := path.Join(configuration.Settings.CacheDirectory, "xdry-cache_level_"+strconv.Itoa(level)+".json")
fileCache, err := cache.InitOrReadCache(cachePath)
if err != nil {
fmt.Println("Error reading cache")
}
Expand Down
2 changes: 1 addition & 1 deletion src/internal/service/analyze_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func Analyze(out io.Writer, configPath string) int {
typedClones := map[int][]clone_detect.Clone{}
for _, directory := range configuration.Directories {
for cloneType := 1; cloneType <= 3; cloneType++ {
err, clonesInDir := clone_detect.DetectInDirectory(directory, cloneType, levelNormalizers)
err, clonesInDir := clone_detect.DetectInDirectory(directory, cloneType, levelNormalizers, *configuration)
if err != nil {
fmt.Fprintln(out, err)

Expand Down

0 comments on commit 6a66862

Please sign in to comment.