darklua 0.18.0

Transform Lua scripts
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
432
433
434
435
436
437
438
439
440
use std::{
    ffi::OsStr,
    fs,
    path::{Path, PathBuf},
};

use assert_cmd::Command;
use insta::assert_snapshot;
use tempfile::{tempdir_in, TempDir};

struct Context {
    command: Command,
    working_directory: TempDir,
    snapshot_replace: Vec<(String, String)>,
}

impl Default for Context {
    fn default() -> Self {
        let working_directory =
            tempdir_in(env!("CARGO_TARGET_TMPDIR")).expect("unable to create temporary directory");

        let mut command = assert_cmd::cargo::cargo_bin_cmd!(env!("CARGO_PKG_NAME"));
        command.current_dir(working_directory.path());

        Self::new(command, working_directory)
    }
}

impl Context {
    pub fn new(command: Command, working_directory: TempDir) -> Self {
        Self {
            command,
            working_directory,
            snapshot_replace: Vec::new(),
        }
    }

    pub fn arg<S: AsRef<OsStr>>(mut self, arg: S) -> Self {
        self.command.arg(arg);
        self
    }

    pub fn with_cwd(mut self, cwd: impl AsRef<Path>) -> Self {
        let full_dir = self.working_directory.path().join(cwd.as_ref());
        self.command.current_dir(full_dir);
        self
    }

    pub fn expect_file<P: AsRef<Path>>(&self, file_path: P) -> &Self {
        let file_path = file_path.as_ref();
        if !file_path.exists() || !file_path.is_file() {
            Self::debug_working_directory(self.working_directory.path());
        }
        assert!(
            file_path.exists(),
            "file `{}` does not exist",
            file_path.display()
        );
        assert!(
            file_path.is_file(),
            "path `{}` is not a file",
            file_path.display()
        );
        self
    }

    pub fn snapshot_file<P: AsRef<Path>>(
        &self,
        snapshot_name: &'static str,
        file_path: P,
    ) -> &Self {
        let file_path = self.path_from_working_directory(file_path.as_ref());
        self.expect_file(&file_path);
        let content = fs::read_to_string(file_path).expect("unable to read file");
        self.snapshot_string(snapshot_name, content);
        self
    }

    pub fn snapshot_command(mut self, snapshot_name: &'static str) -> Self {
        let output = self.full_output();
        self.snapshot_string(snapshot_name, output);
        self
    }

    fn snapshot_string(&self, snapshot_name: &'static str, content: String) {
        let working_directory_display = format!(
            "{}{}",
            self.working_directory.path().display(),
            std::path::MAIN_SEPARATOR
        );
        let content = content.replace(&working_directory_display, "{{CWD}}");

        let mut settings = insta::Settings::clone_current();
        settings.set_prepend_module_to_snapshot(false);
        settings.add_filter("darklua\\.exe", "darklua");
        for (matcher, replacement) in self.snapshot_replace.iter() {
            settings.add_filter(matcher, replacement);
        }
        settings.bind(|| {
            assert_snapshot!(snapshot_name, content);
        });
    }

    pub fn write_file<P: AsRef<Path>>(self, relative_path: P, content: &str) -> Self {
        let file_path = self.path_from_working_directory(relative_path);

        if let Some(parent) = file_path
            .parent()
            .filter(|parent| *parent != self.working_directory.as_ref())
        {
            fs::create_dir_all(parent).expect("unable to create directory");
        }

        fs::write(file_path, content).expect("unable to write file");
        self
    }

    fn path_from_working_directory<P: AsRef<Path>>(&self, path: P) -> PathBuf {
        self.working_directory.path().join(path)
    }

    fn full_output(&mut self) -> String {
        let output = self.command.output().expect("unable to run command");

        let mut string = std::str::from_utf8(&output.stdout)
            .expect("unable to read output")
            .to_owned();
        let err_output = std::str::from_utf8(&output.stderr).expect("unable to read output");

        if !string.is_empty() && !err_output.is_empty() {
            string.push('\n');
        }

        string.push_str(err_output);
        string
    }

    fn debug_working_directory(root: &Path) {
        eprintln!("{}", root.display());
        for entry in root.read_dir().unwrap() {
            let entry = entry.unwrap().path();
            if entry.is_dir() {
                Self::debug_working_directory(&entry);
            } else {
                eprintln!("{}", entry.display());
            }
        }
    }

