private string ChangeDirectoryToRoot(string targetDirectoryName)
        {
            var rootDirectory             = _fileSystemState.GetRootDirectory();
            var isRootDirectorySetSuccess = _fileSystemState.TrySetCurrentDirectory(rootDirectory);

            if (!isRootDirectorySetSuccess)
            {
                return($"Error - Failed to set current directory to root `{targetDirectoryName}`");
            }

            return("Current working directory changed");
        }
Пример #2
0
        public string ExecuteCommand(params string[] args)
        {
            // Validate the arguments to the command
            if (!TryValidateArguments(out var validationResponse, args))
            {
                return(validationResponse);
            }

            // Execute the valid command logic
            var targetPlanetName = args.LastOrDefault();

            // Try to initialize a new directory for this planet
            var rootDirectory    = _fileSystemState.GetRootDirectory();
            var planetPermission = _permissionController.GetCustomPermission(canRead: true, canExecute: true);

            var isAddPlanetDirectorySuccess = _directoryController.TryAddDirectory(targetPlanetName, planetPermission, rootDirectory, out var targetPlanetDirectory);

            if (!isAddPlanetDirectorySuccess)
            {
                return($"Error - Failed to initialize planet `{targetPlanetName}`");
            }

            // Once we know we have a planet folder initialized, set it up with all the needed sub-directories for a planet
            // TODO - Setup the sub-directories for a planet

            return(null);
        }
Пример #3
0
        public void RootDirectoryDoesNotHaveParentDirectory()
        {
            var state = new FileSystemState();
            var root  = state.GetRootDirectory();

            Assert.IsNull(root.ParentDirectory);
        }
Пример #4
0
        public void NewStateHasRootDirectory()
        {
            var state = new FileSystemState();
            var root  = state.GetRootDirectory();

            Assert.IsNotNull(root);
        }
Пример #5
0
        public void RootDirectoryHasExecuteAccess()
        {
            var state      = new FileSystemState();
            var root       = state.GetRootDirectory();
            var rootAccess = _permissionController.GetPermissions(root);

            Assert.IsTrue(rootAccess.Execute);
        }
Пример #6
0
        public void RootDirectoryDoesNotHaveWriteAccess()
        {
            var state      = new FileSystemState();
            var root       = state.GetRootDirectory();
            var rootAccess = _permissionController.GetPermissions(root);

            Assert.IsFalse(rootAccess.Write);
        }
Пример #7
0
        public void FilesInDirectoryDoNotExistUntilCreated()
        {
            var state = new FileSystemState();
            var root  = state.GetRootDirectory();

            Assert.IsNotNull(root.FilesInDirectory);
            Assert.IsEmpty(root.FilesInDirectory);
        }
Пример #8
0
        public void RootDirectoryStartsWithNoFiles()
        {
            var state = new FileSystemState();
            var root  = state.GetRootDirectory();

            Assert.IsNotNull(root.FilesInDirectory);
            Assert.IsEmpty(root.FilesInDirectory);
        }
Пример #9
0
        public void NewStateRootDirectoryIsNotCurrentDirectory()
        {
            var state   = new FileSystemState();
            var root    = state.GetRootDirectory();
            var current = state.GetCurrentDirectory();

            Assert.AreNotEqual(root, current);
        }
        public void CannotDeleteDirectoryThatDoesNotExist()
        {
            var state = new FileSystemState();
            var root  = state.GetRootDirectory();
            var isDeleteDirectorySuccess = _directoryController.TryDeleteDirectory("Test", root);

            Assert.IsFalse(isDeleteDirectorySuccess);
        }
Пример #11
0
        public void CannotDeleteFileThatDoesNotExist()
        {
            var state = new FileSystemState();
            var root  = state.GetRootDirectory();
            var isDeleteFileSuccess = _fileController.TryDeleteFile("Test", FileExtension.None, root);

            Assert.IsFalse(isDeleteFileSuccess);
        }
        public void CurrentDirectoryStartsWithRootParentDirectory()
        {
            var state = new FileSystemState();
            var current = state.GetCurrentDirectory();
            var root = state.GetRootDirectory();

            Assert.IsNotNull(current.ParentDirectory);
            Assert.AreEqual(current.ParentDirectory, root);
        }
Пример #13
0
        public void NewStateHasRootDirectoryNamedSlash()
        {
            var state    = new FileSystemState();
            var expected = "/";
            var root     = state.GetRootDirectory();

            Assert.IsNotNull(root);
            Assert.AreEqual(root.Name, expected);
        }
