File: BuildSystemFrontendTest.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (777 lines) | stat: -rw-r--r-- 20,880 bytes parent folder | download
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
//===- unittests/BuildSystem/BuildSystemFrontendTest.cpp ------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#include "TempDir.h"

#include "llbuild/Basic/FileSystem.h"
#include "llbuild/BuildSystem/BuildDescription.h"
#include "llbuild/BuildSystem/BuildFile.h"
#include "llbuild/BuildSystem/BuildKey.h"
#include "llbuild/BuildSystem/BuildSystemFrontend.h"
#include "llbuild/BuildSystem/Command.h"
#include "llbuild/BuildSystem/Tool.h"

#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Path.h"
#include "llvm/ADT/StringRef.h"

#include "gtest/gtest.h"

#include <condition_variable>
#include <mutex>
#include <thread>
#include <unordered_set>

#ifdef __APPLE__
#include <TargetConditionals.h>
#endif

using namespace llbuild;
using namespace llbuild::basic;
using namespace llbuild::buildsystem;
using namespace llvm;


// TODO Move this into some kind of libtestSupport?
#define ASSERT_NO_ERROR(code)                       \
  do {                                              \
    std::error_code __ec;                           \
    ASSERT_FALSE(__ec = (code)) << __ec.message();  \
  } while (0)

namespace {

/// Records delegate callbacks and makes them available via ``getTrace()``
/// and ``checkTrace()``.
///
/// Allows for command skipping via ``commandsToSkip``.
class TestBuildSystemFrontendDelegate : public BuildSystemFrontendDelegate {
  using super = BuildSystemFrontendDelegate;

  std::mutex traceMutex;
  std::string traceData;
  raw_string_ostream traceStream;
  int currentlyRunningTasks = 0;
  int maxRunningTasks = 0;

public:
  TestBuildSystemFrontendDelegate(SourceMgr& sourceMgr)
    : BuildSystemFrontendDelegate(sourceMgr, "client", 0),
      traceStream(traceData)
  {
  }

  int maxTaskParallism() { return maxRunningTasks; }

  std::unordered_set<std::string> commandsToSkip;

  void clearTrace() {
    std::lock_guard<std::mutex> lock(traceMutex);
    traceStream.flush();
    traceData.clear();
  }

  const std::string& getTrace() {
    std::lock_guard<std::mutex> lock(traceMutex);
    return traceStream.str();
  }

  bool checkTrace(StringRef expected) {
    expected = expected.ltrim();

    bool result = expected == getTrace();
    if (!result) {
      errs() << "error: failed to match expected trace.\n\n"
             << "Actual:\n" << getTrace() << "\n"
             << "Expected:\n" << expected << "\n";

      TmpDir tmpDir;

      std::string outputActual = tmpDir.str() + "/actual.log";
      {
        std::error_code ec;
        llvm::raw_fd_ostream os(outputActual, ec, llvm::sys::fs::F_Text);
        assert(!ec);
        os << getTrace();
        os.close();
        assert(!os.has_error());
      }

      std::string outputExpected = tmpDir.str() + "/expected.log";
      {
        std::error_code ec;
        llvm::raw_fd_ostream os(outputExpected, ec, llvm::sys::fs::F_Text);
        assert(!ec);
        os << expected;
        os.close();
        assert(!os.has_error());
      }

#if !TARGET_OS_IPHONE
      errs() << "Diff:\n";
      system(("diff '" + outputActual + "' '"+ outputExpected +"'").c_str());
#endif
    }

    return result;
  }

  virtual void hadCommandFailure() override {
    {
      std::lock_guard<std::mutex> lock(traceMutex);
      traceStream << __func__ << "\n";
    }
    super::hadCommandFailure();
  }

  virtual void commandPreparing(Command* command) override {
    {
      std::lock_guard<std::mutex> lock(traceMutex);
      traceStream << __func__ << ": " << command->getName() << "\n";
    }
    super::commandPreparing(command);
  }

  virtual bool shouldCommandStart(Command* command) override {
    {
      std::lock_guard<std::mutex> lock(traceMutex);
      traceStream << __func__ << ": " << command->getName() << "\n";
    }

    if (commandsToSkip.find(command->getName()) != commandsToSkip.end()) {
      return false;
    }

    return super::shouldCommandStart(command);
  }

  virtual void commandStarted(Command* command) override {
    {
      std::lock_guard<std::mutex> lock(traceMutex);
      traceStream << __func__ << ": " << command->getName() << "\n";
    }
    super::commandStarted(command);
  }

