From 37a9390101a73ab518aeac164ab147ee5251af9a Mon Sep 17 00:00:00 2001 From: Evan Martin Date: Sat, 5 Mar 2011 17:39:48 -0800 Subject: move VirtualFileSystem into test.* --- src/build_test.cc | 36 ------------------------------------ src/test.cc | 26 ++++++++++++++++++++++++++ src/test.h | 23 +++++++++++++++++++++++ 3 files changed, 49 insertions(+), 36 deletions(-) diff --git a/src/build_test.cc b/src/build_test.cc index cdc6534..c1a253e 100644 --- a/src/build_test.cc +++ b/src/build_test.cc @@ -188,42 +188,6 @@ TEST_F(PlanTest, DependencyCycle) { ASSERT_EQ("dependency cycle: out -> mid -> in -> pre -> out", err); } -struct VirtualFileSystem : public DiskInterface { - struct Entry { - int mtime; - string contents; - }; - - void Create(const string& path, int time, const string& contents) { - files_[path].mtime = time; - files_[path].contents = contents; - } - - // DiskInterface - virtual int Stat(const string& path) { - FileMap::iterator i = files_.find(path); - if (i != files_.end()) - return i->second.mtime; - return 0; - } - virtual bool MakeDir(const string& path) { - directories_made_.push_back(path); - return true; // success - } - virtual string ReadFile(const string& path, string* err) { - files_read_.push_back(path); - FileMap::iterator i = files_.find(path); - if (i != files_.end()) - return i->second.contents; - return ""; - } - - vector directories_made_; - vector files_read_; - typedef map FileMap; - FileMap files_; -}; - struct BuildTest : public StateTestWithBuiltinRules, public CommandRunner { BuildTest() : config_(MakeConfig()), builder_(&state_, config_), now_(1), diff --git a/src/test.cc b/src/test.cc index e78bfd1..241c8d3 100644 --- a/src/test.cc +++ b/src/test.cc @@ -32,3 +32,29 @@ void AssertParse(State* state, const char* input) { ASSERT_TRUE(parser.Parse(input, &err)) << err; ASSERT_EQ("", err); } + +void VirtualFileSystem::Create(const string& path, int time, + const string& contents) { + files_[path].mtime = time; + files_[path].contents = contents; +} + +int VirtualFileSystem::Stat(const string& path) { + FileMap::iterator i = files_.find(path); + if (i != files_.end()) + return i->second.mtime; + return 0; +} + +bool VirtualFileSystem::MakeDir(const string& path) { + directories_made_.push_back(path); + return true; // success +} + +string VirtualFileSystem::ReadFile(const string& path, string* err) { + files_read_.push_back(path); + FileMap::iterator i = files_.find(path); + if (i != files_.end()) + return i->second.contents; + return ""; +} diff --git a/src/test.h b/src/test.h index 646d8f3..89c0974 100644 --- a/src/test.h +++ b/src/test.h @@ -28,3 +28,26 @@ struct StateTestWithBuiltinRules : public testing::Test { }; void AssertParse(State* state, const char* input); + +// An implementation of DiskInterface that uses an in-memory representation +// of disk state. It also logs file accesses and directory creations +// so it can be used by tests to verify disk access patterns. +struct VirtualFileSystem : public DiskInterface { + // "Create" a file with a given mtime and contents. + void Create(const string& path, int time, const string& contents); + + // DiskInterface + virtual int Stat(const string& path); + virtual bool MakeDir(const string& path); + virtual string ReadFile(const string& path, string* err); + + struct Entry { + int mtime; + string contents; + }; + + vector directories_made_; + vector files_read_; + typedef map FileMap; + FileMap files_; +}; -- cgit v0.12