summaryrefslogtreecommitdiffstats
path: root/src/subprocess_test.cc
blob: a72f1cb401cfc16088689ababf2e8a3e5161bb11 (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
// 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(NULL, "ls /"));

  // Pretend we discovered that stdout was ready for writing.
  ls.OnPipeReady();

  EXPECT_TRUE(ls.Finish());
  EXPECT_NE("", ls.GetOutput());
}

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

  // Pretend we discovered that stderr was ready for writing.
  subproc.OnPipeReady();

  EXPECT_FALSE(subproc.Finish());
  EXPECT_NE("", subproc.GetOutput());
}

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

  while (!ls->Done()) {
    subprocs.DoWork();
  }
  ASSERT_TRUE(ls->Finish());
  ASSERT_NE("", ls->GetOutput());

  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(NULL, 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]->GetOutput());
  }

  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]->GetOutput());
    delete processes[i];
  }
}