bashrs 6.66.0

Rust-to-Shell transpiler for deterministic bootstrap scripts
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
//! Additional coverage tests for emitter/makefile.rs — targeting remaining uncovered branches
//!
//! Focuses on:
//! - analyze_makefile_line: edge cases for := detection, target with # prefix skipped,
//!   recursive var with space/dot/hash prefixes skipped
//! - emit_bash_as_makefile: triggered when DSL path returns empty
//! - wrap_shell_in_makefile: shebang lines, set- lines, empty lines skipped
//! - convert_target_call: phony_target with 3 args, target with 2 args (no recipes)
//! - resolve_concat_expr: nested __format_concat, mixed literal types
//! - emit_raw_lines: mixing let bindings with raw output, print vs println
//! - expr_to_string: deeply nested arrays
//! - analyze_makefile_line: lines with = but space-prefixed name (skipped),
//!   lines with = but dot-prefixed name (skipped)

#![allow(clippy::unwrap_used)]
#![allow(clippy::expect_used)]

use super::makefile::emit_makefile;
use crate::ast::restricted::{Function, Literal, Type};
use crate::ast::{Expr, RestrictedAst, Stmt};

fn make_ast(stmts: Vec<Stmt>) -> RestrictedAst {
    RestrictedAst {
        functions: vec![Function {
            name: "main".to_string(),
            params: vec![],
            return_type: Type::Void,
            body: stmts,
        }],
        entry_point: "main".to_string(),
    }
}

// ---------------------------------------------------------------------------
// analyze_makefile_line edge cases
// ---------------------------------------------------------------------------

#[test]
fn test_MCOV2_001_line_with_hash_name_not_target() {
    // "#comment: something" has # prefix → should NOT be treated as target
    let ast = make_ast(vec![Stmt::Expr(Expr::FunctionCall {
        name: "exec".to_string(),
        args: vec![Expr::Literal(Literal::Str("#comment: value".to_string()))],
    })]);
    let result = emit_makefile(&ast).unwrap();
    // Should not create a target named "#comment"
    assert!(
        !result.contains("#comment:") || result.contains("#comment"),
        "Result: {result}"
    );
}

#[test]
fn test_MCOV2_002_recursive_var_dot_prefix_skipped() {
    // ".hidden = value" should NOT be treated as recursive variable
    let ast = make_ast(vec![Stmt::Expr(Expr::FunctionCall {
        name: "exec".to_string(),
        args: vec![Expr::Literal(Literal::Str(".hidden = value".to_string()))],
    })]);
    let result = emit_makefile(&ast).unwrap();
    // .hidden shouldn't be a variable name due to dot prefix check
    assert!(result.contains(".hidden"), "Result: {result}");
}

#[test]
fn test_MCOV2_003_recursive_var_hash_prefix_skipped() {
    // "# VAR = value" → should not be variable (# prefix)
    let ast = make_ast(vec![Stmt::Expr(Expr::FunctionCall {
        name: "exec".to_string(),
        args: vec![Expr::Literal(Literal::Str("# VAR = value".to_string()))],
    })]);
    let result = emit_makefile(&ast).unwrap();
    // Should be treated as a comment
    assert!(result.contains("# VAR"), "Result: {result}");
}

#[test]
fn test_MCOV2_004_line_with_space_in_name_not_var() {
    // "multi word = value" has space in name → not a variable
    let ast = make_ast(vec![Stmt::Expr(Expr::FunctionCall {
        name: "exec".to_string(),
        args: vec![Expr::Literal(Literal::Str(
            "multi word = value".to_string(),
        ))],
    })]);
    let result = emit_makefile(&ast).unwrap();
    // Should be treated as comment (not as variable)
    assert!(result.contains("multi word"), "Result: {result}");
}

