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);
        }
예제 #2
0
        private bool TryGetTargetDirectory(string targetDirectoryPath, out Directory targetDirectory, out string errorMessage)
        {
            // There are three options here - first is that this is an absolute path, which means that the first character is a path delimiter
            if (_pathDelimiters.Contains(targetDirectoryPath.FirstOrDefault()))
            {
                var rootDirectory = _fileSystemState.GetRootDirectory();
                return(TryGetTargetDirectory(rootDirectory, targetDirectoryPath, out targetDirectory, out errorMessage));
            }

            // Second is that this is a relative path, but begins with a `~` symbol, which means the home directory is the starting point of the path
            if (_homeDirectorySymbol.Equals(targetDirectoryPath.FirstOrDefault().ToString(), StringComparison.InvariantCultureIgnoreCase))
            {
                var rootDirectory             = _fileSystemState.GetRootDirectory();
                var isGetHomeDirectorySuccess = _directoryController.TryGetDirectory("home", rootDirectory, out var homeDirectory);
                return(TryGetTargetDirectory(homeDirectory, targetDirectoryPath, out targetDirectory, out errorMessage, ignoreFirstHomeSymbol: true));
            }

            // Third is that this is a relative path that does not begin with a `~` symbol, which means that the current directory is the starting point of the path
            var currentDirectory = _fileSystemState.GetCurrentDirectory();

            return(TryGetTargetDirectory(currentDirectory, targetDirectoryPath, out targetDirectory, out errorMessage));
        }
        private string ChangeDirectoryToHome()
        {
            var rootDirectory             = _fileSystemState.GetRootDirectory();
            var isGetHomeDirectorySuccess = _directoryController.TryGetDirectory(_homeDirectoryName, rootDirectory, out var homeDirectory);

            if (!isGetHomeDirectorySuccess)
            {
                return($"Error - Failed to get `{_homeDirectoryName}` directory in `{rootDirectory.Name}`");
            }

            var isSetCurrentDirectoryAsHomeSuccess = _fileSystemState.TrySetCurrentDirectory(homeDirectory);

            if (!isSetCurrentDirectoryAsHomeSuccess)
            {
                return($"Error - Failed to set current directory to `{_homeDirectoryName}` directory");
            }

            return($"Current working directory changed");
        }