summaryrefslogtreecommitdiffstats
path: root/src/subprocess_test.cc
blob: 4eb878bbfea87a8f3db8ca15c6523c059ef55573 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2011 Google Inc. All Rights Reserved.
//
// 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.

#include "subprocess.h"

#include "test.h"

TEST(Subprocess, Ls) {
  Subprocess ls;
  EXPECT_TRUE(ls.Start("ls /"));

  // Pretend we discovered that stdout was ready for writing.
  ls.OnFDReady(ls.stdout_.fd_);

  EXPECT_TRUE(ls.Finish());
  EXPECT_NE("", ls.stdout_.buf_);
  EXPECT_EQ("", ls.stderr_.buf_);
}

TEST(Subprocess, BadCommand) {
  Subprocess subproc;
  EXPECT_TRUE(subproc.Start("ninja_no_such_command"));

  // Pretend we discovered that stderr was ready for writing.
  subproc.OnFDReady(subproc.stderr_.fd_);

  EXPECT_FALSE(subproc.Finish());
  EXPECT_EQ("", subproc.stdout_.buf_);
  EXPECT_NE("", subproc.stderr_.buf_);
}

TEST(SubprocessSet, Single) {
  SubprocessSet subprocs;
  Subprocess* ls = new Subprocess;
  EXPECT_TRUE(ls->Start("ls /"));
  subprocs.Add(ls);

  while (!ls->done()) {
    subprocs.DoWork();
  }
  ASSERT_TRUE(ls->Finish());
  ASSERT_NE("", ls->stdout_.buf_);

  ASSERT_EQ(1, subprocs.finished_.size());
}

TEST(SubprocessSet, Multi) {
  SubprocessSet subprocs;
  Subprocess* processes[3];
  const char* kCommands[3] = {
    "ls /",
    "whoami",
    "pwd",
  };

  for (int i = 0; i < 3; ++i) {
    processes[i] = new Subprocess;
    EXPECT_TRUE(processes[i]->Start(kCommands[i]));
    subprocs.Add(processes[i]);
  }

  ASSERT_EQ(3, subprocs.running_.size());
  for (int i = 0; i < 3; ++i) {
    ASSERT_FALSE(processes[i]->done());
    ASSERT_EQ("", processes[i]->stdout_.buf_);
    ASSERT_EQ("", processes[i]->stderr_.buf_);
  }

  while (!processes[0]->done() || !processes[1]->done() ||
         !processes[2]->done()) {
    ASSERT_GT(subprocs.running_.size(), 0);
    subprocs.DoWork();
  }

  ASSERT_EQ(0, subprocs.running_.size());
  ASSERT_EQ(3, subprocs.finished_.size());

  for (int i = 0; i < 3; ++i) {
    ASSERT_TRUE(processes[i]->Finish());
    ASSERT_NE("", processes[i]->stdout_.buf_);
    ASSERT_EQ("", processes[i]->stderr_.buf_);
    delete processes[i];
  }
}