Skip to content

Commit

Permalink
Support raw files in package builder
Browse files Browse the repository at this point in the history
  • Loading branch information
mortent committed Dec 3, 2020
1 parent 1387f08 commit 4a651ee
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion internal/testutil/pkgbuilder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ type Pkg struct {

resources []resourceInfoWithSetters

files map[string]string

subPkgs []*Pkg
}

Expand Down Expand Up @@ -162,7 +164,8 @@ func NewSetterRef(name string, path ...string) SetterRef {
// NewPackage creates a new package for testing.
func NewPackage(name string) *Pkg {
return &Pkg{
Name: name,
Name: name,
files: make(map[string]string),
}
}

Expand Down Expand Up @@ -210,6 +213,11 @@ func (p *Pkg) WithResourceAndSetters(resourceName string, setterRefs []SetterRef
return p
}

func (p *Pkg) WithFile(name, content string) *Pkg {
p.files[name] = content
return p
}

// WithSubPackages adds the provided packages as subpackages to the current
// package
func (p *Pkg) WithSubPackages(ps ...*Pkg) *Pkg {
Expand Down Expand Up @@ -270,6 +278,21 @@ func buildRecursive(path string, pkg *Pkg) error {
}
}

for name, content := range pkg.files {
filePath := filepath.Join(pkgPath, name)
_, err := os.Stat(filePath)
if err != nil && !os.IsNotExist(err) {
return err
}
if !os.IsNotExist(err) {
return fmt.Errorf("file %s already exists", name)
}
err = ioutil.WriteFile(filePath, []byte(content), 0600)
if err != nil {
return err
}
}

for i := range pkg.subPkgs {
subPkg := pkg.subPkgs[i]
err = buildRecursive(pkgPath, subPkg)
Expand Down

0 comments on commit 4a651ee

Please sign in to comment.