Skip to content

Commit

Permalink
move VirtualFileSystem into test.*
Browse files Browse the repository at this point in the history
  • Loading branch information
evmar committed Mar 6, 2011
1 parent 2f3e566 commit 37a9390
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 36 deletions.
36 changes: 0 additions & 36 deletions src/build_test.cc
Expand Up @@ -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<string> directories_made_;
vector<string> files_read_;
typedef map<string, Entry> FileMap;
FileMap files_;
};

struct BuildTest : public StateTestWithBuiltinRules,
public CommandRunner {
BuildTest() : config_(MakeConfig()), builder_(&state_, config_), now_(1),
Expand Down
26 changes: 26 additions & 0 deletions src/test.cc
Expand Up @@ -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 "";
}
23 changes: 23 additions & 0 deletions src/test.h
Expand Up @@ -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<string> directories_made_;
vector<string> files_read_;
typedef map<string, Entry> FileMap;
FileMap files_;
};

0 comments on commit 37a9390

Please sign in to comment.