Skip to content

Commit

Permalink
Merge pull request #108 from jmattheis/parallel
Browse files Browse the repository at this point in the history
parallel tests
  • Loading branch information
jmattheis authored Nov 25, 2023
2 parents a9249f6 + f4de45b commit cafe070
Show file tree
Hide file tree
Showing 48 changed files with 93 additions and 82 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
version: v1.51.1
- run: go build ./cmd/goverter
- run: go test -coverpkg ./... -coverprofile=coverage.txt -covermode=atomic ./...
- run: bash <(curl -s https://codecov.io/bash)
- uses: codecov/codecov-action@v3
'build_go116':
runs-on: ubuntu-latest
steps:
Expand Down
8 changes: 8 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
coverage:
status:
project:
default:
target: 90%
patch:
default:
target: 90%
71 changes: 37 additions & 34 deletions runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package goverter

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand All @@ -18,32 +17,39 @@ import (
)

func TestScenario(t *testing.T) {
curPath := getCurrentPath()
scenarios := filepath.Join(curPath, "scenario")
execDir := filepath.Join(curPath, "execution")
files, err := ioutil.ReadDir(scenarios)
rootDir := getCurrentPath()
scenarioDir := filepath.Join(rootDir, "scenario")
workDir := filepath.Join(rootDir, "execution")
scenarioFiles, err := os.ReadDir(scenarioDir)
require.NoError(t, err)
require.NoError(t, clearDir(workDir))

require.NoError(t, os.MkdirAll(execDir, os.ModePerm))
require.NoError(t, clearDir(execDir))

for _, file := range files {
for _, file := range scenarioFiles {
require.False(t, file.IsDir(), "should not be a directory")
file := file

testName := strings.TrimSuffix(file.Name(), filepath.Ext(file.Name()))

t.Run(file.Name(), func(t *testing.T) {
scenarioFileName := filepath.Join(scenarios, file.Name())
scenarioBytes, err := ioutil.ReadFile(scenarioFileName)
t.Run(testName, func(t *testing.T) {
t.Parallel()
testWorkDir := filepath.Join(workDir, testName)
require.NoError(t, os.MkdirAll(testWorkDir, os.ModePerm))
require.NoError(t, clearDir(testWorkDir))
scenarioFilePath := filepath.Join(scenarioDir, file.Name())
scenarioFileBytes, err := os.ReadFile(scenarioFilePath)
require.NoError(t, err)

scenario := Scenario{}
err = yaml.Unmarshal(scenarioBytes, &scenario)
err = yaml.Unmarshal(scenarioFileBytes, &scenario)
require.NoError(t, err)
err = os.WriteFile(filepath.Join(testWorkDir, "go.mod"), []byte("module github.com/jmattheis/goverter/execution\ngo 1.16"), os.ModePerm)
require.NoError(t, err)

for name, content := range scenario.Input {
inPath := filepath.Join(execDir, name)
inPath := filepath.Join(testWorkDir, name)
err = os.MkdirAll(filepath.Dir(inPath), os.ModePerm)
require.NoError(t, err)
err = os.WriteFile(filepath.Join(execDir, name), []byte(content), os.ModePerm)
err = os.WriteFile(filepath.Join(testWorkDir, name), []byte(content), os.ModePerm)
require.NoError(t, err)
}
genPkgName := "generated"
Expand All @@ -57,40 +63,40 @@ func TestScenario(t *testing.T) {

files, err := generateConvertersRaw(
&GenerateConfig{
WorkingDir: execDir,
WorkingDir: testWorkDir,
PackagePatterns: patterns,
Global: config.RawLines{
Lines: global,
Location: "scenario global",
},
})

actualOutputFiles := toOutputFiles(execDir, files)
actualOutputFiles := toOutputFiles(testWorkDir, files)

if os.Getenv("UPDATE_SCENARIO") == "true" && scenario.ErrorStartsWith == "" {
if scenario.ErrorStartsWith != "" {
require.Error(t, err)
strErr := replaceAbsolutePath(testWorkDir, fmt.Sprint(err))
require.Equal(t, scenario.ErrorStartsWith, strErr[:len(scenario.ErrorStartsWith)])
return
}

if os.Getenv("UPDATE_SCENARIO") == "true" {
if err != nil {
scenario.Success = []*OutputFile{}
scenario.Error = replaceAbsolutePath(curPath, fmt.Sprint(err))
scenario.Error = replaceAbsolutePath(testWorkDir, fmt.Sprint(err))
} else {
scenario.Success = toOutputFiles(execDir, files)
scenario.Success = toOutputFiles(testWorkDir, files)
scenario.Error = ""
}
newBytes, err := yaml.Marshal(&scenario)
if assert.NoError(t, err) {
os.WriteFile(scenarioFileName, newBytes, os.ModePerm)
os.WriteFile(scenarioFilePath, newBytes, os.ModePerm)
}
}

if scenario.ErrorStartsWith != "" {
require.Error(t, err)
strErr := replaceAbsolutePath(curPath, fmt.Sprint(err))
require.Equal(t, scenario.ErrorStartsWith, strErr[:len(scenario.ErrorStartsWith)])
return
}

if scenario.Error != "" {
require.Error(t, err)
require.Equal(t, scenario.Error, replaceAbsolutePath(curPath, fmt.Sprint(err)))
require.Equal(t, scenario.Error, replaceAbsolutePath(testWorkDir, fmt.Sprint(err)))
return
}

Expand All @@ -100,16 +106,13 @@ func TestScenario(t *testing.T) {

err = writeFiles(files)
require.NoError(t, err)
require.NoError(t, compile(execDir), "generated converter doesn't build")
require.NoError(t, compile(testWorkDir), "generated converter doesn't build")
})
if os.Getenv("SKIP_CLEAN") != "true" {
clearDir(execDir)
}
}
}

func replaceAbsolutePath(curPath, body string) string {
return strings.ReplaceAll(body, curPath, "/ABSOLUTE")
return strings.ReplaceAll(body, curPath, "@workdir")
}

func compile(dir string) error {
Expand Down
2 changes: 1 addition & 1 deletion scenario/bool_setting_invalid_value.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ input:
type Output struct { Name string }
error: |-
error parsing 'goverter:useZeroValueOnPointerInconsistency' at
/ABSOLUTE/execution/input.go:5:1
@workdir/input.go:5:1
github.com/jmattheis/goverter/execution.Converter
invalid boolean value: 'abc' must be one of 'yes', 'no'
2 changes: 1 addition & 1 deletion scenario/bool_setting_too_many_values.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ input:
type Output struct { Name string }
error: |-
error parsing 'goverter:useZeroValueOnPointerInconsistency' at
/ABSOLUTE/execution/input.go:5:1
@workdir/input.go:5:1
github.com/jmattheis/goverter/execution.Converter
expected at most one value but got 2: []string{"1", "2"}
2 changes: 1 addition & 1 deletion scenario/extend_external_exact_name_missing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ input:
}
error: |-
error parsing 'goverter:extend' at
/ABSOLUTE/execution/input.go:5:1
@workdir/input.go:5:1
github.com/jmattheis/goverter/execution.Converter
ParseSomething does not exist in scope
2 changes: 1 addition & 1 deletion scenario/extend_external_pattern_zero_matches.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ input:
}
error: |-
error parsing 'goverter:extend' at
/ABSOLUTE/execution/input.go:6:1
@workdir/input.go:6:1
github.com/jmattheis/goverter/execution.Converter
package strconv does not have methods with names that match
Expand Down
2 changes: 1 addition & 1 deletion scenario/extend_external_unexported_error.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ input:
}
error: |-
error parsing 'goverter:extend' at
/ABSOLUTE/execution/input.go:6:1
@workdir/input.go:6:1
github.com/jmattheis/goverter/execution.Converter
error parsing type:
Expand Down
2 changes: 1 addition & 1 deletion scenario/extend_name_pattern_required.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ input:
}
error: |-
error parsing 'goverter:extend' at
/ABSOLUTE/execution/input.go:5:1
@workdir/input.go:5:1
github.com/jmattheis/goverter/execution.Converter
method name pattern is required in the custom method "strconv:".
Expand Down
2 changes: 1 addition & 1 deletion scenario/extend_name_pattern_wrong_regexp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ input:
}
error: |-
error parsing 'goverter:extend' at
/ABSOLUTE/execution/input.go:5:1
@workdir/input.go:5:1
github.com/jmattheis/goverter/execution.Converter
could not parse name as regexp "(": error parsing regexp: missing closing ): `(`
2 changes: 1 addition & 1 deletion scenario/extend_no_packages_loaded.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ input:
}
error: |-
error parsing 'goverter:extend' at
/ABSOLUTE/execution/input.go:5:1
@workdir/input.go:5:1
github.com/jmattheis/goverter/execution.Converter
no packages were loaded for "file=/nonexisting", make sure it is a valid golang package
2 changes: 1 addition & 1 deletion scenario/extend_not_exist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ input:
}
error: |-
error parsing 'goverter:extend' at
/ABSOLUTE/execution/input.go:5:1
@workdir/input.go:5:1
github.com/jmattheis/goverter/execution.Converter
HelloWorld does not exist in scope
2 changes: 1 addition & 1 deletion scenario/extend_package_path_required.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ input:
}
error: |-
error parsing 'goverter:extend' at
/ABSOLUTE/execution/input.go:5:1
@workdir/input.go:5:1
github.com/jmattheis/goverter/execution.Converter
package path must not be empty in the custom method ":Convert".
Expand Down
2 changes: 1 addition & 1 deletion scenario/extend_package_unexported_method.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ input:
}
error: |-
error parsing 'goverter:extend' at
/ABSOLUTE/execution/input.go:5:1
@workdir/input.go:5:1
github.com/jmattheis/goverter/execution.Converter
error parsing type:
Expand Down
2 changes: 1 addition & 1 deletion scenario/extend_packages_load_failure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ input:
}
error_starts_with: |-
error parsing 'goverter:extend' at
/ABSOLUTE/execution/input.go:5:1
@workdir/input.go:5:1
github.com/jmattheis/goverter/execution.Converter
packages load failed on "notaquery=abc": invalid query type "notaquery"
2 changes: 1 addition & 1 deletion scenario/extend_pattern_zero_matches.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ input:
}
error: |-
error parsing 'goverter:extend' at
/ABSOLUTE/execution/input.go:5:1
@workdir/input.go:5:1
github.com/jmattheis/goverter/execution.Converter
local package does not have methods with names that match the golang regexp pattern "HelloWorld.*" and a convert signature
2 changes: 1 addition & 1 deletion scenario/extend_wrong_package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ input:
}
error_starts_with: |-
error parsing 'goverter:extend' at
/ABSOLUTE/execution/input.go:5:1
@workdir/input.go:5:1
github.com/jmattheis/goverter/execution.Converter
failed to load package "wrong/package", try adding a blank import for it
2 changes: 1 addition & 1 deletion scenario/extend_wrong_source.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ input:
}
error: |-
error parsing 'goverter:extend' at
/ABSOLUTE/execution/input.go:5:1
@workdir/input.go:5:1
github.com/jmattheis/goverter/execution.Converter
error parsing type:
Expand Down
2 changes: 1 addition & 1 deletion scenario/extend_wrong_source_converter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ input:
}
error: |-
error parsing 'goverter:extend' at
/ABSOLUTE/execution/input.go:5:1
@workdir/input.go:5:1
github.com/jmattheis/goverter/execution.Converter
error parsing type:
Expand Down
2 changes: 1 addition & 1 deletion scenario/extend_wrong_target.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ input:
}
error: |-
error parsing 'goverter:extend' at
/ABSOLUTE/execution/input.go:5:1
@workdir/input.go:5:1
github.com/jmattheis/goverter/execution.Converter
error parsing type:
Expand Down
2 changes: 1 addition & 1 deletion scenario/extend_wrong_target_second.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ input:
}
error: |-
error parsing 'goverter:extend' at
/ABSOLUTE/execution/input.go:5:1
@workdir/input.go:5:1
github.com/jmattheis/goverter/execution.Converter
error parsing type:
Expand Down
2 changes: 1 addition & 1 deletion scenario/extend_wrong_type.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ input:
const HelloWorld = 5
error: |-
error parsing 'goverter:extend' at
/ABSOLUTE/execution/input.go:5:1
@workdir/input.go:5:1
github.com/jmattheis/goverter/execution.Converter
error parsing type:
Expand Down
2 changes: 1 addition & 1 deletion scenario/invalid_comment_const.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ input:
// goverter:converter
const x = 5
error: '/ABSOLUTE/execution/input.go:4:1: goverter:converter may only be applied to type declarations '
error: '@workdir/input.go:4:1: goverter:converter may only be applied to type declarations '
2 changes: 1 addition & 1 deletion scenario/invalid_converter_comment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ input:
}
)
error: '/ABSOLUTE/execution/input.go:4:1: found goverter:converter on type but it has multiple interfaces inside'
error: '@workdir/input.go:4:1: found goverter:converter on type but it has multiple interfaces inside'
2 changes: 1 addition & 1 deletion scenario/invalid_interface.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ input:
// goverter:converter
type Converter struct {}
error: '/ABSOLUTE/execution/input.go:4:1: goverter:converter may only be applied to type interface declarations '
error: '@workdir/input.go:4:1: goverter:converter may only be applied to type interface declarations '
2 changes: 1 addition & 1 deletion scenario/invalid_interface2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ input:
// goverter:converter
Converter struct {}
)
error: '/ABSOLUTE/execution/input.go:3:1: goverter:converter may only be applied to type interface declarations '
error: '@workdir/input.go:3:1: goverter:converter may only be applied to type interface declarations '
2 changes: 1 addition & 1 deletion scenario/invalid_meta.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ input:
}
error: |-
error parsing 'goverter:not_existing' at
/ABSOLUTE/execution/input.go:5:1
@workdir/input.go:5:1
github.com/jmattheis/goverter/execution.Converter
unknown setting: not_existing
2 changes: 1 addition & 1 deletion scenario/invalid_meta2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ input:
)
error: |-
error parsing 'goverter:not_existing' at
/ABSOLUTE/execution/input.go:6:15
@workdir/input.go:6:15
github.com/jmattheis/goverter/execution.Converter
unknown setting: not_existing
2 changes: 1 addition & 1 deletion scenario/invalid_meta_empty.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ input:
)
error: |-
error parsing 'goverter:' at
/ABSOLUTE/execution/input.go:6:15
@workdir/input.go:6:15
github.com/jmattheis/goverter/execution.Converter
missing setting key
2 changes: 1 addition & 1 deletion scenario/invalid_method_comment_param.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ input:
}
error: |-
error parsing 'goverter:oops' at
/ABSOLUTE/execution/input.go:6:5
@workdir/input.go:6:5
func (github.com/jmattheis/goverter/execution.Converter).ConvertString(source string) string
unknown setting: oops
2 changes: 1 addition & 1 deletion scenario/invalid_method_comment_param2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ input:
)
error: |-
error parsing 'goverter:oops' at
/ABSOLUTE/execution/input.go:7:9
@workdir/input.go:7:9
func (github.com/jmattheis/goverter/execution.Converter).ConvertString(source string) string
unknown setting: oops
Loading

0 comments on commit cafe070

Please sign in to comment.