Skip to content

Commit f6edaf9

Browse files
committed
feature: Support --diff-context option to wolfram-cli paclet test
This commits code that was written many months ago.
1 parent a59a36a commit f6edaf9

4 files changed

Lines changed: 71 additions & 24 deletions

File tree

‎WolframCLI/Source/WolframCLI/WolframCLI.wl‎

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,23 @@ SetOptions[$Output, FormatType -> TerminalForm]
3737

3838
(*====================================*)
3939

40+
Options[CommandPacletTest] = {
41+
(*
42+
Maximum lines of context to include around textual diffs shown in
43+
failed test results.
44+
*)
45+
"DiffContext" -> 10
46+
}
47+
4048
(* Handle `$ wolfram paclet test` *)
4149
CommandPacletTest[
4250
pacletDir: _?StringQ,
43-
testsPath: _?StringQ | Automatic : Automatic
44-
] := Module[{
51+
testsPath: _?StringQ | Automatic : Automatic,
52+
OptionsPattern[]
53+
] := With[{
54+
diffContext = RaiseConfirmMatch[OptionValue["DiffContext"], _?IntegerQ]
55+
},
56+
Module[{
4557
result,
4658
linkObj
4759
},
@@ -200,7 +212,7 @@ CommandPacletTest[
200212

201213
logger = Function[testResult,
202214
summaryData[testResult["Outcome"]] += 1;
203-
printTestResult[testResult];
215+
printTestResult[testResult, diffContext];
204216
];
205217

206218
logger = <|
@@ -303,19 +315,13 @@ CommandPacletTest[
303315
)
304316
}];
305317
];
306-
]
318+
]]
307319

308320
(*------------------------------------*)
309321

310-
(*
311-
Function with argument structure used by the
312-
"Log(Success|Failure|MessagesFailure|Error)" events.
313-
*)
314-
testOutcomeLogger[test_TestResultObject] := printTestResult[test]
315-
316-
(*------------------------------------*)
322+
SetFallthroughError[printTestResult]
317323

