Skip to content

Commit 08f925e

Browse files
authored
Merge pull request #3 from libi/fix/profile-test-cases
fix test cause
2 parents f9b3598 + 8d301b1 commit 08f925e

2 files changed

Lines changed: 42 additions & 4 deletions

File tree

tests/diff_trace_state_test.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,10 @@ func TestState_Import_NonexistentFile(t *testing.T) {
565565
// ---------- Profile Option ----------
566566

567567
func TestProfile_Option(t *testing.T) {
568-
tmpDir := t.TempDir()
568+
tmpDir, err := os.MkdirTemp("", "ko-browser-profile-*")
569+
if err != nil {
570+
t.Fatalf("MkdirTemp: %v", err)
571+
}
569572
profileDir := filepath.Join(tmpDir, "test-profile")
570573

571574
b, err := browser.New(browser.Options{
@@ -576,16 +579,27 @@ func TestProfile_Option(t *testing.T) {
576579
if err != nil {
577580
t.Fatalf("browser.New with profile: %v", err)
578581
}
582+
closed := false
583+
closeBrowser := func() {
584+
if !closed {
585+
b.Close()
586+
closed = true
587+
}
588+
}
589+
t.Cleanup(func() {
590+
closeBrowser()
591+
removeDirEventually(t, tmpDir)
592+
})
579593

580594
// Navigate to a page
581595
if err := b.Open(testdataURL("phase7_test.html")); err != nil {
582-
b.Close()
596+
closeBrowser()
583597
t.Fatalf("Open: %v", err)
584598
}
585599

586600
// Set some data in localStorage
587601
if _, err := b.Eval(`localStorage.setItem("profile_test", "yes")`); err != nil {
588-
b.Close()
602+
closeBrowser()
589603
t.Fatalf("set localStorage: %v", err)
590604
}
591605

@@ -595,7 +609,7 @@ func TestProfile_Option(t *testing.T) {
595609
}
596610

597611
t.Logf("Profile directory created at: %s", profileDir)
598-
b.Close()
612+
closeBrowser()
599613
}
600614

601615
// ---------- Config File ----------

tests/helpers_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,30 @@ func must(t *testing.T, err error) {
138138
}
139139
}
140140

141+
func removeDirEventually(t *testing.T, path string) {
142+
t.Helper()
143+
if path == "" {
144+
return
145+
}
146+
147+
deadline := time.Now().Add(5 * time.Second)
148+
var lastErr error
149+
for time.Now().Before(deadline) {
150+
lastErr = os.RemoveAll(path)
151+
if lastErr == nil {
152+
if _, statErr := os.Stat(path); os.IsNotExist(statErr) {
153+
return
154+
}
155+
}
156+
time.Sleep(100 * time.Millisecond)
157+
}
158+
159+
if lastErr == nil {
160+
lastErr = fmt.Errorf("directory still exists after cleanup retries")
161+
}
162+
t.Fatalf("cleanup dir %s: %v", path, lastErr)
163+
}
164+
141165
// evalVoid evaluates a JS expression and discards the result. Fatals on error.
142166
func evalVoid(t *testing.T, b *browser.Browser, expression string) {
143167
t.Helper()

0 commit comments

Comments
 (0)