mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-20 02:15:43 +00:00
Add tint::Command
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. Will be used to invoke HLSL and MSL shader compilers to verify our test generated code actually compiles. Change-Id: I5cd4ca63af9aaa29be7448bb4fa8422e6d42a8ce Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/41942 Commit-Queue: Ben Clayton <bclayton@google.com> Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
committed by
Commit Bot service account
parent
a87fda9225
commit
ce7e18e87c
81
src/utils/command.h
Normal file
81
src/utils/command.h
Normal file
@@ -0,0 +1,81 @@
|
||||
// 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_UTILS_COMMAND_H_
|
||||
#define SRC_UTILS_COMMAND_H_
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
namespace tint {
|
||||
namespace utils {
|
||||
|
||||
/// 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;
|
||||
|
||||
/// 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 utils
|
||||
} // namespace tint
|
||||
|
||||
#endif // SRC_UTILS_COMMAND_H_
|
||||
Reference in New Issue
Block a user