fork/rules_rust

git clone https://git.julia.blue/fork/rules_rust

Geoffry Songprocess_wrapper: Apply substitutions to param files (#1565)b04aa053

rust-analyzer
5.1 KiB190 linesraw
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) {
21  if (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[]) {
28  if (current_dir != "${pwd}") {
29    std::cerr << "error: unsubsituted ${pwd} not found.\n";
30    std::exit(1);
31  }
32  const std::string current_dir_env = "CURRENT_DIR=${pwd}/test_path";
33  bool found = false;
34  for (int i = 0; envp[i] != nullptr; ++i) {
35    if (current_dir_env == envp[i]) {
36      found = true;
37      break;
38    }
39  }
40  if (!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];
48  if (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;
54  for (int i = 1; i < argc; ++i) {
55    if (argv[i][0] == '@') {
56      param_file = std::string(argv[i]+1);
57      break;
58    }
59  }
60  if (param_file.empty()) {
61    std::cerr << "error: no param file.\n";
62    std::exit(1);
63  }
64  std::string param_file_line;
65  getline(std::ifstream(param_file), param_file_line);
66  if (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  }
70  bool found = false;
71  for (int i = 0; envp[i] != nullptr; ++i) {
72    const std::string env = envp[i];
73    if (env.rfind("CURRENT_DIR", 0) == 0) {
74      found = true;
75      if (env.find("${pwd}") != std::string::npos) {
76        std::cerr
77            << "error: environment variable ${pwd} substitution failed.\n";
78        std::exit(1);
79      }
80      break;
81    }
82  }
83  if (!found) {
84    std::cerr << "CURRENT_DIR not found.\n";
85    std::exit(1);
86  }
87}
88
89void env_files_test(const char* envp[]) {
90  const 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  };
98  for (const std::string& env : must_exist) {
99    bool found = false;
100    for (int i = 0; envp[i] != nullptr; ++i) {
101      if (env == envp[i]) {
102        found = true;
103        break;
104      }
105    }
106    if (!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[]) {
114  const 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
127  for (const std::string& arg : must_exist) {
128    bool found = false;
129    for (int i = 0; i < argc; ++i) {
130      if (arg == argv[i]) {
131        found = true;
132        break;
133      }
134    }
135    if (!found) {
136      std::cerr << "error: argument \"" << arg << "\" not found.\n";
137      std::exit(1);
138    }
139  }
140}
141
142void test_stdout() {
143  for (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[]) {
158  if (argc < 4) {
159    std::cerr << "error: Invalid number of args exected at least 4 got " << argc
160              << ".\n";
161    return 1;
162  }
163  std::string test_config = argv[1];
164  bool combined = test_config == "combined";
165  if (combined || test_config == "basic") {
166    basic_part1_test(argv[2]);
167  }
168
169  if (combined || test_config == "subst-pwd") {
170    subst_pwd_test(argc, argv, envp);
171  } else if (test_config == "basic") {
172    basic_part2_test(argv[3], envp);
173  }
174
175  if (combined || test_config == "env-files") {
176    env_files_test(envp);
177  }
178
179  if (combined || test_config == "arg-files") {
180    arg_files_test(argc, argv);
181  }
182
183  if (combined || test_config == "stdout") {
184    test_stdout();
185  }
186
187  if (combined || test_config == "stderr") {
188    test_stderr();
189  }
190}