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_
map< string, Entry > FileMap
Definition: test.h:76
FileMap files_
Definition: test.h:77
An implementation of DiskInterface that uses an in-memory representation of disk state.
Definition: test.h:49
virtual TimeStamp Stat(const string &path)
stat() a file, returning the mtime, or 0 if missing and -1 on other errors.
Definition: test.cc:108
Node * GetNode(const string &path)
Short way to get a Node by its path from state_.
Definition: test.cc:86
int Tick()
Tick "time" forwards; subsequent file operations will be newer than previous ones.
Definition: test.h:57
Information about a node in the dependency graph: the file, whether it's dirty, mtime, etc.
Definition: graph.h:35
set< string > files_removed_
Definition: test.h:78
virtual bool WriteFile(const string &path, const string &contents)
Create a file, with the specified name and contents Returns true on success, false on failure...
Definition: test.cc:115
Interface for accessing the disk.
int now_
A simple fake timestamp for file operations.
Definition: test.h:82
int TimeStamp
Definition: timestamp.h:22
void Create(const string &path, const string &contents)
"Create" a file with contents.
Definition: test.cc:101
set< string > files_created_
Definition: test.h:79
vector< string > directories_made_
Definition: test.h:74
A base test fixture that includes a State object with a builtin "cat" rule.
Definition: test.h:30
void AssertHash(const char *expected, uint64_t actual)
Definition: test.cc:97
void AddCatRule(State *state)
Add a "cat" rule to state.
Definition: test.cc:80
string start_dir_
The temp directory containing our dir.
Definition: test.h:93
VirtualFileSystem()
Definition: test.h:50
virtual bool MakeDir(const string &path)
Create a directory, returning false on failure.
Definition: test.cc:120
void Cleanup()
Clean up the temporary directory.
Definition: test.cc:169
An entry for a single in-memory file.
Definition: test.h:69
void CreateAndEnter(const string &name)
Create a temporary directory and chdir into it.
Definition: test.cc:147
virtual string ReadFile(const string &path, string *err)
Read a file to a string. Fill in |err| on error.
Definition: test.cc:125
void AssertParse(State *state, const char *input)
Definition: test.cc:90
Global state (file status, loaded rules) for a single run.
Definition: state.h:83
vector< string > files_read_
Definition: test.h:75
virtual int RemoveFile(const string &path)
Remove the file named path.
Definition: test.cc:133
unsigned long long uint64_t
Definition: win32port.h:22
string temp_dir_name_
The subdirectory name for our dir, or empty if it hasn't been set up.
Definition: test.h:95