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 #include "src/utils/io/command.h"
16 
17 #include "gtest/gtest.h"
18 
19 namespace tint {
20 namespace utils {
21 namespace {
22 
23 #ifdef _WIN32
24 
TEST(CommandTest, Echo)25 TEST(CommandTest, Echo) {
26   auto cmd = Command::LookPath("cmd");
27   if (!cmd.Found()) {
28     GTEST_SKIP() << "cmd not found on PATH";
29   }
30 
31   auto res = cmd("/C", "echo", "hello world");
32   EXPECT_EQ(res.error_code, 0);
33   EXPECT_EQ(res.out, "hello world\r\n");
34   EXPECT_EQ(res.err, "");
35 }
36 
37 #else
38 
39 TEST(CommandTest, Echo) {
40   auto cmd = Command::LookPath("echo");
41   if (!cmd.Found()) {
42     GTEST_SKIP() << "echo not found on PATH";
43   }
44 
45   auto res = cmd("hello world");
46   EXPECT_EQ(res.error_code, 0);
47   EXPECT_EQ(res.out, "hello world\n");
48   EXPECT_EQ(res.err, "");
49 }
50 
51 TEST(CommandTest, Cat) {
52   auto cmd = Command::LookPath("cat");
53   if (!cmd.Found()) {
54     GTEST_SKIP() << "cat not found on PATH";
55   }
56 
57   cmd.SetInput("hello world");
58   auto res = cmd();
59   EXPECT_EQ(res.error_code, 0);
60   EXPECT_EQ(res.out, "hello world");
61   EXPECT_EQ(res.err, "");
62 }
63 
64 TEST(CommandTest, True) {
65   auto cmd = Command::LookPath("true");
66   if (!cmd.Found()) {
67     GTEST_SKIP() << "true not found on PATH";
68   }
69 
70   auto res = cmd();
71   EXPECT_EQ(res.error_code, 0);
72   EXPECT_EQ(res.out, "");
73   EXPECT_EQ(res.err, "");
74 }
75 
76 TEST(CommandTest, False) {
77   auto cmd = Command::LookPath("false");
78   if (!cmd.Found()) {
79     GTEST_SKIP() << "false not found on PATH";
80   }
81 
82   auto res = cmd();
83   EXPECT_NE(res.error_code, 0);
84   EXPECT_EQ(res.out, "");
85   EXPECT_EQ(res.err, "");
86 }
87 
88 #endif
89 
90 }  // namespace
91 }  // namespace utils
92 }  // namespace tint
93