From 33c471890dfed89c6b1aa8eb4cc0a80ff0b1ec09 Mon Sep 17 00:00:00 2001 From: Michael T Lombardi Date: Mon, 14 Feb 2022 08:53:28 -0600 Subject: [PATCH] (GH-342) Refactor build.Build() for generic input 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. --- pkg/build/build.go | 32 +++++------ pkg/build/build_test.go | 120 ++++++++++++++++++++-------------------- 2 files changed, 76 insertions(+), 76 deletions(-) diff --git a/pkg/build/build.go b/pkg/build/build.go index 9ec45844..9e830204 100644 --- a/pkg/build/build.go +++ b/pkg/build/build.go @@ -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 { @@ -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 } diff --git a/pkg/build/build_test.go b/pkg/build/build_test.go index 992828e8..fb57dba8 100644 --- a/pkg/build/build_test.go +++ b/pkg/build/build_test.go @@ -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 @@ -37,37 +37,37 @@ 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 @@ -75,20 +75,20 @@ template: `, }, 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 @@ -102,11 +102,11 @@ 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 @@ -114,8 +114,8 @@ template: `, }, mockDirs: []string{ - mockTemplateDir, - filepath.Join(mockTemplateDir, "content"), + mockSourceDir, + filepath.Join(mockSourceDir, "content"), }, tarFile: "/path/to/nowhere/pkg/nowhere.tar", expectedFilePath: "", @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 `, @@ -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