Repository navigation
Expand file tree
/
Copy pathdifferential_python_test.go
More file actions
211 lines (196 loc) · 5.85 KB
/
Copy pathdifferential_python_test.go
File metadata and controls
211 lines (196 loc) · 5.85 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package razdel_test
import (
"bytes"
"context"
"encoding/json"
"errors"
"os"
"os/exec"
"path/filepath"
"sync"
"testing"
"time"
"github.com/muonsoft/go-razdel"
"github.com/muonsoft/go-razdel/internal/fixture"
"github.com/muonsoft/go-razdel/internal/testkit"
)
const pyDiffCommandTimeout = 20 * time.Second
type pyDiffCase struct {
Mode string `json:"mode"`
Text string `json:"text"`
}
type pyDiffBatchRequest struct {
Cases []pyDiffCase `json:"cases"`
}
type pyDiffBatchResponse struct {
Results [][]string `json:"results"`
}
var (
pyDiffOnce sync.Once
pyDiffOK bool
pyDiffReason string
pyDiffPython string
)
func pythonDifferentialReady(root string) (ok bool, reason string) {
pyDiffOnce.Do(func() {
if testkit.DifferentialPythonDisabled() {
pyDiffReason = testkit.EnvDifferentialPython + "=0"
return
}
py, err := exec.LookPath("python3")
if err != nil {
pyDiffReason = "python3 not in PATH"
return
}
pyDiffPython = py
razdelRoot := filepath.Join(root, "third_party", "razdel")
if _, err := os.Stat(filepath.Join(razdelRoot, "razdel", "__init__.py")); err != nil {
pyDiffReason = "third_party/razdel submodule not checked out"
return
}
ctx, cancel := context.WithTimeout(context.Background(), pyDiffCommandTimeout)
defer cancel()
cmd := exec.CommandContext(ctx, pyDiffPython, "-c", "import razdel")
cmd.Env = append(os.Environ(), "PYTHONPATH="+pythonPathEnv(root))
cmd.Dir = root
if err := cmd.Run(); err != nil {
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
pyDiffReason = "python import readiness check timed out"
return
}
pyDiffReason = "cannot import razdel with PYTHONPATH=" + razdelRoot + ": " + err.Error()
return
}
pyDiffOK = true
})
return pyDiffOK, pyDiffReason
}
func runPythonDiffBatch(t *testing.T, root string, cases []pyDiffCase) [][]string {
t.Helper()
if pyDiffPython == "" {
t.Fatal("python binary is not initialized")
}
script := filepath.Join(root, "testdata", "python", "razdel_diff_runner.py")
if _, err := os.Stat(script); err != nil {
t.Fatalf("diff runner script: %v", err)
}
body, err := json.Marshal(pyDiffBatchRequest{Cases: cases})
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), pyDiffCommandTimeout)
defer cancel()
cmd := exec.CommandContext(ctx, pyDiffPython, script)
cmd.Dir = root
cmd.Env = append(os.Environ(), "PYTHONPATH="+pythonPathEnv(root))
cmd.Stdin = bytes.NewReader(body)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
t.Fatalf("python diff runner timed out after %s", pyDiffCommandTimeout)
}
var ee *exec.ExitError
if errors.As(err, &ee) {
t.Fatalf("python diff runner: %v\nstderr:\n%s", err, stderr.String())
}
t.Fatal(err)
}
var resp pyDiffBatchResponse
if err := json.Unmarshal(stdout.Bytes(), &resp); err != nil {
t.Fatalf("decode python stdout: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
}
if len(resp.Results) != len(cases) {
t.Fatalf("python returned %d results, want %d", len(resp.Results), len(cases))
}
return resp.Results
}
func pythonPathEnv(root string) string {
razdelRoot := filepath.Join(root, "third_party", "razdel")
if current := os.Getenv("PYTHONPATH"); current != "" {
return razdelRoot + string(os.PathListSeparator) + current
}
return razdelRoot
}
func loadPartitionTexts(t *testing.T, root, rel string) []string {
t.Helper()
path := filepath.Join(root, filepath.FromSlash(rel))
raw, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
var texts []string
for _, p := range fixture.ParsePartitionLines(string(raw)) {
texts = append(texts, p.Text())
}
return texts
}
func goTokenTexts(s string) []string {
toks := razdel.Tokenize(s)
out := make([]string, 0, len(toks))
for _, tok := range toks {
out = append(out, tok.Text)
}
return out
}
func goSentenceTexts(s string) []string {
sents := razdel.Sentenize(s)
out := make([]string, 0, len(sents))
for _, sent := range sents {
out = append(out, sent.Text)
}
return out
}
func TestDifferential_tokenize_vsPython_quickSample(t *testing.T) {
root := testkit.ModuleRoot(t)
ok, why := pythonDifferentialReady(root)
if !ok {
testkit.SkipOrFailPython(t, "differential vs Python: "+why)
}
texts := loadPartitionTexts(t, root, "testdata/upstream/quick_tokens_sample.txt")
cases := make([]pyDiffCase, len(texts))
for i, s := range texts {
cases[i] = pyDiffCase{Mode: "tokenize", Text: s}
}
wantPy := runPythonDiffBatch(t, root, cases)
for i, s := range texts {
got := goTokenTexts(s)
if len(got) != len(wantPy[i]) {
t.Fatalf("case %d text %q: len(go)=%d len(py)=%d\ngo: %#v\npy: %#v",
i+1, s, len(got), len(wantPy[i]), got, wantPy[i])
}
for j := range got {
if got[j] != wantPy[i][j] {
t.Fatalf("case %d text %q token %d: go %q py %q", i+1, s, j, got[j], wantPy[i][j])
}
}
testkit.AssertTokenOffsetContract(t, s, razdel.Tokenize(s))
}
}
func TestDifferential_sentenize_vsPython_quickSample(t *testing.T) {
root := testkit.ModuleRoot(t)
ok, why := pythonDifferentialReady(root)
if !ok {
testkit.SkipOrFailPython(t, "differential vs Python: "+why)
}
texts := loadPartitionTexts(t, root, "testdata/upstream/quick_sents_sample.txt")
cases := make([]pyDiffCase, len(texts))
for i, s := range texts {
cases[i] = pyDiffCase{Mode: "sentenize", Text: s}
}
wantPy := runPythonDiffBatch(t, root, cases)
for i, s := range texts {
got := goSentenceTexts(s)
if len(got) != len(wantPy[i]) {
t.Fatalf("case %d text %q: len(go)=%d len(py)=%d\ngo: %#v\npy: %#v",
i+1, s, len(got), len(wantPy[i]), got, wantPy[i])
}
for j := range got {
if got[j] != wantPy[i][j] {
t.Fatalf("case %d text %q sentence %d: go %q py %q", i+1, s, j, got[j], wantPy[i][j])
}
}
testkit.AssertSentenceOffsetContract(t, s, razdel.Sentenize(s))
}
}