Skip to content
Snippets Groups Projects
Commit dc932fe2 authored by Jan Wichmann's avatar Jan Wichmann
Browse files

merge form workingdir

parents 45883bbf e13f15ce
No related branches found
No related tags found
No related merge requests found
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
......@@ -40,15 +40,39 @@ void FileSystem::listContentDir() {
}
}
void FileSystem::changeDir(std::string dir_name) {
if (dir_name[dir_name.size()-1] == '/') {
dir_name.pop_back();
}
auto target_dir = currentDirectory->subdirs.find(dir_name);
if(target_dir != currentDirectory->subdirs.end()) {
currentDirectory = target_dir->second;
} else {
std::cout << "Directory '" << dir_name << "' dosen't exists!" << std::endl;
void FileSystem::changeDir(const std::string& dir_name) {
std::vector<std::string>path_to_process = this->splitInput(dir_name, '/');
//Absoulte Search
if (!path_to_process.empty()) {
if(path_to_process[0] == "root") {
Directory* backup_dir = currentDirectory;
currentDirectory = root;
for (int i = 1; i < path_to_process.size(); i++) {
auto target_dir = currentDirectory->subdirs.find(path_to_process[i]);
if(target_dir != currentDirectory->subdirs.end()) {
currentDirectory = target_dir->second;
} else {
std::cout << "Directory '" << dir_name << "' dosen't exists!" << std::endl;
currentDirectory = backup_dir;
break;
}
}
}
//Relative Search
else {
for (auto &d: path_to_process) {
if (d == "..") {
go_to_parent_dir();
} else {
auto target_dir = currentDirectory->subdirs.find(d);
if(target_dir != currentDirectory->subdirs.end()) {
currentDirectory = target_dir->second;
} else {
std::cout << "Directory '" << dir_name << "' dosen't exists!" << std::endl;
}
}
}
}
}
}
......@@ -79,7 +103,7 @@ void FileSystem::processCmd(const std::string &command) {
currentDirectory = root;
}
}
else if (cmd == "cd..") {
else if (cmd == "cd ..") {
go_to_parent_dir();
}
else if (cmd == "exit") {
......@@ -107,7 +131,9 @@ std::vector<std::string> FileSystem::splitInput(const std::string &input, char d
// Eingabe in Wörter (Tokens) aufteilen
while (std::getline(stream, token, delimiter)) {
tokens.push_back(token);
if (!token.empty()) {
tokens.push_back(token);
}
}
return tokens;
}
......@@ -116,9 +142,9 @@ void FileSystem::go_to_parent_dir() {
currentDirectory = currentDirectory->parent;
}
FileSystem::Directory *FileSystem::getCurrentDirectory() const {
return currentDirectory;
}
......@@ -25,10 +25,6 @@ private:
Directory* root;
Directory* currentDirectory;
public:
Directory *getCurrentDirectory() const;
private:
bool running = true;
public:
......@@ -37,11 +33,11 @@ public:
void createRoot();
void createSubDir(const std::string& dir_name);
void listContentDir();
void changeDir(std::string dir_name);
void changeDir(const std::string& dir_name);
void go_to_parent_dir();
void text_interface();
bool directoryExists(const std::string &dir_name);
Directory *getCurrentDirectory() const;
};
......
......@@ -16,6 +16,7 @@ bool FileSystem::directoryExists(const std::string& dir_name) {
}
TEST_F(FileSystemTest, CreateRootTest) {
ASSERT_EQ(fs->getCurrentDirectory()->name, "root");
}
......@@ -25,12 +26,62 @@ TEST_F(FileSystemTest, CreateSubDirectoryTest) {
ASSERT_TRUE(fs->directoryExists("testDir"));
}
TEST_F(FileSystemTest, ChangeDirectoryTest) {
fs->createSubDir("testDir");
fs->changeDir("testDir");
ASSERT_EQ(fs->getCurrentDirectory()->name, "root/testDir");
// cd
TEST_F(FileSystemTest, ChangeDirectoryValidPaths) {
fs->createSubDir("a/b/c");
fs->changeDir("a/b");
ASSERT_EQ(fs->getCurrentDirectory()->name, "root/a/b"); // Absoluter Pfad prüfen
fs->changeDir("../b/c");
ASSERT_EQ(fs->getCurrentDirectory()->name, "root/a/b/c"); // Absoluter Pfad prüfen
}
TEST_F(FileSystemTest, ChangeDirectoryAbsolutePaths) {
fs->createSubDir("a/b/c");
fs->changeDir("root/a");
ASSERT_EQ(fs->getCurrentDirectory()->name, "root/a"); // Absoluter Pfad zur Root-Struktur
fs->changeDir("root/a/b/c");
ASSERT_EQ(fs->getCurrentDirectory()->name, "root/a/b/c"); // Absoluter Pfad in tiefere Struktur
}
TEST_F(FileSystemTest, ChangeDirectoryRootDirectory) {
fs->createSubDir("a/b/c");
fs->changeDir("a/b/c");
ASSERT_EQ(fs->getCurrentDirectory()->name, "root/a/b/c"); // Absoluter Pfad in tiefes Verzeichnis
fs->changeDir("root");
ASSERT_EQ(fs->getCurrentDirectory()->name, "root"); // Wechsel zurück in Root
}
TEST_F(FileSystemTest, ChangeDirectoryUsingDot) {
fs->createSubDir("a/b");
fs->changeDir("a/b/.");
ASSERT_EQ(fs->getCurrentDirectory()->name, "root/a/b"); // Wechsel mit `.` bleibt im gleichen Verzeichnis
}
TEST_F(FileSystemTest, ChangeDirectoryUsingDoubleDot) {
fs->createSubDir("a/b");
fs->changeDir("a/b");
fs->changeDir("..");
ASSERT_EQ(fs->getCurrentDirectory()->name, "root/a"); // Wechsel zu übergeordnetem Verzeichnis
fs->changeDir("..");
ASSERT_EQ(fs->getCurrentDirectory()->name, "root"); // Wechsel zurück zur Root
}
TEST_F(FileSystemTest, ChangeDirectoryDeepRelativePaths) {
fs->createSubDir("a/b/c/d/e");
fs->changeDir("a/b/c/d");
ASSERT_EQ(fs->getCurrentDirectory()->name, "root/a/b/c/d"); // Wechsel in tiefes Verzeichnis
fs->changeDir("../../b/c");
ASSERT_EQ(fs->getCurrentDirectory()->name, "root/a/b/c"); // Relativer Pfad zurück und nach vorne
}
// ls
TEST_F(FileSystemTest, ListContentTest) {
fs->createSubDir("dir1");
fs->createSubDir("dir2");
......@@ -43,10 +94,8 @@ TEST_F(FileSystemTest, ListContentTest) {
std::cout.rdbuf(oldCoutBuffer);
std::string output = outputBuffer.str();
std::string expectedOutput = "dir1\n"
"dir2\n";
ASSERT_EQ(output, expectedOutput);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment