Using Google Unit Test with own Shell












1














Basically I created a shell and I want to using the Google Unit Test to test if my shell does the something of the regular terminal, and I am wondering how can I copy the output of my shell output into a string and compare it. Simply, I have created a buffer which reads the output of a regular terminal, and I don't know how to use a buffer to read my shell output.
Here is my code for the Google Unit Test



TEST(lsTest, lsT) {
string bash_cmd = "ls";
std::array<char, 128> buffer;
string result;
FILE* pipe = popen(bash_cmd.c_str(),"r");
while(fgets(buffer.data(),128,pipe)!=NULL){
result += buffer.data();
}
Base* start = parse(bash_command);
start->execute(); // this would output the command of my shell
EXPECT_EQ(result,?output of start->execute()?);}


Since the execute() is a boolean function, I can't using the buffer to convert the output to a string. Is there any way to read to output of my shell into a string? Also, my shell doesn't contains any redirection which are >, >>,| tee, etc. It basically contains ls, echo,mkdir.










share|improve this question






















  • How does your shell prints output? Does it use std::cout or write(STDOUT_FILENO, ...) or printf? Does it also uses std::cerr or write(STDERR_FILENO, ...) or fprintf(stderr, ...)?
    – Kamil Cuk
    Nov 21 at 8:13












  • If it uses std::cout or standard C++ output ostreams, you can use asnwer from this thread.
    – Kamil Cuk
    Nov 21 at 8:24










  • It is using the execvp().
    – Chris Lo
    Nov 21 at 10:25










  • execvp. Ok, so you can only guess what output does the underlying command uses. So, what you need to do, is redirect STDOUT_FILENO into a stringstream and parse it. Create a mkfifo and then dup2(fifo, STDOUT_FILENO) and make sure to read from fifo into strngstream in another thread while the ls command is executing. In other words, you need to do similar as shell redirecters commands stdout like ls > buffer, only the buffer should be a stringstream or similar container.
    – Kamil Cuk
    Nov 21 at 10:27












  • with popen() you will get the stdout of your script - but you won't get eventual errors to stderr. See this stackoverflow.com/questions/6900577/c-popen-wont-catch-stderr
    – Sigismondo
    Nov 21 at 10:34
















1














Basically I created a shell and I want to using the Google Unit Test to test if my shell does the something of the regular terminal, and I am wondering how can I copy the output of my shell output into a string and compare it. Simply, I have created a buffer which reads the output of a regular terminal, and I don't know how to use a buffer to read my shell output.
Here is my code for the Google Unit Test



TEST(lsTest, lsT) {
string bash_cmd = "ls";
std::array<char, 128> buffer;
string result;
FILE* pipe = popen(bash_cmd.c_str(),"r");
while(fgets(buffer.data(),128,pipe)!=NULL){
result += buffer.data();
}
Base* start = parse(bash_command);
start->execute(); // this would output the command of my shell
EXPECT_EQ(result,?output of start->execute()?);}


Since the execute() is a boolean function, I can't using the buffer to convert the output to a string. Is there any way to read to output of my shell into a string? Also, my shell doesn't contains any redirection which are >, >>,| tee, etc. It basically contains ls, echo,mkdir.










share|improve this question






















  • How does your shell prints output? Does it use std::cout or write(STDOUT_FILENO, ...) or printf? Does it also uses std::cerr or write(STDERR_FILENO, ...) or fprintf(stderr, ...)?
    – Kamil Cuk
    Nov 21 at 8:13












  • If it uses std::cout or standard C++ output ostreams, you can use asnwer from this thread.
    – Kamil Cuk
    Nov 21 at 8:24










  • It is using the execvp().
    – Chris Lo
    Nov 21 at 10:25










  • execvp. Ok, so you can only guess what output does the underlying command uses. So, what you need to do, is redirect STDOUT_FILENO into a stringstream and parse it. Create a mkfifo and then dup2(fifo, STDOUT_FILENO) and make sure to read from fifo into strngstream in another thread while the ls command is executing. In other words, you need to do similar as shell redirecters commands stdout like ls > buffer, only the buffer should be a stringstream or similar container.
    – Kamil Cuk
    Nov 21 at 10:27












  • with popen() you will get the stdout of your script - but you won't get eventual errors to stderr. See this stackoverflow.com/questions/6900577/c-popen-wont-catch-stderr
    – Sigismondo
    Nov 21 at 10:34














1












1








1


0





Basically I created a shell and I want to using the Google Unit Test to test if my shell does the something of the regular terminal, and I am wondering how can I copy the output of my shell output into a string and compare it. Simply, I have created a buffer which reads the output of a regular terminal, and I don't know how to use a buffer to read my shell output.
Here is my code for the Google Unit Test



TEST(lsTest, lsT) {
string bash_cmd = "ls";
std::array<char, 128> buffer;
string result;
FILE* pipe = popen(bash_cmd.c_str(),"r");
while(fgets(buffer.data(),128,pipe)!=NULL){
result += buffer.data();
}
Base* start = parse(bash_command);
start->execute(); // this would output the command of my shell
EXPECT_EQ(result,?output of start->execute()?);}


Since the execute() is a boolean function, I can't using the buffer to convert the output to a string. Is there any way to read to output of my shell into a string? Also, my shell doesn't contains any redirection which are >, >>,| tee, etc. It basically contains ls, echo,mkdir.










share|improve this question













Basically I created a shell and I want to using the Google Unit Test to test if my shell does the something of the regular terminal, and I am wondering how can I copy the output of my shell output into a string and compare it. Simply, I have created a buffer which reads the output of a regular terminal, and I don't know how to use a buffer to read my shell output.
Here is my code for the Google Unit Test



