all repos — scriptura @ v0.0.3

a corner in $HOME for your scripts

tests/scriptura.fish (view raw)

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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env fish

# Setup test environment
set --global test_root (mktemp -d)
set --global original_scriptura_root $SCRIPTURA_ROOT
set --global original_sd_root $SD_ROOT
set --prepend fish_function_path (status dirname)/../functions

# Helper function to clean up
function cleanup
    rm -rf $test_root
    if set --query original_scriptura_root
        set --global --export SCRIPTURA_ROOT $original_scriptura_root
    else
        set --erase --global SCRIPTURA_ROOT
    end
    if set --query original_sd_root
        set --global --export SD_ROOT $original_sd_root
    else
        set --erase --global SD_ROOT
    end
end

# Test: SCRIPTURA_ROOT defaults to ~/sd when not set
@test "SCRIPTURA_ROOT defaults to ~/sd when not set" (
    set --erase SCRIPTURA_ROOT
    set --erase SD_ROOT
    source $(status dirname)/../functions/scriptura.fish
    scriptura
) = "\$SCRIPTURA_ROOT $HOME/sd does not exist, use `scriptura --new` to create it"

# Test: SCRIPTURA_ROOT uses SD_ROOT when set
@test "SCRIPTURA_ROOT uses SD_ROOT when set" (
    set --erase SCRIPTURA_ROOT
    set --global --export SD_ROOT "/custom/sd"
    source $(status dirname)/../functions/scriptura.fish
    scriptura
) = "\$SCRIPTURA_ROOT /custom/sd does not exist, use `scriptura --new` to create it"

# Test: SCRIPTURA_ROOT uses existing value when set
@test "SCRIPTURA_ROOT uses existing value when set" (
    set --global --export SCRIPTURA_ROOT "/existing/root"
    set --global --export SD_ROOT "/custom/sd"
    source $(status dirname)/../functions/scriptura.fish
    scriptura
) = "\$SCRIPTURA_ROOT /existing/root does not exist, use `scriptura --new` to create it"

# Test: usage is shown when no arguments provided
@test "usage is shown when no arguments provided" -n (
    set --global --export SCRIPTURA_ROOT $test_root
    mkdir -p $test_root
    scriptura 2>&1 | string match --regex "Usage:"
)

# Test: error when SCRIPTURA_ROOT doesn't exist and --new not used
@test "error when SCRIPTURA_ROOT doesn't exist and --new not used" -n (
    set --global --export SCRIPTURA_ROOT "/nonexistent/path"
    scriptura 2>&1 | string match --regex "does not exist"
)

# Test: --new creates SCRIPTURA_ROOT if it doesn't exist
@test "--new creates SCRIPTURA_ROOT if it doesn't exist" (
    set --global --export SCRIPTURA_ROOT "$test_root/new_root"
    scriptura --new > /dev/null 2>&1
    path is --type dir $test_root/new_root
) $status -eq 0

# Test: --new creates a new script
@test "--new creates a new script" (
    set --global --export SCRIPTURA_ROOT $test_root
    set --global --export SCRIPTURA_EDITOR "touch"
    scriptura --new test_script
    path is --type file --perm exec "$test_root/test_script"
) $status -eq 0

# Test: --new creates script with shebang
@test "--new creates script with shebang" (
    set --global --export SCRIPTURA_ROOT $test_root
    set --global --export SCRIPTURA_EDITOR "touch"
    scriptura --new shebang_test
    head -n 1 "$test_root/shebang_test"
) = "#!/usr/bin/env fish"

# Test: --new creates component directories
@test "--new creates component directories" (
    set --global --export SCRIPTURA_ROOT $test_root
    set --global --export SCRIPTURA_EDITOR "touch"
    scriptura --new foo bar baz
    path is --type dir "$test_root/foo/bar"
    path is --type file --perm exec "$test_root/foo/bar/baz"
) $status -eq 0

