Skip to content

Commit 1b533af

Browse files
committed
Add unit test for run()
We do implicitly use run() in various tests, but are unlikely to exercise error paths in those tests. Take care of doing so in a unit test.
1 parent 4568697 commit 1b533af

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

unit/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ SRC += analyses/ai/ai.cpp \
118118
util/prefix_filter.cpp \
119119
util/range.cpp \
120120
util/replace_symbol.cpp \
121+
util/run.cpp \
121122
util/sharing_map.cpp \
122123
util/sharing_node.cpp \
123124
util/simplify_expr.cpp \

unit/util/run.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*******************************************************************\
2+
3+
Module: Unit test for run.h/run.cpp
4+
5+
Author: Michael Tautschnig
6+
7+
\*******************************************************************/
8+
9+
#include <testing-utils/use_catch.h>
10+
11+
#include <util/run.h>
12+
#include <util/tempfile.h>
13+
14+
#include <fstream>
15+
16+
SCENARIO("run() error reporting", "[core][util][run]")
17+
{
18+
GIVEN("A command invoking a non-existent executable")
19+
{
20+
temporary_filet tmp_stderr("tmp.txt", "");
21+
22+
int result =
23+
run("no-such-binary", {"no-such-binary"}, "", "", tmp_stderr());
24+
25+
THEN("run returns a non-zero exit code")
26+
{
27+
REQUIRE(result != 0);
28+
}
29+
THEN("run provides an error message")
30+
{
31+
std::ifstream read_output{tmp_stderr()};
32+
std::string line;
33+
REQUIRE(std::getline(read_output, line));
34+
#ifdef _WIN32
35+
REQUIRE(line == "The system cannot find the file specified.");
36+
#else
37+
REQUIRE(
38+
line == "execvp no-such-binary failed: No such file or directory");
39+
#endif
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)