fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
b04aa053
rust-analyzer
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 <fstream> 18#include <string> 19 20void basic_part1_test (std::string current_dir_arg ) { 21if (current_dir_arg != "--current-dir" ) { 22 std::cerr <<"error: argument \"--current-dir\" not found.\n" ; 23 std::exit (1 ); 24 } 25} 26 27void basic_part2_test (std::string current_dir ,const char * envp []) { 28if (current_dir != "${pwd}" ) { 29 std::cerr <<"error: unsubsituted ${pwd} not found.\n" ; 30 std::exit (1 ); 31 } 32const std::string current_dir_env = "CURRENT_DIR=${pwd}/test_path" ; 33bool found = false; 34for (int i = 0 ;envp [i ]!= nullptr ;++ i ) { 35if (current_dir_env == envp [i ]) { 36found = true; 37break ; 38 } 39 } 40if (!found ) { 41 std::cerr <<"unsubsituted CURRENT_DIR not found.\n" ; 42 std::exit (1 ); 43 } 44} 45 46void subst_pwd_test (int argc ,const char * argv [],const char * envp []) { 47 std::string current_dir = argv [3 ]; 48if (current_dir .find ("${pwd}" )!= std::string::npos ) { 49 std::cerr <<"error: argument ${pwd} substitution failed.\n" ; 50 std::exit (1 ); 51 } 52// find the param file using its "@" prefix 53 std::string param_file ; 54for (int i = 1 ;i < argc ;++ i ) { 55if (argv [i ][0 ]== '@' ) { 56param_file = std::string (argv [i ]+ 1 ); 57break ; 58 } 59 } 60if (param_file .empty ()) { 61 std::cerr <<"error: no param file.\n" ; 62 std::exit (1 ); 63 } 64 std::string param_file_line ; 65getline (std::ifstream (param_file ),param_file_line ); 66if (param_file_line != current_dir ) { 67 std::cerr <<"error: param file " <<param_file <<" should contain " <<current_dir <<", found " <<param_file_line <<".\n" ; 68 std::exit (1 ); 69 } 70bool found = false; 71for (int i = 0 ;envp [i ]!= nullptr ;++ i ) { 72const std::string env = envp [i ]; 73if (env .rfind ("CURRENT_DIR" ,0 )== 0 ) { 74found = true; 75if (env .find ("${pwd}" )!= std::string::npos ) { 76 std::cerr 77 <<"error: environment variable ${pwd} substitution failed.\n" ; 78 std::exit (1 ); 79 } 80break ; 81 } 82 } 83if (!found ) { 84 std::cerr <<"CURRENT_DIR not found.\n" ; 85 std::exit (1 ); 86 } 87} 88 89void env_files_test (const char * envp []) { 90const std::string must_exist []= { 91"FOO=BAR" , 92"FOOBAR=BARFOO" , 93"BAR=FOO" , 94"ENV_ESCAPE=with\nnew line" , 95"ENV_NO_ESCAPE=with no new line\\" , 96"ENV_ESCAPE_WITH_BACKSLASH=new line\\\nhere" , 97 }; 98for (const std::string & env :must_exist ) { 99bool found = false; 100for (int i = 0 ;envp [i ]!= nullptr ;++ i ) { 101if (env == envp [i ]) { 102found = true; 103break ; 104 } 105 } 106if (!found ) { 107 std::cerr <<"error: environment variable \"" <<env <<"\" not found.\n" ; 108 std::exit (1 ); 109 } 110 } 111} 112 113void arg_files_test (int argc ,const char * argv []) { 114const std::string must_exist []= { 115"--arg1=foo" , 116"--arg2" , 117"foo bar" , 118"--arg2=bar" , 119"--arg3" , 120"foobar" , 121"arg with\nnew line" , 122"arg with\\" , 123"no new line" , 124"arg with\\\nnew line and a trailing backslash" , 125 }; 126 127for (const std::string & arg :must_exist ) { 128bool found = false; 129for (int i = 0 ;i < argc ;++ i ) { 130if (arg == argv [i ]) { 131found = true; 132break ; 133 } 134 } 135if (!found ) { 136 std::cerr <<"error: argument \"" <<arg <<"\" not found.\n" ; 137 std::exit (1 ); 138 } 139 } 140} 141 142void test_stdout () { 143for (int i = 0 ;i < 10000 ;++ i ) { 144// On windows writing LF to any stream in text mode gets changed to CRLF 145// Since the test file is saved using CRLF, we are forcing the same on 146// non windows systems 147#if defined(_WIN32 ) 148 std::cout <<"Child process to stdout : " <<i <<"\n" ; 149#else 150 std::cout <<"Child process to stdout : " <<i <<"\r\n" ; 151#endif // defined(_WIN32) 152 } 153} 154 155void test_stderr () { std::cerr <<"This is the stderr output" ; } 156 157int main (int argc ,const char * argv [],const char * envp []) { 158if (argc < 4 ) { 159 std::cerr <<"error: Invalid number of args exected at least 4 got " <<argc 160 <<".\n" ; 161return 1 ; 162 } 163 std::string test_config = argv [1 ]; 164bool combined = test_config == "combined" ; 165if (combined || test_config == "basic" ) { 166basic_part1_test (argv [2 ]); 167 } 168 169if (combined || test_config == "subst-pwd" ) { 170subst_pwd_test (argc ,argv ,envp ); 171 }else if (test_config == "basic" ) { 172basic_part2_test (argv [3 ],envp ); 173 } 174 175if (combined || test_config == "env-files" ) { 176env_files_test (envp ); 177 } 178 179if (combined || test_config == "arg-files" ) { 180arg_files_test (argc ,argv ); 181 } 182 183if (combined || test_config == "stdout" ) { 184test_stdout (); 185 } 186 187if (combined || test_config == "stderr" ) { 188test_stderr (); 189 } 190}