# Test: --new fails if script already exists
@test "--new fails if script already exists" -n (
    set --global --export SCRIPTURA_ROOT $test_root
    touch "$test_root/existing_script"
    scriptura --new existing_script 2>&1 | string match --regex "already exists"
)

# Test: --new with -- creates script with remaining arguments
@test "--new with -- creates script with remaining arguments" (
    set --global --export SCRIPTURA_ROOT $test_root
    scriptura --new new_script -- echo foo
    path is --type file --perm exec "$test_root/new_script"
    tail -n 1 "$test_root/new_script"
) = "echo foo"

# Test: --new --edit with -- creates script with remaining arguments
@test "--new --edit with -- creates script with remaining arguments" (
    set --global --export SCRIPTURA_ROOT $test_root
    set --global --export SCRIPTURA_EDITOR "sed -i s/foo/bar/"
    scriptura --new --edit new_edit_script -- echo foo
    path is --type file --perm exec "$test_root/new_edit_script"
    tail -n 1 "$test_root/new_edit_script"
) = "echo bar"

# Test: --which shows path for executable script
@test "--which shows path for executable script" (
    set --global --export SCRIPTURA_ROOT $test_root
    printf "#!/usr/bin/env fish\necho test\n" > "$test_root/which_test"
    chmod +x "$test_root/which_test"
    scriptura --which which_test
) = "$test_root/which_test"

# Test: --which fails for non-executable file
@test "--which fails for non-executable file" (
    set --global --export SCRIPTURA_ROOT $test_root
    touch "$test_root/non_exec"
    scriptura --which non_exec
) $status -ne 0

# Test: --cat displays script content
@test "--cat displays script content" (
    set --global --export SCRIPTURA_ROOT $test_root
    echo "# Test content" > "$test_root/cat_test"
    scriptura --cat cat_test
) = "# Test content"

# Test: --help shows help for script
@test "--help shows help for script" (
    set --global --export SCRIPTURA_ROOT $test_root
    printf "#!/usr/bin/env fish\n# This is a test script\necho hello\n" > "$test_root/help_test"
    chmod +x "$test_root/help_test"
    scriptura --help help_test
) = "This is a test script"

# Test: script execution works for executable scripts
@test "script execution works for executable scripts" (
    set --global --export SCRIPTURA_ROOT $test_root
    printf "#!/usr/bin/env fish\necho 'executed successfully'\n" > "$test_root/exec_test"
    chmod +x "$test_root/exec_test"
    scriptura exec_test
) = "executed successfully"

# Test: non-executable script shows error and content
@test "non-executable script shows error and content" -n (
    set --global --export SCRIPTURA_ROOT $test_root
    echo "# Non-executable content" > "$test_root/non_exec_test"
    scriptura non_exec_test 2>&1 | string match --regex "not executable"
)

# Test: directory listing shows directories
@test "directory listing shows directories" -n (
    set --global --export SCRIPTURA_ROOT $test_root
    mkdir -p "$test_root/test_dir"
    scriptura 2>&1 | string match --regex "test_dir"
)

# Test: directory listing shows executable files
@test "directory listing shows executable files" -n (
    set --global --export SCRIPTURA_ROOT $test_root
    printf "#!/usr/bin/env fish\n# Test script\n" > "$test_root/list_test"
    chmod +x "$test_root/list_test"
    scriptura 2>&1 | string match --regex "list_test"
)

# Test: directory listing with argument shows children
@test "directory listing with argument shows children" -n (
    set --global --export SCRIPTURA_ROOT $test_root
    mkdir -p "$test_root/test_dir"
    printf "#!/usr/bin/env fish\n# Test script\n" > "$test_root/test_dir/test_script"
    chmod +x "$test_root/test_dir/test_script"
    scriptura test_dir 2>&1 | string match "test_script*"
)

