Skip to content

Commit

Permalink
Handle missing and invalid shuttle configuration gracefully (#31)
Browse files Browse the repository at this point in the history
The current implementation panics and logs a stack trace along with a
rather hard to read error message.

This change reports if the file is missing or invalid along with some
guidance on what to do next.

Related to #25
  • Loading branch information
Crevil authored and kaspernissen committed Feb 12, 2019
1 parent 465dfed commit fecd19a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
3 changes: 3 additions & 0 deletions examples/bad/yaml-invalid/shuttle.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
&aplan: '../../station-plan'
vars:
23.1
9 changes: 4 additions & 5 deletions pkg/config/shuttleconfig.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package config

import (
"fmt"
"io/ioutil"
"os"
"path"
Expand Down Expand Up @@ -35,7 +34,7 @@ type ShuttleProjectContext struct {

// Setup the ShuttleProjectContext for a specific path
func (c *ShuttleProjectContext) Setup(projectPath string, uii ui.UI, clean bool, skipGitPlanPulling bool) *ShuttleProjectContext {
c.Config.getConf(projectPath)
c.Config.getConf(uii, projectPath)
c.UI = uii
c.ProjectPath = projectPath
c.LocalShuttleDirectoryPath = path.Join(c.ProjectPath, ".shuttle")
Expand All @@ -60,18 +59,18 @@ func (c *ShuttleProjectContext) Setup(projectPath string, uii ui.UI, clean bool,
}

// getConf loads the ShuttleConfig from yaml file in the project path
func (c *ShuttleConfig) getConf(projectPath string) *ShuttleConfig {
func (c *ShuttleConfig) getConf(uii ui.UI, projectPath string) *ShuttleConfig {
var configPath = path.Join(projectPath, "shuttle.yaml")

//log.Printf("configpath: %s", configPath)

yamlFile, err := ioutil.ReadFile(configPath)
if err != nil {
panic(fmt.Sprintf("yamlFile.Get err #%v ", err))
uii.ExitWithErrorCode(2, "Failed to load shuttle configuration: %s\n\nMake sure you are in a project using shuttle and that a 'shuttle.yaml' file is available.", err)
}
err = yaml.Unmarshal(yamlFile, c)
if err != nil {
panic(fmt.Sprintf("Unmarshal: %v", err))
uii.ExitWithErrorCode(2, "Failed to parse shuttle configuration: %s\n\nMake sure your 'shuttle.yaml' is valid.", err)
}

switch c.PlanRaw {
Expand Down
22 changes: 16 additions & 6 deletions tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ function assertErrorCode() {
fi
}

function assertContains() {
local expectedResult=$1
local actualResult=$2
if [[ ! "$actualResult" =~ "$expectedResult" ]]; then
fail "Expected output to contain '$expectedResult', but it was:\n$actualResult"
fi
}

test_moon_base_builds() {
assertRun -p examples/moon-base run build tag=test
}
Expand All @@ -30,6 +38,7 @@ test_moon_base_builds_with_absolute_path() {

test_venus_base_fails() {
assertErrorCode 2 -p examples/venus-base run build tag=test
assertContains "Failed to load shuttle configuration" "$result"
}

test_can_get_variable_from_local_plan() {
Expand All @@ -44,9 +53,12 @@ test_can_get_variable_from_repo_plan() {

test_fails_getting_no_repo_plan() {
assertErrorCode 4 -p examples/bad/no-repo-project ls
if [[ ! "$result" =~ "Failed executing git command \`clone" ]]; then
fail "Expected output to contain 'Failed executing git command \`clone', but it was:\n$result"
fi
assertContains "Failed executing git command \`clone" "$result"
}

test_fails_loading_invalid_shuttle_configuration() {
assertErrorCode 2 -p examples/bad/yaml-invalid ls
assertContains "Failed to parse shuttle configuration" "$result"
}

test_get_a_boolean() {
Expand Down Expand Up @@ -95,9 +107,7 @@ test_can_execute_shuttle_version_without_error() {

test_run_shell_error_outputs_exit_code() {
assertErrorCode 4 -p examples/moon-base run crash
if [[ ! "$result" =~ "Exit code: 1" ]]; then
fail "Expected output to contain 'Exit code: 1', but it was:\n$result"
fi
assertContains "Exit code: 1" "$result"
}

test_run_shell_error_outputs_script_name() {
Expand Down

0 comments on commit fecd19a

Please sign in to comment.