diff --git a/cmd/sqlcmd/main.go b/cmd/sqlcmd/main.go index f9a893be..2cd5bff9 100644 --- a/cmd/sqlcmd/main.go +++ b/cmd/sqlcmd/main.go @@ -266,6 +266,8 @@ func run(vars *sqlcmd.Variables, args *SQLCmdArguments) (int, error) { } else { for f := range args.InputFile { if err = s.IncludeFile(args.InputFile[f], true); err != nil { + _, _ = os.Stderr.Write([]byte(err.Error() + sqlcmd.SqlcmdEol)) + s.Exitcode = 1 break } } diff --git a/cmd/sqlcmd/main_test.go b/cmd/sqlcmd/main_test.go index cd292464..ea13dbee 100644 --- a/cmd/sqlcmd/main_test.go +++ b/cmd/sqlcmd/main_test.go @@ -310,6 +310,23 @@ func TestAzureAuth(t *testing.T) { } } +func TestMissingInputFile(t *testing.T) { + args = newArguments() + args.InputFile = []string{"testdata/missingFile.sql"} + + if canTestAzureAuth() { + args.UseAad = true + } + + vars := sqlcmd.InitializeVariables(!args.DisableCmdAndWarn) + setVars(vars, &args) + + exitCode, err := run(vars, &args) + assert.Error(t, err, "run") + assert.Contains(t, err.Error(), "Error occurred while opening or operating on file", "Unexpected error: "+err.Error()) + assert.Equal(t, 1, exitCode, "exitCode") +} + // Assuming public Azure, use AAD when SQLCMDUSER environment variable is not set func canTestAzureAuth() bool { server := os.Getenv(sqlcmd.SQLCMDSERVER) diff --git a/pkg/sqlcmd/sqlcmd.go b/pkg/sqlcmd/sqlcmd.go index a4f9ffdf..a5f0192f 100644 --- a/pkg/sqlcmd/sqlcmd.go +++ b/pkg/sqlcmd/sqlcmd.go @@ -286,7 +286,7 @@ func (s *Sqlcmd) promptPassword() (string, error) { func (s *Sqlcmd) IncludeFile(path string, processAll bool) error { f, err := os.Open(path) if err != nil { - return err + return InvalidFileError(err, path) } defer f.Close() b := s.batch.batchline