    pub fn expect_success(mut self) -> Self {
        self.command.assert().code(0);
        self
    }

    pub fn replace_snapshot_content(
        mut self,
        matcher: impl Into<String>,
        replace_with: impl Into<String>,
    ) -> Self {
        self.snapshot_replace
            .push((matcher.into(), replace_with.into()));
        self
    }

    pub fn replace_duration_labels(self) -> Self {
        self.replace_snapshot_content("\\d+\\.\\d+[µm]s", "{{DURATION}}")
            .replace_snapshot_content("\\d+µs", "{{DURATION}}")
    }

    pub fn replace_backslashes(self) -> Self {
        self.replace_snapshot_content("\\\\", "/")
    }
}

#[test]
fn snapshot_help_command() {
    Context::default()
        .arg("help")
        .snapshot_command("help_command");
}

#[test]
fn snapshot_short_help_command() {
    Context::default()
        .arg("-h")
        .snapshot_command("short_help_command");
}

#[test]
fn snapshot_process_help_command() {
    Context::default()
        .arg("process")
        .arg("--help")
        .snapshot_command("process_help_command");
}

#[test]
fn snapshot_minify_help_command() {
    Context::default()
        .arg("minify")
        .arg("--help")
        .snapshot_command("minify_help_command");
}

#[test]
fn snapshot_convert_help_command() {
    Context::default()
        .arg("convert")
        .arg("--help")
        .snapshot_command("convert_help_command");
}

#[test]
fn run_minify_command() {
    Context::default()
        .write_file("src/init.lua", "return 1 + 1\n")
        .arg("minify")
        .arg("src")
        .arg("out")
        .replace_duration_labels()
        .snapshot_command("run_minify_command")
        .snapshot_file("run_minify_command_init_out", "out/init.lua");
}

#[test]
fn run_minify_with_column_span_command() {
    Context::default()
        .write_file("src/init.lua", "return 1 + 1\n")
        .arg("minify")
        .arg("--column-span")
        .arg("2")
        .arg("src")
        .arg("out")
        .replace_duration_labels()
        .expect_success()
        .snapshot_file(
            "run_minify_with_column_span_command_init_out",
            "out/init.lua",
        );
}

#[test]
fn run_minify_with_column_span_in_config_command() {
    Context::default()
        .write_file("src/init.lua", "return 1 + 1\n")
        // minify does not read the configuration file anymore, so we should expect
        // the snapshot to have the default generator used
        .write_file(".darklua.json", "{ column_span: 2 }")
        .arg("minify")
        .arg("src")
        .arg("out")
        .replace_duration_labels()
        .expect_success()
        .snapshot_file(
            "run_minify_with_column_span_in_config_command_init_out",
            "out/init.lua",
        );
}

#[test]
fn run_minify_verbose_command() {
    Context::default()
        .write_file("src/init.lua", "return 1 + 1\n")
        .arg("minify")
        .arg("-v")
        .arg("src")
        .arg("out")
        .replace_duration_labels()
        .replace_backslashes()
        .snapshot_command("run_minify_verbose_command");
}

#[test]
fn run_process_command() {
    Context::default()
        .write_file("src/init.lua", "return 1 + 1\n")
        .arg("process")
        .arg("src")
        .arg("out")
        .replace_duration_labels()
        .snapshot_command("run_process_command")
        .snapshot_file("run_process_command_init_out", "out/init.lua");
}

#[test]
fn run_process_command_with_input_path_starting_with_dot() {
    Context::default()
        .write_file("src/init.lua", "return 1 + 1\n")
        .arg("process")
        .arg("./src")
        .arg("out")
        .replace_duration_labels()
        .snapshot_command("run_process_command")
        .snapshot_file("run_process_command_init_out", "out/init.lua");
}

#[test]
fn run_process_command_with_output_path_starting_with_dot() {
    Context::default()
        .write_file("src/init.lua", "return 1 + 1\n")
        .arg("process")
        .arg("src")
        .arg("./out")
        .replace_duration_labels()
        .snapshot_command("run_process_command")
        .snapshot_file("run_process_command_init_out", "out/init.lua");
}

