fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
8fca4381
0.7.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 57 <<"error: environment variable ${pwd} substitution failed.\n" ; 58 std::exit (1 ); 59 } 60break ; 61 } 62 } 63if (!found ) { 64 std::cerr <<"CURRENT_DIR not found.\n" ; 65 std::exit (1 ); 66 } 67} 68 69void env_files_test (const char * envp []) { 70const std::string must_exist []= { 71"FOO=BAR" , 72"FOOBAR=BARFOO" , 73"BAR=FOO" , 74"ENV_ESCAPE=with\nnew line" , 75"ENV_NO_ESCAPE=with no new line\\" , 76"ENV_ESCAPE_WITH_BACKSLASH=new line\\\nhere" , 77 }; 78for (const std::string & env :must_exist ) { 79bool found = false; 80for (int i = 0 ;envp [i ]!= nullptr ;++ i ) { 81if (env == envp [i ]) { 82found = true; 83break ; 84 } 85 } 86if (!found ) { 87 std::cerr <<"error: environment variable \"" <<env <<"\" not found.\n" ; 88 std::exit (1 ); 89 } 90 } 91} 92 93void arg_files_test (int argc ,const char * argv []) { 94const std::string must_exist []= { 95"--arg1=foo" , 96"--arg2" , 97"foo bar" , 98"--arg2=bar" , 99"--arg3" , 100"foobar" , 101"arg with\nnew line" , 102"arg with\\" , 103"no new line" , 104"arg with\\\nnew line and a trailing backslash" , 105 }; 106 107for (const std::string & arg :must_exist ) { 108bool found = false; 109for (int i = 0 ;i < argc ;++ i ) { 110if (arg == argv [i ]) { 111found = true; 112break ; 113 } 114 } 115if (!found ) { 116 std::cerr <<"error: argument \"" <<arg <<"\" not found.\n" ; 117 std::exit (1 ); 118 } 119 } 120} 121 122void test_stdout () { 123for (int i = 0 ;i < 10000 ;++ i ) { 124// On windows writing LF to any stream in text mode gets changed to CRLF 125// Since the test file is saved using CRLF, we are forcing the same on 126// non windows systems 127#if defined(_WIN32 ) 128 std::cout <<"Child process to stdout : " <<i <<"\n" ; 129#else 130 std::cout <<"Child process to stdout : " <<i <<"\r\n" ; 131#endif // defined(_WIN32) 132 } 133} 134 135void test_stderr () { std::cerr <<"This is the stderr output" ; } 136 137int main (int argc ,const char * argv [],const char * envp []) { 138if (argc < 4 ) { 139 std::cerr <<"error: Invalid number of args exected at least 4 got " <<argc 140 <<".\n" ; 141return 1 ; 142 } 143 std::string test_config = argv [1 ]; 144bool combined = test_config == "combined" ; 145if (combined || test_config == "basic" ) { 146basic_part1_test (argv [2 ]); 147 } 148 149if (combined || test_config == "subst-pwd" ) { 150subst_pwd_test (argv [3 ],envp ); 151 }else if (test_config == "basic" ) { 152basic_part2_test (argv [3 ],envp ); 153 } 154 155if (combined || test_config == "env-files" ) { 156env_files_test (envp ); 157 } 158 159if (combined || test_config == "arg-files" ) { 160arg_files_test (argc ,argv ); 161 } 162 163if (combined || test_config == "stdout" ) { 164test_stdout (); 165 } 166 167if (combined || test_config == "stderr" ) { 168test_stderr (); 169 } 170}