diff --git a/examples/bad/yaml-invalid/shuttle.yaml b/examples/bad/yaml-invalid/shuttle.yaml new file mode 100644 index 0000000..87e9f5e --- /dev/null +++ b/examples/bad/yaml-invalid/shuttle.yaml @@ -0,0 +1,3 @@ +&aplan: '../../station-plan' +vars: + 23.1 diff --git a/pkg/config/shuttleconfig.go b/pkg/config/shuttleconfig.go index 0b47854..3249f22 100644 --- a/pkg/config/shuttleconfig.go +++ b/pkg/config/shuttleconfig.go @@ -1,7 +1,6 @@ package config import ( - "fmt" "io/ioutil" "os" "path" @@ -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") @@ -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 { diff --git a/tests.sh b/tests.sh index 89e59b0..4f4c80d 100755 --- a/tests.sh +++ b/tests.sh @@ -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 } @@ -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() { @@ -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() { @@ -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() {