|
1 | 1 | package utils |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "fmt" |
5 | 6 | "os" |
6 | 7 | "strings" |
7 | 8 | "testing" |
8 | 9 |
|
9 | 10 | qt "github.com/frankban/quicktest" |
10 | 11 |
|
11 | | - "github.com/libsql/libsql-shell-go/internal/cmd" |
| 12 | + "github.com/libsql/libsql-shell-go/internal/db" |
| 13 | + "github.com/libsql/libsql-shell-go/internal/shell" |
| 14 | + "github.com/libsql/libsql-shell-go/pkg/shell/enums" |
12 | 15 | ) |
13 | 16 |
|
14 | 17 | type DbTestContext struct { |
15 | 18 | *testing.T |
16 | 19 | *qt.C |
17 | 20 |
|
18 | 21 | dbPath string |
| 22 | + |
| 23 | + db *db.Db |
19 | 24 | } |
20 | 25 |
|
21 | 26 | func NewTestContext(t *testing.T, dbPath string) *DbTestContext { |
22 | | - return &DbTestContext{T: t, C: qt.New(t), dbPath: dbPath} |
| 27 | + db, err := db.NewDb(dbPath) |
| 28 | + if err != nil { |
| 29 | + t.Fatalf("Fail to create new db") |
| 30 | + } |
| 31 | + |
| 32 | + return &DbTestContext{T: t, C: qt.New(t), dbPath: dbPath, db: db} |
| 33 | +} |
| 34 | + |
| 35 | +func (tc *DbTestContext) Close() { |
| 36 | + tc.db.Close() |
23 | 37 | } |
24 | 38 |
|
25 | 39 | func (tc *DbTestContext) Execute(statements string) (string, string, error) { |
26 | | - rootCmd := cmd.NewRootCmd() |
27 | | - return Execute(tc.T, rootCmd, "--exec", statements, tc.dbPath) |
| 40 | + bufOut, bufErr, config := tc.createShellConfig("") |
| 41 | + shellInstance, err := shell.NewShell(config, tc.db) |
| 42 | + if err != nil { |
| 43 | + tc.T.Fatalf("Fail to create new shell") |
| 44 | + } |
| 45 | + |
| 46 | + executionError := shellInstance.ExecuteCommandOrStatements(statements) |
| 47 | + return strings.TrimSpace(bufOut.String()), strings.TrimSpace(bufErr.String()), executionError |
28 | 48 | } |
29 | 49 |
|
30 | 50 | func (tc *DbTestContext) ExecuteShell(commands []string) (outS string, errS string, err error) { |
31 | | - rootCmd := cmd.NewRootCmd() |
32 | | - return ExecuteWithInitialInput(tc.T, rootCmd, strings.Join(commands, "\n"), tc.dbPath, "--quiet") |
| 51 | + bufOut, bufErr, config := tc.createShellConfig(strings.Join(commands, "\n")) |
| 52 | + shellInstance, err := shell.NewShell(config, tc.db) |
| 53 | + if err != nil { |
| 54 | + tc.T.Fatalf("Fail to create new shell") |
| 55 | + } |
| 56 | + |
| 57 | + executionError := shellInstance.Run() |
| 58 | + return strings.TrimSpace(bufOut.String()), strings.TrimSpace(bufErr.String()), executionError |
| 59 | +} |
| 60 | + |
| 61 | +func (tc *DbTestContext) createShellConfig(initialInput string) (bufOut *bytes.Buffer, bufErr *bytes.Buffer, config shell.ShellConfig) { |
| 62 | + bufOut = new(bytes.Buffer) |
| 63 | + bufErr = new(bytes.Buffer) |
| 64 | + bufIn := new(bytes.Buffer) |
| 65 | + |
| 66 | + _, err := bufIn.Write([]byte(initialInput)) |
| 67 | + if err != nil { |
| 68 | + tc.T.Fatalf("Fail to write inside initial buffer") |
| 69 | + } |
| 70 | + |
| 71 | + config = shell.ShellConfig{InF: bufIn, OutF: bufOut, ErrF: bufErr, HistoryMode: enums.SingleHistory, QuietMode: true} |
| 72 | + return |
33 | 73 | } |
34 | 74 |
|
35 | 75 | func (tc *DbTestContext) CreateEmptySimpleTable(tableName string) { |
|
0 commit comments