Skip to content

Commit 4e0b95c

Browse files
Raise error for missing input file (#104)
* Raise error for missing input file * Fix testcase issue seen in test pipeline
1 parent e6b1b9e commit 4e0b95c

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

cmd/sqlcmd/main.go

+2
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ func run(vars *sqlcmd.Variables, args *SQLCmdArguments) (int, error) {
266266
} else {
267267
for f := range args.InputFile {
268268
if err = s.IncludeFile(args.InputFile[f], true); err != nil {
269+
_, _ = os.Stderr.Write([]byte(err.Error() + sqlcmd.SqlcmdEol))
270+
s.Exitcode = 1
269271
break
270272
}
271273
}

cmd/sqlcmd/main_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,23 @@ func TestAzureAuth(t *testing.T) {
310310
}
311311
}
312312

313+
func TestMissingInputFile(t *testing.T) {
314+
args = newArguments()
315+
args.InputFile = []string{"testdata/missingFile.sql"}
316+
317+
if canTestAzureAuth() {
318+
args.UseAad = true
319+
}
320+
321+
vars := sqlcmd.InitializeVariables(!args.DisableCmdAndWarn)
322+
setVars(vars, &args)
323+
324+
exitCode, err := run(vars, &args)
325+
assert.Error(t, err, "run")
326+
assert.Contains(t, err.Error(), "Error occurred while opening or operating on file", "Unexpected error: "+err.Error())
327+
assert.Equal(t, 1, exitCode, "exitCode")
328+
}
329+
313330
// Assuming public Azure, use AAD when SQLCMDUSER environment variable is not set
314331
func canTestAzureAuth() bool {
315332
server := os.Getenv(sqlcmd.SQLCMDSERVER)

pkg/sqlcmd/sqlcmd.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ func (s *Sqlcmd) promptPassword() (string, error) {
286286
func (s *Sqlcmd) IncludeFile(path string, processAll bool) error {
287287
f, err := os.Open(path)
288288
if err != nil {
289-
return err
289+
return InvalidFileError(err, path)
290290
}
291291
defer f.Close()
292292
b := s.batch.batchline

0 commit comments

Comments
 (0)