Ninja
test.h
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 #ifndef NINJA_TEST_H_
00016 #define NINJA_TEST_H_
00017 
00018 #include <gtest/gtest.h>
00019 
00020 #include "disk_interface.h"
00021 #include "state.h"
00022 #include "util.h"
00023 
00024 // Support utilites for tests.
00025 
00026 struct Node;
00027 
00028 /// A base test fixture that includes a State object with a
00029 /// builtin "cat" rule.
00030 struct StateTestWithBuiltinRules : public testing::Test {
00031   StateTestWithBuiltinRules();
00032 
00033   /// Add a "cat" rule to \a state.  Used by some tests; it's
00034   /// otherwise done by the ctor to state_.
00035   void AddCatRule(State* state);
00036 
00037   /// Short way to get a Node by its path from state_.
00038   Node* GetNode(const string& path);
00039 
00040   State state_;
00041 };
00042 
00043 void AssertParse(State* state, const char* input);
00044 void AssertHash(const char* expected, uint64_t actual);
00045 
00046 /// An implementation of DiskInterface that uses an in-memory representation
00047 /// of disk state.  It also logs file accesses and directory creations
00048 /// so it can be used by tests to verify disk access patterns.
00049 struct VirtualFileSystem : public DiskInterface {
00050   VirtualFileSystem() : now_(1) {}
00051 
00052   /// "Create" a file with contents.
00053   void Create(const string& path, const string& contents);
00054 
00055   /// Tick "time" forwards; subsequent file operations will be newer than
00056   /// previous ones.
00057   int Tick() {
00058     return ++now_;
00059   }
00060 
00061   // DiskInterface
00062   virtual TimeStamp Stat(const string& path) const;
00063   virtual bool WriteFile(const string& path, const string& contents);
00064   virtual bool MakeDir(const string& path);
00065   virtual string ReadFile(const string& path, string* err);
00066   virtual int RemoveFile(const string& path);
00067 
00068   /// An entry for a single in-memory file.
00069   struct Entry {
00070     int mtime;
00071     string contents;
00072   };
00073 
00074   vector<string> directories_made_;
00075   vector<string> files_read_;
00076   typedef map<string, Entry> FileMap;
00077   FileMap files_;
00078   set<string> files_removed_;
00079   set<string> files_created_;
00080 
00081   /// A simple fake timestamp for file operations.
00082   int now_;
00083 };
00084 
00085 struct ScopedTempDir {
00086   /// Create a temporary directory and chdir into it.
00087   void CreateAndEnter(const string& name);
00088 
00089   /// Clean up the temporary directory.
00090   void Cleanup();
00091 
00092   /// The temp directory containing our dir.
00093   string start_dir_;
00094   /// The subdirectory name for our dir, or empty if it hasn't been set up.
00095   string temp_dir_name_;
00096 };
00097 
00098 #endif // NINJA_TEST_H_