assay-lua 0.10.3

General-purpose enhanced Lua runtime. Batteries-included scripting, automation, and web services.
Documentation
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
mod common;

use common::run_lua;

#[tokio::test]
async fn test_fs_read() {
    let script = r#"
        local content = fs.read("Cargo.toml")
        assert.contains(content, "assay")
    "#;
    run_lua(script).await.unwrap();
}

#[tokio::test]
async fn test_fs_read_nonexistent() {
    let result = run_lua(r#"fs.read("/nonexistent/file.txt")"#).await;
    assert!(result.is_err());
}

#[tokio::test]
async fn test_fs_write_basic() {
    let dir = std::env::temp_dir().join("assay_test_write_basic");
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();
    let path = dir.join("test.txt");

    let script = format!(
        r#"fs.write("{}", "hello from assay")"#,
        path.display().to_string().replace('\\', "\\\\")
    );
    run_lua(&script).await.unwrap();

    let content = std::fs::read_to_string(&path).unwrap();
    assert_eq!(content, "hello from assay");
    let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test]
async fn test_fs_write_creates_parent_dirs() {
    let dir = std::env::temp_dir().join("assay_test_write_nested/a/b/c");
    let _ = std::fs::remove_dir_all(std::env::temp_dir().join("assay_test_write_nested"));
    let path = dir.join("deep.txt");

    let script = format!(
        r#"fs.write("{}", "nested content")"#,
        path.display().to_string().replace('\\', "\\\\")
    );
    run_lua(&script).await.unwrap();

    let content = std::fs::read_to_string(&path).unwrap();
    assert_eq!(content, "nested content");
    let _ = std::fs::remove_dir_all(std::env::temp_dir().join("assay_test_write_nested"));
}

#[tokio::test]
async fn test_fs_write_overwrite() {
    let dir = std::env::temp_dir().join("assay_test_write_overwrite");
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();
    let path = dir.join("overwrite.txt");

    std::fs::write(&path, "original").unwrap();

    let script = format!(
        r#"fs.write("{}", "replaced")"#,
        path.display().to_string().replace('\\', "\\\\")
    );
    run_lua(&script).await.unwrap();

    let content = std::fs::read_to_string(&path).unwrap();
    assert_eq!(content, "replaced");
    let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test]
async fn test_fs_write_then_read() {
    let dir = std::env::temp_dir().join("assay_test_write_read");
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();
    let path = dir.join("roundtrip.txt");

    let script = format!(
        r#"
        fs.write("{path}", "roundtrip data")
        local content = fs.read("{path}")
        assert.eq(content, "roundtrip data")
        "#,
        path = path.display().to_string().replace('\\', "\\\\")
    );
    run_lua(&script).await.unwrap();
    let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test]
async fn test_fs_write_invalid_path() {
    let result = run_lua(r#"fs.write("/proc/0/nonexistent", "data")"#).await;
    assert!(result.is_err());
}

#[tokio::test]
async fn test_fs_exists() {
    let script = r#"
        assert.eq(fs.exists("Cargo.toml"), true)
        assert.eq(fs.exists("/nonexistent/path"), false)
    "#;
    run_lua(script).await.unwrap();
}

#[tokio::test]
async fn test_fs_mkdir_and_remove() {
    let dir = std::env::temp_dir().join("assay_test_mkdir");
    let _ = std::fs::remove_dir_all(&dir);

    let script = format!(
        r#"
        fs.mkdir("{}")
        assert.eq(fs.exists("{}"), true)
        fs.remove("{}")
        assert.eq(fs.exists("{}"), false)
        "#,
        dir.display(),
        dir.display(),
        dir.display(),
        dir.display()
    );
    run_lua(&script).await.unwrap();
    let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test]
async fn test_fs_stat() {
    let dir = std::env::temp_dir().join("assay_test_stat");
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();
    let path = dir.join("stat_test.txt");
    std::fs::write(&path, "hello stat").unwrap();

    let script = format!(
        r#"
        local info = fs.stat("{}")
        assert.eq(info.is_file, true)
        assert.eq(info.is_dir, false)
        assert.eq(info.size, 10)
        assert.not_nil(info.modified)
        "#,
        path.display().to_string().replace('\\', "\\\\")
    );
    run_lua(&script).await.unwrap();
    let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test]
async fn test_fs_stat_directory() {
    let script = r#"
        local info = fs.stat("src")
        assert.eq(info.is_dir, true)
        assert.eq(info.is_file, false)
    "#;
    run_lua(script).await.unwrap();
}

#[tokio::test]
async fn test_fs_stat_nonexistent() {
    let result = run_lua(r#"fs.stat("/nonexistent/file")"#).await;
    assert!(result.is_err());
}

#[tokio::test]
async fn test_fs_list() {
    let dir = std::env::temp_dir().join("assay_test_list");
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();
    std::fs::write(dir.join("a.txt"), "a").unwrap();
    std::fs::write(dir.join("b.txt"), "b").unwrap();
    std::fs::create_dir_all(dir.join("subdir")).unwrap();

    let script = format!(
        r#"
        local entries = fs.list("{}")
        assert.not_nil(entries)
        local count = 0
        local has_file = false
        local has_dir = false
        for _, e in ipairs(entries) do
            count = count + 1
            if e.type == "file" then has_file = true end
            if e.type == "directory" then has_dir = true end
        end
        assert.eq(count, 3)
        assert.eq(has_file, true)
        assert.eq(has_dir, true)
        "#,
        dir.display().to_string().replace('\\', "\\\\")
    );
    run_lua(&script).await.unwrap();
    let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test]
async fn test_fs_remove_file() {
    let dir = std::env::temp_dir().join("assay_test_remove_file");
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();
    let path = dir.join("removeme.txt");
    std::fs::write(&path, "bye").unwrap();

    let script = format!(
        r#"
        assert.eq(fs.exists("{}"), true)
        fs.remove("{}")
        assert.eq(fs.exists("{}"), false)
        "#,
        path.display().to_string().replace('\\', "\\\\"),
        path.display().to_string().replace('\\', "\\\\"),
        path.display().to_string().replace('\\', "\\\\")
    );
    run_lua(&script).await.unwrap();
    let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test]
async fn test_fs_remove_nonexistent() {
    let result = run_lua(r#"fs.remove("/nonexistent/file.txt")"#).await;
    assert!(result.is_err());
}

#[tokio::test]
async fn test_fs_copy() {
    let script = r#"
        local dir = fs.tempdir()
        fs.write(dir .. "/source.txt", "copy me")
        local bytes = fs.copy(dir .. "/source.txt", dir .. "/dest.txt")
        assert.eq(bytes, 7)
        local content = fs.read(dir .. "/dest.txt")
        assert.eq(content, "copy me")
        -- Source should still exist
        assert.eq(fs.exists(dir .. "/source.txt"), true)
        fs.remove(dir)
    "#;
    run_lua(script).await.unwrap();
}

#[tokio::test]
async fn test_fs_copy_nonexistent() {
    let result = run_lua(r#"fs.copy("/nonexistent/src", "/tmp/dst")"#).await;
    assert!(result.is_err());
}

#[tokio::test]
async fn test_fs_rename() {
    let script = r#"
        local dir = fs.tempdir()
        fs.write(dir .. "/before.txt", "rename me")
        fs.rename(dir .. "/before.txt", dir .. "/after.txt")
        assert.eq(fs.exists(dir .. "/before.txt"), false)
        assert.eq(fs.exists(dir .. "/after.txt"), true)
        local content = fs.read(dir .. "/after.txt")
        assert.eq(content, "rename me")
        fs.remove(dir)
    "#;
    run_lua(script).await.unwrap();
}

#[tokio::test]
async fn test_fs_rename_nonexistent() {
    let result = run_lua(r#"fs.rename("/nonexistent/src", "/tmp/dst")"#).await;
    assert!(result.is_err());
}

#[tokio::test]
async fn test_fs_glob() {
    let script = r#"
        local dir = fs.tempdir()
        fs.write(dir .. "/a.txt", "a")
        fs.write(dir .. "/b.txt", "b")
        fs.write(dir .. "/c.log", "c")

        local matches = fs.glob(dir .. "/*.txt")
        local count = 0
        for _ in ipairs(matches) do count = count + 1 end
        assert.eq(count, 2, "should match 2 .txt files")

        local all = fs.glob(dir .. "/*")
        local all_count = 0
        for _ in ipairs(all) do all_count = all_count + 1 end
        assert.eq(all_count, 3, "should match all 3 files")

        fs.remove(dir)
    "#;
    run_lua(script).await.unwrap();
}

#[tokio::test]
async fn test_fs_glob_recursive() {
    let script = r#"
        local dir = fs.tempdir()
        fs.mkdir(dir .. "/sub")
        fs.write(dir .. "/top.txt", "top")
        fs.write(dir .. "/sub/nested.txt", "nested")

        local matches = fs.glob(dir .. "/**/*.txt")
        local count = 0
        for _ in ipairs(matches) do count = count + 1 end
        assert.eq(count, 2, "should match 2 .txt files recursively")

        fs.remove(dir)
    "#;
    run_lua(script).await.unwrap();
}

#[tokio::test]
async fn test_fs_glob_no_match() {
    let script = r#"
        local matches = fs.glob("/tmp/assay-nonexistent-pattern-*.xyz")
        local count = 0
        for _ in ipairs(matches) do count = count + 1 end
        assert.eq(count, 0)
    "#;
    run_lua(script).await.unwrap();
}

#[tokio::test]
async fn test_fs_glob_invalid_pattern() {
    let result = run_lua(r#"fs.glob("[invalid")"#).await;
    assert!(result.is_err());
}

#[tokio::test]
async fn test_fs_tempdir() {
    let script = r#"
        local dir = fs.tempdir()
        assert.not_nil(dir)
        assert.gt(#dir, 0, "tempdir path should not be empty")
        assert.eq(fs.exists(dir), true)

        -- Should be able to write into it
        fs.write(dir .. "/test.txt", "hello")
        assert.eq(fs.read(dir .. "/test.txt"), "hello")

        fs.remove(dir)
    "#;
    run_lua(script).await.unwrap();
}

#[tokio::test]
async fn test_fs_chmod() {
    let script = r#"
        local dir = fs.tempdir()
        local path = dir .. "/chmod_test.txt"
        fs.write(path, "test")

        -- Set to 0o644 = 420 decimal
        fs.chmod(path, 420)
        local stat = fs.stat(path)
        assert.eq(stat.is_file, true)

        -- Set to 0o755 = 493 decimal
        fs.chmod(path, 493)

        fs.remove(dir)
    "#;
    run_lua(script).await.unwrap();
}

#[tokio::test]
async fn test_fs_chmod_nonexistent() {
    let result = run_lua(r#"fs.chmod("/nonexistent/file", 420)"#).await;
    assert!(result.is_err());
}

#[tokio::test]
async fn test_fs_readdir() {
    let script = r#"
        local dir = fs.tempdir()
        fs.write(dir .. "/a.txt", "a")
        fs.mkdir(dir .. "/sub")
        fs.write(dir .. "/sub/b.txt", "b")

        local entries = fs.readdir(dir)
        local count = 0
        local has_file = false
        local has_dir = false
        local has_nested = false
        for _, e in ipairs(entries) do
            count = count + 1
            if e.path == "a.txt" and e.type == "file" then has_file = true end
            if e.path == "sub" and e.type == "directory" then has_dir = true end
            if e.path == "sub/b.txt" and e.type == "file" then has_nested = true end
        end
        assert.eq(count, 3, "should have 3 entries (a.txt, sub, sub/b.txt)")
        assert.eq(has_file, true, "should have a.txt")
        assert.eq(has_dir, true, "should have sub directory")
        assert.eq(has_nested, true, "should have sub/b.txt")

        fs.remove(dir)
    "#;
    run_lua(script).await.unwrap();
}

#[tokio::test]
async fn test_fs_readdir_with_depth() {
    let script = r#"
        local dir = fs.tempdir()
        fs.write(dir .. "/top.txt", "top")
        fs.mkdir(dir .. "/a")
        fs.write(dir .. "/a/mid.txt", "mid")
        fs.mkdir(dir .. "/a/b")
        fs.write(dir .. "/a/b/deep.txt", "deep")

        -- depth=1 should only list top level
        local entries = fs.readdir(dir, {depth = 1})
        local count = 0
        local has_deep = false
        for _, e in ipairs(entries) do
            count = count + 1
            if e.path == "a/b/deep.txt" then has_deep = true end
        end
        -- Should have: top.txt, a, a/mid.txt (a is listed, mid.txt at depth 1 inside a)
        -- but NOT a/b/deep.txt (that's depth 2)
        assert.eq(has_deep, false, "should not include deep entries at depth=1")

        fs.remove(dir)
    "#;
    run_lua(script).await.unwrap();
}

#[tokio::test]
async fn test_fs_readdir_nonexistent() {
    let result = run_lua(r#"fs.readdir("/nonexistent/path")"#).await;
    assert!(result.is_err());
}