fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
09ec54ca
0.5.0
1// Copyright 2020 The Bazel Authors. All rights reserved. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15#include <cstdlib> 16#include <iostream> 17#include <string> 18 19void basic_part1_test (std::string current_dir_arg ) { 20if (current_dir_arg != "--current-dir" ) { 21 std::cerr <<"error: argument \"--current-dir\" not found.\n" ; 22 std::exit (1 ); 23 } 24} 25 26void basic_part2_test (std::string current_dir ,const char * envp []) { 27if (current_dir != "${pwd}" ) { 28 std::cerr <<"error: unsubsituted ${pwd} not found.\n" ; 29 std::exit (1 ); 30 } 31const std::string current_dir_env = "CURRENT_DIR=${pwd}/test_path" ; 32bool found = false; 33for (int i = 0 ;envp [i ]!= nullptr ;++ i ) { 34if (current_dir_env == envp [i ]) { 35found = true; 36break ; 37 } 38 } 39if (!found ) { 40 std::cerr <<"unsubsituted CURRENT_DIR not found.\n" ; 41 std::exit (1 ); 42 } 43} 44 45void subst_pwd_test (std::string current_dir ,const char * envp []) { 46if (current_dir .find ("${pwd}" )!= std::string::npos ) { 47 std::cerr <<"error: argument ${pwd} substitution failed.\n" ; 48 std::exit (1 ); 49 } 50bool found = false; 51for (int i = 0 ;envp [i ]!= nullptr ;++ i ) { 52const std::string env = envp [i ]; 53if (env .rfind ("CURRENT_DIR" ,0 )== 0 ) { 54found = true; 55if (env .find ("${pwd}" )!= std::string::npos ) { 56 std::cerr <<"error: environment variable ${pwd} substitution failed.\n" ; 57 std::exit (1 ); 58 } 59break ; 60 } 61 } 62if (!found ) { 63 std::cerr <<"CURRENT_DIR not found.\n" ; 64 std::exit (1 ); 65 } 66} 67 68void env_files_test (const char * envp []) { 69const std::string must_exist []= { 70"FOO=BAR" , 71"FOOBAR=BARFOO" , 72"BAR=FOO" , 73"ENV_ESCAPE=with\nnew line" , 74"ENV_NO_ESCAPE=with no new line\\" , 75"ENV_ESCAPE_WITH_BACKSLASH=new line\\\nhere" , 76 }; 77for (const std::string & env :must_exist ) { 78bool found = false; 79for (int i = 0 ;envp [i ]!= nullptr ;++ i ) { 80if (env == envp [i ]) { 81found = true; 82break ; 83 } 84 } 85if (!found ) { 86 std::cerr <<"error: environment variable \"" <<env <<"\" not found.\n" ; 87 std::exit (1 ); 88 } 89 } 90} 91 92void arg_files_test (int argc ,const char * argv []) { 93const std::string must_exist []= { 94"--arg1=foo" , 95"--arg2" , 96"foo bar" , 97"--arg2=bar" , 98"--arg3" , 99"foobar" , 100"arg with\nnew line" , 101"arg with\\" , 102"no new line" , 103"arg with\\\nnew line and a trailing backslash" , 104 }; 105 106for (const std::string & arg :must_exist ) { 107bool found = false; 108for (int i = 0 ;i < argc ;++ i ) { 109if (arg == argv [i ]) { 110found = true; 111break ; 112 } 113 } 114if (!found ) { 115 std::cerr <<"error: argument \"" <<arg <<"\" not found.\n" ; 116 std::exit (1 ); 117 } 118 } 119} 120 121void test_stdout () { 122for (int i = 0 ;i < 10000 ;++ i ) { 123// On windows writing LF to any stream in text mode gets changed to CRLF 124// Since the test file is saved using CRLF, we are forcing the same on 125// non windows systems 126#if defined(_WIN32 ) 127 std::cout <<"Child process to stdout : " <<i <<"\n" ; 128#else 129 std::cout <<"Child process to stdout : " <<i <<"\r\n" ; 130#endif // defined(_WIN32) 131 } 132} 133 134void test_stderr () { 135 std::cerr <<"This is the stderr output" ; 136} 137 138int main (int argc ,const char * argv [],const char * envp []) { 139if (argc < 4 ) { 140 std::cerr <<"error: Invalid number of args exected at least 4 got " <<argc 141 <<".\n" ; 142return 1 ; 143 } 144 std::string test_config = argv [1 ]; 145bool combined = test_config == "combined" ; 146if (combined || test_config == "basic" ) { 147basic_part1_test (argv [2 ]); 148 } 149 150if (combined || test_config == "subst-pwd" ) { 151subst_pwd_test (argv [3 ],envp ); 152 }else if (test_config == "basic" ) { 153basic_part2_test (argv [3 ],envp ); 154 } 155 156if (combined || test_config == "env-files" ) { 157env_files_test (envp ); 158 } 159 160if (combined || test_config == "arg-files" ) { 161arg_files_test (argc ,argv ); 162 } 163 164if (combined || test_config == "stdout" ) { 165test_stdout (); 166 } 167 168if (combined || test_config == "stderr" ) { 169test_stderr (); 170 } 171}