|
12 | 12 | #include "ze_api.h" |
13 | 13 | #include "zes_api.h" |
14 | 14 |
|
| 15 | +#include <fstream> |
| 16 | + |
15 | 17 | #if defined(_WIN32) |
| 18 | + #include <io.h> |
| 19 | + #include <cstdio> |
| 20 | + #include <fcntl.h> |
16 | 21 | #define putenv_safe _putenv |
17 | 22 | #else |
| 23 | + #include <cstdlib> |
| 24 | + #include <sys/types.h> |
| 25 | + #include <unistd.h> |
| 26 | + #define _dup dup |
| 27 | + #define _dup2 dup2 |
| 28 | + #define _close close |
18 | 29 | #define putenv_safe putenv |
19 | 30 | #endif |
20 | 31 |
|
@@ -365,4 +376,68 @@ TEST( |
365 | 376 | EXPECT_FALSE(zelCheckIsLoaderInTearDown()); |
366 | 377 | } |
367 | 378 |
|
| 379 | +class CaptureOutput { |
| 380 | +private: |
| 381 | + int original_fd; |
| 382 | + int fd; |
| 383 | + int stream; |
| 384 | + char filename[50] = "/tmp/capture_output_XXXXXX"; |
| 385 | + |
| 386 | +public: |
| 387 | + enum { Stdout = 1, Stderr = 2 }; |
| 388 | + |
| 389 | + CaptureOutput(int stream_) : stream(stream_) { |
| 390 | + original_fd = _dup(stream); |
| 391 | +#if defined(__linux__) |
| 392 | + fd = mkstemp(filename); |
| 393 | +#elif defined(_WIN32) |
| 394 | + tmpnam_s(filename, sizeof(filename)); |
| 395 | + _sopen_s(&fd, filename, _O_CREAT | _O_RDWR, _SH_DENYNO, _S_IREAD | _S_IWRITE); |
| 396 | +#endif |
| 397 | + fflush(nullptr); |
| 398 | + _dup2(fd, stream); |
| 399 | + _close(fd); |
| 400 | + } |
| 401 | + |
| 402 | + ~CaptureOutput() { |
| 403 | + if (original_fd != -1) { |
| 404 | + fflush(nullptr); |
| 405 | + _dup2(original_fd, stream); |
| 406 | + _close(original_fd); |
| 407 | + original_fd = -1; |
| 408 | + } |
| 409 | + if (remove(filename) != 0) { |
| 410 | + std::cerr << "Deleting file " << filename << " failed."; |
| 411 | + } |
| 412 | + } |
| 413 | + |
| 414 | + std::string GetOutput() { |
| 415 | + if (original_fd != -1) { |
| 416 | + fflush(nullptr); |
| 417 | + _dup2(original_fd, stream); |
| 418 | + _close(original_fd); |
| 419 | + original_fd = -1; |
| 420 | + } |
| 421 | + std::ifstream stream(filename); |
| 422 | + std::string output = std::string((std::istreambuf_iterator<char>(stream)), |
| 423 | + std::istreambuf_iterator<char>()); |
| 424 | + return output; |
| 425 | + } |
| 426 | +}; |
| 427 | + |
| 428 | +TEST( |
| 429 | + LoaderInitDrivers, |
| 430 | + GivenZeInitDriverWhenCalledThenNoOutputIsPrintedToStdout) { |
| 431 | + uint32_t pInitDriversCount = 0; |
| 432 | + ze_init_driver_type_desc_t desc = { ZE_STRUCTURE_TYPE_INIT_DRIVER_TYPE_DESC }; |
| 433 | + desc.flags = UINT32_MAX; |
| 434 | + desc.pNext = nullptr; |
| 435 | + |
| 436 | + CaptureOutput capture(CaptureOutput::Stdout); |
| 437 | + EXPECT_EQ(ZE_RESULT_SUCCESS, zeInitDrivers(&pInitDriversCount, nullptr, &desc)); |
| 438 | + |
| 439 | + std::string output = capture.GetOutput(); |
| 440 | + EXPECT_TRUE(output.empty()); |
| 441 | +} |
| 442 | + |
368 | 443 | } // namespace |
0 commit comments