Пример #14
0
        public void HomeDirectoryHasRootParentDirectory()
        {
            var state = new FileSystemState();
            var home  = state.GetHomeDirectory();
            var root  = state.GetRootDirectory();

            Assert.IsNotNull(home.ParentDirectory);
            Assert.AreEqual(home.ParentDirectory, root);
        }
        public void CreatedDirectoryHasParentDirectory()
        {
            var state = new FileSystemState();
            var root  = state.GetRootDirectory();
            var isAddDirectorySuccess = _directoryController.TryAddDirectory("Test", new Permission(), root, out var testDirectory);

            Assert.IsTrue(isAddDirectorySuccess);
            Assert.AreEqual(testDirectory.ParentDirectory, root);
        }
        public void HomeSubDirectoryExistsOnCreation()
        {
            var state = new FileSystemState();
            var root  = state.GetRootDirectory();

            Assert.IsNotNull(root.SubDirectories);
            Assert.IsNotEmpty(root.SubDirectories);
            Assert.AreEqual(root.SubDirectories.Count, 1);
            Assert.AreEqual(root.SubDirectories.FirstOrDefault().Name, "home");
        }
Пример #17
0
        public void RootDirectoryPathIsDirectoryName()
        {
            var state = new FileSystemState();
            var root  = state.GetRootDirectory();

            var path         = _directoryController.GetDirectoryPath(root);
            var expectedPath = "/";

            Assert.AreEqual(path, expectedPath);
        }
        public void CanGetCurrentDirectory()
        {
            var state = new FileSystemState();
            var root  = state.GetRootDirectory();

            var isGetDirectorySuccess = _directoryController.TryGetDirectory(".", root, out var target);

            Assert.IsTrue(isGetDirectorySuccess);
            Assert.AreEqual(target, root);
        }
        public void CannotGetParentDirectoryOfRootDirectory()
        {
            var state = new FileSystemState();
            var root  = state.GetRootDirectory();

            var isGetDirectorySuccess = _directoryController.TryGetDirectory("..", root, out var target);

            Assert.IsFalse(isGetDirectorySuccess);
            Assert.IsNull(target);
        }
        public void CannotGetDirectoryThatDoesNotExist()
        {
            var state = new FileSystemState();
            var root  = state.GetRootDirectory();

            var isGetDirectorySuccess = _directoryController.TryGetDirectory("Test", root, out var target);

            Assert.IsFalse(isGetDirectorySuccess);
            Assert.IsNull(target);
        }
Пример #21
0
        public void CanDeleteFileThatExists()
        {
            var state            = new FileSystemState();
            var root             = state.GetRootDirectory();
            var isAddFileSuccess = _fileController.TryAddFile("Test", FileExtension.None, new Permission(), root, out var addedFile);

            var isDeleteFileSuccess = _fileController.TryDeleteFile("Test", FileExtension.None, root);

            Assert.IsTrue(isAddFileSuccess);
            Assert.IsTrue(isDeleteFileSuccess);
        }
        public void SubDirectoriesExistWhenCreated()
        {
            var state = new FileSystemState();
            var root  = state.GetRootDirectory();

            var isAddDirectorySuccess = _directoryController.TryAddDirectory("Test", new Permission(), root, out var testDirectory);

            Assert.IsTrue(isAddDirectorySuccess);
            Assert.IsNotNull(root.SubDirectories);
            Assert.That(root.SubDirectories.Contains(testDirectory));
        }
        public void CanDeleteDirectoryThatExists()
        {
            var state = new FileSystemState();
            var root  = state.GetRootDirectory();
            var isAddDirectorySuccess = _directoryController.TryAddDirectory("Test", new Permission(), root, out var addedDirectory);

            var isDeleteDirectorySuccess = _directoryController.TryDeleteDirectory("Test", root);

            Assert.IsTrue(isAddDirectorySuccess);
            Assert.IsTrue(isDeleteDirectorySuccess);
        }
Пример #24
0
        public void DirectoryPathIncludesRootDirectory()
        {
            var state = new FileSystemState();
            var root  = state.GetRootDirectory();

            var childDirectory = new Directory
            {
                Name            = "Child",
                ParentDirectory = root
            };

            var path         = _directoryController.GetDirectoryPath(childDirectory);
            var expectedPath = "/Child";

            Assert.AreEqual(path, expectedPath);
        }
Пример #25
0
        public void HomeDirectoryPathIsShortened()
        {
            var state = new FileSystemState();
            var root  = state.GetRootDirectory();

            var homeDirectory = new Directory
            {
                Name            = "home",
                ParentDirectory = root
            };

            var path         = _directoryController.GetDirectoryPath(homeDirectory);
            var expectedPath = "~";

            Assert.AreEqual(path, expectedPath);
        }
Пример #26
0
        public void FilesInDirectoryExistWhenCreated()
        {
            var state = new FileSystemState();
            var root  = state.GetRootDirectory();

            // Need to store the boolean we want to test directly rather than the state
            // This is because the underlying state will change but the reference pointer will not, leading to incorrect testing behavior
            var isFilesInDirectoryNullBeforeAdd      = root.FilesInDirectory == null;
            var isFilesInDirectoryPopulatedBeforeAdd = root.FilesInDirectory.Any();
            var isAddFileSuccess = _fileController.TryAddFile("Test", FileExtension.None, new Permission(), root, out var testFile);

            Assert.IsTrue(isAddFileSuccess);
            Assert.IsFalse(isFilesInDirectoryNullBeforeAdd);
            Assert.IsFalse(isFilesInDirectoryPopulatedBeforeAdd);
            Assert.IsNotNull(root.FilesInDirectory);
            Assert.That(root.FilesInDirectory.Contains(testFile));
        }
