Skip to content

Commit 5d3d2ed

Browse files
Add integration test for -q with -i execution order
1 parent d625c0e commit 5d3d2ed

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

cmd/sqlcmd/sqlcmd_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,33 @@ func TestRunInputFiles(t *testing.T) {
273273
}
274274
}
275275

276+
// TestInitialQueryWithInputFile verifies that -q (initial query) executes before -i (input files)
277+
func TestInitialQueryWithInputFile(t *testing.T) {
278+
o, err := os.CreateTemp("", "sqlcmdmain")
279+
assert.NoError(t, err, "os.CreateTemp")
280+
defer os.Remove(o.Name())
281+
defer o.Close()
282+
args = newArguments()
283+
// Use -q to change session language, then -i to verify the setting persists
284+
// The initial query sets LANGUAGE to German, then the script selects @@LANGUAGE
285+
args.InitialQuery = "SET LANGUAGE German"
286+
args.InputFile = []string{"testdata/select_init_value.sql"}
287+
args.OutputFile = o.Name()
288+
setAzureAuthArgIfNeeded(&args)
289+
vars := sqlcmd.InitializeVariables(args.useEnvVars())
290+
vars.Set(sqlcmd.SQLCMDMAXVARTYPEWIDTH, "0")
291+
setVars(vars, &args)
292+
293+
exitCode, err := run(vars, &args)
294+
assert.NoError(t, err, "run")
295+
assert.Equal(t, 0, exitCode, "exitCode")
296+
bytes, err := os.ReadFile(o.Name())
297+
if assert.NoError(t, err, "os.ReadFile") {
298+
// Verify that the language set in the initial query is reflected in the script output
299+
assert.Contains(t, string(bytes), "Deutsch", "Initial query should execute before input file")
300+
}
301+
}
302+
276303
func TestUnicodeOutput(t *testing.T) {
277304
o, err := os.CreateTemp("", "sqlcmdmain")
278305
assert.NoError(t, err, "os.CreateTemp")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
select @@LANGUAGE

0 commit comments

Comments
 (0)