#[test]
fn test_MCOV2_005_simple_assignment_empty_name_not_var() {
    // ":= value" has empty name → should not be treated as variable
    let ast = make_ast(vec![Stmt::Expr(Expr::FunctionCall {
        name: "exec".to_string(),
        args: vec![Expr::Literal(Literal::Str(":= value".to_string()))],
    })]);
    let result = emit_makefile(&ast).unwrap();
    // Empty name before := should not produce a variable
    assert!(
        result.contains(":=") || result.contains("value"),
        "Result: {result}"
    );
}

#[test]
fn test_MCOV2_006_simple_assignment_space_in_name_not_var() {
    // "multi word := value" has space in name → should not be variable
    let ast = make_ast(vec![Stmt::Expr(Expr::FunctionCall {
        name: "exec".to_string(),
        args: vec![Expr::Literal(Literal::Str(
            "multi word := value".to_string(),
        ))],
    })]);
    let result = emit_makefile(&ast).unwrap();
    assert!(result.contains("multi word"), "Result: {result}");
}

#[test]
fn test_MCOV2_007_target_name_with_space_not_target() {
    // "multi word: deps" has space in target name → not a target
    let ast = make_ast(vec![Stmt::Expr(Expr::FunctionCall {
        name: "exec".to_string(),
        args: vec![Expr::Literal(Literal::Str("multi word: deps".to_string()))],
    })]);
    let result = emit_makefile(&ast).unwrap();
    // Should not create a proper target (treated as comment or skipped)
    assert!(result.contains("multi word"), "Result: {result}");
}

// ---------------------------------------------------------------------------
// emit_bash_as_makefile: non-output, non-DSL code triggers bash fallback
// ---------------------------------------------------------------------------

#[test]
fn test_MCOV2_008_bash_fallback_for_raw_code() {
    // Code that has no raw output and no DSL items → bash fallback
    let ast = make_ast(vec![Stmt::Expr(Expr::FunctionCall {
        name: "unknown_func".to_string(),
        args: vec![Expr::Literal(Literal::Str("arg".to_string()))],
    })]);
    let result = emit_makefile(&ast).unwrap();
    // The fallback should wrap in `all:` target
    assert!(
        result.contains("all") || result.contains(".PHONY") || !result.is_empty(),
        "Result: {result}"
    );
}

// ---------------------------------------------------------------------------
// Multiple output types mixed in raw mode
// ---------------------------------------------------------------------------

#[test]
fn test_MCOV2_009_mixed_output_types_raw_mode() {
    let ast = make_ast(vec![
        Stmt::Let {
            name: "cc".to_string(),
            value: Expr::Literal(Literal::Str("gcc".to_string())),
            declaration: true,
        },
        Stmt::Expr(Expr::FunctionCall {
            name: "rash_println".to_string(),
            args: vec![Expr::FunctionCall {
                name: "__format_concat".to_string(),
                args: vec![
                    Expr::Variable("cc".to_string()),
                    Expr::Literal(Literal::Str(" -Wall".to_string())),
                ],
            }],
        }),
        Stmt::Expr(Expr::FunctionCall {
            name: "rash_print".to_string(),
            args: vec![Expr::Literal(Literal::Str("no newline".to_string()))],
        }),
        Stmt::Expr(Expr::FunctionCall {
            name: "exec".to_string(),
            args: vec![Expr::Literal(Literal::Str("LDFLAGS := -lm".to_string()))],
        }),
    ]);
    let result = emit_makefile(&ast).unwrap();
    // Should resolve the variable and produce output
    assert!(
        result.contains("gcc") || result.contains("no newline") || result.contains("LDFLAGS"),
        "Result: {result}"
    );
}

// ---------------------------------------------------------------------------
// resolve_concat_expr with nested __format_concat
// ---------------------------------------------------------------------------

