fork/rules_rust

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

UebelAndreRan clang-format on all C++ sources (#1396)8fca4381

0.6.0
4.5 KiB170 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
57            << "error: environment variable ${pwd} substitution failed.\n";
58        std::exit(1);
59      }
60      break;
61    }
62  }
63  if (!found) {
64    std::cerr << "CURRENT_DIR not found.\n";
65    std::exit(1);
66  }
67}
68
69void env_files_test(const char* envp[]) {
70  const 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  };
78  for (const std::string& env : must_exist) {
79    bool found = false;
80    for (int i = 0; envp[i] != nullptr; ++i) {
81      if (env == envp[i]) {
82        found = true;
83        break;
84      }
85    }
86    if (!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[]) {
94  const 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
107  for (const std::string& arg : must_exist) {
108    bool found = false;
109    for (int i = 0; i < argc; ++i) {
110      if (arg == argv[i]) {
111        found = true;
112        break;
113      }
114    }
115    if (!found) {
116      std::cerr << "error: argument \"" << arg << "\" not found.\n";
117      std::exit(1);
118    }
119  }
120}
121
122void test_stdout() {
123  for (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[]) {
138  if (argc < 4) {
139    std::cerr << "error: Invalid number of args exected at least 4 got " << argc
140              << ".\n";
141    return 1;
142  }
143  std::string test_config = argv[1];
144  bool combined = test_config == "combined";
145  if (combined || test_config == "basic") {
146    basic_part1_test(argv[2]);
147  }
148
149  if (combined || test_config == "subst-pwd") {
150    subst_pwd_test(argv[3], envp);
151  } else if (test_config == "basic") {
152    basic_part2_test(argv[3], envp);
153  }
154
155  if (combined || test_config == "env-files") {
156    env_files_test(envp);
157  }
158
159  if (combined || test_config == "arg-files") {
160    arg_files_test(argc, argv);
161  }
162
163  if (combined || test_config == "stdout") {
164    test_stdout();
165  }
166
167  if (combined || test_config == "stderr") {
168    test_stderr();
169  }
170}