318-
printTestResult[test_TestResultObject] := Module[{},
324+
printTestResult[test_TestResultObject, diffContext_?IntegerQ] := Module[{},
319325
Replace[test["Outcome"], {
320326
"Success" :> Print[Format[test, TerminalForm]],
321327
"Failure" :> Module[{
@@ -341,7 +347,7 @@ printTestResult[test_TestResultObject] := Module[{},
341347
a = Replace[test["ExpectedOutput"], HoldForm[x_] :> x];
342348
b = Replace[test["ActualOutput"], HoldForm[x_] :> x];
343349

344-
printTextualExprDiff[a, b]
350+
printTextualExprDiff[a, b, diffContext]
345351
],
346352
"Error" :> Module[{},
347353
Print[Format[test, TerminalForm]];
@@ -389,7 +395,7 @@ printTestResult[test_TestResultObject] := Module[{},
389395
a = Replace[test["ExpectedMessages"], HoldForm[x_] :> x];
390396
b = Replace[test["ActualMessages"], HoldForm[x_] :> x];
391397

392-
printTextualExprDiff[a, b]
398+
printTextualExprDiff[a, b, diffContext]
393399
],
394400
(* FIXME: Expand this to cover all outcomes *)
395401
other_ :> (
@@ -399,9 +405,14 @@ printTestResult[test_TestResultObject] := Module[{},
399405
}]
400406
]
401407

408+
(*------------------------------------*)
409+
410+
SetFallthroughError[printTextualExprDiff]
411+
402412
printTextualExprDiff[
403413
expr1_,
404-
expr2_
414+
expr2_,
415+
diffContext : _?IntegerQ : 10
405416
] := (
406417
Needs["CodeFormatter`" -> None];
407418

@@ -417,17 +428,17 @@ printTextualExprDiff[
417428

418429
Scan[
419430
Replace[{
420-
common:{__?StringQ} :> (
431+
common:{__?StringQ} :> Module[{context},
421432
(* If the diff is large, elide everything but the first and
422433
last 10 lines. *)
423-
If[Length[common] > 20,
424-
Print[" ", StringRiffle[Take[common, 10], "\n"]]
425-
Print[TerminalStyle["<<" <> ToString[Length[common] - 20] <> ">>", "Blue"]];
426-
Print[" ", StringRiffle[Take[common, -10], "\n"]]
434+
If[Length[common] > 2 * diffContext,
435+
Print[" ", StringRiffle[Take[common, diffContext], "\n"]]
436+
Print[TerminalStyle["<<" <> ToString[Length[common] - 2 * diffContext] <> ">>", "Blue"]];
437+
Print[" ", StringRiffle[Take[common, -diffContext], "\n"]]
427438
,
428439
Print[" ", StringRiffle[common, "\n"]]
429440
]
430-
),
441+
],
431442
{expected:{___?StringQ}, got:{___?StringQ}} :> (
432443
Print[TerminalStyle["- ", "Red"], TerminalStyle[StringRiffle[expected, "\n"], "Red"]];
433444
Print[TerminalStyle["+ ", "Green"], TerminalStyle[StringRiffle[got, "\n"], "Green"]];

‎crates/wolfram-cli/src/main.rs‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ enum PacletCommand {
125125

126126
/// Optional file or directory containing tests to be run.
127127
tests_path: Option<PathBuf>,
128+
129+
/// Lines of context to print before and after a diff in test output.
130+
#[arg(long, short = 'C')]
131+
diff_context: Option<usize>,
128132
},
129133
}
130134

@@ -214,7 +218,8 @@ fn handle_paclet_command(command: PacletCommand) {
214218
PacletCommand::Test {
215219
paclet_dir,
216220
tests_path,
217-
} => handle_paclet_test(paclet_dir, tests_path),
221+
diff_context,
222+
} => handle_paclet_test(paclet_dir, tests_path, diff_context),
218223
}
219224
}
220225

@@ -229,7 +234,8 @@ fn handle_wolfram() {
229234
let mut line = String::new();
230235

231236
loop {
232-
let Some(input_name) = process_until_ready_for_input(&mut kernel) else {
237+
let Some(input_name) = process_until_ready_for_input(&mut kernel)
238+
else {
233239
break;
234240
};
235241

@@ -607,6 +613,7 @@ fn handle_paclet_doc(
607613
fn handle_paclet_test(
608614
paclet_dir: Option<PathBuf>,
609615
tests_path: Option<PathBuf>,
616+
diff_context: Option<usize>,
610617
) {
611618
let paclet_dir = unwrap_path_or_default_to_current_dir(paclet_dir);
612619
let paclet_dir: &str = match paclet_dir.to_str() {
@@ -625,13 +632,32 @@ fn handle_paclet_test(
625632

626633
let mut args = vec![Expr::string(paclet_dir)];
627634

635+
//
636+
// Optional arguments
637+
//
638+
628639
if let Some(tests_path) = tests_path {
629640
let tests_path = tests_path
630641
.to_str()
631642
.expect("paclet tests path is not valid UTF-8");
632643
args.push(Expr::string(tests_path));
633644
}
634645

646+
//
647+
// Options
648+
//
649+
650+
if let Some(diff_context) = diff_context {
651+
args.push(Expr::rule(
652+
Expr::string("DiffContext"),
653+
Expr::from(
654+
i64::try_from(diff_context)
655+
.expect("diff context overflows i64"),
656+
),
657+
));
658+
}
659+
660+
635661
// Evaluate:
636662
//
637663
// CommandPacletTest[paclet_dir]

‎docs/CommandLineHelp.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,17 @@ Run tests defined for a paclet
125125

126126
This uses `` PacletTools`PacletTest `` to execute any tests defined by the specified paclet.
127127

128-
**Usage:** `wolfram-cli paclet test [PACLET_DIR] [TESTS_PATH]`
128+
**Usage:** `wolfram-cli paclet test [OPTIONS] [PACLET_DIR] [TESTS_PATH]`
129129

130130
###### **Arguments:**
131131

132132
* `<PACLET_DIR>` — Optional path to a paclet directory
133133
* `<TESTS_PATH>` — Optional file or directory containing tests to be run
134134

135+
###### **Options:**
136+
137+
* `-C`, `--diff-context <DIFF_CONTEXT>` — Lines of context to print before and after a diff in test output
138+
135139

136140

137141
<hr/>

‎docs/Development.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,10 @@ $ wolfram-cli paclet test ./build/ConnorGray__WolframCLI ./Tests
2424

2525
```
2626
$ wolfram print-terminal-form-debug
27+
```
28+
29+
#### Regenerate docs/CommandLineHelp.md
30+
31+
```
32+
$ cargo run -- print-all-help --markdown > docs/CommandLineHelp.md
2733
```

0 commit comments

Comments
 (0)