Skip to content
This repository has been archived by the owner on Jan 14, 2020. It is now read-only.

Commit

Permalink
Add more unit tests to plugin manager (#44)
Browse files Browse the repository at this point in the history
Add more unit tests to plugin manager
  • Loading branch information
ojkelly committed Jun 13, 2018
1 parent e17871a commit 95b943e
Show file tree
Hide file tree
Showing 16 changed files with 2,963 additions and 58 deletions.
36 changes: 33 additions & 3 deletions internal/cloudformation/parameters_test.go
Expand Up @@ -78,7 +78,16 @@ func TestResolveEnvironmentParameters(t *testing.T) {
for i, test := range tests {
assert := assert.New(t)
testOutput := resolveEnvironmentParameters(&test.manifest, test.environment)
assert.Equal(test.output, testOutput, fmt.Sprintf("Test %d: %s", i, test.name))

matches := 0
for key, value := range testOutput {
for paramKey, paramVal := range test.output {
if key == paramKey && value == paramVal {
matches = matches + 1
}
}
}
assert.Equal(len(test.output), matches, fmt.Sprintf("Test %d: %s", i, test.name))
}
}

Expand Down Expand Up @@ -186,7 +195,17 @@ func TestResolveParameters(t *testing.T) {
test.input.cfYaml,
test.input.manifestFile,
)
assert.Equal(test.output, testOutput, fmt.Sprintf("Test %d: %s", i, test.name))
matches := 0
for _, outputParam := range testOutput {
for _, param := range test.output {
if *outputParam.ParameterKey == *param.ParameterKey &&
*outputParam.ParameterValue == *param.ParameterValue {
matches = matches + 1
}
}
}

assert.Equal(len(test.output), matches, fmt.Sprintf("Test %d: %s", i, test.name))
}
}

Expand Down Expand Up @@ -273,7 +292,18 @@ func TestResolveParametersS3(t *testing.T) {
test.input.ctx,
test.input.manifestFile,
)
assert.Equal(test.output, testOutput, fmt.Sprintf("Test %d: %s", i, test.name))

matches := 0
for _, outputParam := range testOutput {
for _, param := range test.output {
if *outputParam.ParameterKey == *param.ParameterKey &&
*outputParam.ParameterValue == *param.ParameterValue {
matches = matches + 1
}
}
}

assert.Equal(len(test.output), matches, fmt.Sprintf("Test %d: %s", i, test.name))
}
}

Expand Down
7 changes: 3 additions & 4 deletions internal/manifest/init.go
Expand Up @@ -4,8 +4,7 @@ import (
"fmt"
"strings"

"github.com/KablamoOSS/go-cli-printer"

printer "github.com/KablamoOSS/go-cli-printer"
"gopkg.in/AlecAivazis/survey.v1"
)

