Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
(GH-342) Refactor build.Build() for generic input
Browse files Browse the repository at this point in the history
Prior to this commit the `build.Build()` function took `templatePath` as
the input for the directory to attempt to package and referenced the
"template" in both comments and messages.

This commit updates the function to instead take `sourceDir` as the
input for the directory to attempt to package and removes all references
to "template" from comments and messages, replacing it where coherent
with "project"

This further genericizes the build package for external library usage.
  • Loading branch information
michaeltlombardi committed Feb 14, 2022
1 parent c63c191 commit 33c4718
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 76 deletions.
32 changes: 16 additions & 16 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

type BuilderI interface {
Build(templatePath, targetDir string) (gzipArchiveFilePath string, err error)
Build(sourceDir, targetDir string) (gzipArchiveFilePath string, err error)
}

type Builder struct {
Expand All @@ -24,46 +24,46 @@ type Builder struct {
ConfigFile string
}

func (b *Builder) Build(templatePath, targetDir string) (gzipArchiveFilePath string, err error) {
// Check template dir exists
if _, err := b.AFS.Stat(templatePath); os.IsNotExist(err) {
return "", fmt.Errorf("No template directory at %v", templatePath)
func (b *Builder) Build(sourceDir, targetDir string) (gzipArchiveFilePath string, err error) {
// Check project dir exists
if _, err := b.AFS.Stat(sourceDir); os.IsNotExist(err) {
return "", fmt.Errorf("No project directory at %v", sourceDir)
}

// Check if config file exists
if _, err := b.AFS.Stat(filepath.Join(templatePath, b.ConfigFile)); os.IsNotExist(err) {
return "", fmt.Errorf("No '%v' found in %v", b.ConfigFile, templatePath)
if _, err := b.AFS.Stat(filepath.Join(sourceDir, b.ConfigFile)); os.IsNotExist(err) {
return "", fmt.Errorf("No '%v' found in %v", b.ConfigFile, sourceDir)
}

err = b.ConfigProcessor.CheckConfig(filepath.Join(templatePath, b.ConfigFile))
err = b.ConfigProcessor.CheckConfig(filepath.Join(sourceDir, b.ConfigFile))
if err != nil {
return "", fmt.Errorf("Invalid config: %v", err.Error())
}

// Check if content dir exists
if _, err := b.AFS.Stat(filepath.Join(templatePath, "content")); os.IsNotExist(err) {
return "", fmt.Errorf("No 'content' dir found in %v", templatePath)
if _, err := b.AFS.Stat(filepath.Join(sourceDir, "content")); os.IsNotExist(err) {
return "", fmt.Errorf("No 'content' dir found in %v", sourceDir)
}

// Create temp dir and TAR template there
// Create temp dir and TAR project there
tempDir, err := b.AFS.TempDir("", "")
defer os.Remove(tempDir)

if err != nil {
log.Error().Msgf("Could not create tempdir to TAR template: %v", err)
log.Error().Msgf("Could not create tempdir to TAR project: %v", err)
return "", err
}

tarArchiveFilePath, err := b.Tar.Tar(templatePath, tempDir)
tarArchiveFilePath, err := b.Tar.Tar(sourceDir, tempDir)
if err != nil {
log.Error().Msgf("Could not TAR template (%v): %v", templatePath, err)
log.Error().Msgf("Could not TAR project (%v): %v", sourceDir, err)
return "", err
}

// GZIP the TAR created in the temp dir and output to the $MODULE_ROOT/pkg directory
// GZIP the TAR created in the temp dir and output to the /pkg directory in the target directory
gzipArchiveFilePath, err = b.Gzip.Gzip(tarArchiveFilePath, targetDir)
if err != nil {
log.Error().Msgf("Could not GZIP template TAR archive (%v): %v", tarArchiveFilePath, err)
log.Error().Msgf("Could not GZIP project TAR archive (%v): %v", tarArchiveFilePath, err)
return "", err
}

Expand Down
120 changes: 60 additions & 60 deletions pkg/build/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import (
func TestBuild(t *testing.T) {

type args struct {
templatePath string
targetDir string
projectPath string
targetDir string
}

var mockTemplateDir = "/path/to/my/cool-template"
var mockConfigFilePath = filepath.Clean(filepath.Join(mockTemplateDir, "pct-config.yml"))
var mockSourceDir = "/path/to/my/cool-project"
var mockConfigFilePath = filepath.Clean(filepath.Join(mockSourceDir, "my-config.yml"))

tests := []struct {
name string
Expand All @@ -37,58 +37,58 @@ func TestBuild(t *testing.T) {
testTempDir string
}{
{
name: "Should return err if template path does not exist",
name: "Should return err if project folder path does not exist",
args: args{
templatePath: mockTemplateDir,
targetDir: mockTemplateDir,
projectPath: mockSourceDir,
targetDir: mockSourceDir,
},
expectedFilePath: "",
expectedErr: "No template directory at /path/to/my/cool-template",
expectedErr: "No project directory at /path/to/my/cool-project",
},
{
name: "Should return err if template path does not contain pct-config.yml",
name: "Should return err if project path does not contain my-config.yml",
args: args{
templatePath: mockTemplateDir,
targetDir: mockTemplateDir,
projectPath: mockSourceDir,
targetDir: mockSourceDir,
},
mockDirs: []string{
mockTemplateDir,
mockSourceDir,
},
expectedFilePath: "",
expectedErr: "No 'pct-config.yml' found in /path/to/my/cool-template",
expectedErr: "No 'my-config.yml' found in /path/to/my/cool-project",
},
{
name: "Should return err if content dir does not exist",
args: args{
templatePath: mockTemplateDir,
targetDir: mockTemplateDir,
projectPath: mockSourceDir,
targetDir: mockSourceDir,
},
mockDirs: []string{
mockTemplateDir,
mockSourceDir,
},
mockFiles: map[string]string{
filepath.Clean(filepath.Join(mockTemplateDir, "pct-config.yml")): `---
filepath.Clean(filepath.Join(mockSourceDir, "my-config.yml")): `---
template:
id: builder
author: puppetlabs
version: 1.0.0
`,
},
expectedFilePath: "",
expectedErr: "No 'content' dir found in /path/to/my/cool-template",
expectedErr: "No 'content' dir found in /path/to/my/cool-project",
},
{
name: "Should not attempt to GZIP when TAR operation fails",
args: args{
templatePath: mockTemplateDir,
targetDir: mockTemplateDir,
projectPath: mockSourceDir,
targetDir: mockSourceDir,
},
mockDirs: []string{
mockTemplateDir,
filepath.Join(mockTemplateDir, "content"),
mockSourceDir,
filepath.Join(mockSourceDir, "content"),
},
mockFiles: map[string]string{
filepath.Clean(filepath.Join(mockTemplateDir, "pct-config.yml")): `---
filepath.Clean(filepath.Join(mockSourceDir, "my-config.yml")): `---
template:
id: builder
author: puppetlabs
Expand All @@ -102,20 +102,20 @@ template:
{
name: "Should return error and empty path if GZIP operation fails",
args: args{
templatePath: mockTemplateDir,
targetDir: mockTemplateDir,
projectPath: mockSourceDir,
targetDir: mockSourceDir,
},
mockFiles: map[string]string{
filepath.Clean(filepath.Join(mockTemplateDir, "pct-config.yml")): `---
filepath.Clean(filepath.Join(mockSourceDir, "my-config.yml")): `---
template:
id: builder
author: puppetlabs
version: 1.0.0
`,
},
mockDirs: []string{
mockTemplateDir,
filepath.Join(mockTemplateDir, "content"),
mockSourceDir,
filepath.Join(mockSourceDir, "content"),
},
tarFile: "/path/to/nowhere/pkg/nowhere.tar",
expectedFilePath: "",
Expand All @@ -124,17 +124,17 @@ template:
mockGzipErr: true,
},
{
name: "Should TAR.GZ valid template to $MODULE_ROOT/pkg and return path",
name: "Should TAR.GZ valid project to $MODULE_ROOT/pkg and return path",
args: args{
templatePath: mockTemplateDir,
targetDir: mockTemplateDir,
projectPath: mockSourceDir,
targetDir: mockSourceDir,
},
mockDirs: []string{
mockTemplateDir,
filepath.Join(mockTemplateDir, "content"),
mockSourceDir,
filepath.Join(mockSourceDir, "content"),
},
mockFiles: map[string]string{
filepath.Clean(filepath.Join(mockTemplateDir, "pct-config.yml")): `---
filepath.Clean(filepath.Join(mockSourceDir, "my-config.yml")): `---
template:
id: builder
author: puppetlabs
Expand All @@ -147,17 +147,17 @@ template:
mockTarErr: false,
},
{
name: "Should complain that `id` is missing from pct-config.yml",
name: "Should complain that `id` is missing from my-config.yml",
args: args{
templatePath: mockTemplateDir,
targetDir: mockTemplateDir,
projectPath: mockSourceDir,
targetDir: mockSourceDir,
},
mockDirs: []string{
mockTemplateDir,
filepath.Join(mockTemplateDir, "content"),
mockSourceDir,
filepath.Join(mockSourceDir, "content"),
},
mockFiles: map[string]string{
filepath.Clean(filepath.Join(mockTemplateDir, "pct-config.yml")): `---
filepath.Clean(filepath.Join(mockSourceDir, "my-config.yml")): `---
template:
author: puppetlabs
version: 1.0.0
Expand All @@ -167,17 +167,17 @@ template:
mockTarErr: false,
},
{
name: "Should complain that `author` is missing from pct-config.yml",
name: "Should complain that `author` is missing from my-config.yml",
args: args{
templatePath: mockTemplateDir,
targetDir: mockTemplateDir,
projectPath: mockSourceDir,
targetDir: mockSourceDir,
},
mockDirs: []string{
mockTemplateDir,
filepath.Join(mockTemplateDir, "content"),
mockSourceDir,
filepath.Join(mockSourceDir, "content"),
},
mockFiles: map[string]string{
filepath.Clean(filepath.Join(mockTemplateDir, "pct-config.yml")): `---
filepath.Clean(filepath.Join(mockSourceDir, "my-config.yml")): `---
template:
id: builder
version: 1.0.0
Expand All @@ -187,17 +187,17 @@ template:
mockTarErr: false,
},
{
name: "Should complain that `version` is missing from pct-config.yml",
name: "Should complain that `version` is missing from my-config.yml",
args: args{
templatePath: mockTemplateDir,
targetDir: mockTemplateDir,
projectPath: mockSourceDir,
targetDir: mockSourceDir,
},
mockDirs: []string{
mockTemplateDir,
filepath.Join(mockTemplateDir, "content"),
mockSourceDir,
filepath.Join(mockSourceDir, "content"),
},
mockFiles: map[string]string{
filepath.Clean(filepath.Join(mockTemplateDir, "pct-config.yml")): `---
filepath.Clean(filepath.Join(mockSourceDir, "my-config.yml")): `---
template:
id: builder
author: puppetlabs
Expand All @@ -207,17 +207,17 @@ template:
mockTarErr: false,
},
{
name: "Should complain all required are missing from pct-config.yml",
name: "Should complain all required are missing from my-config.yml",
args: args{
templatePath: mockTemplateDir,
targetDir: mockTemplateDir,
projectPath: mockSourceDir,
targetDir: mockSourceDir,
},
mockDirs: []string{
mockTemplateDir,
filepath.Join(mockTemplateDir, "content"),
mockSourceDir,
filepath.Join(mockSourceDir, "content"),
},
mockFiles: map[string]string{
filepath.Clean(filepath.Join(mockTemplateDir, "pct-config.yml")): `---
filepath.Clean(filepath.Join(mockSourceDir, "my-config.yml")): `---
template:
foo: bar
`,
Expand Down Expand Up @@ -246,10 +246,10 @@ template:
&mock.Gzip{ReturnedPath: tt.gzipFile, ErrResponse: tt.mockGzipErr},
afs,
&pct_config_processor.PctConfigProcessor{AFS: afs},
"pct-config.yml",
"my-config.yml",
}

gotGzipArchiveFilePath, err := p.Build(tt.args.templatePath, tt.args.targetDir)
gotGzipArchiveFilePath, err := p.Build(tt.args.projectPath, tt.args.targetDir)
if (err != nil) && tt.expectedErr != "" {
assert.Equal(t, tt.expectedErr, err.Error())
return
Expand Down

0 comments on commit 33c4718

Please sign in to comment.