  virtual void commandFinished(Command* command, ProcessStatus result) override {
    {
      std::lock_guard<std::mutex> lock(traceMutex);
      traceStream << __func__ << ": " << command->getName() << ": "
                  << (int)result << "\n";
    }
    super::commandFinished(command, result);
  }

  virtual void commandProcessStarted(Command* command, ProcessHandle handle) override {
    {
      std::lock_guard<std::mutex> lock(traceMutex);

      // Keep track of max build parallelism
      currentlyRunningTasks++;
      if (currentlyRunningTasks > maxRunningTasks)
        maxRunningTasks = currentlyRunningTasks;

      traceStream << __func__ << ": " << command->getName() << "\n";
    }
    super::commandProcessStarted(command, handle);
  }

  virtual void commandProcessHadError(Command* command, ProcessHandle handle,
                                      const Twine& message) override {
    {
      std::lock_guard<std::mutex> lock(traceMutex);
      traceStream << __func__ << ": " << command->getName() << ": " << message
                  << "\n";
    }
    super::commandProcessHadError(command, handle, message);
  }

  virtual void commandProcessFinished(Command* command, ProcessHandle handle,
                                      const ProcessResult& result) override {
    {
      std::lock_guard<std::mutex> lock(traceMutex);
      currentlyRunningTasks--;
      traceStream << __func__ << ": " << command->getName() << ": "
                  << result.exitCode << "\n";
    }
    super::commandProcessFinished(command, handle, result);
  }

  virtual std::unique_ptr<Tool> lookupTool(StringRef name) override {
    return nullptr;
  }

  virtual void cycleDetected(const std::vector<core::Rule*>& items) override { }

  virtual void error(StringRef filename, const Token& at, const Twine& message) override {
    std::lock_guard<std::mutex> lock(traceMutex);
    traceStream << __func__ << ": " << message << "\n";
  }
};


/// Sets up state and provides utils for build system frontend tests.
class BuildSystemFrontendTest : public ::testing::Test {
protected:
  TmpDir tempDir{"FrontendTest"};
  std::unique_ptr<FileSystem> fs = createLocalFileSystem();
  SourceMgr sourceMgr;
  BuildSystemInvocation invocation;

  virtual void SetUp() override {
    invocation.chdirPath = tempDir.str();
    invocation.traceFilePath = tempDir.str() + "/trace.log";
  }

