Skip to content

Commit 8a7279c

Browse files
committed
eval: scrub temp file paths from rugo eval error messages
1 parent 259fd19 commit 8a7279c

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

cmd/cmd.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,15 @@ func evalAction(ctx context.Context, cmd *cli.Command) error {
370370

371371
comp := &compiler.Compiler{BaseDir: tmpDir}
372372
if err := comp.Run(srcFile); err != nil {
373-
return fmt.Errorf("%s", strings.ReplaceAll(err.Error(), srcFile, "eval"))
373+
msg := err.Error()
374+
// Replace the display path form (relative from CWD, used in error messages).
375+
if wd, err2 := os.Getwd(); err2 == nil {
376+
if rel, err2 := filepath.Rel(wd, srcFile); err2 == nil {
377+
msg = strings.ReplaceAll(msg, rel, "eval")
378+
}
379+
}
380+
msg = strings.ReplaceAll(msg, srcFile, "eval")
381+
return fmt.Errorf("%s", msg)
374382
}
375383
return nil
376384
}

rats/core/94_eval_cli_test.rugo

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use "test"
2+
use "str"
23

34
rats "rugo eval with inline argument"
45
result = test.run("rugo eval 'puts 1 + 1'")
@@ -53,3 +54,38 @@ rats "rugo eval compilation error"
5354
result = test.run("rugo eval 'def'")
5455
test.assert_neq(result["status"], 0)
5556
end
57+
58+
rats "rugo eval error shows eval not temp path"
59+
result = test.run("rugo eval 'def'")
60+
test.assert_contains(result["output"], "eval:")
61+
test.assert_false(str.contains(result["output"], "/tmp/"))
62+
test.assert_false(str.contains(result["output"], "rugo-eval"))
63+
end
64+
65+
rats "rugo eval unknown package error shows eval not temp path"
66+
result = test.run("rugo eval 'import \"nonexistent\"'")
67+
test.assert_neq(result["status"], 0)
68+
test.assert_contains(result["output"], "eval:")
69+
test.assert_false(str.contains(result["output"], "/tmp/"))
70+
end
71+
72+
rats "rugo eval semicolon error shows eval not temp path"
73+
result = test.run("rugo eval 'x = 1; y = 2'")
74+
test.assert_neq(result["status"], 0)
75+
test.assert_contains(result["output"], "eval:")
76+
test.assert_false(str.contains(result["output"], "/tmp/"))
77+
end
78+
79+
rats "rugo eval parse error shows eval not temp path"
80+
result = test.run("rugo eval 'if true'")
81+
test.assert_neq(result["status"], 0)
82+
test.assert_contains(result["output"], "eval:")
83+
test.assert_false(str.contains(result["output"], "/tmp/"))
84+
end
85+
86+
rats "rugo eval unknown module error shows eval not temp path"
87+
result = test.run("rugo eval 'use \"nonexistent\"'")
88+
test.assert_neq(result["status"], 0)
89+
test.assert_contains(result["output"], "eval:")
90+
test.assert_false(str.contains(result["output"], "/tmp/"))
91+
end

0 commit comments

Comments
 (0)