• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Tint Authors.
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 #ifndef SRC_UTILS_IO_COMMAND_H_
16 #define SRC_UTILS_IO_COMMAND_H_
17 
18 #include <string>
19 #include <utility>
20 
21 namespace tint {
22 namespace utils {
23 
24 /// Command is a helper used by tests for executing a process with a number of
25 /// arguments and an optional stdin string, and then collecting and returning
26 /// the process's stdout and stderr output as strings.
27 class Command {
28  public:
29   /// Output holds the output of the process
30   struct Output {
31     /// stdout from the process
32     std::string out;
33     /// stderr from the process
34     std::string err;
35     /// process error code
36     int error_code = 0;
37   };
38 
39   /// Constructor
40   /// @param path path to the executable
41   explicit Command(const std::string& path);
42 
43   /// Looks for an executable with the given name in the current working
44   /// directory, and if not found there, in each of the directories in the
45   /// `PATH` environment variable.
46   /// @param executable the executable name
47   /// @returns a Command which will return true for Found() if the executable
48   /// was found.
49   static Command LookPath(const std::string& executable);
50 
51   /// @return true if the executable exists at the path provided to the
52   /// constructor
53   bool Found() const;
54 
55   /// @returns the path of the command
Path()56   const std::string& Path() const { return path_; }
57 
58   /// Invokes the command with the given argument strings, blocking until the
59   /// process has returned.
60   /// @param args the string arguments to pass to the process
61   /// @returns the process output
62   template <typename... ARGS>
operator()63   Output operator()(ARGS... args) const {
64     return Exec({std::forward<ARGS>(args)...});
65   }
66 
67   /// Exec invokes the command with the given argument strings, blocking until
68   /// the process has returned.
69   /// @param args the string arguments to pass to the process
70   /// @returns the process output
71   Output Exec(std::initializer_list<std::string> args) const;
72 
73   /// @param input the input data to pipe to the process's stdin
SetInput(const std::string & input)74   void SetInput(const std::string& input) { input_ = input; }
75 
76  private:
77   std::string const path_;
78   std::string input_;
79 };
80 
81 }  // namespace utils
82 }  // namespace tint
83 
84 #endif  //  SRC_UTILS_IO_COMMAND_H_
85