  void writeBuildFile(StringRef s) {
    std::error_code ec;
    raw_fd_ostream os(std::string(tempDir.str()) + "/build.llbuild", ec,
                      llvm::sys::fs::F_Text);
    ASSERT_NO_ERROR(ec);

    os << s;
  }
};


// We have commands { 3 -> 2 -> 1 }. We'd like to skip 2, but we still
// want 1 and 3 to run.
TEST_F(BuildSystemFrontendTest, commandSkipping) {
  writeBuildFile(R"END(
client:
    name: client

targets:
    "": ["3"]

commands:
    1:
        tool: shell
        outputs: ["1"]
        args: touch 1

    2:
        tool: shell
        inputs: ["1"]
        outputs: ["2"]
        args: touch 2

    3:
        tool: shell
        inputs: ["2"]
        outputs: ["3"]
        args: touch 3
)END");

  {
    TestBuildSystemFrontendDelegate delegate(sourceMgr);
    delegate.commandsToSkip.insert("2");

    BuildSystemFrontend frontend(delegate, invocation, createLocalFileSystem());
    ASSERT_TRUE(frontend.build(""));

    ASSERT_TRUE(delegate.checkTrace(R"END(
commandPreparing: 3
commandPreparing: 2
commandPreparing: 1
shouldCommandStart: 1
commandStarted: 1
commandProcessStarted: 1
commandProcessFinished: 1: 0
commandFinished: 1: 0
shouldCommandStart: 2
commandFinished: 2: 3
shouldCommandStart: 3
commandStarted: 3
commandProcessStarted: 3
commandProcessFinished: 3: 0
commandFinished: 3: 0
)END"));

    ASSERT_FALSE(fs->getFileInfo(tempDir.str() + "/1").isMissing());
    ASSERT_TRUE(fs->getFileInfo(tempDir.str() + "/2").isMissing());
    ASSERT_FALSE(fs->getFileInfo(tempDir.str() + "/3").isMissing());
  }

  // If we rebuild incrementally without skipping, we expect to run 2 and
  // re-run 3. 1 doesn't have to run at all, so we don't expect to get asked
  // if it should start.
  {
    TestBuildSystemFrontendDelegate delegate(sourceMgr);

    BuildSystemFrontend frontend(delegate, invocation, createLocalFileSystem());
    ASSERT_TRUE(frontend.build(""));

    ASSERT_TRUE(delegate.checkTrace(R"END(
commandPreparing: 2
shouldCommandStart: 2
commandStarted: 2
commandProcessStarted: 2
commandProcessFinished: 2: 0
commandFinished: 2: 0
commandPreparing: 3
shouldCommandStart: 3
commandStarted: 3
commandProcessStarted: 3
commandProcessFinished: 3: 0
commandFinished: 3: 0
)END"));

    ASSERT_FALSE(fs->getFileInfo(tempDir.str() + "/2").isMissing());
  }

}


// We have a dependency tree such that three branches, left, middle, and right,
// should be able to run in parallel. The tree contains both real dependcies on
// the products and virtual/phony dependencies. This tests what parallism we
// actually achieve in both a clean build and subsequent incremental builds.
TEST_F(BuildSystemFrontendTest, dependencyScanOrderBuildParallelism) {
  writeBuildFile(R"END(
client:
    name: client

targets:
    "<all>": ["top"]

commands:
    top:
        tool: shell
        inputs: ["left", "middle", "right", "<left>", "<middle>", "<right>"]
        outputs: ["top"]
        args: touch top

    gate-left:
        tool: phony
        inputs: ["left"]
        outputs: ["<left>"]
    left:
        tool: shell
        inputs: ["base", "<base>"]
        outputs: ["left"]
        args: sleep 1 && touch left

    gate-middle:
        tool: phony
        inputs: ["middle"]
        outputs: ["<middle>"]
    middle:
        tool: shell
        inputs: ["base", "<base>"]
        outputs: ["middle"]
        args: sleep 1 && touch middle

    gate-right:
        tool: phony
        inputs: ["right"]
        outputs: ["<right>"]
    right:
        tool: shell
        inputs: ["base", "<base>"]
        outputs: ["right"]
        args: sleep 1 && touch right

    gate-base:
        tool: phony
        inputs: ["base"]
        outputs: ["<base>"]
    base:
        tool: shell
        outputs: ["base"]
        args: touch base
        always-out-of-date: true
)END");

  // Test a clean build
  {
    TestBuildSystemFrontendDelegate delegate(sourceMgr);
    BuildSystemFrontend frontend(delegate, invocation, createLocalFileSystem());
    ASSERT_TRUE(frontend.build("<all>"));

    ASSERT_EQ(delegate.maxTaskParallism(), std::min(3, (int)std::thread::hardware_concurrency()));
  }

  // Test an 'incremental' build where we have an existing build database
  {
    TestBuildSystemFrontendDelegate delegate(sourceMgr);
    BuildSystemFrontend frontend(delegate, invocation, createLocalFileSystem());
    ASSERT_TRUE(frontend.build("<all>"));

    // FIXME: This build graph triggers degenerate build behavior. Due to the
    // way we scan tasks, we end up waiting for the first task to resolve before
    // we unlock the other two branches to run in parallel.
    ASSERT_EQ(delegate.maxTaskParallism(), std::min(2, (int)std::thread::hardware_concurrency()));
  }
}


TEST_F(BuildSystemFrontendTest, missingShellArguments) {
  writeBuildFile(R"END(
client:
    name: basic

commands:
    C0:
        tool: shell
        args: []
)END");

  TestBuildSystemFrontendDelegate delegate(sourceMgr);

  BuildSystemFrontend frontend(delegate, invocation, createLocalFileSystem());
  ASSERT_FALSE(frontend.build(""));

  ASSERT_TRUE(delegate.checkTrace(R"END(
error: unable to configure client
error: unable to load build file
)END"));
}



class TestCancellationFrontendDelegate : public TestBuildSystemFrontendDelegate {
  using super = TestBuildSystemFrontendDelegate;

  std::mutex shouldCancelMutex;
  std::condition_variable shouldCancelCond;
  std::atomic<bool> shouldCancel{false};
  std::atomic<bool> done{false};

  std::thread cancelWorker;

public:
  TestCancellationFrontendDelegate(SourceMgr& sourceMgr)
  : TestBuildSystemFrontendDelegate(sourceMgr),
    cancelWorker(&TestCancellationFrontendDelegate::cancelHandler, this)
  {
  }

  ~TestCancellationFrontendDelegate() {
    done = true;
    shouldCancelCond.notify_all();
    cancelWorker.join();
  }

  std::atomic<bool> cancelAtStart{false};
  std::atomic<bool> cancelAtCommand{false};

  virtual void commandPreparing(Command* command) override {
    super::commandPreparing(command);

    if (cancelAtStart) {
      shouldCancel = true;
      shouldCancelCond.notify_all();
    }
  }

  virtual void commandProcessStarted(Command* command, ProcessHandle handle) override {
    super::commandProcessStarted(command, handle);

    if (cancelAtCommand) {
      shouldCancel = true;
      shouldCancelCond.notify_all();
    }
  }

private:
  void cancelHandler() {
    std::unique_lock<std::mutex> lock(shouldCancelMutex);
    while (!done) {
      if (shouldCancel) {
        cancel();
        shouldCancel = false;
      }
      shouldCancelCond.wait(lock);
    }
  }
};


TEST_F(BuildSystemFrontendTest, cancellation) {
  writeBuildFile(R"END(
client:
    name: client

targets:
    "": ["output"]

commands:
    C0:
        tool: shell
        outputs: ["output"]
        args: yes >/dev/null
)END");

  TestCancellationFrontendDelegate delegate(sourceMgr);
  BuildSystemFrontend frontend(delegate, invocation, createLocalFileSystem());

  // FIXME: This is a good candidate for a 'long running' testing mode. Increase
  // the number of reps to catch subtle race conditions. For example, some of
  // the issues fixed when this test was developed sometimes took thousands of
  // iterations to trigger.
  for (int i = 0; i < 100; ++i) {
    delegate.cancelAtStart = true;
    delegate.cancelAtCommand = false;
    frontend.build("");
    delegate.clearTrace();

    delegate.cancelAtStart = false;
    delegate.cancelAtCommand = true;
    frontend.build("");
    delegate.clearTrace();
  }
}



// We have commands { 2 -> 1 }, where two requires 1's output. We'd like to
// skip 1, we expect 2 to run and fail because 1 did not produce output.
TEST_F(BuildSystemFrontendTest, commandSkippingFailure) {
  writeBuildFile(R"END(
client:
    name: client

targets:
    "": ["2"]

commands:
    1:
        tool: shell
        outputs: ["1"]
        args: touch 1

    2:
        tool: shell
        inputs: ["1"]
        outputs: ["2"]
        args: cp 1 2
)END");

  {
    TestBuildSystemFrontendDelegate delegate(sourceMgr);
    delegate.commandsToSkip.insert("1");

    BuildSystemFrontend frontend(delegate, invocation, createLocalFileSystem());
    ASSERT_FALSE(frontend.build(""));
    ASSERT_EQ(1u, delegate.getNumFailedCommands());

#if defined(_WIN32)
    ASSERT_TRUE(delegate.checkTrace(R"END(
commandPreparing: 2
commandPreparing: 1
shouldCommandStart: 1
commandFinished: 1: 3
shouldCommandStart: 2
commandStarted: 2
commandProcessStarted: 2
commandProcessFinished: 2: 1
commandFinished: 2: 1
hadCommandFailure
)END"));
#else
    ASSERT_TRUE(delegate.checkTrace(R"END(
commandPreparing: 2
commandPreparing: 1
shouldCommandStart: 1
commandFinished: 1: 3
shouldCommandStart: 2
commandStarted: 2
commandProcessStarted: 2
commandProcessFinished: 2: 256
commandFinished: 2: 1
hadCommandFailure
)END"));
#endif
    ASSERT_TRUE(fs->getFileInfo(tempDir.str() + "/1").isMissing());
    ASSERT_TRUE(fs->getFileInfo(tempDir.str() + "/2").isMissing());
  }

  // If we rebuild incrementally without skipping, we expect to run 1 and 2.
  {
    TestBuildSystemFrontendDelegate delegate(sourceMgr);

    BuildSystemFrontend frontend(delegate, invocation, createLocalFileSystem());
    ASSERT_TRUE(frontend.build(""));

    ASSERT_TRUE(delegate.checkTrace(R"END(
commandPreparing: 2
commandPreparing: 1
shouldCommandStart: 1
commandStarted: 1
commandProcessStarted: 1
commandProcessFinished: 1: 0
commandFinished: 1: 0
shouldCommandStart: 2
commandStarted: 2
commandProcessStarted: 2
commandProcessFinished: 2: 0
commandFinished: 2: 0
)END"));

    ASSERT_FALSE(fs->getFileInfo(tempDir.str() + "/1").isMissing());
    ASSERT_FALSE(fs->getFileInfo(tempDir.str() + "/2").isMissing());
  }

}

// Built-in commands don't process skipped inputs the same way as external
// commands (for now). Add explicit tests to make sure they behave as expected.
TEST_F(BuildSystemFrontendTest, commandSkipping_NonExternalCommands_mkdir) {
  writeBuildFile(R"END(
client:
    name: client

targets:
    "": ["2"]

commands:
    1:
        tool: mkdir
        outputs: ["1"]
    2:
        tool: mkdir
        inputs: ["1"]
        outputs: ["2"]
)END");

  TestBuildSystemFrontendDelegate delegate(sourceMgr);
  delegate.commandsToSkip.insert("1");
  
  BuildSystemFrontend frontend(delegate, invocation, createLocalFileSystem());
  ASSERT_TRUE(frontend.build(""));

  ASSERT_TRUE(delegate.checkTrace(R"END(
commandPreparing: 2
commandPreparing: 1
shouldCommandStart: 1
commandFinished: 1: 3
shouldCommandStart: 2
commandStarted: 2
commandFinished: 2: 0
)END"));
}

// Built-in commands don't process skipped inputs the same way as external
// commands (for now). Add explicit tests to make sure they behave as expected.
TEST_F(BuildSystemFrontendTest, commandSkipping_NonExternalCommands_symlink) {
  writeBuildFile(R"END(
client:
    name: client

targets:
    "": ["3"]

commands:
    1:
        tool: symlink
        outputs: ["1"]
        contents: "x"
    2:
        tool: symlink
        inputs: ["1"]
        outputs: ["2"]
        contents: "y"
    3:
        tool: shell
        inputs: ["2"]
        outputs: ["3"]
)END" +
#if defined(_WIN32)
                 std::string("        args: del 2")
#else
                 std::string("        args: rm 2")
#endif
  );
  // We need to delete the symlink ourselves, because llvm's remove()
  // currently refuses to delete symlinks.

  TestBuildSystemFrontendDelegate delegate(sourceMgr);
  delegate.commandsToSkip.insert("1");
  
  BuildSystemFrontend frontend(delegate, invocation, createLocalFileSystem());
  ASSERT_TRUE(frontend.build(""));

  ASSERT_TRUE(delegate.checkTrace(R"END(
commandPreparing: 3
commandPreparing: 2
commandPreparing: 1
shouldCommandStart: 1
commandFinished: 1: 3
shouldCommandStart: 2
commandStarted: 2
commandFinished: 2: 0
shouldCommandStart: 3
commandStarted: 3
commandProcessStarted: 3
commandProcessFinished: 3: 0
commandFinished: 3: 0
)END"));
}

TEST_F(BuildSystemFrontendTest, singleNodeBuildLogsMissingInputs) {
  writeBuildFile(R"END(
client:
  name: client
)END");

  TestBuildSystemFrontendDelegate delegate(sourceMgr);
  BuildSystemFrontend frontend(delegate, invocation, createLocalFileSystem());

  ASSERT_FALSE(frontend.buildNode("/missing"));
  ASSERT_TRUE(delegate.checkTrace("error: missing input '/missing' and no rule to build it\n"));
}


TEST(BuildSystemInvocationTest, formatCycle) {
  BuildSystemInvocation invocation;

  class NullRule : public core::Rule {
  public:
    NullRule(const KeyType& key) : Rule(key) { }
    Task* createTask(core::BuildEngine&) override { return nullptr; }
    bool isResultValid(core::BuildEngine&, const core::ValueType&) override {
      return true;
    }
  };

  NullRule command{BuildKey::makeCommand("c").getKeyData()};
  NullRule customtask{BuildKey::makeCustomTask("c","t").getKeyData()};
  NullRule dircontents{BuildKey::makeDirectoryContents("/").getKeyData()};
  NullRule filtdircontents{BuildKey::makeFilteredDirectoryContents("/", {}).getKeyData()};
  NullRule dirtree{BuildKey::makeDirectoryTreeSignature("/", {}).getKeyData()};
  NullRule dirtreestruct{BuildKey::makeDirectoryTreeStructureSignature("/", {}).getKeyData()};
  NullRule node{BuildKey::makeNode("n").getKeyData()};
  NullRule stat{BuildKey::makeStat("f").getKeyData()};
  NullRule target{BuildKey::makeTarget("t").getKeyData()};

  std::vector<core::Rule*> cycle{
    &command,
    &customtask,
    &dircontents,
    &filtdircontents,
    &dirtree,
    &dirtreestruct,
    &node,
    &stat,
    &target
  };


  auto cyclestr = invocation.formatDetectedCycle(cycle);
  ASSERT_EQ(cyclestr, "cycle detected while building: command 'c' -> custom task 'c' -> directory-contents '/' -> filtered-directory-contents '/' -> directory-tree-signature '/' -> directory-tree-structure-signature '/' -> node 'n' -> stat 'f' -> target 't'");
}

}