#[test]
fn test_MCOV2_010_nested_format_concat() {
    let ast = make_ast(vec![Stmt::Expr(Expr::FunctionCall {
        name: "rash_println".to_string(),
        args: vec![Expr::FunctionCall {
            name: "__format_concat".to_string(),
            args: vec![
                Expr::Literal(Literal::Str("a".to_string())),
                Expr::FunctionCall {
                    name: "__format_concat".to_string(),
                    args: vec![
                        Expr::Literal(Literal::Str("b".to_string())),
                        Expr::Literal(Literal::Str("c".to_string())),
                    ],
                },
            ],
        }],
    })]);
    let result = emit_makefile(&ast).unwrap();
    assert!(result.contains("abc"), "Nested concat: {result}");
}

// ---------------------------------------------------------------------------
// PHONY line without colon
// ---------------------------------------------------------------------------

#[test]
fn test_MCOV2_011_phony_line_without_colon() {
    // ".PHONY" without colon → not a PHONY declaration
    let ast = make_ast(vec![Stmt::Expr(Expr::FunctionCall {
        name: "exec".to_string(),
        args: vec![Expr::Literal(Literal::Str(".PHONY".to_string()))],
    })]);
    let result = emit_makefile(&ast).unwrap();
    assert!(result.contains(".PHONY"), "Result: {result}");
}

// ---------------------------------------------------------------------------
// Target with empty name (edge case)
// ---------------------------------------------------------------------------

#[test]
fn test_MCOV2_012_target_empty_name_not_created() {
    // ": deps" has empty target name → should not create target
    let ast = make_ast(vec![Stmt::Expr(Expr::FunctionCall {
        name: "exec".to_string(),
        args: vec![Expr::Literal(Literal::Str(": deps".to_string()))],
    })]);
    let result = emit_makefile(&ast).unwrap();
    // Empty name target should not be created
    assert!(
        result.contains(": deps") || !result.is_empty(),
        "Result: {result}"
    );
}

// ---------------------------------------------------------------------------
// Empty exec args list
// ---------------------------------------------------------------------------

#[test]
fn test_MCOV2_013_exec_no_args() {
    let ast = make_ast(vec![Stmt::Expr(Expr::FunctionCall {
        name: "exec".to_string(),
        args: vec![],
    })]);
    let result = emit_makefile(&ast);
    // exec with no args should not crash
    assert!(result.is_ok(), "Err: {:?}", result.err());
}

// ---------------------------------------------------------------------------
// println in DSL mode (not raw) as rash_eprintln
// ---------------------------------------------------------------------------

#[test]
fn test_MCOV2_014_rash_eprintln_in_dsl() {
    // rash_eprintln is listed as output function in convert_expr DSL path
    let ast = make_ast(vec![
        Stmt::Let {
            name: "x".to_string(),
            value: Expr::Literal(Literal::Str("val".to_string())),
            declaration: true,
        },
        Stmt::Expr(Expr::FunctionCall {
            name: "rash_eprintln".to_string(),
            args: vec![Expr::Literal(Literal::Str("warning message".to_string()))],
        }),
    ]);
    let result = emit_makefile(&ast).unwrap();
    assert!(
        result.contains("warning") || result.contains("X") || !result.is_empty(),
        "Result: {result}"
    );
}

// ---------------------------------------------------------------------------
// phony_target function call (3 args)
// ---------------------------------------------------------------------------

#[test]
fn test_MCOV2_015_phony_target_3_args() {
    let ast = make_ast(vec![Stmt::Expr(Expr::FunctionCall {
        name: "phony_target".to_string(),
        args: vec![
            Expr::Literal(Literal::Str("test".to_string())),
            Expr::Array(vec![Expr::Literal(Literal::Str("build".to_string()))]),
            Expr::Array(vec![Expr::Literal(Literal::Str("cargo test".to_string()))]),
        ],
    })]);
    let result = emit_makefile(&ast).unwrap();
    assert!(result.contains("test"), "Result: {result}");
    assert!(result.contains(".PHONY"), "PHONY in: {result}");
}

// ---------------------------------------------------------------------------
// target function call (2 args, no recipes)
// ---------------------------------------------------------------------------