Expand All @@ -14,8 +13,8 @@ func InitaliseNewManifest() error {
// TODO: Check if there is a manifest file and exit

// Load the manifest file from this directory
manifestSearch := FindAndLoadManifest()
if &manifestSearch != nil {
manifestExists := CheckManifestExists()
if manifestExists {
printer.Fatal(
fmt.Errorf("Sorry we can't create a new kombustion.yaml, one already exists."),
"If you want to re-initialise your kombustion.yaml file, first remove it.",
Expand Down
13 changes: 13 additions & 0 deletions internal/manifest/manifest.go
Expand Up @@ -46,6 +46,19 @@ func FindAndLoadManifest() *Manifest {
return loadedManifest
}

// CheckManifestExists to determine if there is a manifest in the current dir
func CheckManifestExists() bool {
path, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
return false
}
_, err = findAndLoadManifest(path)
if err != nil {
return false
}
return true
}

// findAndLoadManifest - Search the given directory for a manifest file, and load it
// This is seperated to allow for easy testing
func findAndLoadManifest(path string) (Manifest, error) {
Expand Down
72 changes: 46 additions & 26 deletions internal/plugins/add.go
Expand Up @@ -13,31 +13,30 @@ import (
"github.com/google/go-github/github"
)

var githubClient *github.Client

func init() {
githubClient = github.NewClient(nil)
}

// AddPluginsToManifest - Add all new plugin to the manifest
// update it if it's already there
// then write the manifest to disk
func AddPluginsToManifest(manifest *manifestType.Manifest, pluginLocations []string) (*manifestType.Manifest, error) {
printer.Progress("Kombusting")

// Get the lockFile
lockFile, err := lock.FindAndLoadLock()

for _, pluginLocation := range pluginLocations {
plugin, pluginLock, err := constructGithubPlugin(manifest, pluginLocation)
printer.SubStep(fmt.Sprintf("Adding plugin: %s", plugin.Name), 2, true)
if err != nil {
return manifest, err
}
if manifest.Plugins == nil {
manifest.Plugins = make(map[string]manifestType.Plugin)
}
if lockFile.Plugins == nil {
lockFile.Plugins = make(map[string]lock.Plugin)
}
manifest.Plugins[fmt.Sprintf("%s@%s", plugin.Name, plugin.Version)] = plugin
lockFile.Plugins[fmt.Sprintf("%s@%s", plugin.Name, plugin.Version)] = pluginLock
// Add all the plugins to the manifest and lockfile
manifest, lockFile, err = addPluginsToManifestAndLock(manifest, lockFile, pluginLocations)
if err != nil {
printer.Error(err, config.ErrorHelpInfo, "")
return manifest, err
}

err = updatePluginInManifest(manifest)
printer.Progress("Updating manifest")
err = manifestType.WriteManifestToDisk(manifest)
if err != nil {
printer.Error(err, config.ErrorHelpInfo, "")
return manifest, err
Expand All @@ -52,14 +51,31 @@ func AddPluginsToManifest(manifest *manifestType.Manifest, pluginLocations []str
return manifest, nil
}

// updatePluginInManifest - Write a new manifest to disk
func updatePluginInManifest(manifest *manifestType.Manifest) error {
printer.Progress("Updating manifest")
err := manifestType.WriteManifestToDisk(manifest)
if err != nil {
return err
func addPluginsToManifestAndLock(
manifest *manifestType.Manifest,
lockFile *lock.Lock,
pluginLocations []string,
) (
*manifestType.Manifest,
*lock.Lock,
error,
) {
for _, pluginLocation := range pluginLocations {
plugin, pluginLock, err := constructGithubPlugin(manifest, pluginLocation)
printer.SubStep(fmt.Sprintf("Adding plugin: %s", plugin.Name), 2, true)
if err != nil {
return manifest, lockFile, err
}
if manifest.Plugins == nil {
manifest.Plugins = make(map[string]manifestType.Plugin)
}
if lockFile.Plugins == nil {
lockFile.Plugins = make(map[string]lock.Plugin)
}
manifest.Plugins[fmt.Sprintf("%s@%s", plugin.Name, plugin.Version)] = plugin
lockFile.Plugins[fmt.Sprintf("%s@%s", plugin.Name, plugin.Version)] = pluginLock
}
return nil
return manifest, lockFile, nil
}

// constructGithubPlugin - Create a plugin based on a github url
Expand Down Expand Up @@ -130,12 +146,16 @@ func constructGithubPlugin(
}

// getLatestRelease - Return the latest release of the repository
func getLatestRelease(githubOrg string, githubProject string) (latestRelease *github.RepositoryRelease, err error) {

client := github.NewClient(nil)
func getLatestRelease(
githubOrg string,
githubProject string,
) (
latestRelease *github.RepositoryRelease,
err error,
) {

// Get latest release
latestRelease, _, err = client.Repositories.GetLatestRelease(
latestRelease, _, err = githubClient.Repositories.GetLatestRelease(
context.Background(),
githubOrg,
githubProject,
Expand Down

0 comments on commit 95b943e

Please sign in to comment.