| // Copyright 2021 The Tint Authors. |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| #ifndef SRC_TINT_UTILS_COMMAND_COMMAND_H_ |
| #define SRC_TINT_UTILS_COMMAND_COMMAND_H_ |
| |
| #include <string> |
| #include <utility> |
| |
| namespace tint { |
| |
| /// Command is a helper used by tests for executing a process with a number of |
| /// arguments and an optional stdin string, and then collecting and returning |
| /// the process's stdout and stderr output as strings. |
| class Command { |
| public: |
| /// Output holds the output of the process |
| struct Output { |
| /// stdout from the process |
| std::string out; |
| /// stderr from the process |
| std::string err; |
| /// process error code |
| int error_code = 0; |
| }; |
| |
| /// Constructor |
| /// @param path path to the executable |
| explicit Command(const std::string& path); |
| |
| /// Looks for an executable with the given name in the current working |
| /// directory, and if not found there, in each of the directories in the |
| /// `PATH` environment variable. |
| /// @param executable the executable name |
| /// @returns a Command which will return true for Found() if the executable |
| /// was found. |
| static Command LookPath(const std::string& executable); |
| |
| /// @return true if the executable exists at the path provided to the |
| /// constructor |
| bool Found() const; |
| |
| /// @returns the path of the command |
| const std::string& Path() const { return path_; } |
| |
| /// Invokes the command with the given argument strings, blocking until the |
| /// process has returned. |
| /// @param args the string arguments to pass to the process |
| /// @returns the process output |
| template <typename... ARGS> |
| Output operator()(ARGS... args) const { |
| return Exec({std::forward<ARGS>(args)...}); |
| } |
| |
| /// Exec invokes the command with the given argument strings, blocking until |
| /// the process has returned. |
| /// @param args the string arguments to pass to the process |
| /// @returns the process output |
| Output Exec(std::initializer_list<std::string> args) const; |
| |
| /// @param input the input data to pipe to the process's stdin |
| void SetInput(const std::string& input) { input_ = input; } |
| |
| private: |
| std::string const path_; |
| std::string input_; |
| }; |
| |
| } // namespace tint |
| |
| #endif // SRC_TINT_UTILS_COMMAND_COMMAND_H_ |