# Test: help file is used for directories
@test "help file is used for directories" (
    set --global --export SCRIPTURA_ROOT $test_root
    mkdir -p "$test_root/help_dir"
    echo "Directory help text" > "$test_root/help_dir/help"
    scriptura --help help_dir
) = "Directory help text"

# Test: .help file is used for scripts
@test ".help file is used for scripts" (
    set --global --export SCRIPTURA_ROOT $test_root
    printf "#!/usr/bin/env fish\n" > "$test_root/help_file_test"
    chmod +x "$test_root/help_file_test"
    echo "External help text" > "$test_root/help_file_test.help"
    scriptura --help help_file_test
) = "External help text"

# Test: script description extracted from comments
@test "script description extracted from comments" (
    set --global --export SCRIPTURA_ROOT $test_root
    printf "#!/usr/bin/env fish\n# This is the description\n# Another comment\n" > "$test_root/desc_test"
    chmod +x "$test_root/desc_test"
    scriptura --help desc_test
) = "This is the description\nAnother comment"

# Test: nested path resolution works
@test "nested path resolution works" (
    set --global --export SCRIPTURA_ROOT $test_root
    mkdir -p "$test_root/level1/level2"
    printf "#!/usr/bin/env fish\necho nested\n" > "$test_root/level1/level2/nested_test"
    chmod +x "$test_root/level1/level2/nested_test"
    scriptura level1 level2 nested_test
) = nested

# Test: arguments passed to executed script
@test "arguments passed to executed script" (
    set --global --export SCRIPTURA_ROOT $test_root
    printf "#!/usr/bin/env fish\necho \$argv\n" > "$test_root/args_test"
    chmod +x "$test_root/args_test"
    scriptura args_test arg1 arg2
) = "arg1 arg2"

# Test: -- stops argument processing
@test "-- stops argument processing" -n (
    set --global --export SCRIPTURA_ROOT $test_root
    mkdir -p "$test_root/dash_test"
    printf "#!/usr/bin/env fish\necho dash_dir\n" > "$test_root/dash_test/script"
    chmod +x "$test_root/dash_test/script"
    scriptura dash_test -- script 2>&1 | string match "script*"
)

# Test: SCRIPTURA_CAT is used when set
@test "SCRIPTURA_CAT is used when set" "$(
    set --global --export SCRIPTURA_ROOT $test_root
    set --global --export SCRIPTURA_CAT "head -n 1"
    printf "line1\nline2\nline3" > "$test_root/cat_custom_test"
    scriptura --cat cat_custom_test
)" = line1

# Test: SD_CAT is used as fallback for SCRIPTURA_CAT
@test "SD_CAT is used as fallback for SCRIPTURA_CAT" (
    set --global --export SCRIPTURA_ROOT $test_root
    set --erase SCRIPTURA_CAT
    set --global --export SD_CAT "head -n 1"
    printf "line1\nline2\nline3" > "$test_root/sd_cat_test"
    scriptura --cat sd_cat_test
) = line1

# Test: unknown target shows error
@test "unknown target shows error" "$(
    set --global --export SCRIPTURA_ROOT $test_root
    scriptura nonexistent_target 2>&1
)" = 'scriptura: don\'t know what to do with target "nonexistent_target"'

# Test: help for directory without help file shows default
@test "help for directory without help file shows default" (
    set --global --export SCRIPTURA_ROOT $test_root
    mkdir -p "$test_root/no_help_dir"
    scriptura --help no_help_dir
) = "no_help_dir commands"

# Test: shebang lines are excluded from description
@test "shebang lines are excluded from description" (
    set --global --export SCRIPTURA_ROOT $test_root
    printf "#!/usr/bin/env fish\n#! Not a shebang\n# Real description\n" > "$test_root/shebang_desc_test"
    chmod +x "$test_root/shebang_desc_test"
    scriptura --help shebang_desc_test
) = "Real description"

# Cleanup
cleanup