Skip to content

Commit cf7fab1

Browse files
authored
Merge pull request #7 from hymkor/go-test-v
Migrate SQLite3 tests from PowerShell 7 to Go’s standard go test framework
2 parents c136287 + e73a125 commit cf7fab1

File tree

7 files changed

+94
-12
lines changed

7 files changed

+94
-12
lines changed

Makefile

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@ all:
2626
$(SET) "CGO_ENABLED=0" && $(GO) build $(GOOPT) && $(GO) build -C "$(CURDIR)/cmd/sqlbless" -o "$(CURDIR)/$(NAME)$(EXE)" $(GOOPT)
2727

2828
test:
29-
ifeq ($(OS),Windows_NT)
30-
pwsh "test/test-sqlite3.ps1"
31-
# pwsh "test/test.ps1"
32-
endif
33-
$(GO) test -v
29+
$(GO) test -v ./...
3430

3531
_dist:
3632
$(MAKE) all

interactive.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ func (i *interactiveIn) Read(ctx context.Context) ([]string, error) {
9494
w.Flush()
9595
return []string{}, nil
9696
}
97-
return lines, err
97+
if err != nil {
98+
return nil, fmt.Errorf("interactiveIn.Read: %w", err)
99+
}
100+
return lines, nil
98101
}
99102

100103
func (i *interactiveIn) GetKey() (string, error) {

loop.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (ss *session) Loop(ctx context.Context, commandIn commandIn) error {
103103
}
104104
return nil
105105
}
106-
return err
106+
return fmt.Errorf("commandIn.Read: %w", err)
107107
}
108108
queryAndTerm := strings.Join(lines, "\n")
109109
query, _ := misc.HasTerm(queryAndTerm, ss.Term)
@@ -262,11 +262,11 @@ func (cfg *Config) Run(driver, dataSourceName string, dbDialect *dialect.Entry)
262262
defer db.Close()
263263

264264
if err = db.Ping(); err != nil {
265-
return err
265+
return fmt.Errorf("db.Ping: %w", err)
266266
}
267267
conn, err := db.Conn(ctx)
268268
if err != nil {
269-
return err
269+
return fmt.Errorf("db.Conn: %w", err)
270270
}
271271
defer conn.Close()
272272

@@ -287,9 +287,8 @@ func (cfg *Config) Run(driver, dataSourceName string, dbDialect *dialect.Entry)
287287
return ss.Start(ctx, cfg.Script)
288288
}
289289

290-
if !term.IsTerminal(int(os.Stdin.Fd())) {
290+
if !term.IsTerminal(int(os.Stdin.Fd())) && !ss.automatic() {
291291
return ss.StartFromStdin(ctx)
292292
}
293-
294293
return ss.Loop(ctx, ss.newInteractiveIn())
295294
}

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func Run() error {
8686
}
8787
d, err := dialect.ReadDBInfoFromArgs(args)
8888
if err != nil {
89-
return err
89+
return fmt.Errorf("dialect.ReadDBInfoFromArgs: %w", err)
9090
}
9191
if cfg.Debug {
9292
fmt.Fprintln(os.Stderr, d.DataSource)

main_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package sqlbless
2+
3+
import (
4+
"bufio"
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
"testing"
9+
10+
"github.com/hymkor/sqlbless/dialect"
11+
_ "github.com/hymkor/sqlbless/dialect/sqlite"
12+
)
13+
14+
func disableColor() (restore func()) {
15+
if noColor, ok := os.LookupEnv("NO_COLOR"); ok {
16+
restore = func() { os.Setenv("NO_COLOR", noColor) }
17+
} else {
18+
restore = func() { os.Unsetenv("NO_COLOR") }
19+
}
20+
os.Setenv("NO_COLOR", "1")
21+
return
22+
}
23+
24+
func TestConfigRun(t *testing.T) {
25+
restoreColor := disableColor()
26+
defer restoreColor()
27+
28+
testLst := filepath.Join(t.TempDir(), "output.lst")
29+
auto :=
30+
"CREATE TABLE TESTTBL|" +
31+
"( TESTNO NUMERIC ,|" +
32+
" DT CHARACTER VARYING(20) ,|" +
33+
" PRIMARY KEY (TESTNO) )||" +
34+
"INSERT INTO TESTTBL VALUES|" +
35+
"(10,'2024-05-25 13:45:33')||" +
36+
"COMMIT||" +
37+
"EDIT TESTTBL||" +
38+
"/10|lr2015-06-07 20:21:22|cyy" +
39+
"SPOOL " + testLst + "||" +
40+
"SELECT * FROM TESTTBL||" +
41+
"SPOOL OFF||" +
42+
"ROLLBACK||" +
43+
"EXIT||"
44+
45+
cfg := New()
46+
cfg.Auto = auto
47+
cfg.Debug = true
48+
d, err := dialect.ReadDBInfoFromArgs([]string{"sqlite3", ":memory:"})
49+
if err != nil {
50+
t.Fatal(err.Error())
51+
}
52+
err = cfg.Run(d.Driver, d.DataSource, d.Dialect)
53+
if err != nil {
54+
t.Fatal(err.Error())
55+
}
56+
fd, err := os.Open(testLst)
57+
if err != nil {
58+
t.Fatal(err.Error())
59+
}
60+
defer fd.Close()
61+
sc := bufio.NewScanner(fd)
62+
count := 0
63+
for sc.Scan() {
64+
line := sc.Text()
65+
if len(line) > 0 && line[0] == '#' {
66+
continue
67+
}
68+
count++
69+
if count == 1 {
70+
continue
71+
}
72+
field := strings.Split(line, ",")
73+
if len(field) >= 2 && field[1] == "2015-06-07 20:21:22" {
74+
// OK
75+
return
76+
}
77+
}
78+
if sc.Err() != nil {
79+
t.Fatal(err.Error())
80+
}
81+
t.Fatalf("%s: not found", "2015-06-07 20:21:22")
82+
}

release_note_en.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- Updated internal dependencies (go-multiline-ny v0.22.1, go-box v3) (#4)
1111
- Removed obsolete go-box v2 references (#4)
1212
- Use context from completion.CmdCompletionOrList.CandidatesContext instead of context.TODO()
13+
- Rewrote the SQLite3 test written in PowerShell 7 using Go’s standard `go test` framework to enable testing on Linux and GitHub Actions.
1314

1415
v0.24.0
1516
=======

release_note_ja.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- 依存パッケージを更新 (go-multiline-ny v0.22.1, go-box v3) (#4)
1111
- go-box v2 への参照を削除 (#4)
1212
- テーブル名・カラム名補完の際に使用していた `context.TODO()``completion.CmdCompletionOrList.CandidatesContext` が提供するコンテキストに変更
13+
- Linux 環境や GitHub Actions からテストができるよう、PowerShell 7 で記述していた SQLite3 を使ったテストを、go test に置き変えた。
1314

1415
v0.24.0
1516
=======

0 commit comments

Comments
 (0)