Skip to content

Commit

Permalink
Fix newlines for builder errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jmattheis committed Mar 3, 2022
1 parent 24835bf commit a0f0141
Show file tree
Hide file tree
Showing 38 changed files with 54 additions and 64 deletions.
2 changes: 1 addition & 1 deletion builder/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,6 @@ func ToString(err *Error) string {
_, _ = fmt.Fprintln(&buf, strings.TrimSpace(string(line)))
}
fmt.Fprintln(&buf)
fmt.Fprintln(&buf, err.Cause)
fmt.Fprint(&buf, err.Cause)
return buf.String()
}
43 changes: 16 additions & 27 deletions runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestScenario(t *testing.T) {

body, _ := ioutil.ReadFile(genFile)

if os.Getenv("UPDATE_SCENARIO") == "true" {
if os.Getenv("UPDATE_SCENARIO") == "true" && scenario.ErrorStartsWith == "" {
if err != nil {
scenario.Success = ""
scenario.Error = replaceAbsolutePath(fmt.Sprint(err))
Expand All @@ -66,34 +66,23 @@ func TestScenario(t *testing.T) {
}
}

if scenario.Error != "" || scenario.ErrorStartsWith != "" {
if scenario.ErrorStartsWith != "" {
require.Error(t, err)
actualErr := replaceAbsolutePath(fmt.Sprint(err))
var expectedErr string
if scenario.Error != "" {
expectedErr = scenario.Error
// YAML parser inject new line at the end of multi-line string literal, remove it
// but only do so if actualErr does not have it
if strings.HasSuffix(expectedErr, "\n") && !strings.HasSuffix(actualErr, "\n") {
expectedErr = strings.TrimSuffix(expectedErr, "\n")
}
} else {
// always remove yaml-injected new line, we need prefix len without it
expectedErr = strings.TrimSuffix(scenario.ErrorStartsWith, "\n")
if len(actualErr) > len(expectedErr) {
// trim it to the prefix size to use Equal for a nice diff on test failures
actualErr = actualErr[0:len(expectedErr)]
}
}
// use Equal to show a nice diff message, other require methods do not show diffs
// making it hard to troubleshoot test failures
require.Equal(t, expectedErr, actualErr)
} else {
require.NoError(t, err)
require.NotEmpty(t, scenario.Success, "scenario.Success may not be empty")
require.Equal(t, scenario.Success, string(body))
require.NoError(t, compile(genFile), "generated converter doesn't build")
strErr := replaceAbsolutePath(fmt.Sprint(err))
require.Equal(t, scenario.ErrorStartsWith, strErr[:len(scenario.ErrorStartsWith)])
return
}

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

require.NoError(t, err)
require.NotEmpty(t, scenario.Success, "scenario.Success may not be empty")
require.Equal(t, scenario.Success, string(body))
require.NoError(t, compile(genFile), "generated converter doesn't build")
})
clearDir(execDir)
}
Expand Down
3 changes: 2 additions & 1 deletion scenario/3_struct_extend_pkg_pattern_via_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ input:
type Output struct {
Done bool
}
extends: ["strconv:Parse.*"]
extends:
- strconv:Parse.*
success: |
// Code generated by github.com/jmattheis/goverter, DO NOT EDIT.
Expand Down
2 changes: 1 addition & 1 deletion scenario/3_struct_unexported.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ input:
name string
Age int
}
error: |
error: |-
Error while creating converter method:
func (github.com/jmattheis/goverter/execution.Converter).Convert(source github.com/jmattheis/goverter/execution.Input) github.com/jmattheis/goverter/execution.Output
Expand Down
2 changes: 1 addition & 1 deletion scenario/6_return_error_extend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ input:
Name string
Age int
}
error: |
error: |-
Error while creating converter method:
func (github.com/jmattheis/goverter/execution.Converter).Convert(source github.com/jmattheis/goverter/execution.Input) github.com/jmattheis/goverter/execution.Output
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ input:
// goverter:extend strconv:ParseSomething
type Converter interface {
}
error: |
error: |-
Error while parsing extend in
github.com/jmattheis/goverter/execution.Converter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ input:
// goverter:extend strconv:Parse*
type Converter interface {
}
error: |
error: |-
Error while parsing extend in
github.com/jmattheis/goverter/execution.Converter
Expand Down
2 changes: 1 addition & 1 deletion scenario/7_error_invalid_extend_name_pattern_required.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ input:
// goverter:extend strconv:
type Converter interface {
}
error: |
error: |-
Error while parsing extend in
github.com/jmattheis/goverter/execution.Converter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ input:
// goverter:extend strconv:(
type Converter interface {
}
error: |
error: |-
Error while parsing extend in
github.com/jmattheis/goverter/execution.Converter
Expand Down
2 changes: 1 addition & 1 deletion scenario/7_error_invalid_extend_no_packages_loaded.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ input:
// goverter:extend file=/nonexisting:Convert
type Converter interface {
}
error_starts_with: |
error: |-
Error while parsing extend in
github.com/jmattheis/goverter/execution.Converter
Expand Down
2 changes: 1 addition & 1 deletion scenario/7_error_invalid_extend_not_exist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ input:
// goverter:extend HelloWorld
type Converter interface {
}
error: |
error: |-
Error while parsing extend in
github.com/jmattheis/goverter/execution.Converter
Expand Down
2 changes: 1 addition & 1 deletion scenario/7_error_invalid_extend_package_path_required.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ input:
// goverter:extend :Convert
type Converter interface {
}
error: |
error: |-
Error while parsing extend in
github.com/jmattheis/goverter/execution.Converter
Expand Down
2 changes: 1 addition & 1 deletion scenario/7_error_invalid_extend_packages_load_failure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ input:
// goverter:extend notaquery=abc:Convert
type Converter interface {
}
error_starts_with: |
error_starts_with: |-
Error while parsing extend in
github.com/jmattheis/goverter/execution.Converter
Expand Down
2 changes: 1 addition & 1 deletion scenario/7_error_invalid_extend_pattern_zero_matches.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ input:
// goverter:extend HelloWorld.*
type Converter interface {
}
error: |
error: |-
Error while parsing extend in
github.com/jmattheis/goverter/execution.Converter
Expand Down
2 changes: 1 addition & 1 deletion scenario/7_error_invalid_extend_wrong_package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ input:
// goverter:extend wrong/package:Convert
type Converter interface {
}
error_starts_with: |
error_starts_with: |-
Error while parsing extend in
github.com/jmattheis/goverter/execution.Converter
Expand Down
2 changes: 1 addition & 1 deletion scenario/7_error_invalid_extend_wrong_source.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ input:
func HelloWorld() int {
return 0
}
error: |
error: |-
Error while parsing extend in
github.com/jmattheis/goverter/execution.Converter
Expand Down
2 changes: 1 addition & 1 deletion scenario/7_error_invalid_extend_wrong_source_converter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ input:
func HelloWorld(int, int) int {
return 0
}
error: |
error: |-
Error while parsing extend in
github.com/jmattheis/goverter/execution.Converter
Expand Down
2 changes: 1 addition & 1 deletion scenario/7_error_invalid_extend_wrong_target.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ input:
}
func HelloWorld(int) {
}
error: |
error: |-
Error while parsing extend in
github.com/jmattheis/goverter/execution.Converter
Expand Down
2 changes: 1 addition & 1 deletion scenario/7_error_invalid_extend_wrong_target_second.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ input:
func HelloWorld(int) (int, int) {
return 0, 0
}
error: |
error: |-
Error while parsing extend in
github.com/jmattheis/goverter/execution.Converter
Expand Down
2 changes: 1 addition & 1 deletion scenario/7_error_invalid_extend_wrong_type.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ input:
type Converter interface {
}
const HelloWorld = 5
error: |
error: |-
Error while parsing extend in
github.com/jmattheis/goverter/execution.Converter
Expand Down
2 changes: 1 addition & 1 deletion scenario/7_error_map_key_mismatch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ input:
type Converter interface {
Convert(source map[string]int) map[int]int
}
error: |
error: |-
Error while creating converter method:
func (github.com/jmattheis/goverter/execution.Converter).Convert(source map[string]int) map[int]int
Expand Down
2 changes: 1 addition & 1 deletion scenario/7_error_map_value_mismatch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ input:
type Converter interface {
Convert(source map[int]int) map[int]int64
}
error: |
error: |-
Error while creating converter method:
func (github.com/jmattheis/goverter/execution.Converter).Convert(source map[int]int) map[int]int64
Expand Down
2 changes: 1 addition & 1 deletion scenario/7_error_missing_error_return.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ input:
Name string
Age int
}
error: |
error: |-
Error while creating converter method:
func (github.com/jmattheis/goverter/execution.Converter).Convert(source []github.com/jmattheis/goverter/execution.Input) []github.com/jmattheis/goverter/execution.Output
Expand Down
2 changes: 1 addition & 1 deletion scenario/7_error_nested_mismatch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ input:
type Output4 struct {
Value int
}
error: |
error: |-
Error while creating converter method:
func (github.com/jmattheis/goverter/execution.Converter).Convert(source github.com/jmattheis/goverter/execution.Input) github.com/jmattheis/goverter/execution.Output
Expand Down
2 changes: 1 addition & 1 deletion scenario/7_error_pointer_mismatch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ input:
type Converter interface {
Convert(source *int) *string
}
error: |
error: |-
Error while creating converter method:
func (github.com/jmattheis/goverter/execution.Converter).Convert(source *int) *string
Expand Down
2 changes: 1 addition & 1 deletion scenario/7_error_pointer_primitive_mismatch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ input:
type Converter interface {
Convert(source int) *string
}
error: |
error: |-
Error while creating converter method:
func (github.com/jmattheis/goverter/execution.Converter).Convert(source int) *string
Expand Down
2 changes: 1 addition & 1 deletion scenario/7_error_primitive_mismatch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ input:
type Converter interface {
Convert(source int16) int8
}
error: |
error: |-
Error while creating converter method:
func (github.com/jmattheis/goverter/execution.Converter).Convert(source int16) int8
Expand Down
2 changes: 1 addition & 1 deletion scenario/7_error_slice_mismatch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ input:
type Converter interface {
Convert(source []int16) int8
}
error: |
error: |-
Error while creating converter method:
func (github.com/jmattheis/goverter/execution.Converter).Convert(source []int16) int8
Expand Down
2 changes: 1 addition & 1 deletion scenario/7_error_slice_value_mismatch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ input:
type Converter interface {
Convert(source []int16) []int8
}
error: |
error: |-
Error while creating converter method:
func (github.com/jmattheis/goverter/execution.Converter).Convert(source []int16) []int8
Expand Down
2 changes: 1 addition & 1 deletion scenario/7_error_struct_identity_mapping.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ input:
Street string
City string
}
error: |
error: |-
Error while creating converter method:
func (github.com/jmattheis/goverter/execution.Converter).ConvertPerson(source github.com/jmattheis/goverter/execution.Person) github.com/jmattheis/goverter/execution.APIPerson
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ input:
type Output struct {
Name string
}
error: |
error: |-
Error while creating converter method:
func (github.com/jmattheis/goverter/execution.Converter).Convert(source github.com/jmattheis/goverter/execution.Input) github.com/jmattheis/goverter/execution.Output
Expand Down
2 changes: 1 addition & 1 deletion scenario/7_error_struct_mapped_field_type_missmatch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ input:
type Output struct {
Name string
}
error: |
error: |-
Error while creating converter method:
func (github.com/jmattheis/goverter/execution.Converter).Convert(source github.com/jmattheis/goverter/execution.Input) github.com/jmattheis/goverter/execution.Output
Expand Down
2 changes: 1 addition & 1 deletion scenario/7_error_struct_mapped_really_nested.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ input:
type Output struct {
Name *string
}
error: |
error: |-
Error while creating converter method:
func (github.com/jmattheis/goverter/execution.Converter).Convert(source github.com/jmattheis/goverter/execution.Input) github.com/jmattheis/goverter/execution.Output
Expand Down
2 changes: 1 addition & 1 deletion scenario/7_error_struct_missing_field.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ input:
Name string
Age int
}
error: |
error: |-
Error while creating converter method:
func (github.com/jmattheis/goverter/execution.Converter).Convert(source github.com/jmattheis/goverter/execution.Input) github.com/jmattheis/goverter/execution.Output
Expand Down
2 changes: 1 addition & 1 deletion scenario/7_error_struct_missing_mapped_field.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ input:
Name string
Age int
}
error: |
error: |-
Error while creating converter method:
func (github.com/jmattheis/goverter/execution.Converter).Convert(source github.com/jmattheis/goverter/execution.Input) github.com/jmattheis/goverter/execution.Output
Expand Down
2 changes: 1 addition & 1 deletion scenario/7_error_struct_missing_mapped_nested_field.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ input:
Name string
Age int
}
error: |
error: |-
Error while creating converter method:
func (github.com/jmattheis/goverter/execution.Converter).Convert(source github.com/jmattheis/goverter/execution.Input) github.com/jmattheis/goverter/execution.Output
Expand Down
2 changes: 1 addition & 1 deletion scenario/7_error_struct_no_struct.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ input:
type Output struct {
Name string
}
error: |
error: |-
Error while creating converter method:
func (github.com/jmattheis/goverter/execution.Converter).Convert(source github.com/jmattheis/goverter/execution.Input) github.com/jmattheis/goverter/execution.Output
Expand Down
2 changes: 1 addition & 1 deletion scenario/7_error_struct_pointer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ input:
Name string
Age int64
}
error: |
error: |-
Error while creating converter method:
func (github.com/jmattheis/goverter/execution.Converter).Convert(source github.com/jmattheis/goverter/execution.Input) *github.com/jmattheis/goverter/execution.Output
Expand Down

0 comments on commit a0f0141

Please sign in to comment.