cling/test/Prompt/OutputRedirect.C
Frederich Munch e0ddb56b20 Fixes for output redirection.
Decouple redirection state from MetaProcessor and MaybeRedirectOutputRAII.
Only suspend redirection when writing to the prompt.
Allow user to redirect to files named 1, 2, &1, &2.
Allow stderr and stdout to be redirected to one another.
Don't create _IO_2_1_stdout_ file.
2016-12-14 09:29:10 +01:00

129 lines
3.1 KiB
C

// RUN: cat %s | %cling | FileCheck --check-prefix=CHECKOUT %s
// RUN: cat /tmp/outfile.txt | FileCheck --check-prefix=CHECK-REDIRECTOUT %s
// RUN: cat /tmp/errfile.txt | FileCheck --check-prefix=CHECK-REDIRECTERR %s
// RUN: cat /tmp/bothfile.txt | FileCheck --check-prefix=CHECK-REDIRECTBOTH %s
// RUN: cat /tmp/anotheroutfile.txt | FileCheck --check-prefix=CHECK-REDIRECTANOTHER %s
// RUN: cat %s | %cling 2> /tmp/stderr.txt && cat /tmp/stderr.txt | FileCheck --check-prefix=CHECKERR %s
// RUN: cat %s | %cling 2>&1 | FileCheck --check-prefix=CHECKERR --check-prefix=CHECKOUT %s
#include <iostream>
.2>&1
std::cerr << "Error into stdout.\n";
//CHECKOUT: Error into stdout.
.2>
std::cerr << "Error back from stdout.\n";
//CHECKERR: Error back from stdout.
.1>&2
std::cout << "stdout into stderr.\n";
//CHECKERR: stdout into stderr.
.1>
std::cout << "stdout back from stderr.\n";
//CHECKOUT: stdout back from stderr.
// Test redirect stdout
.> /tmp/outfile.txt
int a = 101
//CHECK-REDIRECTOUT: (int) 101
int b = 102
//CHECK-REDIRECTOUT: (int) 102
int c = 103
//CHECK-REDIRECTOUT: (int) 103
// Test stderr is not redirected as well.
std::cerr << "Into Error\n";
//CHECKERR: Into Error
// Test toggle back to prompt.
.>
int var = 9
//CHECKOUT: (int) 9
// Test append mode.
.>> /tmp/outfile.txt
a = 991
//CHECK-REDIRECTOUT: (int) 991
b = 992
//CHECK-REDIRECTOUT: (int) 992
c = 993
//CHECK-REDIRECTOUT: (int) 993
// Test redirect stderr
.2> /tmp/errfile.txt
std::cerr << "Error redirected.\n";
//CHECK-REDIRECTERR: Error redirected.
// Test stdout is still redirected to the correct file.
var = 20
//CHECK-REDIRECTOUT: (int) 20
// Test toggle only stdout and stderr still redirected.
.>
a = 100
//CHECKOUT: (int) 100
std::cerr << "Error still redirected.\n";
//CHECK-REDIRECTERR: Error still redirected.
// Test toggle stderr back to prompt.
.2>
std::cerr << "Error back to prompt.\n";
//CHECKERR: Error back to prompt.
// Test redirect of both streams.
.&> /tmp/bothfile.txt
a=310
//CHECK-REDIRECTBOTH: (int) 310
b=311
//CHECK-REDIRECTBOTH: (int) 311
c=312
//CHECK-REDIRECTBOTH: (int) 312
std::cerr << "Redirect both out & err.\n";
//CHECK-REDIRECTBOTH: Redirect both out & err.
// Test toggle both back to the prompt.
.&>
var = 400
//CHECKOUT: (int) 400
std::cerr << "Both back to prompt.\n";
//CHECKERR: Both back to prompt.
// Test append mode for both streams.
.&>> /tmp/bothfile.txt
a=491
//CHECK-REDIRECTBOTH: (int) 491
b=492
//CHECK-REDIRECTBOTH: (int) 492
c=493
//CHECK-REDIRECTBOTH: (int) 493
std::cerr << "Append mode for both streams.\n";
//CHECK-REDIRECTBOTH: Append mode for both streams.
// Test toggle only stdout to prompt and stderr to file.
.>
var = 699
//CHECKOUT: (int) 699
std::cerr << "Err is still in &> file.\n";
//CHECK-REDIRECTBOTH: Err is still in &> file.
// Test toggle stderr to the prompt when redirected with &.
.2>
std::cerr << "Err back from &> file.\n";
//CHECKERR: Err back from &> file.
// Test redirect with & and toggle to out file.
.&>> /tmp/bothfile.txt
var = 999
//CHECK-REDIRECTBOTH: (int) 999
.1> /tmp/anotheroutfile.txt
a = 710
//CHECK-REDIRECTANOTHER: (int) 710
b = 711
//CHECK-REDIRECTANOTHER: (int) 711
c = 712
//CHECK-REDIRECTANOTHER: (int) 712