Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/sqlcmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the :r command handler print the error returned by IncludeFile too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. This is handled here.

s.Exitcode = 1
break
}
}
Expand Down
17 changes: 17 additions & 0 deletions cmd/sqlcmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/sqlcmd/sqlcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down