Skip to content

Commit

Permalink
Raise error for missing input file (#104)
Browse files Browse the repository at this point in the history
* Raise error for missing input file
* Fix testcase issue seen in test pipeline
  • Loading branch information
apoorvdeshmukh authored Jul 12, 2022
1 parent e6b1b9e commit 4e0b95c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
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))
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

0 comments on commit 4e0b95c

Please sign in to comment.