Skip to content

Commit 80647dd

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 35cf377 commit 80647dd

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
// strip terminating \r
36+
REQUIRE(
37+
Catch::trim(line) == "The system cannot find the file specified.");
38+
#else
39+
REQUIRE(
40+
line == "execvp no-such-binary failed: No such file or directory");
41+
#endif
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)