Ninja
graph_test.cc
Go to the documentation of this file.
00001 // Copyright 2011 Google Inc. All Rights Reserved.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //     http://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include "graph.h"
00016 
00017 #include "test.h"
00018 
00019 struct GraphTest : public StateTestWithBuiltinRules {
00020   VirtualFileSystem fs_;
00021 };
00022 
00023 TEST_F(GraphTest, MissingImplicit) {
00024   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
00025 "build out: cat in | implicit\n"));
00026   fs_.Create("in", 1, "");
00027   fs_.Create("out", 1, "");
00028 
00029   Edge* edge = GetNode("out")->in_edge_;
00030   string err;
00031   EXPECT_TRUE(edge->RecomputeDirty(&state_, &fs_, &err));
00032   ASSERT_EQ("", err);
00033 
00034   // A missing implicit dep *should* make the output dirty.
00035   // (In fact, a build will fail.)
00036   // This is a change from prior semantics of ninja.
00037   EXPECT_TRUE(GetNode("out")->dirty_);
00038 }
00039 
00040 TEST_F(GraphTest, ModifiedImplicit) {
00041   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
00042 "build out: cat in | implicit\n"));
00043   fs_.Create("in", 1, "");
00044   fs_.Create("out", 1, "");
00045   fs_.Create("implicit", 2, "");
00046 
00047   Edge* edge = GetNode("out")->in_edge_;
00048   string err;
00049   EXPECT_TRUE(edge->RecomputeDirty(&state_, &fs_, &err));
00050   ASSERT_EQ("", err);
00051 
00052   // A modified implicit dep should make the output dirty.
00053   EXPECT_TRUE(GetNode("out")->dirty_);
00054 }
00055 
00056 TEST_F(GraphTest, FunkyMakefilePath) {
00057   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
00058 "rule catdep\n"
00059 "  depfile = $out.d\n"
00060 "  command = cat $in > $out\n"
00061 "build out.o: catdep foo.cc\n"));
00062   fs_.Create("implicit.h", 2, "");
00063   fs_.Create("foo.cc", 1, "");
00064   fs_.Create("out.o.d", 1, "out.o: ./foo/../implicit.h\n");
00065   fs_.Create("out.o", 1, "");
00066 
00067   Edge* edge = GetNode("out.o")->in_edge_;
00068   string err;
00069   EXPECT_TRUE(edge->RecomputeDirty(&state_, &fs_, &err));
00070   ASSERT_EQ("", err);
00071 
00072   // implicit.h has changed, though our depfile refers to it with a
00073   // non-canonical path; we should still find it.
00074   EXPECT_TRUE(GetNode("out.o")->dirty_);
00075 }
00076 
00077 TEST_F(GraphTest, ExplicitImplicit) {
00078   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
00079 "rule catdep\n"
00080 "  depfile = $out.d\n"
00081 "  command = cat $in > $out\n"
00082 "build implicit.h: cat data\n"
00083 "build out.o: catdep foo.cc || implicit.h\n"));
00084   fs_.Create("data", 2, "");
00085   fs_.Create("implicit.h", 1, "");
00086   fs_.Create("foo.cc", 1, "");
00087   fs_.Create("out.o.d", 1, "out.o: implicit.h\n");
00088   fs_.Create("out.o", 1, "");
00089 
00090   Edge* edge = GetNode("out.o")->in_edge_;
00091   string err;
00092   EXPECT_TRUE(edge->RecomputeDirty(&state_, &fs_, &err));
00093   ASSERT_EQ("", err);
00094 
00095   // We have both an implicit and an explicit dep on implicit.h.
00096   // The implicit dep should "win" (in the sense that it should cause
00097   // the output to be dirty).
00098   EXPECT_TRUE(GetNode("out.o")->dirty_);
00099 }
00100 
00101 TEST_F(GraphTest, PathWithCurrentDirectory) {
00102   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
00103 "rule catdep\n"
00104 "  depfile = $out.d\n"
00105 "  command = cat $in > $out\n"
00106 "build ./out.o: catdep ./foo.cc\n"));
00107   fs_.Create("foo.cc", 1, "");
00108   fs_.Create("out.o.d", 1, "out.o: foo.cc\n");
00109   fs_.Create("out.o", 1, "");
00110 
00111   Edge* edge = GetNode("out.o")->in_edge_;
00112   string err;
00113   EXPECT_TRUE(edge->RecomputeDirty(&state_, &fs_, &err));
00114   ASSERT_EQ("", err);
00115 
00116   EXPECT_FALSE(GetNode("out.o")->dirty_);
00117 }
00118 
00119 TEST_F(GraphTest, RootNodes) {
00120   ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
00121 "build out1: cat in1\n"
00122 "build mid1: cat in1\n"
00123 "build out2: cat mid1\n"
00124 "build out3 out4: cat mid1\n"));
00125 
00126   string err;
00127   vector<Node*> root_nodes = state_.RootNodes(&err);
00128   EXPECT_EQ(4u, root_nodes.size());
00129   for (size_t i = 0; i < root_nodes.size(); ++i) {
00130     string name = root_nodes[i]->file_->path_;
00131     EXPECT_EQ("out", name.substr(0, 3));
00132   }
00133 }