Skip to content

Commit

Permalink
feat: Print response optionally in BYOB shell-cli
Browse files Browse the repository at this point in the history
Bug: N/A
Change-Id: Id0fddea72f9003334c7fbbecf90ef99dace10aa7
GitOrigin-RevId: 7d6946b9758189458de1ddb80c85a509b75cf8d7
  • Loading branch information
Privacy Sandbox Team authored and copybara-github committed Oct 26, 2024
1 parent 77cfbca commit c8be812
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 29 deletions.
6 changes: 4 additions & 2 deletions src/roma/byob/test/example_shell.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ Usage: Echo <request_file> [response_file]
}
std::string line;
while (std::getline(ifs, line)) {
switch (evaluator.EvalAndPrint(line, /*disable_commands=*/true)) {
switch (evaluator.EvalAndPrint(line, /*disable_commands=*/true,
/*print_response=*/true)) {
case ShellEvaluator::NextStep::kExit:
return 0;
case ShellEvaluator::NextStep::kError:
Expand All @@ -131,7 +132,8 @@ Usage: Echo <request_file> [response_file]
std::string line;
std::cout << "> ";
while (std::getline(std::cin, line)) {
switch (evaluator.EvalAndPrint(line, /*disable_commands=*/false)) {
switch (evaluator.EvalAndPrint(line, /*disable_commands=*/false,
/*print_response=*/true)) {
case ShellEvaluator::NextStep::kExit:
return 0;
case ShellEvaluator::NextStep::kError:
Expand Down
8 changes: 5 additions & 3 deletions src/roma/byob/tools/shell_evaluator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ Usage: exit
} // namespace

ShellEvaluator::NextStep ShellEvaluator::EvalAndPrint(std::string_view line,
bool disable_commands) {
bool disable_commands,
bool print_response) {
const std::vector<std::string_view> command =
absl::StrSplit(line, ' ', absl::SkipWhitespace());
if (command.empty()) {
Expand Down Expand Up @@ -90,7 +91,7 @@ ShellEvaluator::NextStep ShellEvaluator::EvalAndPrint(std::string_view line,
while (std::getline(ifs, line)) {
// recurse with commands disabled
const NextStep loop_next_step =
EvalAndPrint(line, /*disable_commands=*/true);
EvalAndPrint(line, /*disable_commands=*/false, print_response);
switch (loop_next_step) {
case NextStep::kExit:
case NextStep::kError:
Expand Down Expand Up @@ -151,7 +152,8 @@ ShellEvaluator::NextStep ShellEvaluator::EvalAndPrint(std::string_view line,
ofs << *json_response;
}
std::cout << "{code_token=" << *it->second
<< ", response=" << *json_response << "}\n";
<< ", response=" << (print_response ? *json_response : "<elided>")
<< "}\n";
}
return NextStep::kContinue;
}
Expand Down
3 changes: 2 additions & 1 deletion src/roma/byob/tools/shell_evaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class ShellEvaluator {
std::string_view, std::string_view, std::string_view)>
execute_fn);

NextStep EvalAndPrint(std::string_view line, bool disable_commands);
NextStep EvalAndPrint(std::string_view line, bool disable_commands,
bool print_response);

private:
std::string_view service_specific_message_;
Expand Down
63 changes: 42 additions & 21 deletions src/roma/byob/tools/shell_evaluator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,47 +40,54 @@ absl::StatusOr<std::string> TrivialExecuteFn(
TEST(ShellEvaluatorTest, ContinueWithEmptyOrWhitespaceInputLine) {
ShellEvaluator evaluator(/*service_specific_message=*/"", /*rpcs=*/{},
TrivialLoadFn, TrivialExecuteFn);
EXPECT_EQ(evaluator.EvalAndPrint("", /*disable_commands=*/true),
EXPECT_EQ(evaluator.EvalAndPrint("", /*disable_commands=*/true,
/*print_response=*/true),
ShellEvaluator::NextStep::kContinue);
EXPECT_EQ(evaluator.EvalAndPrint(" \t\n", /*disable_commands=*/false),
EXPECT_EQ(evaluator.EvalAndPrint(" \t\n", /*disable_commands=*/false,
/*print_response=*/true),
ShellEvaluator::NextStep::kContinue);
}

TEST(ShellEvaluatorTest, ExitWhenInputSaysSo) {
ShellEvaluator evaluator(/*service_specific_message=*/"", /*rpcs=*/{},
TrivialLoadFn, TrivialExecuteFn);
EXPECT_EQ(evaluator.EvalAndPrint("exit", /*disable_commands=*/false),
EXPECT_EQ(evaluator.EvalAndPrint("exit", /*disable_commands=*/false,
/*print_response=*/true),
ShellEvaluator::NextStep::kExit);
}

TEST(ShellEvaluatorTest, ErrorWhenCommandsPassedNoArguments) {
ShellEvaluator evaluator(/*service_specific_message=*/"", /*rpcs=*/{},
TrivialLoadFn, TrivialExecuteFn);
EXPECT_EQ(evaluator.EvalAndPrint("commands", /*disable_commands=*/false),
EXPECT_EQ(evaluator.EvalAndPrint("commands", /*disable_commands=*/false,
/*print_response=*/true),
ShellEvaluator::NextStep::kError);
}

TEST(ShellEvaluatorTest, ErrorWhenCommandsFileDoesntExist) {
ShellEvaluator evaluator(/*service_specific_message=*/"", /*rpcs=*/{},
TrivialLoadFn, TrivialExecuteFn);
EXPECT_EQ(evaluator.EvalAndPrint("commands file_that_doesnt_exist.txt",
/*disable_commands=*/false),
/*disable_commands=*/false,
/*print_response=*/true),
ShellEvaluator::NextStep::kError);
}

TEST(ShellEvaluatorTest, ContinueWhenCommandsPassedEmptyFile) {
ShellEvaluator evaluator(/*service_specific_message=*/"", /*rpcs=*/{},
TrivialLoadFn, TrivialExecuteFn);
EXPECT_EQ(
evaluator.EvalAndPrint("commands /dev/null", /*disable_commands=*/false),
evaluator.EvalAndPrint("commands /dev/null", /*disable_commands=*/false,
/*print_response=*/true),
ShellEvaluator::NextStep::kContinue);
}

TEST(ShellEvaluatorTest, ErrorWhenInputSaysCommandsAndCommandsIsDisabled) {
ShellEvaluator evaluator(/*service_specific_message=*/"", /*rpcs=*/{},
TrivialLoadFn, TrivialExecuteFn);
EXPECT_EQ(
evaluator.EvalAndPrint("commands /dev/null", /*disable_commands=*/true),
evaluator.EvalAndPrint("commands /dev/null", /*disable_commands=*/true,
/*print_response=*/true),
ShellEvaluator::NextStep::kError);
}

Expand All @@ -92,7 +99,8 @@ TEST(ShellEvaluatorTest, ErrorWhenCommandsFileContainsCommands) {
ofs << "commands\n";
}
EXPECT_EQ(evaluator.EvalAndPrint("commands commands.txt",
/*disable_commands=*/false),
/*disable_commands=*/false,
/*print_response=*/true),
ShellEvaluator::NextStep::kError);
}

Expand All @@ -104,67 +112,78 @@ TEST(ShellEvaluatorTest, ExitWhenCommandsFileContainsExit) {
ofs << "exit\n";
}
EXPECT_EQ(evaluator.EvalAndPrint("commands exit.txt",
/*disable_commands=*/false),
/*disable_commands=*/false,
/*print_response=*/true),
ShellEvaluator::NextStep::kExit);
}

TEST(ShellEvaluatorTest, ContinueAfterHelpCall) {
ShellEvaluator evaluator(/*service_specific_message=*/"", /*rpcs=*/{},
TrivialLoadFn, TrivialExecuteFn);
EXPECT_EQ(evaluator.EvalAndPrint("help", /*disable_commands=*/false),
EXPECT_EQ(evaluator.EvalAndPrint("help", /*disable_commands=*/false,
/*print_response=*/true),
ShellEvaluator::NextStep::kContinue);
}

TEST(ShellEvaluatorTest, ErrorWhenLoadMissingArguments) {
ShellEvaluator evaluator(/*service_specific_message=*/"", {"rpc_1"},
TrivialLoadFn, TrivialExecuteFn);
EXPECT_EQ(evaluator.EvalAndPrint("load", /*disable_commands=*/false),
EXPECT_EQ(evaluator.EvalAndPrint("load", /*disable_commands=*/false,
/*print_response=*/true),
ShellEvaluator::NextStep::kError);
EXPECT_EQ(evaluator.EvalAndPrint("load rpc_1", /*disable_commands=*/false),
EXPECT_EQ(evaluator.EvalAndPrint("load rpc_1", /*disable_commands=*/false,
/*print_response=*/true),
ShellEvaluator::NextStep::kError);
}

TEST(ShellEvaluatorTest, ErrorWhenLoadingUnrecognizedRpc) {
ShellEvaluator evaluator(/*service_specific_message=*/"", /*rpcs=*/{},
TrivialLoadFn, TrivialExecuteFn);
EXPECT_EQ(evaluator.EvalAndPrint("load some_rpc /dev/null",
/*disable_commands=*/false),
/*disable_commands=*/false,
/*print_response=*/true),
ShellEvaluator::NextStep::kError);
}

TEST(ShellEvaluatorTest, ErrorWhenCommandUnrecognized) {
ShellEvaluator evaluator(/*service_specific_message=*/"", /*rpcs=*/{},
TrivialLoadFn, TrivialExecuteFn);
EXPECT_EQ(evaluator.EvalAndPrint("unregistered_rpc",
/*disable_commands=*/false),
/*disable_commands=*/false,
/*print_response=*/true),
ShellEvaluator::NextStep::kError);
}
TEST(ShellEvaluatorTest, ErrorWhenExecBeforeLoad) {
ShellEvaluator evaluator(/*service_specific_message=*/"", {"rpc_1"},
TrivialLoadFn, TrivialExecuteFn);
EXPECT_EQ(evaluator.EvalAndPrint("rpc_1 /dev/null",
/*disable_commands=*/false),
/*disable_commands=*/false,
/*print_response=*/true),
ShellEvaluator::NextStep::kError);
}

TEST(ShellEvaluatorTest, ErrorWhenExecWrongNumberOfArgs) {
ShellEvaluator evaluator(/*service_specific_message=*/"", {"rpc_1"},
TrivialLoadFn, TrivialExecuteFn);
EXPECT_EQ(evaluator.EvalAndPrint("load rpc_1 /dev/null",
/*disable_commands=*/false),
/*disable_commands=*/false,
/*print_response=*/true),
ShellEvaluator::NextStep::kContinue);
EXPECT_EQ(evaluator.EvalAndPrint("rpc_1", /*disable_commands=*/false),
EXPECT_EQ(evaluator.EvalAndPrint("rpc_1", /*disable_commands=*/false,
/*print_response=*/true),
ShellEvaluator::NextStep::kError);
}

TEST(ShellEvaluatorTest, ErrorWhenExecRequestFileNotFound) {
ShellEvaluator evaluator(/*service_specific_message=*/"", {"rpc_1"},
TrivialLoadFn, TrivialExecuteFn);
EXPECT_EQ(evaluator.EvalAndPrint("load rpc_1 /dev/null",
/*disable_commands=*/false),
/*disable_commands=*/false,
/*print_response=*/true),
ShellEvaluator::NextStep::kContinue);
EXPECT_EQ(evaluator.EvalAndPrint("rpc_1 nonexistant_file.txt",
/*disable_commands=*/false),
/*disable_commands=*/false,
/*print_response=*/true),
ShellEvaluator::NextStep::kError);
}

Expand All @@ -175,10 +194,12 @@ TEST(ShellEvaluatorTest, ExecAppendsToResponseFile) {
ShellEvaluator evaluator(/*service_specific_message=*/"", {"rpc_1"},
TrivialLoadFn, execute_fn);
EXPECT_EQ(evaluator.EvalAndPrint("load rpc_1 /dev/null",
/*disable_commands=*/false),
/*disable_commands=*/false,
/*print_response=*/true),
ShellEvaluator::NextStep::kContinue);
EXPECT_EQ(evaluator.EvalAndPrint("rpc_1 /dev/null response.txt",
/*disable_commands=*/false),
/*disable_commands=*/false,
/*print_response=*/true),
ShellEvaluator::NextStep::kContinue);
std::ifstream ifs("response.txt");
EXPECT_THAT(std::string(std::istreambuf_iterator<char>(ifs),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ Usage: {{$rpc.Name}} <request_file> [response_file]
}
std::string line;
while (std::getline(ifs, line)) {
switch (evaluator.EvalAndPrint(line, /*disable_commands=*/true)) {
switch (evaluator.EvalAndPrint(line, /*disable_commands=*/true, /*print_response=*/true)) {
case ShellEvaluator::NextStep::kExit:
return 0;
case ShellEvaluator::NextStep::kError:
Expand All @@ -153,7 +153,7 @@ Usage: {{$rpc.Name}} <request_file> [response_file]
std::string line;
std::cout << "> ";
while (std::getline(std::cin, line)) {
switch (evaluator.EvalAndPrint(line, /*disable_commands=*/false)) {
switch (evaluator.EvalAndPrint(line, /*disable_commands=*/false, /*print_response=*/true)) {
case ShellEvaluator::NextStep::kExit:
return 0;
case ShellEvaluator::NextStep::kError:
Expand Down

0 comments on commit c8be812

Please sign in to comment.