-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworking_directory_test.go
More file actions
131 lines (103 loc) · 3.12 KB
/
working_directory_test.go
File metadata and controls
131 lines (103 loc) · 3.12 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
package headlessterm
import (
"testing"
)
func TestWorkingDirectory_Basic(t *testing.T) {
term := New(WithSize(24, 80))
// OSC 7 ; file://hostname/path BEL
term.WriteString("\x1b]7;file://localhost/home/user\x07")
uri := term.WorkingDirectory()
expected := "file://localhost/home/user"
if uri != expected {
t.Errorf("expected %q, got %q", expected, uri)
}
}
func TestWorkingDirectory_STTerminator(t *testing.T) {
term := New(WithSize(24, 80))
// OSC 7 ; file://hostname/path ST (ESC \)
term.WriteString("\x1b]7;file://myhost/var/log\x1b\\")
uri := term.WorkingDirectory()
expected := "file://myhost/var/log"
if uri != expected {
t.Errorf("expected %q, got %q", expected, uri)
}
}
func TestWorkingDirectory_Multiple(t *testing.T) {
term := New(WithSize(24, 80))
// Set first directory
term.WriteString("\x1b]7;file://localhost/home/user\x07")
uri := term.WorkingDirectory()
if uri != "file://localhost/home/user" {
t.Errorf("expected file://localhost/home/user, got %q", uri)
}
// Change directory
term.WriteString("\x1b]7;file://localhost/tmp\x07")
uri = term.WorkingDirectory()
if uri != "file://localhost/tmp" {
t.Errorf("expected file://localhost/tmp, got %q", uri)
}
}
func TestWorkingDirectory_NotSet(t *testing.T) {
term := New(WithSize(24, 80))
uri := term.WorkingDirectory()
if uri != "" {
t.Errorf("expected empty string, got %q", uri)
}
}
func TestWorkingDirectoryPath_Basic(t *testing.T) {
term := New(WithSize(24, 80))
term.WriteString("\x1b]7;file://localhost/home/user\x07")
path := term.WorkingDirectoryPath()
expected := "/home/user"
if path != expected {
t.Errorf("expected %q, got %q", expected, path)
}
}
func TestWorkingDirectoryPath_WithHostname(t *testing.T) {
term := New(WithSize(24, 80))
term.WriteString("\x1b]7;file://mycomputer.local/var/log/system\x07")
path := term.WorkingDirectoryPath()
expected := "/var/log/system"
if path != expected {
t.Errorf("expected %q, got %q", expected, path)
}
}
func TestWorkingDirectoryPath_EmptyHostname(t *testing.T) {
term := New(WithSize(24, 80))
// Some systems emit file:///path (empty hostname)
term.WriteString("\x1b]7;file:///home/user\x07")
path := term.WorkingDirectoryPath()
expected := "/home/user"
if path != expected {
t.Errorf("expected %q, got %q", expected, path)
}
}
func TestWorkingDirectoryPath_NotSet(t *testing.T) {
term := New(WithSize(24, 80))
path := term.WorkingDirectoryPath()
if path != "" {
t.Errorf("expected empty string, got %q", path)
}
}
func TestWorkingDirectory_Middleware(t *testing.T) {
var middlewareCalled bool
var receivedURI string
mw := &Middleware{
SetWorkingDirectory: func(uri string, next func(string)) {
middlewareCalled = true
receivedURI = uri
next(uri)
},
}
term := New(WithSize(24, 80), WithMiddleware(mw))
term.WriteString("\x1b]7;file://localhost/test\x07")
if !middlewareCalled {
t.Error("expected middleware to be called")
}
if receivedURI != "file://localhost/test" {
t.Errorf("expected file://localhost/test, got %q", receivedURI)
}
if term.WorkingDirectory() != "file://localhost/test" {
t.Errorf("expected working directory to be set")
}
}