Skip to content

Commit

Permalink
fix: fix @cwd add tests for multiple package converters
Browse files Browse the repository at this point in the history
  • Loading branch information
jmattheis committed Oct 22, 2023
1 parent 3df74d9 commit ba843a6
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 4 deletions.
7 changes: 5 additions & 2 deletions generator/filemanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type fileManager struct {

type managedFile struct {
Package string
Initial *config.Converter
Content *jen.File
}

Expand All @@ -26,6 +27,7 @@ func (m *fileManager) Get(c *config.Converter, workDirectory string) (*jen.File,
if !ok {
f = &managedFile{
Package: c.OutputPackage,
Initial: c,
}

parts := strings.SplitN(c.OutputPackage, ":", 2)
Expand All @@ -43,7 +45,8 @@ func (m *fileManager) Get(c *config.Converter, workDirectory string) (*jen.File,
}

if f.Package != c.OutputPackage {
return nil, fmt.Errorf("damn") // TODO fix
return nil, fmt.Errorf("Error creating converters\n %s\n %s\nand\n %s\n %s\n\nCannot use different packages\n %s\n %s\nin the same output file:\n %s",
c.FileSource, c.Type, f.Initial.FileSource, f.Initial.Type, c.OutputPackage, f.Initial.OutputPackage, output)
}

return f.Content, nil
Expand All @@ -63,7 +66,7 @@ func (m *fileManager) renderFiles() (map[string][]byte, error) {

func getOutputDir(c *config.Converter, cwd string) string {
if strings.HasPrefix(c.OutputFile, "@cwd/") {
return filepath.Join(cwd, c.OutputFile)
return filepath.Join(cwd, strings.TrimPrefix(c.OutputFile, "@cwd/"))
}
return filepath.Join(filepath.Dir(c.FileSource), c.OutputFile)
}
10 changes: 8 additions & 2 deletions runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,15 @@ func TestScenario(t *testing.T) {

global := append([]string{"outputPackage github.com/jmattheis/goverter/execution/" + genPkgName}, scenario.Global...)

patterns := scenario.Patterns
if len(patterns) == 0 {
patterns = append(patterns, "github.com/jmattheis/goverter/execution")
}

files, err := generateConvertersRaw(
&GenerateConfig{
WorkingDir: execDir,
PackagePatterns: []string{"github.com/jmattheis/goverter/execution"},
PackagePatterns: patterns,
Global: config.RawLines{
Lines: global,
Location: "scenario global",
Expand Down Expand Up @@ -138,7 +143,8 @@ type Scenario struct {
Input map[string]string `yaml:"input"`
Global []string `yaml:"global,omitempty"`

Success []*OutputFile `yaml:"success,omitempty"`
Patterns []string `yaml:"patterns,omitempty"`
Success []*OutputFile `yaml:"success,omitempty"`
// for error cases, use either Error or ErrorStartsWith, not both
Error string `yaml:"error,omitempty"`
ErrorStartsWith string `yaml:"error_starts_with,omitempty"`
Expand Down
33 changes: 33 additions & 0 deletions scenario/cwd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
input:
pkg1/input.go: |
package pkg1
// goverter:converter
// goverter:output @cwd/generated/output.go
type Converter interface {
Convert(source Input) Output
}
type Input struct {
ID int
}
type Output struct {
ID int
}
patterns:
- github.com/jmattheis/goverter/execution/pkg1
success:
- generated/output.go: |
// Code generated by github.com/jmattheis/goverter, DO NOT EDIT.
package generated
import pkg1 "github.com/jmattheis/goverter/execution/pkg1"
type ConverterImpl struct{}
func (c *ConverterImpl) Convert(source pkg1.Input) pkg1.Output {
var pkg1Output pkg1.Output
pkg1Output.ID = source.ID
return pkg1Output
}
56 changes: 56 additions & 0 deletions scenario/multiple_package_one_output.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
input:
model/model.go: |
package model
type Input struct {
ID int
}
type Output struct {
ID int
}
pkg1/input.go: |
package pkg1
import model "github.com/jmattheis/goverter/execution/model"
// goverter:converter
// goverter:output ../generated/output.go
type Converter interface {
Convert(source model.Input) model.Output
}
pkg2/input.go: |
package pkg2
import model "github.com/jmattheis/goverter/execution/model"
// goverter:converter
// goverter:output ../generated/output.go
type Converter2 interface {
Convert(source model.Input) model.Output
}
patterns:
- github.com/jmattheis/goverter/execution/pkg1
- github.com/jmattheis/goverter/execution/pkg2
success:
- generated/output.go: |
// Code generated by github.com/jmattheis/goverter, DO NOT EDIT.
package generated
import model "github.com/jmattheis/goverter/execution/model"
type Converter2Impl struct{}
func (c *Converter2Impl) Convert(source model.Input) model.Output {
var modelOutput model.Output
modelOutput.ID = source.ID
return modelOutput
}
type ConverterImpl struct{}
func (c *ConverterImpl) Convert(source model.Input) model.Output {
var modelOutput model.Output
modelOutput.ID = source.ID
return modelOutput
}
55 changes: 55 additions & 0 deletions scenario/multiple_package_one_output_wildcard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
input:
model/model.go: |
package model
type Input struct {
ID int
}
type Output struct {
ID int
}
pkg1/input.go: |
package pkg1
import model "github.com/jmattheis/goverter/execution/model"
// goverter:converter
// goverter:output ../generated/output.go
type Converter interface {
Convert(source model.Input) model.Output
}
pkg2/input.go: |
package pkg2
import model "github.com/jmattheis/goverter/execution/model"
// goverter:converter
// goverter:output ../generated/output.go
type Converter2 interface {
Convert(source model.Input) model.Output
}
patterns:
- github.com/jmattheis/goverter/execution/...
success:
- generated/output.go: |
// Code generated by github.com/jmattheis/goverter, DO NOT EDIT.
package generated
import model "github.com/jmattheis/goverter/execution/model"
type Converter2Impl struct{}
func (c *Converter2Impl) Convert(source model.Input) model.Output {
var modelOutput model.Output
modelOutput.ID = source.ID
return modelOutput
}
type ConverterImpl struct{}
func (c *ConverterImpl) Convert(source model.Input) model.Output {
var modelOutput model.Output
modelOutput.ID = source.ID
return modelOutput
}
35 changes: 35 additions & 0 deletions scenario/output_package_mismatch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
input:
input.go: |
package structs
// goverter:converter
// goverter:outputPackage pkg1
type Converter interface {
Convert(source Input) Output
}
// goverter:converter
// goverter:outputPackage pkg2
type Converter2 interface {
Convert(source Input) Output
}
type Input struct {
ID int
}
type Output struct {
ID int
}
error: |-
Error creating converters
/ABSOLUTE/execution/input.go
github.com/jmattheis/goverter/execution.Converter
and
/ABSOLUTE/execution/input.go
github.com/jmattheis/goverter/execution.Converter2
Cannot use different packages
pkg1
pkg2
in the same output file:
/ABSOLUTE/execution/generated/generated.go

0 comments on commit ba843a6

Please sign in to comment.