fork/rules_rust

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

Jalal El MansouriAdd more solid and generic process wrapper (#372)09ec54ca

0.5.0
4.4 KiB171 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 <string>
18
19void basic_part1_test(std::string current_dir_arg) {
20  if (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[]) {
27  if (current_dir != "${pwd}") {
28    std::cerr << "error: unsubsituted ${pwd} not found.\n";
29    std::exit(1);
30  }
31  const std::string current_dir_env = "CURRENT_DIR=${pwd}/test_path";
32  bool found = false;
33  for (int i = 0; envp[i] != nullptr; ++i) {
34    if (current_dir_env == envp[i]) {
35      found = true;
36      break;
37    }
38  }
39  if (!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[]) {
46  if (current_dir.find("${pwd}") != std::string::npos) {
47    std::cerr << "error: argument ${pwd} substitution failed.\n";
48    std::exit(1);
49  }
50  bool found = false;
51  for (int i = 0; envp[i] != nullptr; ++i) {
52    const std::string env = envp[i];
53    if (env.rfind("CURRENT_DIR", 0) == 0) {
54      found = true;
55      if (env.find("${pwd}") != std::string::npos) {
56        std::cerr << "error: environment variable ${pwd} substitution failed.\n";
57        std::exit(1);
58      }
59      break;
60    }
61  }
62  if (!found) {
63    std::cerr << "CURRENT_DIR not found.\n";
64    std::exit(1);
65  }
66}
67
68void env_files_test(const char* envp[]) {
69  const 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  };
77  for (const std::string& env : must_exist) {
78    bool found = false;
79    for (int i = 0; envp[i] != nullptr; ++i) {
80      if (env == envp[i]) {
81        found = true;
82        break;
83      }
84    }
85    if (!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[]) {
93  const 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
106  for (const std::string& arg : must_exist) {
107    bool found = false;
108    for (int i = 0; i < argc; ++i) {
109      if (arg == argv[i]) {
110        found = true;
111        break;
112      }
113    }
114    if (!found) {
115      std::cerr << "error: argument \"" << arg << "\" not found.\n";
116      std::exit(1);
117    }
118  }
119}
120
121void test_stdout() {
122  for (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[]) {
139  if (argc < 4) {
140    std::cerr << "error: Invalid number of args exected at least 4 got " << argc
141              << ".\n";
142    return 1;
143  }
144  std::string test_config = argv[1];
145  bool combined = test_config == "combined";
146  if (combined || test_config == "basic") {
147    basic_part1_test(argv[2]);
148  }
149
150  if (combined || test_config == "subst-pwd") {
151    subst_pwd_test(argv[3], envp);
152  } else if (test_config == "basic") {
153    basic_part2_test(argv[3], envp);
154  }
155
156  if (combined || test_config == "env-files") {
157    env_files_test(envp);
158  }
159
160  if (combined || test_config == "arg-files") {
161    arg_files_test(argc, argv);
162  }
163
164  if (combined || test_config == "stdout") {
165    test_stdout();
166  }
167
168  if (combined || test_config == "stderr") {
169    test_stderr();
170  }
171}