-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathstdin_console_test.go
More file actions
80 lines (66 loc) · 3.05 KB
/
stdin_console_test.go
File metadata and controls
80 lines (66 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package sqlcmd
import (
"os"
"testing"
"github.com/microsoft/go-sqlcmd/pkg/sqlcmd"
"github.com/stretchr/testify/assert"
)
func TestIsConsoleInitializationRequiredWithRedirectedStdin(t *testing.T) {
// Create a temp file to simulate redirected stdin
tempFile, err := os.CreateTemp("", "stdin-test-*.txt")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
defer os.Remove(tempFile.Name())
defer tempFile.Close()
// Write some data to it
_, err = tempFile.WriteString("SELECT 1;\nGO\n")
if err != nil {
t.Fatalf("Failed to write to temp file: %v", err)
}
// Remember the original stdin
originalStdin := os.Stdin
defer func() { os.Stdin = originalStdin }()
// Test with a file redirection
stdinFile, err := os.Open(tempFile.Name())
if err != nil {
t.Fatalf("Failed to open temp file: %v", err)
}
defer stdinFile.Close()
// Replace stdin with our redirected file
os.Stdin = stdinFile
// Set up a connect settings instance for SQL authentication
connectConfig := sqlcmd.ConnectSettings{
UserName: "testuser", // This will trigger SQL authentication, requiring a password
}
// Test regular args
args := &SQLCmdArguments{}
// Print file stat mode for debugging
fileStat, _ := os.Stdin.Stat()
t.Logf("File mode: %v", fileStat.Mode())
t.Logf("Is character device: %v", (fileStat.Mode()&os.ModeCharDevice) != 0)
t.Logf("Connection config: %+v", connectConfig)
t.Logf("RequiresPassword() returns: %v", connectConfig.RequiresPassword())
// Test with SQL authentication that requires a password
needsConsole, isInteractive := isConsoleInitializationRequired(&connectConfig, args)
// Should need console since password is required, but not be interactive
assert.True(t, needsConsole, "Console should be needed when SQL authentication is used")
assert.False(t, isInteractive, "Should not be interactive mode with redirected stdin")
// Now test with no authentication (no password required)
connectConfig = sqlcmd.ConnectSettings{}
needsConsole, isInteractive = isConsoleInitializationRequired(&connectConfig, args)
// Should not need console and not be interactive
assert.False(t, needsConsole, "Console should not be needed with redirected stdin and no password")
assert.False(t, isInteractive, "Should not be interactive mode with redirected stdin")
// Test with direct terminal input (simulated by restoring original stdin)
os.Stdin = originalStdin
connectConfig = sqlcmd.ConnectSettings{} // No password needed
needsConsole, isInteractive = isConsoleInitializationRequired(&connectConfig, args)
// If no input file or query is specified, it should be interactive mode
assert.Equal(t, args.InputFile == nil && args.Query == "" && len(args.ChangePasswordAndExit) == 0, needsConsole,
"Console needs should match interactive mode requirements with terminal stdin")
assert.Equal(t, args.InputFile == nil && args.Query == "" && len(args.ChangePasswordAndExit) == 0, isInteractive,
"Interactive mode should be true with terminal stdin and no input files or queries")
}