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
|
// SPDX-License-Identifier: MIT
// Copyright (C) 2025 Artem Senichev <artemsen@gmail.com>
extern "C" {
#include "shellcmd.h"
}
#include <gtest/gtest.h>
class ShellCmd : public ::testing::Test {
protected:
void TearDown() override { free(cmd); }
char* cmd;
};
TEST_F(ShellCmd, Expression)
{
cmd = shellcmd_expr("Expression: %/%%/ < %", "test123");
EXPECT_STREQ(cmd, "Expression: test123/%/ < test123");
}
TEST_F(ShellCmd, ExpressionEmpty)
{
cmd = shellcmd_expr("", "test123");
EXPECT_FALSE(cmd);
}
class ShellExec : public ::testing::Test {
protected:
void TearDown() override
{
arr_free(out);
arr_free(err);
}
struct array* out = nullptr;
struct array* err = nullptr;
};
TEST_F(ShellExec, Execute)
{
const char last_null = 0;
EXPECT_EQ(shellcmd_exec("echo out && echo err >&2", &out, &err), 0);
ASSERT_TRUE(out);
out = arr_append(out, &last_null, 1);
ASSERT_TRUE(out);
EXPECT_STREQ(reinterpret_cast<const char*>(out->data), "out\n");
ASSERT_TRUE(err);
err = arr_append(err, &last_null, 1);
ASSERT_TRUE(err);
EXPECT_STREQ(reinterpret_cast<const char*>(err->data), "err\n");
}
TEST_F(ShellExec, Fail)
{
EXPECT_EQ(shellcmd_exec("exit 42", &out, &err), 42);
EXPECT_FALSE(out);
EXPECT_FALSE(err);
}
TEST_F(ShellExec, Empty)
{
EXPECT_EQ(shellcmd_exec("", &out, &err), EINVAL);
EXPECT_FALSE(out);
EXPECT_FALSE(err);
}
TEST_F(ShellExec, Stdin)
{
// should not hang
EXPECT_NE(shellcmd_exec("read", &out, &err), 0);
}
class ShellBad : public ShellExec {
protected:
void SetUp() override
{
shell = getenv("SHELL");
setenv("SHELL", "/bad/shell", 1);
}
void TearDown() override
{
if (shell) {
setenv("SHELL", shell, 1);
} else {
unsetenv("SHELL");
}
ShellExec::TearDown();
}
const char* shell;
};
TEST_F(ShellBad, Execute)
{
EXPECT_EQ(shellcmd_exec("echo test123", &out, &err), ENOENT);
}
|