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 "manifest_parser.h"
22#include "state.h"
23
24// Support utilities for tests.
25
26struct Node;
27
28/// A base test fixture that includes a State object with a
29/// builtin "cat" rule.
30struct 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 std::string& path);
39
41};
42
43void AssertParse(State* state, const char* input,
45void AssertHash(const char* expected, uint64_t actual);
46void VerifyGraph(const State& state);
47
48/// An implementation of DiskInterface that uses an in-memory representation
49/// of disk state. It also logs file accesses and directory creations
50/// so it can be used by tests to verify disk access patterns.
53
54 /// "Create" a file with contents.
55 void Create(const std::string& path, const std::string& contents);
56
57 /// Tick "time" forwards; subsequent file operations will be newer than
58 /// previous ones.
59 int Tick() {
60 return ++now_;
61 }
62
63 // DiskInterface
64 TimeStamp Stat(const std::string& path, std::string* err) const override;
65 bool WriteFile(const std::string& path, const std::string& contents,
66 bool /*crlf_on_windows*/) override;
67 bool MakeDir(const std::string& path) override;
68 Status ReadFile(const std::string& path, std::string* contents,
69 std::string* err) override;
70 int RemoveFile(const std::string& path) override;
71
72 /// An entry for a single in-memory file.
73 struct Entry {
74 int mtime;
75 std::string stat_error; // If mtime is -1.
76 std::string contents;
77 };
78
79 std::vector<std::string> directories_made_;
80 std::vector<std::string> files_read_;
81 typedef std::map<std::string, Entry> FileMap;
83 std::set<std::string> files_removed_;
84 std::set<std::string> files_created_;
85
86 /// A simple fake timestamp for file operations.
87 int now_;
88};
89
91 /// Create a temporary directory and chdir into it.
92 void CreateAndEnter(const std::string& name);
93
94 /// Clean up the temporary directory.
95 void Cleanup();
96
97 /// The temp directory containing our dir.
98 std::string start_dir_;
99 /// The subdirectory name for our dir, or empty if it hasn't been set up.
100 std::string temp_dir_name_;
101};
102
103/// A class that records a file path and ensures that it is removed
104/// on destruction. This ensures that tests do not keep stale files in the
105/// current directory where they run, even in case of assertion failure.
107 /// Constructor just records the file path.
108 ScopedFilePath(const std::string& path) : path_(path) {}
109 ScopedFilePath(const char* path) : path_(path) {}
110
111 /// Allow move operations.
112 ScopedFilePath(ScopedFilePath&&) noexcept;
113 ScopedFilePath& operator=(ScopedFilePath&&) noexcept;
114
115 /// Destructor destroys the file, unless Release() was called.
117
118 /// Release the file, the destructor will not remove the file.
119 void Release();
120
121 const char* c_str() const { return path_.c_str(); }
122 const std::string& path() const { return path_; }
123 bool released() const { return released_; }
124
125 private:
126 std::string path_;
127 bool released_ = false;
128};
129
130#endif // NINJA_TEST_H_
Interface for accessing the disk.
Information about a node in the dependency graph: the file, whether it's dirty, mtime,...
Definition graph.h:42
A class that records a file path and ensures that it is removed on destruction.
Definition test.h:106
ScopedFilePath(const char *path)
Definition test.h:109
bool released_
Definition test.h:127
std::string path_
Definition test.h:126
void Release()
Release the file, the destructor will not remove the file.
Definition test.cc:262
bool released() const
Definition test.h:123
const std::string & path() const
Definition test.h:122
const char * c_str() const
Definition test.h:121
ScopedFilePath(const std::string &path)
Constructor just records the file path.
Definition test.h:108
void CreateAndEnter(const std::string &name)
Create a temporary directory and chdir into it.
Definition test.cc:199
std::string start_dir_
The temp directory containing our dir.
Definition test.h:98
std::string temp_dir_name_
The subdirectory name for our dir, or empty if it hasn't been set up.
Definition test.h:100
void Cleanup()
Clean up the temporary directory.
Definition test.cc:221
void AddCatRule(State *state)
Add a "cat" rule to state.
Definition test.cc:89
Node * GetNode(const std::string &path)
Short way to get a Node by its path from state_.
Definition test.cc:95
Global state (file status) for a single run.
Definition state.h:95
Abstract interface to object that tracks the status of a build: completion fraction,...
Definition status.h:27
An entry for a single in-memory file.
Definition test.h:73
std::string contents
Definition test.h:76
std::string stat_error
Definition test.h:75
bool WriteFile(const std::string &path, const std::string &contents, bool) override
Create a file, with the specified name and contents If crlf_on_windows is true, will be converted t...
Definition test.cc:161
std::map< std::string, Entry > FileMap
Definition test.h:81
bool MakeDir(const std::string &path) override
Create a directory, returning false on failure.
Definition test.cc:167
int RemoveFile(const std::string &path) override
Remove the file named path.
Definition test.cc:185
Status ReadFile(const std::string &path, std::string *contents, std::string *err) override
Read and store in given string.
Definition test.cc:172
TimeStamp Stat(const std::string &path, std::string *err) const override
stat() a file, returning the mtime, or 0 if missing and -1 on other errors.
Definition test.cc:152
std::set< std::string > files_removed_
Definition test.h:83
FileMap files_
Definition test.h:82
int Tick()
Tick "time" forwards; subsequent file operations will be newer than previous ones.
Definition test.h:59
int now_
A simple fake timestamp for file operations.
Definition test.h:87
std::set< std::string > files_created_
Definition test.h:84
std::vector< std::string > files_read_
Definition test.h:80
void Create(const std::string &path, const std::string &contents)
"Create" a file with contents.
Definition test.cc:145
std::vector< std::string > directories_made_
Definition test.h:79
VirtualFileSystem()
Definition test.h:52
void AssertHash(const char *expected, uint64_t actual)
Definition test.cc:109
void AssertParse(State *state, const char *input, ManifestParserOptions=ManifestParserOptions())
Definition test.cc:100
void VerifyGraph(const State &state)
Definition test.cc:113
int64_t TimeStamp
Definition timestamp.h:31
unsigned long long uint64_t
Definition win32port.h:29