Ninja
test.h
Go to the documentation of this file.
1 // Copyright 2011 Google Inc. All Rights Reserved.
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 #ifndef NINJA_TEST_H_
16 #define NINJA_TEST_H_
17 
18 #include <gtest/gtest.h>
19 
20 #include "disk_interface.h"
21 #include "state.h"
22 #include "util.h"
23 
24 // Support utilites for tests.
25 
26 struct Node;
27 
28 /// A base test fixture that includes a State object with a
29 /// builtin "cat" rule.
30 struct StateTestWithBuiltinRules : public testing::Test {
32 
33  /// Add a "cat" rule to \a state. Used by some tests; it's
34  /// otherwise done by the ctor to state_.
35  void AddCatRule(State* state);
36 
37  /// Short way to get a Node by its path from state_.
38  Node* GetNode(const string& path);
39 
41 };
42 
43 void AssertParse(State* state, const char* input);
44 void AssertHash(const char* expected, uint64_t actual);
45 
46 /// An implementation of DiskInterface that uses an in-memory representation
47 /// of disk state. It also logs file accesses and directory creations
48 /// so it can be used by tests to verify disk access patterns.
51 
52  /// "Create" a file with contents.
53  void Create(const string& path, const string& contents);
54 
55  /// Tick "time" forwards; subsequent file operations will be newer than
56  /// previous ones.
57  int Tick() {
58  return ++now_;
59  }
60 
61  // DiskInterface
62  virtual TimeStamp Stat(const string& path);
63  virtual bool WriteFile(const string& path, const string& contents);
64  virtual bool MakeDir(const string& path);
65  virtual string ReadFile(const string& path, string* err);
66  virtual int RemoveFile(const string& path);
67 
68  /// An entry for a single in-memory file.
69  struct Entry {
70  int mtime;
71  string contents;
72  };
73 
74  vector<string> directories_made_;
75  vector<string> files_read_;
76  typedef map<string, Entry> FileMap;
78  set<string> files_removed_;
79  set<string> files_created_;
80 
81  /// A simple fake timestamp for file operations.
82  int now_;
83 };
84 
85 struct ScopedTempDir {
86  /// Create a temporary directory and chdir into it.
87  void CreateAndEnter(const string& name);
88 
89  /// Clean up the temporary directory.
90  void Cleanup();
91 
92  /// The temp directory containing our dir.
93  string start_dir_;
94  /// The subdirectory name for our dir, or empty if it hasn't been set up.
96 };
97 
98 #endif // NINJA_TEST_H_