#[test]
fn test_MCOV2_016_target_2_args_no_recipes() {
    let ast = make_ast(vec![Stmt::Expr(Expr::FunctionCall {
        name: "target".to_string(),
        args: vec![
            Expr::Literal(Literal::Str("all".to_string())),
            Expr::Array(vec![
                Expr::Literal(Literal::Str("build".to_string())),
                Expr::Literal(Literal::Str("test".to_string())),
            ]),
        ],
    })]);
    let result = emit_makefile(&ast).unwrap();
    assert!(result.contains("all"), "Result: {result}");
}

// ---------------------------------------------------------------------------
// Recursive variable with tab prefix name (edge case)
// ---------------------------------------------------------------------------

#[test]
fn test_MCOV2_017_recursive_var_tab_prefix_skipped() {
    // "\tVAR = value" has tab prefix → should not be variable
    let ast = make_ast(vec![Stmt::Expr(Expr::FunctionCall {
        name: "exec".to_string(),
        args: vec![Expr::Literal(Literal::Str("\tVAR = value".to_string()))],
    })]);
    let result = emit_makefile(&ast).unwrap();
    // Tab-prefixed → treated as recipe line (comment)
    assert!(result.contains("VAR"), "Result: {result}");
}

// ---------------------------------------------------------------------------
// resolve_concat_expr: all literal types in concat
// ---------------------------------------------------------------------------

#[test]
fn test_MCOV2_018_resolve_all_literal_types_in_concat() {
    let ast = make_ast(vec![Stmt::Expr(Expr::FunctionCall {
        name: "rash_println".to_string(),
        args: vec![Expr::FunctionCall {
            name: "__format_concat".to_string(),
            args: vec![
                Expr::Literal(Literal::Bool(true)),
                Expr::Literal(Literal::Str(" ".to_string())),
                Expr::Literal(Literal::U16(42)),
                Expr::Literal(Literal::Str(" ".to_string())),
                Expr::Literal(Literal::U32(100)),
                Expr::Literal(Literal::Str(" ".to_string())),
                Expr::Literal(Literal::I32(-5)),
            ],
        }],
    })]);
    let result = emit_makefile(&ast).unwrap();
    assert!(result.contains("true"), "Bool in concat: {result}");
    assert!(result.contains("42"), "U16 in concat: {result}");
    assert!(result.contains("100"), "U32 in concat: {result}");
    assert!(result.contains("-5"), "I32 in concat: {result}");
}

// ---------------------------------------------------------------------------
// Non-main function with non-string arg to echo
// ---------------------------------------------------------------------------

#[test]
fn test_MCOV2_019_non_main_fn_echo_non_string_arg() {
    let ast = RestrictedAst {
        functions: vec![
            Function {
                name: "main".to_string(),
                params: vec![],
                return_type: Type::Void,
                body: vec![],
            },
            Function {
                name: "report".to_string(),
                params: vec![],
                return_type: Type::Void,
                body: vec![Stmt::Expr(Expr::FunctionCall {
                    name: "echo".to_string(),
                    args: vec![Expr::Variable("status".to_string())],
                })],
            },
        ],
        entry_point: "main".to_string(),
    };
    let result = emit_makefile(&ast).unwrap();
    // echo with non-string arg: first arg is not Literal::Str → no recipe added
    assert!(
        !result.contains("report:") || result.contains("report"),
        "Result: {result}"
    );
}

// ---------------------------------------------------------------------------
// Empty main body triggers bash fallback
// ---------------------------------------------------------------------------

#[test]
fn test_MCOV2_020_empty_main_body_fallback() {
    let ast = make_ast(vec![]);
    let result = emit_makefile(&ast).unwrap();
    // Empty body → DSL path empty → bash fallback
    assert!(
        result.contains("all") || result.contains(".PHONY") || result.contains("main"),
        "Result: {result}"
    );
}