Пример #27
0
        public void InitializeCommandsInFileSystemState(
            FileSystemState fileSystemState,
            PermissionController permissionController,
            DirectoryController directoryController,
            FileController fileController,
            IList <string> commandNames)
        {
            var root = fileSystemState.GetRootDirectory();

            var binDirectoryPermission   = permissionController.GetCustomPermission(canRead: true, canExecute: true);
            var isAddBinDirectorySuccess = directoryController.TryAddDirectory("bin", binDirectoryPermission, root, out var binDirectory);

            Debug.Assert(isAddBinDirectorySuccess, $"Failed to add `bin` directory under `{root.Name}` directory");

            foreach (var commandName in commandNames)
            {
                var filePermission          = permissionController.GetCustomPermission(canExecute: true);
                var isAddCommandFileSuccess = fileController.TryAddFile(commandName, FileExtension.exe, filePermission, binDirectory, out _);

                Debug.Assert(isAddCommandFileSuccess, $"Failed to add `{commandName}{FileExtension.exe.ToString()}` file under `{binDirectory.Name}` directory");
            }
        }
Пример #28
0
        public string ExecuteCommand(params string[] args)
        {
            // Validate the arguments to the command
            if (!TryValidateArguments(out var validationResponse, args))
            {
                return(validationResponse);
            }

            // Extract the arguments and flags
            string targetDirectoryName = null;
            bool   showHiddenFiles     = false;
            bool   showFileListView    = false;

            // Ignore the first parameter because we know it is the command name since validation succeeded
            for (var i = 1; i < args.Length; i++)
            {
                if (args[i].StartsWith(_flagParameterStartingCharacter.ToString(), StringComparison.InvariantCultureIgnoreCase))
                {
                    // Loop through all the flags provided like `-abcdefg` and extract each one as necessary
                    var shortOptions = args[i];
                    shortOptions = shortOptions.TrimStart(_flagParameterStartingCharacter);
                    foreach (var shortOption in shortOptions)
                    {
                        if (shortOption.Equals(_showHiddenFilesFlag))
                        {
                            showHiddenFiles = true;
                        }
                        else if (shortOption.Equals(_showFileListViewFlag))
                        {
                            showFileListView = true;
                        }
                        else
                        {
                            return($"Error - Unknown flag provided to `{GetCommandName()}`: `{shortOption}`");
                        }
                    }
                }
                else
                {
                    targetDirectoryName = args[i];
                }
            }

            // Execute the valid command logic
            Directory targetDirectory = null;

            // Handle the special case where the user wants to list the contents of the `/` or '\' directory (root)
            if (targetDirectoryName != null && targetDirectoryName.Length == 1 && _pathDelimiters.Contains(targetDirectoryName.FirstOrDefault()))
            {
                targetDirectory = _fileSystemState.GetRootDirectory();
            }

            // Handle the special case where the user wants to list the contents of the `.` directory (current directory)
            if (targetDirectoryName != null && targetDirectoryName.Equals(_currentDirectorySymbol, StringComparison.InvariantCultureIgnoreCase))
            {
                targetDirectory = _fileSystemState.GetCurrentDirectory();
            }

            // Handle the special case where the user wants to change directory to `..` (parent directory)
            if (targetDirectoryName != null && targetDirectoryName.Equals(_parentDirectorySymbol, StringComparison.InvariantCultureIgnoreCase))
            {
                var currentDirectory = _fileSystemState.GetCurrentDirectory();
                targetDirectory = currentDirectory.ParentDirectory;
                if (targetDirectory == null)
                {
                    return($"Error - Failed to find parent directory for `{currentDirectory.Name}`");
                }
            }

            // Handle the special case where the user has supplied a directory name or path they wish to see the contents of
            if (targetDirectoryName != null)
            {
                var isGetDirectorySuccess = TryGetTargetDirectory(targetDirectoryName, out targetDirectory, out var errorMessage);
                if (!isGetDirectorySuccess)
                {
                    return(errorMessage);
                }
            }

            // At this point, if we do not already have a target directory, the user did not specify a target directory to see its contents
            // If that is the case, use the "default" target directory - the current working directory
            if (targetDirectory == null)
            {
                targetDirectory = _fileSystemState.GetCurrentDirectory();
            }

            // If the user specified that they want to see the long list view, show them that view
            if (showFileListView)
            {
                return(FormatDirectoryContentsForLongView(targetDirectory, showHiddenFiles));
            }

            // Otherwise, show them the default view
            return(FormatDirectoryContents(targetDirectory, showHiddenFiles));
        }