Exemplo n.º 1
0
        public void GetFullPathWithBasePathTests(string path, string basePath, string expected)
        {
            IExtendedFileService extendedFileService = Substitute.For <IExtendedFileService>();

            extendedFileService.GetVolumeName(@"C:\").Returns("TestCVolumeName");
            extendedFileService.GetVolumeName(@"D:\").Returns("TestDVolumeName");

            var fileService = new FileService(extendedFileService, initialCurrentDirectory: @"C:\Users");

            fileService.GetFullPath(path, basePath).Should().Be(expected);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Get the canonical volume root (e.g. the \\?\VolumeGuid format) for the given path. The path must not be relative.
        /// </summary>
        /// <exception cref="ArgumentException">Thrown if the path is relative or indeterminate.</exception>
        /// <exception cref="System.IO.IOException">Thrown if unable to get the root from the OS.</exception>
        public static string GetCanonicalRoot(this IExtendedFileService extendedFileService, IFileService fileService, string path)
        {
            if (Paths.IsPartiallyQualified(path))
            {
                throw new ArgumentException(nameof(path));
            }

            path = fileService.GetFullPath(path);
            int rootLength;
            var format = Paths.GetPathFormat(path, out rootLength);

            if (format == PathFormat.UnknownFormat)
            {
                throw new ArgumentException(nameof(format));
            }

            string root          = path.Substring(0, rootLength);
            string canonicalRoot = root;

            switch (format)
            {
            case PathFormat.UniformNamingConvention:
                if (Paths.IsDevice(path))
                {
                    canonicalRoot = @"\\" + root.Substring(Paths.ExtendedUncPrefix.Length);
                }
                break;

            case PathFormat.LocalFullyQualified:
                canonicalRoot = extendedFileService.GetVolumeName(root);
                break;
            }

            return(canonicalRoot);
        }
Exemplo n.º 3
0
        public void SetCurrentDirectoryThrowsOnRelative()
        {
            IExtendedFileService extendedFileService = Substitute.For <IExtendedFileService>();
            IFileService         fileService         = Substitute.For <IFileService>();

            fileService.GetFullPath("").ReturnsForAnyArgs(x => x[0]);
            fileService.GetAttributes("").ReturnsForAnyArgs(System.IO.FileAttributes.Directory);
            extendedFileService.GetVolumeName("").ReturnsForAnyArgs("TestRoot");

            CurrentDirectory cd     = new CurrentDirectory(fileService, extendedFileService);
            Action           action = () => cd.SetCurrentDirectory("a");

            action.Should().Throw <ArgumentException>();
        }
        /// <summary>
        /// Get the canonical volume root (e.g. the \\?\VolumeGuid format) for the given path. The path must not be relative.
        /// </summary>
        /// <exception cref="ArgumentException">Thrown if the path is relative or indeterminate.</exception>
        /// <exception cref="System.IO.IOException">Thrown if unable to get the root from the OS.</exception>
        public static string GetCanonicalRoot(this IExtendedFileService extendedFileService, IFileService fileService, string path)
        {
            if (Paths.IsRelative(path))
            {
                throw new ArgumentException();
            }

            path = fileService.GetFullPath(path);
            int rootLength;
            var format = Paths.GetPathFormat(path, out rootLength);

            if (format == PathFormat.UnknownFormat)
            {
                throw new ArgumentException();
            }

            string root          = path.Substring(0, rootLength);
            string simpleRoot    = root;
            string canonicalRoot = root;

            switch (format)
            {
            case PathFormat.UniformNamingConventionExtended:
                simpleRoot = @"\\" + root.Substring(Paths.ExtendedUncPrefix.Length);
                goto case PathFormat.UniformNamingConvention;

            case PathFormat.UniformNamingConvention:
                canonicalRoot = simpleRoot;
                break;

            case PathFormat.VolumeAbsoluteExtended:
            case PathFormat.DriveAbsolute:
                canonicalRoot = extendedFileService.GetVolumeName(root);
                simpleRoot    = extendedFileService.GetMountPoint(root);
                break;
            }

            return(canonicalRoot);
        }