TEST(lsTest, lsT) {
string bash_cmd = "ls";
std::array<char, 128> buffer;
string result;
FILE* pipe = popen(bash_cmd.c_str(),"r");
while(fgets(buffer.data(),128,pipe)!=NULL){
result += buffer.data();
}
Base* start = parse(bash_command);
start->execute(); // this would output the command of my shell
EXPECT_EQ(result,?output of start->execute()?);}


Since the execute() is a boolean function, I can't using the buffer to convert the output to a string. Is there any way to read to output of my shell into a string? Also, my shell doesn't contains any redirection which are >, >>,| tee, etc. It basically contains ls, echo,mkdir.







c++ shell






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 8:05









Chris Lo

61




61












  • How does your shell prints output? Does it use std::cout or write(STDOUT_FILENO, ...) or printf? Does it also uses std::cerr or write(STDERR_FILENO, ...) or fprintf(stderr, ...)?
    – Kamil Cuk
    Nov 21 at 8:13












  • If it uses std::cout or standard C++ output ostreams, you can use asnwer from this thread.
    – Kamil Cuk
    Nov 21 at 8:24










  • It is using the execvp().
    – Chris Lo
    Nov 21 at 10:25










  • execvp. Ok, so you can only guess what output does the underlying command uses. So, what you need to do, is redirect STDOUT_FILENO into a stringstream and parse it. Create a mkfifo and then dup2(fifo, STDOUT_FILENO) and make sure to read from fifo into strngstream in another thread while the ls command is executing. In other words, you need to do similar as shell redirecters commands stdout like ls > buffer, only the buffer should be a stringstream or similar container.
    – Kamil Cuk
    Nov 21 at 10:27












  • with popen() you will get the stdout of your script - but you won't get eventual errors to stderr. See this stackoverflow.com/questions/6900577/c-popen-wont-catch-stderr
    – Sigismondo
    Nov 21 at 10:34


















  • How does your shell prints output? Does it use std::cout or write(STDOUT_FILENO, ...) or printf? Does it also uses std::cerr or write(STDERR_FILENO, ...) or fprintf(stderr, ...)?
    – Kamil Cuk
    Nov 21 at 8:13












  • If it uses std::cout or standard C++ output ostreams, you can use asnwer from this thread.
    – Kamil Cuk
    Nov 21 at 8:24










  • It is using the execvp().
    – Chris Lo
    Nov 21 at 10:25










  • execvp. Ok, so you can only guess what output does the underlying command uses. So, what you need to do, is redirect STDOUT_FILENO into a stringstream and parse it. Create a mkfifo and then dup2(fifo, STDOUT_FILENO) and make sure to read from fifo into strngstream in another thread while the ls command is executing. In other words, you need to do similar as shell redirecters commands stdout like ls > buffer, only the buffer should be a stringstream or similar container.
    – Kamil Cuk
    Nov 21 at 10:27












  • with popen() you will get the stdout of your script - but you won't get eventual errors to stderr. See this stackoverflow.com/questions/6900577/c-popen-wont-catch-stderr
    – Sigismondo
    Nov 21 at 10:34
















How does your shell prints output? Does it use std::cout or write(STDOUT_FILENO, ...) or printf? Does it also uses std::cerr or write(STDERR_FILENO, ...) or fprintf(stderr, ...)?
– Kamil Cuk
Nov 21 at 8:13






How does your shell prints output? Does it use std::cout or write(STDOUT_FILENO, ...) or printf? Does it also uses std::cerr or write(STDERR_FILENO, ...) or fprintf(stderr, ...)?
– Kamil Cuk
Nov 21 at 8:13














If it uses std::cout or standard C++ output ostreams, you can use asnwer from this thread.
– Kamil Cuk
Nov 21 at 8:24




If it uses std::cout or standard C++ output ostreams, you can use asnwer from this thread.
– Kamil Cuk
Nov 21 at 8:24












It is using the execvp().
– Chris Lo
Nov 21 at 10:25




It is using the execvp().
– Chris Lo
Nov 21 at 10:25












execvp. Ok, so you can only guess what output does the underlying command uses. So, what you need to do, is redirect STDOUT_FILENO into a stringstream and parse it. Create a mkfifo and then dup2(fifo, STDOUT_FILENO) and make sure to read from fifo into strngstream in another thread while the ls command is executing. In other words, you need to do similar as shell redirecters commands stdout like ls > buffer, only the buffer should be a stringstream or similar container.
– Kamil Cuk
Nov 21 at 10:27






execvp. Ok, so you can only guess what output does the underlying command uses. So, what you need to do, is redirect STDOUT_FILENO into a stringstream and parse it. Create a mkfifo and then dup2(fifo, STDOUT_FILENO) and make sure to read from fifo into strngstream in another thread while the ls command is executing. In other words, you need to do similar as shell redirecters commands stdout like ls > buffer, only the buffer should be a stringstream or similar container.
– Kamil Cuk
Nov 21 at 10:27














with popen() you will get the stdout of your script - but you won't get eventual errors to stderr. See this stackoverflow.com/questions/6900577/c-popen-wont-catch-stderr
– Sigismondo
Nov 21 at 10:34




with popen() you will get the stdout of your script - but you won't get eventual errors to stderr. See this stackoverflow.com/questions/6900577/c-popen-wont-catch-stderr
– Sigismondo
Nov 21 at 10:34

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407611%2fusing-google-unit-test-with-own-shell%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407611%2fusing-google-unit-test-with-own-shell%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'