#[test]
fn run_process_command_with_input_and_output_path_starting_with_dot() {
    Context::default()
        .write_file("src/init.lua", "return 1 + 1\n")
        .arg("process")
        .arg("./src")
        .arg("./out")
        .replace_duration_labels()
        .snapshot_command("run_process_command")
        .snapshot_file("run_process_command_init_out", "out/init.lua");
}

#[test]
fn run_process_command_with_input_path_starting_with_parent_dir() {
    Context::default()
        .write_file("src/init.lua", "return 1 + 1\n")
        .write_file("lib/.darklua.json", "{}")
        .arg("process")
        .arg("../src")
        .arg("out")
        .with_cwd("lib")
        .replace_duration_labels()
        .snapshot_command("run_process_command")
        .snapshot_file("run_process_command_init_out", "lib/out/init.lua");
}

#[test]
fn run_process_verbose_command() {
    Context::default()
        .write_file("src/init.lua", "return 1 + 1\n")
        .arg("process")
        .arg("-v")
        .arg("src")
        .arg("out")
        .replace_duration_labels()
        .replace_backslashes()
        .snapshot_command("run_process_verbose_command");
}

#[test]
fn run_process_single_file_custom_config_command() {
    Context::default()
        .write_file("test.lua", "return _G.CONSTANT\n")
        .write_file(
            "custom.json5",
            "{ rules: [{ rule: 'inject_global_value', identifier: 'CONSTANT', value: true }] }",
        )
        .arg("process")
        .arg("--config")
        .arg("custom.json5")
        .arg("test.lua")
        .arg("out.lua")
        .replace_duration_labels()
        .snapshot_command("run_process_single_file_custom_config")
        .snapshot_file("run_process_custom_config_command_out", "out.lua");
}

#[test]
fn run_process_single_file_custom_config_command_deprecated_config_path() {
    Context::default()
        .write_file("test.lua", "return _G.CONSTANT\n")
        .write_file(
            "custom.json5",
            "{ rules: [{ rule: 'inject_global_value', identifier: 'CONSTANT', value: true }] }",
        )
        .arg("process")
        .arg("--config-path")
        .arg("custom.json5")
        .arg("test.lua")
        .arg("out.lua")
        .replace_duration_labels()
        .snapshot_command("run_process_single_file_custom_config")
        .snapshot_file("run_process_custom_config_command_out", "out.lua");
}

#[test]
fn run_convert_command_on_json_file_with_output() {
    Context::default()
        .write_file("data.json", "{ \"property\": true }")
        .arg("convert")
        .arg("data.json")
        .arg("out.lua")
        .replace_duration_labels()
        .snapshot_command("run_convert_command_on_json_file_with_output")
        .snapshot_file("run_convert_command_on_json_file_out", "out.lua");
}

#[test]
fn run_convert_command_on_json_without_extension_file_with_output() {
    Context::default()
        .write_file("data", "{ \"property\": true }")
        .arg("convert")
        .arg("data")
        .arg("out.lua")
        .arg("--format")
        .arg("json")
        .replace_duration_labels()
        .snapshot_command("run_convert_command_on_json_without_extension_file_with_output")
        .snapshot_file("run_convert_command_on_json_file_out", "out.lua");
}

#[test]
fn run_convert_command_on_json_file() {
    Context::default()
        .write_file("data.json", "{ \"property\": true }")
        .arg("convert")
        .arg("data.json")
        .replace_duration_labels()
        .snapshot_command("run_convert_command_on_json_file_stdout");
}

#[test]
fn run_convert_command_errors_when_no_extension_and_no_format() {
    Context::default()
        .write_file("data", "{ \"property\": true }")
        .arg("convert")
        .arg("data")
        .arg("out.lua")
        .replace_duration_labels()
        .snapshot_command("run_convert_command_errors_when_no_extension_and_no_format");
}

#[test]
fn run_convert_command_errors_when_unrecognized_extension() {
    Context::default()
        .write_file("data.yoyo", "{ \"property\": true }")
        .arg("convert")
        .arg("data.yoyo")
        .arg("out.lua")
        .replace_duration_labels()
        .snapshot_command("run_convert_command_errors_when_unrecognized_extension");
}